Advertisement
s243a

Draft: ppa2pup (awk)(4)

Nov 18th, 2019
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 13.32 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.     if [ -z "$repo_url" ]; then
  178.       PPA_NAME="$(grep -m1 "^${distro_ver}-${stream:-main}|" /root/.pkg/sources-all | cut -f1 -d'|' 2>/dev/null)"
  179.     else
  180.       PPA_NAME="$(grep "^${distro_ver}-${stream:-main}|" /root/.pkg/sources-all | grep -m1 "$repo_url"  | cut -f1 -d'|' 2>/dev/null)"
  181.       if [ -z "$PPA_NAME" ]; then
  182.          PPA_NAME="$(grep "^${distro_ver}-${stream:-main}-" /root/.pkg/sources-all | grep -m1 "$repo_url"  | cut -f1 -d'|' 2>/dev/null)"
  183.       fi
  184.     fi
  185.     # get repo name and filename
  186.     if [ "$PPA_NAME" = "" ];then
  187.       echo
  188.       read -e -p "Enter a repo name, such as '${distro_ver}-${stream:-main}':  " -i "${distro_ver}-${stream:-main}" PPA_NAME
  189.     fi
  190.     # replace any spaces or underscores with dashes, all lower case
  191.     PPA_NAME="$( echo "${PPA_NAME// /-}" | tr '_' '-' | tr '[:upper:]' '[:lower:]' )"
  192.  
  193.     repo_name="$PPA_NAME"
  194.     repo_filename="Packages-$distro-${PPA_NAME}"
  195.  
  196.   fi
  197.  
  198.   echo
  199.   echo "Found URL:"
  200.   echo
  201.   echo "  $download_url"
  202.   echo
  203.   echo "Repo to create:"
  204.   echo "   $repo_name"
  205.   echo
  206.   echo "Repo file to create:"
  207.   echo "  $REPO_DIR/$repo_filename"
  208.   echo
  209.  
  210.   ## break Packages file into separate files
  211.   #number=1
  212.   #cat /tmp/ppa_Packages | while read line
  213.   #do
  214.   #
  215.   #  echo "$line" | grep -qE '^Priority|^Installed-Size|^Maintainer|^MD5|^SHA|^Description-md5|^Suggests|^Provides|^Source|^Conflicts|^Breaks' && continue
  216.   #  echo "$line" >> /tmp/ppa_pkgs/pkginfo$number
  217.   #  [ "$line" = "" ] && number=$(($number + 1))
  218.   #
  219.   #  #[ $number -gt 5 ] && break # for testing only
  220.   #
  221.   #done
  222.  
  223.  
  224.   rm /tmp/$repo_filename $REPO_DIR/$repo_filename &>/dev/null
  225.  
  226.   # go through each file and make a pet.spec entry from it,
  227.   # and append that entry tp /tmp/ppa_pkgs/$repo_filename
  228. #---------------New AWK Version-------------------------------------------
  229.  
  230.             cat /tmp/ppa_Packages | awk -v PKGOS="$distro" -v PKGOSVER="$distro_ver" -v REPOFNM="$repo_filename" \
  231. 'function fixdepends(s,   a,p,sout) {
  232.     split(s,a,",")
  233.     for (p in a) {
  234.         gsub(/[ \t]*\(.*\)|[ \t]\|.*|:any/,"",a[p])
  235.         sout = sout "," a[p]
  236.     }
  237.     sub(/^,/,"",sout) ; return sout;
  238. }
  239.  
  240. /^Package:/     { sub(/^Package: /,"");  PKG=$0; }
  241. /^Version:/     { sub(/^Version: /,"");  PKGVER=$0; }
  242. /^Filename:/    { sub(/^Filename: /,""); PKGPATH=$0; sub(/.*\//,""); PKGFILE=$0; }
  243. /^Priority:/    { sub(/^Priority: /,""); PKGPRIO=$0; }
  244. /^Section:/     { sub(/^Section: /,"");  PKGSECTION=$0; }
  245. /^Installed-Size:/ { sub(/^Installed-Size: /,"");  PKGSIZE=$0; }
  246. /^MD5sum:/      { sub(/^MD5sum: /,"");   PKGMD5=$0; }
  247. /^Depends:/     { sub(/^Depends: /,"");     PKGDEP=fixdepends($0) "," PKGDEP; }
  248. /^Pre-Depends:/ { sub(/^Pre-Depends: /,""); PKGDEP=fixdepends($0) "," PKGDEP; }
  249. /^Description:/ { sub(/^Description: /,""); PKGINFO=substr($0,1,200); }
  250. #/^$/            { print PKG "|" PKGVER "|" PKGFILE "|" repo_url "/" PKGPATH "|" PKGPRIO "|" PKGSECTION "|" PKGMD5 "|" PKGDEP ;
  251. /^$/            { print PKG _ PKGVER "|" PKG "|" PKGVER "||" PKGSECTION "|" PKGSIZE "|" PKGFILE  "|" PKGPATH "|" PKGDEP "|" PKGINFO "|" PKGOS "|" PKGOSVER "|"  PKGMD5 "|"  PKGPRIO "|" REPOFNM  ;
  252.    
  253.                  PKG=""; PKGVER=""; PKGSECTION=""; PKGSIZE=""; PKGFILE=""; PKGPATH=""; PKGDEP=""; PKGINFO=""; PKGPRIO="";  PKGMD5="";  PKGDEP="";  }
  254. #"$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|"                
  255. ' > /tmp/$repo_filename
  256.  
  257. #  pkg_desc="$(echo  "$pkginfo" | grep -m1 '^Descripti' | cut -f2-200 -d' ')"
  258.  
  259. #           # remove duplicates, use the "later" version if duplicate packages are found
  260. #           < $REPO_DIR/$LOCAL_PKGDB > /tmp/t.$$ \
  261. #           awk -F"|" '{if (!a[$1]) b[n++]=$1; a[$1]=$0} END {for (i=0;i<n;i++) {print a[b[i]]}}'
  262. #           mv /tmp/t.$$ $REPO_DIR/$LOCAL_PKGDB
  263. #       fi
  264. #       if [ -z "$WITH_APT_DB" ] || [ $DRY_RUN ]; then rm -f $CHROOT_DIR$APT_PKGDB_DIR/"$apt_pkgdb"; fi
  265. #   done
  266.  
  267.  
  268. #---------------Replace with AWK Version-------------------------------------------    
  269.   #for pkginfo_file in $(ls -1 /tmp/ppa_pkgs/)
  270.   #do
  271.   #
  272.   #  [ ! -f /tmp/ppa_pkgs/$pkginfo_file ] && continue
  273.   #
  274.   #  pkginfo="`cat /tmp/ppa_pkgs/$pkginfo_file 2>/dev/null`"
  275.   #  [ "$pkginfo" = "" ] && continue
  276.   #
  277.   #  # convert deps list from:
  278.   #  #
  279.   #  #  nginx-full (<< 1.14.0-0+bionic0.1~) | libc6 (>= 2.4), libpam0g (>= 0.99.7.1)
  280.   #  #
  281.   #  # into:
  282.   #  #
  283.   #  #  +nginx-full-1.14.0-0+bionic0,+libc6-2.4,+libpam0g-0.99.7.1
  284.   #  #
  285.   #
  286.   #  pkgdeps=`echo "$pkginfo" | grep '^Depends: '`
  287.   #
  288.   #  newpkgdeps="$(echo "$pkgdeps" \
  289.   #    | sed \
  290.   #      -e 's/  / /g'     \
  291.   #      -e 's/ (= 2:/_/g' \
  292.   #      -e 's/ (= 1:/_/g' \
  293.   #      -e 's/ (<< /-/g'  \
  294.   #      -e 's/ (= /-/g'   \
  295.   #      -e 's/ (>= /-/g'  \
  296.   #      -e 's/ (<= /-/g'  \
  297.   #      -e 's/) / /g'     \
  298.   #      -e 's/), /,/g'    \
  299.   #      -e 's/)//g'       \
  300.   #      -e 's/, /,/g'     \
  301.   #      -e 's/ | /,/g'    \
  302.   #      -e 's/| /,/g'     \
  303.   #      -e 's/ |/,/g'     \
  304.   #      -e "s/,$//g"      \
  305.   #      -e 's/\.1~//g'    \
  306.   #      -e 's/\.2~//g'    \
  307.   #      -e 's/,/,+/g'     \
  308.   #      -e 's/-2:/_/g'    \
  309.   #      -e 's/-1:/_/g'    \
  310.   #      -e 's/Depends: /Depends: +/g' \
  311.   #    2>/dev/null)"
  312.   #
  313.   #  # replace olddeps entry with new one in the file
  314.   #  if [ ! -z "pkgdeps" ] && [ ! -z "$newpkgdeps" ] && [ -f /tmp/ppa_pkgs/$pkginfo_file ]
  315.   #  then
  316.   #
  317.   #    sed -i "s/${pkgdeps}/${newpkgdeps}/" /tmp/ppa_pkgs/$pkginfo_file 2>/dev/null
  318.   #
  319.   #  fi
  320.   #
  321.   #
  322.   #  # deps should now be in the right format in the file itself,
  323.   #  # update our info var
  324.   #  pkginfo="$(cat /tmp/ppa_pkgs/$pkginfo_file)"
  325.   #
  326.   #  # so lets build pet.specs entry:
  327.   #  # like this one:
  328.   #  #   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||
  329.   #
  330.   #  case $DISTRO_BINARY_COMPAT in
  331.   #    debian|ubuntu|trisquel|devuan) ver_sep='_';;
  332.   #    *) ver_sep='-';;
  333.   #  esac
  334.   #
  335.   #  pkg_build_no=''  # need to get this out of version
  336.   #  pkg_name="$(echo  "$pkginfo" | grep -m1 '^Package: ' | cut -f2 -d' ')"
  337.   #
  338.   #  pkgname_only="$pkg_name"
  339.   #  pkg_ver="$(echo   "$pkginfo" | grep -m1 '^Version: ' | sed -e 's/Version: [0-9]://g' -e 's/Version: //g')"
  340.   #  pkg_name="$pkg_name${ver_sep}$pkg_ver"
  341.   #  pkg_cat="$(echo   "$pkginfo" | grep -m1 '^Section: ' | cut -f2 -d' ')"
  342.   #  pkg_size="$(echo  "$pkginfo" | grep -m1 '^Size: '    | cut -f2 -d' ')"
  343.   #  pkg_size="$(($pkg_size / 1024))K"
  344.   #  pkg_path="$(echo "$pkginfo" | grep -m1 '^Filename: '| cut -f2 -d' ')"
  345.   #  pkg_fname="$(basename "$pkg_path")"
  346.   #  pkg_path="$(dirname "$pkg_path")"
  347.   #  pkg_deps="$(echo "$pkginfo" | grep -m1 '^Depends: ' | cut -f2 -d' ')"
  348.   #  pkg_desc="$(echo  "$pkginfo" | grep -m1 '^Descripti' | cut -f2-200 -d' ')"
  349.   #  pkg_os="$DISTRO_BINARY_COMPAT"
  350.   #  pkg_os_ver="$DISTRO_COMPAT_VERSION"
  351.   #
  352.   #  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|"
  353.   #
  354.   #  echo "$entry" >> /tmp/$repo_filename
  355.   #
  356.   #done
  357.   #
  358.   #
  359.   #rm /tmp/ppa_pkgs/* /tmp/${repo_filename}_sorted 2>/dev/null
  360. #------------------End Code Replaced with AWK Version---------------------------------
  361.  
  362.   # sort & move the repo file
  363.   sort -u /tmp/$repo_filename > /tmp/${repo_filename}_sorted 2>/dev/null
  364.  
  365.  
  366.   if [ ! -f /tmp/${repo_filename}_sorted ];then
  367.     echo "Error: Repo file not created!"
  368.     exit 1
  369.   fi
  370.  
  371.   #mv  /tmp/${repo_filename}_sorted  $REPO_DIR/$repo_filename
  372.   cut -f1-12 /tmp/${repo_filename}_sorted > $REPO_DIR/$repo_filename
  373.  
  374.   echo "Success! File created."
  375.   echo
  376.  
  377.   fallback_repos="$(pkg repo-list | grep -v $repo_name | tr '\n' ' ')"
  378.   repo_entry="$repo_name|deb|$repo_filename|$repo_url||||$fallback_repos"
  379.  
  380.  
  381.   # if already added to ~/.pkg/sources[-all], remove it
  382.   if [ "$(cat ~/.pkg/sources     | grep -m1 "^$repo_name|")" != "" ] || \
  383.      [ "$(cat ~/.pkg/sources-all | grep -m1 "^$repo_name|")" != "" ];then
  384.     cat ~/.pkg/sources | grep -v "^$repo_name|" > /tmp/pkgsources
  385.     cat ~/.pkg/sources-all | grep -v "^$repo_name|" > /tmp/pkgsources-all
  386.     mv /tmp/pkgsources ~/.pkg/sources
  387.     mv /tmp/pkgsources-all ~/.pkg/sources-all
  388.   fi
  389.  
  390.  
  391.   # add repo entry to ~/.pkg/sources
  392.   pkg add-source "$repo_entry"
  393.   echo
  394.  
  395.   # refresh list of available repos
  396.   pkg update-sources
  397.   echo
  398.   echo "Repo info:"
  399.   pkg repo-info $repo_name
  400.   echo
  401.  
  402.  
  403.   if [ "$(cat ~/.pkg/sources | grep -m1 "^$repo_name|")" != "" ];then
  404.     echo "Success! Repo added and available to use."
  405.     echo
  406.     echo "To use this repo, simply type the following and hit ENTER:"
  407.     echo "  pkg repo $repo_name"
  408.     echo
  409.   fi
  410.  
  411. done
  412.  
  413. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement