Advertisement
s243a

Draft: ppa2pup (pgp) (2)

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