Advertisement
s243a

ppa2pup

Dec 21st, 2019
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.74 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # ppa2pup
  4. # convert an Ubuntu PPA repo file, into a Puppy compatible repo file.
  5.  
  6. # @TODO1 - fix last few fields of package entries (should contain the supported ubuntu distro, not current system)
  7. [ -z "${whoami}" ] && whoami="$(whoami || $(echo user) )"  #One can break the whoami command if the folder premissions are wrong.
  8. [ -z "$TMPDIR" ] && TMPDIR=/tmp/pkg/${whoami} || TMPDIR="${TMPDIR%/}"
  9. TMPDIR=/tmp/pkg/${whoami}
  10. if [-z "$REPO_DIR" ]; then
  11.   REPO_DIR=${HOME}/.packages       # where the repo files live
  12.   [ -d /var/packages ] && REPO_DIR=/var/packages
  13. fi
  14. . /etc/DISTRO_SPECS
  15.  
  16. #if [ "$DISTRO_BINARY_COMPAT" != "ubuntu" ] && [ "$DISTRO_BINARY_COMPAT" != "debian" ];then
  17. #  echo "Sorry, you must be running a .deb compatible Puppy Linux"
  18. #  echo "to use this tool. Your Puppy is based on '$DISTRO_BINARY_COMPAT'"
  19. #  exit 1
  20. #fi
  21.  
  22.  
  23. if [ ! "$1" ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]
  24. then
  25.  
  26.   echo "This script creates a Puppy-compatible repo file from a PPA or Debian repo."
  27.   echo
  28.   echo "For Launchpad PPA repos:"
  29.   echo
  30.   echo "  Usage: ppa2pup ppa:<user>/<repo> [debian|ubuntu] [bionic|stretch|artful|etc] [main|all|contrib|non-free|etc]"
  31.   echo
  32.   echo "Examples:"
  33.   echo
  34.   echo "  ppa2pup ppa:team-xbmc/ppa"
  35.   echo
  36.   echo "  ppa2pup ppa:team-xbmc/ppa ubuntu bionic"
  37.   echo
  38.   echo "  ppa2pup ppa:team-xbmc/ppa ubuntu artful"
  39.   echo
  40.   echo "  ppa2pup ppa:team-xbmc/ppa debian stretch"
  41.   echo
  42.   echo
  43.   echo "For other third-party Debian repos:"
  44.   echo
  45.   echo "  Usage: ppa2pup http://site.com/[debian|ubuntu]/ [stretch|bionic|etc] [main|contrib|non-free|etc]"
  46.   echo
  47.   echo "Examples:"
  48.   echo
  49.   echo "  ppa2pup http://rpms.litespeedtech.com/debian/"
  50.   echo
  51.   echo "  ppa2pup http://rpms.litespeedtech.com/debian/ stretch main"
  52.   echo
  53.   echo "  ppa2pup http://repo.steampowered.com/steam/ precise steam"
  54.   echo
  55.   echo "  ppa2pup http://http.kali.org/kali/ kali-bleeding-edge main contrib non-free"
  56.   echo
  57.   echo "NOTE: Any ommitted distro names or versions will be guessed."
  58.   echo
  59.   exit 1
  60.  
  61. fi
  62.  
  63.  
  64. # create a dir to work in
  65. mkdir -p ${TMPDIR}/ppa_pkgs
  66.  
  67. # remove any old files
  68. rm ${TMPDIR}/ppa_pkgs/* 2>/dev/null
  69.  
  70.  
  71. # we need to find the correct Packages.gz for the distro (debian/ubuntu),
  72. # distro version (stretch/bionic/etc), and architecture (i386/amd64)
  73.  
  74. arch='i386'
  75. case $(uname -m) in
  76.   i*86) arch='i386'  ;;
  77.   *64)  arch='amd64' ;;
  78. esac
  79.  
  80.  
  81.  
  82. if [[ "$1" =~ 'ppa:' ]]
  83. then
  84.  
  85.   # we got a 'PPA' URL, lets parse it and get the Packages.gz
  86.   ppaname="${1/*\//}"
  87.   username="${1/\/*/}"
  88.   username="${username//ppa:/}"
  89.   ppaname="${ppaname//ppa:/}"
  90.  
  91.   # get username, but strip special chars
  92.   PPA_NAME="${username//[-_:]/}"
  93.  
  94.   distro=${2:-$DISTRO_BINARY_COMPAT}
  95.   distro_ver=${3:-$DISTRO_COMPAT_VERSION}
  96.   repo_name="$distro_ver-$PPA_NAME"
  97.   repo_filename="Packages-$distro-$distro_ver-${PPA_NAME}"
  98.   repo_stream=${4:-main}
  99.   repo_stream2=''
  100.   repo_stream3=''
  101.   repo_stream4=''
  102.   [ "$5" != '' ] && repo_stream2=${5}
  103.   [ "$6" != '' ] && repo_stream3=${6}
  104.   [ "$7" != '' ] && repo_stream4=${7}
  105.  
  106.  
  107.   URL=http://ppa.launchpad.net/${username}/${ppaname}/${distro}/dists/${distro_ver}/${repo_stream}/binary-${arch}/Packages.gz
  108.   repo_url=http://ppa.launchpad.net/${username}/${ppaname}/${distro}/
  109.  
  110. elif [[ "$1" =~ 'http://' ]] || [[ "$1" =~ 'https://' ]];then
  111.  
  112.   # we got a Debian repo source URL, lets parse it and get the Packages.gz
  113.  
  114.   distro=${DISTRO_BINARY_COMPAT}
  115.   distro_ver=${2:-$DISTRO_COMPAT_VERSION}
  116.   repo_url=$(echo $1 | sed -e 's#/$##g')/
  117.   repo_stream=${3:-main}
  118.   repo_stream2=''
  119.   repo_stream3=''
  120.   repo_stream4=''
  121.   [ "$4" != '' ] && repo_stream2=${4}
  122.   [ "$5" != '' ] && repo_stream3=${5}
  123.   [ "$6" != '' ] && repo_stream4=${6}
  124.  
  125.   URL=$(echo $1 | sed -e 's#/$##g')/dists/${distro_ver}/${repo_stream}/binary-${arch}/Packages.gz
  126.  
  127. else
  128.  
  129.   # didnt get ppa:foo/bar, exit with usage
  130.   $0 -h
  131.   exit 1
  132.  
  133. fi
  134.  
  135.  
  136. for stream in $repo_stream $repo_stream2 $repo_stream3 $repo_stream4
  137. do
  138.  
  139.   [ "$stream" = '' ] && continue
  140.  
  141.   rm ${TMPDIR}/ppa_Packages ${TMPDIR}/ppa_Packages.gz 2>/dev/null
  142.   download_failed=false
  143.  
  144.   download_url=${URL//$repo_stream/$stream}
  145.  
  146.   wget --quiet $download_url -O ${TMPDIR}/ppa_Packages.gz 1>/dev/null \
  147.     || download_failed=true
  148.  
  149.   if [ ! -f ${TMPDIR}/ppa_Packages.gz ] || [ $download_failed = true ];then
  150.     echo
  151.     echo "ERROR: the PPA repo '$repo_name' not found for $distro $distro_ver:"
  152.     echo
  153.     echo "  $download_url"
  154.     echo
  155.     echo "You could try a different version of the repo."
  156.     echo
  157.     $0 -h
  158.     exit 1
  159.   fi
  160.  
  161.  
  162.   gunzip ${TMPDIR}/ppa_Packages.gz
  163.  
  164.  
  165.   # if Packages file is empty, dont create a repo for it
  166.   if [ -z ${TMPDIR}/ppa_Packages ] || [ ! -s ${TMPDIR}/ppa_Packages ];then
  167.     continue
  168.   fi
  169.  
  170.   # if we didn't get the name from the ppa:foo/bar style URL
  171.   if [[ ! "$1" =~ 'ppa:' ]]
  172.   then
  173.  
  174.     # check if this repo is already installed (find its name in Pkg sources files)
  175.     PPA_NAME="$(grep -m1 "^${distro_ver}-${stream:-main}|" /root/.pkg/sources-all | cut -f1 -d'|' 2>/dev/null)"
  176.  
  177.     # get repo name and filename
  178.     if [ "$PPA_NAME" = "" ];then
  179.       echo
  180.       read -e -p "Enter a repo name, such as '${distro_ver}-${stream:-main}':  " -i "${distro_ver}-${stream:-main}" PPA_NAME
  181.     fi
  182.     # replace any spaces or underscores with dashes, all lower case
  183.     PPA_NAME="$( echo "${PPA_NAME// /-}" | tr '_' '-' | tr '[:upper:]' '[:lower:]' )"
  184.  
  185.     repo_name="$PPA_NAME"
  186.     repo_filename="Packages-$distro-${PPA_NAME}"
  187.  
  188.   fi
  189.  
  190.   echo
  191.   echo "Found URL:"
  192.   echo
  193.   echo "  $download_url"
  194.   echo
  195.   echo "Repo to create:"
  196.   echo "   $repo_name"
  197.   echo
  198.   echo "Repo file to create:"
  199.   echo "   ${REPO_DIR}/$repo_filename"
  200.   echo
  201.  
  202.   # break Packages file into separate files
  203.   number=1
  204.   cat ${TMPDIR}/ppa_Packages | while read line
  205.   do
  206.  
  207.     echo "$line" | grep -qE '^Priority|^Installed-Size|^Maintainer|^MD5|^SHA|^Description-md5|^Suggests|^Provides|^Source|^Conflicts|^Breaks' && continue
  208.     echo "$line" >> ${TMPDIR}/ppa_pkgs/pkginfo$number
  209.     [ "$line" = "" ] && number=$(($number + 1))
  210.  
  211.     #[ $number -gt 5 ] && break # for testing only
  212.  
  213.   done
  214.  
  215.  
  216.   rm ${TMPDIR}/$repo_filename ${REPO_DIR}/$repo_filename &>/dev/null
  217.  
  218.   # go through each file and make a pet.spec entry from it,
  219.   # and append that entry tp ${TMPDIR}/ppa_pkgs/$repo_filename
  220.   for pkginfo_file in $(ls -1 ${TMPDIR}/ppa_pkgs/)
  221.   do
  222.  
  223.     [ ! -f ${TMPDIR}/ppa_pkgs/$pkginfo_file ] && continue
  224.  
  225.     pkginfo="`cat ${TMPDIR}/ppa_pkgs/$pkginfo_file 2>/dev/null`"
  226.     [ "$pkginfo" = "" ] && continue
  227.  
  228.     # convert deps list from:
  229.     #
  230.     #  nginx-full (<< 1.14.0-0+bionic0.1~) | libc6 (>= 2.4), libpam0g (>= 0.99.7.1)
  231.     #
  232.     # into:
  233.     #
  234.     #  +nginx-full-1.14.0-0+bionic0,+libc6-2.4,+libpam0g-0.99.7.1
  235.     #
  236.  
  237.     pkgdeps=`echo "$pkginfo" | grep '^Depends: '`
  238.  
  239.     newpkgdeps="$(echo "$pkgdeps" \
  240.      | sed \
  241.        -e 's/  / /g'     \
  242.        -e 's/ (= 2:/_/g' \
  243.        -e 's/ (= 1:/_/g' \
  244.        -e 's/ (<< /-/g'  \
  245.        -e 's/ (= /-/g'   \
  246.        -e 's/ (>= /-/g'  \
  247.        -e 's/ (<= /-/g'  \
  248.        -e 's/) / /g'     \
  249.        -e 's/), /,/g'    \
  250.        -e 's/)//g'       \
  251.        -e 's/, /,/g'     \
  252.        -e 's/ | /,/g'    \
  253.        -e 's/| /,/g'     \
  254.        -e 's/ |/,/g'     \
  255.        -e "s/,$//g"      \
  256.        -e 's/\.1~//g'    \
  257.        -e 's/\.2~//g'    \
  258.        -e 's/,/,+/g'     \
  259.        -e 's/-2:/_/g'    \
  260.        -e 's/-1:/_/g'    \
  261.        -e 's/Depends: /Depends: +/g' \
  262.      2>/dev/null)"
  263.  
  264.     # replace olddeps entry with new one in the file
  265.     if [ ! -z "pkgdeps" ] && [ ! -z "$newpkgdeps" ] && [ -f ${TMPDIR}/ppa_pkgs/$pkginfo_file ]
  266.     then
  267.  
  268.       sed -i "s/${pkgdeps}/${newpkgdeps}/" ${TMPDIR}/ppa_pkgs/$pkginfo_file 2>/dev/null
  269.  
  270.     fi
  271.  
  272.  
  273.     # deps should now be in the right format in the file itself,
  274.     # update our info var
  275.     pkginfo="$(cat ${TMPDIR}/ppa_pkgs/$pkginfo_file)"
  276.  
  277.     # so lets build pet.specs entry:
  278.     # like this one:
  279.     #   pkgname-1.2.3-4|pkgname|1.2.3|4|Category|54kb|/path/to/pkg|pkgname-1.2.3.pet|+dep1,+dep2|the description|debian|stretch||
  280.  
  281.     case $DISTRO_BINARY_COMPAT in
  282.       debian|ubuntu|trisquel|devuan) ver_sep='_';;
  283.       *) ver_sep='-';;
  284.     esac
  285.  
  286.     pkg_build_no=''  # need to get this out of version
  287.     pkg_name="$(echo  "$pkginfo" | grep -m1 '^Package: ' | cut -f2 -d' ')"
  288.  
  289.     pkgname_only="$pkg_name"
  290.     pkg_ver="$(echo   "$pkginfo" | grep -m1 '^Version: ' | sed -e 's/Version: [0-9]://g' -e 's/Version: //g')"
  291.     pkg_name="$pkg_name${ver_sep}$pkg_ver"
  292.     pkg_cat="$(echo   "$pkginfo" | grep -m1 '^Section: ' | cut -f2 -d' ')"
  293.     pkg_size="$(echo  "$pkginfo" | grep -m1 '^Size: '    | cut -f2 -d' ')"
  294.     pkg_size="$(($pkg_size / 1024))K"
  295.     pkg_path="$(echo "$pkginfo" | grep -m1 '^Filename: '| cut -f2 -d' ')"
  296.     pkg_fname="$(basename "$pkg_path")"
  297.     pkg_path="$(dirname "$pkg_path")"
  298.     pkg_deps="$(echo "$pkginfo" | grep -m1 '^Depends: ' | cut -f2 -d' ')"
  299.     pkg_desc="$(echo  "$pkginfo" | grep -m1 '^Descripti' | cut -f2-200 -d' ')"
  300.     pkg_os="$DISTRO_BINARY_COMPAT"
  301.     pkg_os_ver="$DISTRO_COMPAT_VERSION"
  302.  
  303.     entry="$pkg_name|$pkgname_only|$pkg_ver|$pkg_build_no|$pkg_cat|$pkg_size|$pkg_path|$pkg_fname|$pkg_deps|$pkg_desc|$pkg_os|$pkg_os_ver|"
  304.  
  305.     echo "$entry" >> ${TMPDIR}/$repo_filename
  306.  
  307.   done
  308.  
  309.  
  310.   rm ${TMPDIR}/ppa_pkgs/* ${TMPDIR}/${repo_filename}_sorted 2>/dev/null
  311.  
  312.  
  313.   # sort & move the repo file
  314.   sort -u ${TMPDIR}/$repo_filename > ${TMPDIR}/${repo_filename}_sorted 2>/dev/null
  315.  
  316.  
  317.   if [ ! -f ${TMPDIR}/${repo_filename}_sorted ];then
  318.     echo "Error: Repo file not created!"
  319.     exit 1
  320.   fi
  321.  
  322.   mv  ${TMPDIR}/${repo_filename}_sorted  ${REPO_DIR}/$repo_filename
  323.   echo "Success! File created."
  324.   echo
  325.  
  326.   fallback_repos="$(pkg repo-list | grep -v $repo_name | tr '\n' ' ')"
  327.   repo_entry="$repo_name|deb|$repo_filename|$repo_url||||$fallback_repos"
  328.  
  329.  
  330.   # if already added to ~/.pkg/sources[-all], remove it
  331.   if [ "$(cat ~/.pkg/sources     | grep -m1 "^$repo_name|")" != "" ] || \
  332.      [ "$(cat ~/.pkg/sources-all | grep -m1 "^$repo_name|")" != "" ];then
  333.     cat ~/.pkg/sources | grep -v "^$repo_name|" > ${TMPDIR}/pkgsources
  334.     cat ~/.pkg/sources-all | grep -v "^$repo_name|" > ${TMPDIR}/pkgsources-all
  335.     mv ${TMPDIR}/pkgsources ~/.pkg/sources
  336.     mv ${TMPDIR}/pkgsources-all ~/.pkg/sources-all
  337.   fi
  338.  
  339.  
  340.   # add repo entry to ~/.pkg/sources
  341.   pkg add-source "$repo_entry"
  342.   echo
  343.  
  344.   # refresh list of available repos
  345.   pkg update-sources
  346.   echo
  347.   echo "Repo info:"
  348.   pkg repo-info $repo_name
  349.   echo
  350.  
  351.  
  352.   if [ "$(cat ~/.pkg/sources | grep -m1 "^$repo_name|")" != "" ];then
  353.     echo "Success! Repo added and available to use."
  354.     echo
  355.     echo "To use this repo, simply type the following and hit ENTER:"
  356.     echo "  pkg repo $repo_name"
  357.     echo
  358.   fi
  359.  
  360. done
  361.  
  362. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement