Advertisement
s243a

Draft: ppa2pup (pgp)

Nov 24th, 2019
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 13.25 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # ppa2pup
  4. # convert an Ubuntu PPA repo file, into a Puppy compatible repo file.
  5. TMPDIR=/tmp/pkg/${whoami}
  6. # @TODO1 - fix last few fields of package entries (should contain the supported ubuntu distro, not current system)
  7. if [ -z "$PGP_VERIFY_CMD" ]; then
  8.   if [ ! -z `which gpgv` ]; then
  9.     PGP_VERIFY_CMD="$(which gpgv)"
  10.   elif [ ! -z `gpg` ]; then
  11.     PGP_VERIFY_CMD="$(which gpgv2)"
  12.   elif [ ! -z `gpg` ]; then
  13.     PGP_VERIFY_CMD="$(which gpg) --verify"
  14.   else
  15.     echo "ppa2pup: No pgp verify command" 1>&2
  16.   fi
  17. fi
  18. . /etc/DISTRO_SPECS
  19. if [ -d ~/.packages/repo ]; then
  20.   REPO_DIR=`realpath  ~/.packages/repo` || \
  21.   REPO_DIR=`readlink ~/.packages/repo` || \
  22.   REPO_DIR=~/.packages/repo
  23. elif [ -d /var/packages/repo ]; then
  24.   REPO_DIR=/var/packages/repo
  25. else
  26.   REPO_DIR=~/.packages
  27. fi
  28. #if [ "$DISTRO_BINARY_COMPAT" != "ubuntu" ] && [ "$DISTRO_BINARY_COMPAT" != "debian" ];then
  29. #  echo "Sorry, you must be running a .deb compatible Puppy Linux"
  30. #  echo "to use this tool. Your Puppy is based on '$DISTRO_BINARY_COMPAT'"
  31. #  exit 1
  32. #fi
  33.  
  34.  
  35. if [ ! "$1" ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]
  36. then
  37.  
  38.   echo "This script creates a Puppy-compatible repo file from a PPA or Debian repo."
  39.   echo
  40.   echo "For Launchpad PPA repos:"
  41.   echo
  42.   echo "  Usage: ppa2pup ppa:<user>/<repo> [debian|ubuntu] [bionic|stretch|artful|etc] [main|all|contrib|non-free|etc]"
  43.   echo
  44.   echo "Examples:"
  45.   echo
  46.   echo "  ppa2pup ppa:team-xbmc/ppa"
  47.   echo
  48.   echo "  ppa2pup ppa:team-xbmc/ppa ubuntu bionic"
  49.   echo
  50.   echo "  ppa2pup ppa:team-xbmc/ppa ubuntu artful"
  51.   echo
  52.   echo "  ppa2pup ppa:team-xbmc/ppa debian stretch"
  53.   echo
  54.   echo
  55.   echo "For other third-party Debian repos:"
  56.   echo
  57.   echo "  Usage: ppa2pup http://site.com/[debian|ubuntu]/ [stretch|bionic|etc] [main|contrib|non-free|etc]"
  58.   echo
  59.   echo "Examples:"
  60.   echo
  61.   echo "  ppa2pup http://rpms.litespeedtech.com/debian/"
  62.   echo
  63.   echo "  ppa2pup http://rpms.litespeedtech.com/debian/ stretch main"
  64.   echo
  65.   echo "  ppa2pup http://repo.steampowered.com/steam/ precise steam"
  66.   echo
  67.   echo "  ppa2pup http://http.kali.org/kali/ kali-bleeding-edge main contrib non-free"
  68.   echo
  69.   echo "NOTE: Any ommitted distro names or versions will be guessed."
  70.   echo
  71.   exit 1
  72.  
  73. fi
  74.  
  75.  
  76. RELEASE_DIR=/tmp/ppa_release
  77.  
  78. # create a dir to work in
  79. #mkdir -p /tmp/ppa_pkgs
  80. mkdir -p "$RELEASE_DIR"
  81.  
  82. # remove any old files
  83. #rm /tmp/ppa_pkgs/* 2>/dev/null
  84. #rm -rf "$RELEASE_DIR"/* 2>/dev/null
  85.  
  86. # we need to find the correct Packages.gz for the distro (debian/ubuntu),
  87. # distro version (stretch/bionic/etc), and architecture (i386/amd64)
  88. if [ -z "$SOURCES_LIST_OPT__arch" ]; then
  89.   arch='i386'
  90.   case $(uname -m) in
  91.     i*86) arch='i386'  ;;
  92.     *64)  arch='amd64' ;;
  93.   esac
  94. else
  95.   arch="$SOURCES_LIST_OPT__arch"
  96. fi
  97.  
  98.  
  99. if [[ "$1" =~ 'ppa:' ]]
  100. then
  101.  
  102.   # we got a 'PPA' URL, lets parse it and get the Packages.gz
  103.   ppaname="${1/*\//}"
  104.   username="${1/\/*/}"
  105.   username="${username//ppa:/}"
  106.   ppaname="${ppaname//ppa:/}"
  107.  
  108.   # get username, but strip special chars
  109.   PPA_NAME="${username//[-_:]/}"
  110.  
  111.   distro=${2:-$DISTRO_BINARY_COMPAT}
  112.   distro_ver=${3:-$DISTRO_COMPAT_VERSION}
  113.   repo_name="$distro_ver-$PPA_NAME"
  114.   repo_filename="Packages-$distro-$distro_ver-${PPA_NAME}"
  115.   repo_stream=${4:-main}
  116.   repo_stream2=''
  117.   repo_stream3=''
  118.   repo_stream4=''
  119.   [ "$5" != '' ] && repo_stream2=${5}
  120.   [ "$6" != '' ] && repo_stream3=${6}
  121.   [ "$7" != '' ] && repo_stream4=${7}
  122.  
  123.  
  124.   URL=http://ppa.launchpad.net/${username}/${ppaname}/${distro}/dists/${distro_ver}/${repo_stream}/binary-${arch}/Packages.gz
  125.   repo_url=http://ppa.launchpad.net/${username}/${ppaname}/${distro}/
  126.  
  127. elif [[ "$1" =~ 'http://' ]] || [[ "$1" =~ 'https://' ]];then
  128.  
  129.   # we got a Debian repo source URL, lets parse it and get the Packages.gz
  130.  
  131.   distro=${DISTRO_BINARY_COMPAT}
  132.   distro_ver=${2:-$DISTRO_COMPAT_VERSION}
  133.   repo_url=$(echo $1 | sed -e 's#/$##g')/
  134.   repo_stream=${3:-main}
  135.   repo_stream2=''
  136.   repo_stream3=''
  137.   repo_stream4=''
  138.   [ "$4" != '' ] && repo_stream2=${4}
  139.   [ "$5" != '' ] && repo_stream3=${5}
  140.   [ "$6" != '' ] && repo_stream4=${6}
  141.  
  142.   URL=$(echo $1 | sed -e 's#/$##g')/dists/${distro_ver}/${repo_stream}/binary-${arch}/Packages.gz
  143.   ARCH_PATH="/binary-${arch}/Packages.gz"
  144. else
  145.  
  146.   # didnt get ppa:foo/bar, exit with usage
  147.   $0 -h
  148.   exit 1
  149.  
  150. fi
  151.  
  152.  
  153.  
  154.  
  155.   # if we didn't get the name from the ppa:foo/bar style URL
  156.   if [[ ! "$1" =~ 'ppa:' ]]
  157.   then
  158.  
  159.     # check if this repo is already installed (find its name in Pkg sources files)
  160.     if [ -z "$repo_url" ]; then
  161.       PPA_NAME="$(grep -m1 "^${distro_ver}-${stream:-main}|" /root/.pkg/sources-all | cut -f1 -d'|' 2>/dev/null)"
  162.     else
  163.       PPA_NAME="$(grep "^${distro_ver}-${stream:-main}|" /root/.pkg/sources-all | grep -m1 "$repo_url"  | cut -f1 -d'|' 2>/dev/null)"
  164.       if [ -z "$PPA_NAME" ]; then
  165.          PPA_NAME="$(grep "^${distro_ver}-${stream:-main}-" /root/.pkg/sources-all | grep -m1 "$repo_url"  | cut -f1 -d'|' 2>/dev/null)"
  166.       fi
  167.     fi
  168.     # get repo name and filename
  169.     if [ "$PPA_NAME" = "" ];then
  170.       echo
  171.       read -e -p "Enter a repo name, such as '${distro_ver}-${stream:-main}':  " -i "${distro_ver}-${stream:-main}" PPA_NAME
  172.     fi
  173.     # replace any spaces or underscores with dashes, all lower case
  174.     PPA_NAME="$( echo "${PPA_NAME// /-}" | tr '_' '-' | tr '[:upper:]' '[:lower:]' )"
  175.  
  176.     repo_name="$PPA_NAME"
  177.     repo_filename="Packages-$distro-${PPA_NAME}"
  178.  
  179.   fi
  180.  
  181. PACKAGES_GZ="ppa_Packages.gz"
  182. PACKAGES_GZ_PATH="$RELEASE_DIR/$PACKAGES_GZ"
  183. PACKAGES_PATH="$RELEASE_DIR/${PACKAGES_GZ%.*}" #Remove extension
  184.  
  185.  
  186. for stream in $repo_stream $repo_stream2 $repo_stream3 $repo_stream4
  187. do
  188.  
  189.   [ "$stream" = '' ] && continue
  190.  
  191.   rm /"$PACKAGES_PATH" "$PACKAGES_GZ_PATH" 2>/dev/null
  192.   download_failed=false
  193.  
  194.   download_url=${URL//$repo_stream/$stream}
  195.  
  196.   wget --quiet $download_url -O "$PACKAGES_GZ_PATH" 1>/dev/null \
  197.     || download_failed=true
  198.  
  199.   if [ ! -f "$PACKAGES_GZ_PATH" ] || [ $download_failed = true ];then
  200.     echo
  201.     echo "ERROR: the PPA repo '$repo_name' not found for $distro $distro_ver:"
  202.     echo
  203.     echo "  $download_url"
  204.     echo
  205.     echo "You could try a different version of the repo."
  206.     echo
  207.     $0 -h
  208.     exit 1
  209.   fi
  210.  
  211.  
  212.   gunzip "$PACKAGES_GZ_PATH"
  213.  
  214.  
  215.   # if Packages file is empty, dont create a repo for it
  216.   if [ -z "$PACKAGES_PATH" ] || [ ! -s "$PACKAGES_PATH" ];then
  217.     continue
  218.   fi
  219.  
  220.  
  221.   if [ ! -z "$SOURCES_LIST_OPT__signed_by" ]; then
  222.     meta_url=${download_url//$ARCH_PATH/}
  223.     "$RELEASE_DIR"
  224.     mkdir -p "$RELEASE_DIR/$repo_name"
  225.     rm -f "$RELEASE_DIR/$repo_name/*" 2>/dev/null
  226.     meta_download_failed=false
  227.     wget --quiet "${meta_url}/Release" -O "$RELEASE_DIR/$repo_name/Release" 1>/dev/null && \
  228.     wget --quiet"${meta_url}/Release.gpg" -O "$RELEASE_DIR/$repo_name/Release.gpg" 1>/dev/null || \
  229.       meta_download_failed=true  
  230.     if [ "$meta_download_failed" = false ]; then
  231.       if ( cd $RELEASE_DIR/$repo_name;
  232.            $PGP_VERIFY_CMD --keyring "$SOURCES_LIST_OPT__signed_by" Release.pgp Release ); then
  233.          pgp_verified=maybe
  234.          ReleaseFile="$RELEASE_DIR/$repo_name/InRelease"    
  235.       else
  236.          pgp_verified=false
  237.       fi
  238.     else
  239.         meta_download_failed=false
  240.         wget --quiet "${meta_url}/InRelease" -O "$RELEASE_DIR/$repo_name/InRelease" 1>/dev/null \
  241.           || meta_download_failed=true        
  242.        if [ "$meta_download_failed" = false ]; then
  243.          if ( cd $RELEASE_DIR/$repo_name;
  244.             $PGP_VERIFY_CMD --keyring "$SOURCES_LIST_OPT__signed_by" InRelease ); then
  245.             pgp_verified=maybe
  246.             ReleaseFile="$RELEASE_DIR/$repo_name/InRelease"  
  247.          else
  248.            pgp_verified=false
  249.          fi        
  250.        fi  
  251.     fi
  252.     if [ "$pgp_verified" = false ]; then  
  253.       if [ ! -z "$SOURCES_LIST_OPT__Trusted" ] && [ "$SOURCES_LIST_OPT__Trusted" = yes ]; then
  254.         FAIL_CLASS=WARNING
  255.       else
  256.         FAIL_CLASS=ERROR
  257.       fi    
  258.       echo
  259.       echo "$FAIL_CLASS: could not verify pgp signature of Release/InRelease file"
  260.       echo
  261.       echo "  ${meta_url}"
  262.       if [ ! "$SOURCES_LIST_OPT__Trusted" ]; then #Maybe should exit if this
  263.         exit 1
  264.       fi
  265.     fi        
  266.     if [ SOURCES_LIST_OPT__Allow_Insecure = yes ]; then  
  267.       hash_function=( sha256sum md5sum sha1sum none )
  268.     elif [ "$SOURCES_LIST_OPT__allow_weak" = yes ]; then
  269.       hash_function=( sha256sum md5sum sha1sum )
  270.     else
  271.       hash_function=( sha256sum )
  272.     fi
  273.     for a_hash_fn in "${hash_function[@]}"; then
  274.       case "$a_hash_fn" in
  275.       sha256sum)
  276.         fsum="$(sha256sum "$file_path" | cut -f1 -d' ')"
  277.         if [ $(grep -m -c "$fsum" "$ReleaseFile" ) -gt 0 ]; then
  278.            break
  279.         fi ;;
  280.       md5sum)
  281.         fsum="$(md5sum "$file_path" | cut -f1 -d' ')"
  282.         if [ $(grep -m -c "$fsum" "$ReleaseFile" ) -gt 0 ]; then
  283.            echo "WARNING: weak checksum for $repo_name"
  284.            break
  285.         fi ;;
  286.       sha1sum)
  287.         fsum="$(sha1sum "$file_path" | cut -f1 -d' ')"
  288.         if [ $(grep -m -c "$fsum" "$ReleaseFile" ) -gt 0 ]; then
  289.            echo "WARNING: weak checksum for $repo_name"
  290.            break
  291.         fi ;;
  292.       none)
  293.         fsum="$(md5sum "$file_path" | cut -f1 -d' ')"
  294.         echo "WARNING: repo metadata unverified"
  295.         break ;;
  296.     fi
  297.   fi
  298.  
  299.   echo
  300.   echo "Found URL:"
  301.   echo
  302.   echo "  $download_url"
  303.   echo
  304.   echo "Repo to create:"
  305.   echo "   $repo_name"
  306.   echo
  307.   echo "Repo file to create:"
  308.   echo "  $REPO_DIR/$repo_filename"
  309.   echo
  310.  
  311.  
  312.  
  313.   rm /tmp/$repo_filename $REPO_DIR/$repo_filename &>/dev/null
  314.  
  315.  
  316. #---------------New AWK Version-------------------------------------------
  317.  
  318.             cat "$PACKAGES_PATH" | awk -v PKGOS="$distro" -v PKGOSVER="$distro_ver" -v REPOFNM="$repo_filename" \
  319. 'function fixdepends(s,   a,p,sout) {
  320.     split(s,a,",")
  321.     for (p in a) {
  322.         gsub(/[ \t]*\(.*\)|[ \t]\|.*|:any/,"",a[p])
  323.         sout = sout "," a[p]
  324.     }
  325.     sub(/^,/,"",sout) ; return sout;
  326. }
  327.  
  328. /^Package:/     { sub(/^Package: /,"");  PKG=$0; }
  329. /^Version:/     { sub(/^Version: /,"");  PKGVER=$0; }
  330. /^Filename:/    { sub(/^Filename: /,""); PKGPATH=$0; sub(/\/[^\/]*$/,"",PKGPATH); sub(/.*\//,""); PKGFILE=$0; }
  331. /^Priority:/    { sub(/^Priority: /,""); PKGPRIO=$0; }
  332. /^Section:/     { sub(/^Section: /,"");  PKGSECTION=$0; }
  333. /^Installed-Size:/ { sub(/^Installed-Size: /,"");  PKGSIZE=$0; }
  334. /^MD5sum:/      { sub(/^MD5sum: /,"");   PKGMD5=$0; }
  335. /^SHA1:/      { sub(/^SHA1: /,"");   SHA1=$0; }
  336. /^SHA256:/      { sub(/^SHA256: /,"");   SHA256=$0; }
  337. /^Depends:/     { sub(/^Depends: /,"");     PKGDEP=fixdepends($0) "," PKGDEP; }
  338. /^Pre-Depends:/ { sub(/^Pre-Depends: /,""); PKGDEP=fixdepends($0) "," PKGDEP; }
  339. /^Description:/ { sub(/^Description: /,""); PKGINFO=substr($0,1,200); }
  340. /^$/            { print PKG "_" PKGVER "|" PKG "|" PKGVER "||" PKGSECTION "|" PKGSIZE "|" PKGPATH "|" PKGFILE  "|" PKGDEP "|" PKGINFO "|" PKGOS "|" PKGOSVER "|"  PKGMD5 "|" SHA1 "|" SHA256 "|" PKGPRIO "|" REPOFNM  ;
  341.    
  342.                  PKG=""; PKGVER=""; PKGSECTION=""; PKGSIZE=""; PKGFILE=""; PKGPATH=""; PKGDEP=""; PKGINFO=""; PKGPRIO="";  PKGMD5=""; SHA1=""; SHA256=""; PKGDEP="";  }
  343. ' > /tmp/$repo_filename
  344.  
  345.  
  346.  
  347.   # sort & move the repo file
  348.   sort -u /tmp/$repo_filename > /tmp/${repo_filename}_sorted 2>/dev/null
  349.  
  350.  
  351.   if [ ! -f /tmp/${repo_filename}_sorted ];then
  352.     echo "Error: Repo file not created!"
  353.     exit 1
  354.   fi
  355.  
  356.   #mv  /tmp/${repo_filename}_sorted  $REPO_DIR/$repo_filename
  357.   set -x
  358.   cut -d'|' -f1-12 /tmp/${repo_filename}_sorted > $REPO_DIR/$repo_filename
  359.   [ -z "$PKGS_DIR" ] && export PKGS_DIR="$(realpath "${HOME}/.packages")"
  360.   [ -z "$HASHES_DIR" ] && export HASHES_DIR="$PKGS_DIR/hashes" && mkdir -p "$HASHES_DIR"
  361.   cut -d'|' -f1,2,7,8,13 /tmp/${repo_filename}_sorted > "$HASHES_DIR/${repo_filename//Packages/PKG_SUMS}"
  362.   cut -d'|' -f1,2,7,8,13,14,15 /tmp/${repo_filename}_sorted | sort -d -k 4 > /tmp/${repo_filename}_sorted_md5
  363.   awk -F '|' '{print $5 "|" $3 "|" $4}' /tmp/${repo_filename}_sorted_md5 > "$HASHES_DIR/${repo_filename//Packages/MD5}"
  364.   #cut -d'|' -f4,5 /tmp/${repo_filename}_sorted_md5 > "$HASHES_DIR/${repo_filename//Packages/SHA1}"
  365.   cut -d'|' -f4,7 /tmp/${repo_filename}_sorted_md5 > "$HASHES_DIR/${repo_filename//Packages/SHA256}"
  366.   set +x
  367.   echo "Success! File created."
  368.   echo
  369.  
  370.   fallback_repos="$(pkg repo-list | grep -v $repo_name | tr '\n' ' ')"
  371.   repo_entry="$repo_name|deb|$repo_filename|$repo_url||||$fallback_repos"
  372.  
  373.  
  374.   # if already added to ~/.pkg/sources[-all], remove it
  375.   if [ "$(cat ~/.pkg/sources     | grep -m1 "^$repo_name|")" != "" ] || \
  376.      [ "$(cat ~/.pkg/sources-all | grep -m1 "^$repo_name|")" != "" ];then
  377.     cat ~/.pkg/sources | grep -v "^$repo_name|" > /tmp/pkgsources
  378.     cat ~/.pkg/sources-all | grep -v "^$repo_name|" > /tmp/pkgsources-all
  379.     mv /tmp/pkgsources ~/.pkg/sources
  380.     mv /tmp/pkgsources-all ~/.pkg/sources-all
  381.   fi
  382.  
  383.  
  384.   # add repo entry to ~/.pkg/sources
  385.   pkg add-source "$repo_entry"
  386.   echo
  387.  
  388.   # refresh list of available repos
  389.   pkg update-sources
  390.   echo
  391.   echo "Repo info:"
  392.   pkg repo-info $repo_name
  393.   echo
  394.  
  395.  
  396.   if [ "$(cat ~/.pkg/sources | grep -m1 "^$repo_name|")" != "" ];then
  397.     echo "Success! Repo added and available to use."
  398.     echo
  399.     echo "To use this repo, simply type the following and hit ENTER:"
  400.     echo "  pkg repo $repo_name"
  401.     echo
  402.   fi
  403.  
  404. done
  405.  
  406. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement