Advertisement
s243a

ppa2pup (awk)(1)

Nov 17th, 2019
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 12.22 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. #---------------New AWK Version-------------------------------------------
  223.  
  224.             cat /tmp/ppa_Packages | awk \
  225. 'function fixdepends(s,   a,p,sout) {
  226.     split(s,a,",")
  227.     for (p in a) {
  228.         gsub(/[ \t]*\(.*\)|[ \t]\|.*|:any/,"",a[p])
  229.         sout = sout "," a[p]
  230.     }
  231.     sub(/^,/,"",sout) ; return sout;
  232. }
  233.  
  234. /^Package:/     { sub(/^Package: /,"");  PKG=$0; }
  235. /^Version:/     { sub(/^Version: /,"");  PKGVER=$0; }
  236. /^Filename:/    { sub(/^Filename: /,""); PKGPATH=$0; sub(/.*\//,""); PKGFILE=$0; }
  237. /^Priority:/    { sub(/^Priority: /,""); PKGPRIO=$0; }
  238. /^Section:/     { sub(/^Section: /,"");  PKGSECTION=$0; }
  239. /^MD5sum:/      { sub(/^MD5sum: /,"");   PKGMD5=$0; }
  240. /^Depends:/     { sub(/^Depends: /,"");     PKGDEP=fixdepends($0) "," PKGDEP; }
  241. /^Pre-Depends:/ { sub(/^Pre-Depends: /,""); PKGDEP=fixdepends($0) "," PKGDEP; }
  242. /^$/            { print PKG "|" PKGVER "|" PKGFILE "|" repo_url "/" PKGPATH "|" PKGPRIO "|" PKGSECTION "|" PKGMD5 "|" PKGDEP ;
  243.                  PKG=""; PKGVER=""; PKGFILE=""; PKGPATH=""; PKGPRIO=""; PKGSECTION=""; PKGMD5="";  PKGDEP=""; }
  244. ' > /tmp/$repo_filename
  245. #           # remove duplicates, use the "later" version if duplicate packages are found
  246. #           < $REPO_DIR/$LOCAL_PKGDB > /tmp/t.$$ \
  247. #           awk -F"|" '{if (!a[$1]) b[n++]=$1; a[$1]=$0} END {for (i=0;i<n;i++) {print a[b[i]]}}'
  248. #           mv /tmp/t.$$ $REPO_DIR/$LOCAL_PKGDB
  249. #       fi
  250. #       if [ -z "$WITH_APT_DB" ] || [ $DRY_RUN ]; then rm -f $CHROOT_DIR$APT_PKGDB_DIR/"$apt_pkgdb"; fi
  251. #   done
  252.  
  253.  
  254. #---------------Replace with AWK Version-------------------------------------------    
  255.   #for pkginfo_file in $(ls -1 /tmp/ppa_pkgs/)
  256.   #do
  257.   #
  258.   #  [ ! -f /tmp/ppa_pkgs/$pkginfo_file ] && continue
  259.   #
  260.   #  pkginfo="`cat /tmp/ppa_pkgs/$pkginfo_file 2>/dev/null`"
  261.   #  [ "$pkginfo" = "" ] && continue
  262.   #
  263.   #  # convert deps list from:
  264.   #  #
  265.   #  #  nginx-full (<< 1.14.0-0+bionic0.1~) | libc6 (>= 2.4), libpam0g (>= 0.99.7.1)
  266.   #  #
  267.   #  # into:
  268.   #  #
  269.   #  #  +nginx-full-1.14.0-0+bionic0,+libc6-2.4,+libpam0g-0.99.7.1
  270.   #  #
  271.   #
  272.   #  pkgdeps=`echo "$pkginfo" | grep '^Depends: '`
  273.   #
  274.   #  newpkgdeps="$(echo "$pkgdeps" \
  275.   #    | sed \
  276.   #      -e 's/  / /g'     \
  277.   #      -e 's/ (= 2:/_/g' \
  278.   #      -e 's/ (= 1:/_/g' \
  279.   #      -e 's/ (<< /-/g'  \
  280.   #      -e 's/ (= /-/g'   \
  281.   #      -e 's/ (>= /-/g'  \
  282.   #      -e 's/ (<= /-/g'  \
  283.   #      -e 's/) / /g'     \
  284.   #      -e 's/), /,/g'    \
  285.   #      -e 's/)//g'       \
  286.   #      -e 's/, /,/g'     \
  287.   #      -e 's/ | /,/g'    \
  288.   #      -e 's/| /,/g'     \
  289.   #      -e 's/ |/,/g'     \
  290.   #      -e "s/,$//g"      \
  291.   #      -e 's/\.1~//g'    \
  292.   #      -e 's/\.2~//g'    \
  293.   #      -e 's/,/,+/g'     \
  294.   #      -e 's/-2:/_/g'    \
  295.   #      -e 's/-1:/_/g'    \
  296.   #      -e 's/Depends: /Depends: +/g' \
  297.   #    2>/dev/null)"
  298.   #
  299.   #  # replace olddeps entry with new one in the file
  300.   #  if [ ! -z "pkgdeps" ] && [ ! -z "$newpkgdeps" ] && [ -f /tmp/ppa_pkgs/$pkginfo_file ]
  301.   #  then
  302.   #
  303.   #    sed -i "s/${pkgdeps}/${newpkgdeps}/" /tmp/ppa_pkgs/$pkginfo_file 2>/dev/null
  304.   #
  305.   #  fi
  306.   #
  307.   #
  308.   #  # deps should now be in the right format in the file itself,
  309.   #  # update our info var
  310.   #  pkginfo="$(cat /tmp/ppa_pkgs/$pkginfo_file)"
  311.   #
  312.   #  # so lets build pet.specs entry:
  313.   #  # like this one:
  314.   #  #   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||
  315.   #
  316.   #  case $DISTRO_BINARY_COMPAT in
  317.   #    debian|ubuntu|trisquel|devuan) ver_sep='_';;
  318.   #    *) ver_sep='-';;
  319.   #  esac
  320.   #
  321.   #  pkg_build_no=''  # need to get this out of version
  322.   #  pkg_name="$(echo  "$pkginfo" | grep -m1 '^Package: ' | cut -f2 -d' ')"
  323.   #
  324.   #  pkgname_only="$pkg_name"
  325.   #  pkg_ver="$(echo   "$pkginfo" | grep -m1 '^Version: ' | sed -e 's/Version: [0-9]://g' -e 's/Version: //g')"
  326.   #  pkg_name="$pkg_name${ver_sep}$pkg_ver"
  327.   #  pkg_cat="$(echo   "$pkginfo" | grep -m1 '^Section: ' | cut -f2 -d' ')"
  328.   #  pkg_size="$(echo  "$pkginfo" | grep -m1 '^Size: '    | cut -f2 -d' ')"
  329.   #  pkg_size="$(($pkg_size / 1024))K"
  330.   #  pkg_path="$(echo "$pkginfo" | grep -m1 '^Filename: '| cut -f2 -d' ')"
  331.   #  pkg_fname="$(basename "$pkg_path")"
  332.   #  pkg_path="$(dirname "$pkg_path")"
  333.   #  pkg_deps="$(echo "$pkginfo" | grep -m1 '^Depends: ' | cut -f2 -d' ')"
  334.   #  pkg_desc="$(echo  "$pkginfo" | grep -m1 '^Descripti' | cut -f2-200 -d' ')"
  335.   #  pkg_os="$DISTRO_BINARY_COMPAT"
  336.   #  pkg_os_ver="$DISTRO_COMPAT_VERSION"
  337.   #
  338.   #  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|"
  339.   #
  340.   #  echo "$entry" >> /tmp/$repo_filename
  341.   #
  342.   #done
  343.   #
  344.   #
  345.   #rm /tmp/ppa_pkgs/* /tmp/${repo_filename}_sorted 2>/dev/null
  346. #------------------End Code Replaced with AWK Version---------------------------------
  347.  
  348.   # sort & move the repo file
  349.   sort -u /tmp/$repo_filename > /tmp/${repo_filename}_sorted 2>/dev/null
  350.  
  351.  
  352.   if [ ! -f /tmp/${repo_filename}_sorted ];then
  353.     echo "Error: Repo file not created!"
  354.     exit 1
  355.   fi
  356.  
  357.   mv  /tmp/${repo_filename}_sorted  $REPO_DIR/$repo_filename
  358.   echo "Success! File created."
  359.   echo
  360.  
  361.   fallback_repos="$(pkg repo-list | grep -v $repo_name | tr '\n' ' ')"
  362.   repo_entry="$repo_name|deb|$repo_filename|$repo_url||||$fallback_repos"
  363.  
  364.  
  365.   # if already added to ~/.pkg/sources[-all], remove it
  366.   if [ "$(cat ~/.pkg/sources     | grep -m1 "^$repo_name|")" != "" ] || \
  367.      [ "$(cat ~/.pkg/sources-all | grep -m1 "^$repo_name|")" != "" ];then
  368.     cat ~/.pkg/sources | grep -v "^$repo_name|" > /tmp/pkgsources
  369.     cat ~/.pkg/sources-all | grep -v "^$repo_name|" > /tmp/pkgsources-all
  370.     mv /tmp/pkgsources ~/.pkg/sources
  371.     mv /tmp/pkgsources-all ~/.pkg/sources-all
  372.   fi
  373.  
  374.  
  375.   # add repo entry to ~/.pkg/sources
  376.   pkg add-source "$repo_entry"
  377.   echo
  378.  
  379.   # refresh list of available repos
  380.   pkg update-sources
  381.   echo
  382.   echo "Repo info:"
  383.   pkg repo-info $repo_name
  384.   echo
  385.  
  386.  
  387.   if [ "$(cat ~/.pkg/sources | grep -m1 "^$repo_name|")" != "" ];then
  388.     echo "Success! Repo added and available to use."
  389.     echo
  390.     echo "To use this repo, simply type the following and hit ENTER:"
  391.     echo "  pkg repo $repo_name"
  392.     echo
  393.   fi
  394.  
  395. done
  396.  
  397. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement