Advertisement
s243a

ppa2pup

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