tazpkg annotate modules/getenv @ rev 864
Module "getenv": better checking of 'installed.info'; module "get": display abridged wget status, move here test for package already installed from "tazpkg" code
author | Aleksej Bobylev <al.bobylev@gmail.com> |
---|---|
date | Tue Nov 17 12:45:13 2015 +0200 (2015-11-17) |
parents | b6daeaa95431 |
children | 19fad7781af3 |
rev | line source |
---|---|
al@840 | 1 #!/bin/sh |
al@840 | 2 # TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg |
al@840 | 3 # getenv - TazPkg module |
al@840 | 4 # Get TazPkg working environment |
al@840 | 5 |
al@840 | 6 |
al@844 | 7 # Set up the aliases to guaranteed to work with Busybox applets rather with the GNU Coreutils ones |
al@844 | 8 # due to some incompatibilities. |
al@844 | 9 # Please don't hesitate to expand or shrink this list (with justification). |
al@844 | 10 for i in awk basename bzcat cat chmod chroot clear cmp cp cpio cut date dd diff dirname dpkg-deb \ |
al@844 | 11 du egrep fgrep find grep gzip head httpd id ln ls lzcat md5sum mkdir mktemp mv readlink \ |
al@844 | 12 realpath rm rmdir rpm rpm2cpio sed sort stat su tac tail tar tee touch tr tty uniq unlzma wc \ |
al@844 | 13 wget which xzcat zcat; do |
al@844 | 14 alias $i="busybox $i" |
al@844 | 15 done |
al@844 | 16 |
al@844 | 17 |
al@844 | 18 . /lib/libtaz.sh |
al@844 | 19 |
al@844 | 20 # Report error and finish work |
al@844 | 21 die() { longline "$(_ "$@")" >&2; exit 1; } |
al@844 | 22 |
al@844 | 23 # Show debug messages |
al@846 | 24 debug() { |
al@846 | 25 if [ -n "$debug" ]; then |
al@846 | 26 colorize 036 "$@" >&2 |
al@849 | 27 # $LOG is unavailable at the early stage on 'getenv' |
al@849 | 28 [ -n "$LOG" ] && echo -e "$(date +%f) $@" >> "${LOG/.log/.debug}" |
al@846 | 29 fi |
al@846 | 30 } |
al@844 | 31 |
al@844 | 32 debug "\n========\n$0 '$1' '$2' '$3' '$4'" |
al@844 | 33 |
al@840 | 34 # Check and re-create files and folders (if permissions enough) |
al@844 | 35 missing_file() { |
al@844 | 36 if [ ! -f "$1" ]; then |
al@844 | 37 case $(id -u) in |
al@844 | 38 0) mkdir -p "$(dirname "$1")"; touch "$1" |
al@844 | 39 [ -n "$2" ] && cp -a "$2" "$(dirname "$1")" |
al@844 | 40 ;; |
al@844 | 41 *) _ 'Missing: %s' "$1" >&2; die 'Please run tazpkg as root.';; |
al@844 | 42 esac |
al@844 | 43 fi |
al@844 | 44 } |
al@844 | 45 missing_dir() { |
al@844 | 46 if [ ! -d "$1" ]; then |
al@844 | 47 case $(id -u) in |
al@844 | 48 0) mkdir -p "$1";; |
al@844 | 49 *) _ 'Missing: %s' "$1" >&2; die 'Please run tazpkg as root.';; |
al@844 | 50 esac |
al@844 | 51 fi |
al@840 | 52 } |
al@840 | 53 |
al@840 | 54 # Fill empty file with value |
al@840 | 55 fill() { |
al@840 | 56 if [ ! -s "$1" ]; then |
al@840 | 57 case $(id -u) in |
al@840 | 58 0) echo "$2" > "$1";; |
al@844 | 59 *) _ 'File "%s" empty.' "$1" >&2; die 'Please run tazpkg as root.';; |
al@840 | 60 esac |
al@840 | 61 fi |
al@840 | 62 } |
al@840 | 63 |
al@840 | 64 |
al@840 | 65 |
al@840 | 66 |
al@844 | 67 # Normalize $root |
al@844 | 68 root="${root%/}" |
al@844 | 69 debug "root = '$root'" |
al@844 | 70 |
al@840 | 71 # Setup main config files |
al@844 | 72 missing_dir "$root/etc/slitaz/" |
al@844 | 73 missing_file "$root/etc/slitaz/slitaz.conf" '/etc/slitaz/slitaz.conf' |
al@844 | 74 missing_file "$root/etc/slitaz/tazpkg.conf" '/etc/slitaz/tazpkg.conf' |
al@844 | 75 missing_file "$root/etc/slitaz-release"; fill "$root/etc/slitaz-release" 'cooking' |
al@840 | 76 |
al@840 | 77 # Read configuration |
al@840 | 78 if [ -n "$root" ]; then |
al@840 | 79 # Patch external conf files to correctly handle --root value |
al@840 | 80 slitaz_conf=$(mktemp); cp "$root/etc/slitaz/slitaz.conf" "$slitaz_conf" |
al@840 | 81 tazpkg_conf=$(mktemp); cp "$root/etc/slitaz/tazpkg.conf" "$tazpkg_conf" |
al@840 | 82 sed -i "s| /| $root/|g; s|\"/|\"$root/|g" "$slitaz_conf" "$tazpkg_conf" |
al@840 | 83 . "$slitaz_conf"; . "$tazpkg_conf" |
al@840 | 84 rm "$slitaz_conf" "$tazpkg_conf" |
al@840 | 85 else |
al@840 | 86 . /etc/slitaz/slitaz.conf; . /etc/slitaz/tazpkg.conf |
al@840 | 87 fi |
al@840 | 88 |
al@844 | 89 debug "PKGS_DB = '$PKGS_DB'" |
al@844 | 90 debug "INSTALLED = '$INSTALLED'" |
al@844 | 91 debug "SLITAZ_LOGS = '$SLITAZ_LOGS'" |
al@844 | 92 debug "LOG = '$LOG'" |
al@844 | 93 |
al@840 | 94 BLOCKED="$PKGS_DB/blocked-packages.list" |
al@844 | 95 debug "BLOCKED = '$BLOCKED'" |
al@840 | 96 UP_LIST="$PKGS_DB/packages.up" |
al@844 | 97 debug "UP_LIST = '$UP_LIST'" |
al@844 | 98 debug "CACHE_DIR = '$CACHE_DIR'" |
al@844 | 99 SAVE_CACHE_DIR="$CACHE_DIR" |
al@844 | 100 |
al@840 | 101 |
al@840 | 102 # Re-create TazPkg working folders and files |
al@840 | 103 for dir in "$PKGS_DB" "$CACHE_DIR" "$INSTALLED" "$SLITAZ_LOGS"; do |
al@844 | 104 missing_dir "$dir" |
al@840 | 105 done |
al@840 | 106 for file in "$BLOCKED" "$UP_LIST" "$LOG" "$PKGS_DB/packages.info" "$PKGS_DB/mirror"; do |
al@844 | 107 missing_file "$file" |
al@840 | 108 done |
al@840 | 109 fill "$PKGS_DB/mirror" "${ONLINE_PKGS%/}/" |
al@840 | 110 |
al@844 | 111 |
al@840 | 112 # Check for installed.info |
al@840 | 113 info_path="$PKGS_DB/installed.info" |
al@844 | 114 missing_file "$info_path" |
al@840 | 115 if [ ! -s "$info_path" ]; then |
al@840 | 116 # Empty installed.info |
al@864 | 117 if [ -n "$(find "$INSTALLED" -name 'receipt')" ]; then |
al@844 | 118 # Some packages are installed |
al@844 | 119 if [ "$(id -u)" -eq 0 ]; then |
al@844 | 120 # Root can re-create installed.info |
al@844 | 121 _ 'File "%s" generated. Please wait...' 'installed.info' >&2 |
al@844 | 122 for pkg in $(find "$INSTALLED" -name receipt); do |
al@844 | 123 unset PACKAGE VERSION EXTRAVERSION CATEGORY SHORT_DESC WEB_SITE \ |
al@844 | 124 TAGS PACKED_SIZE UNPACKED_SIZE DEPENDS |
al@844 | 125 . $pkg |
al@844 | 126 SIZES=$(echo $PACKED_SIZE $UNPACKED_SIZE | sed 's|\.0||g') |
al@844 | 127 # remove newlines from some receipts |
al@844 | 128 DEPENDS=$(echo $DEPENDS) |
al@844 | 129 MD5="$(fgrep " $PACKAGE-$VERSION$EXTRAVERSION.tazpkg" "$PKGS_DB/installed.md5" | awk '{print $1}')" |
al@844 | 130 cat >> "$info_path" << EOT |
al@840 | 131 $PACKAGE $VERSION$EXTRAVERSION $CATEGORY $SHORT_DESC $WEB_SITE $TAGS $SIZES $DEPENDS $MD5 |
al@840 | 132 EOT |
al@844 | 133 done |
al@844 | 134 else |
al@844 | 135 # User can't re-create installed.info |
al@844 | 136 fill "$info_path" |
al@844 | 137 fi |
al@840 | 138 fi |
al@840 | 139 else |
al@840 | 140 # Non-empty installed.info |
al@840 | 141 |
al@840 | 142 # Check for md5 field (#9) in the installed.info: older version missed it |
al@840 | 143 if [ -n "$(awk -F$'\t' 'BEGIN{ n = "" } { if(NF != 9){ n = "o"; } } END{ print n }' $info_path)" ]; then |
al@840 | 144 if [ "$(id -u)" -eq 0 ]; then |
al@840 | 145 # Root can re-create it |
al@844 | 146 _n 'File "%s" generated. Please wait...' 'installed.info.new' >&2 |
al@840 | 147 awk -F$'\t' -vm="$PKGS_DB/installed.md5" 'BEGIN{OFS="\t"} |
al@840 | 148 { |
al@840 | 149 if (NF != 9) { |
al@840 | 150 pkg = $1 "-" $2 ".tazpkg"; |
al@840 | 151 "fgrep " pkg " " m " | cut -c-32" | getline $9; |
al@840 | 152 $9 = ($9 == "") ? "00000000000000000000000000000000" : $9; |
al@840 | 153 } |
al@840 | 154 print; |
al@840 | 155 }' $info_path > $info_path.new |
al@840 | 156 mv -f $info_path.new $info_path |
al@840 | 157 status |
al@840 | 158 else |
al@840 | 159 # User can't re-create it |
al@844 | 160 _ 'Old "%s".' 'installed.info' >&2; die 'Please run tazpkg as root.' |
al@840 | 161 fi |
al@840 | 162 fi |
al@840 | 163 fi |
al@840 | 164 |
al@840 | 165 |
al@844 | 166 # Check for packages.info |
al@844 | 167 if [ ! -s "$PKGS_DB/packages.info" -a "$(id -u)" -eq 0 -a "$0" != '@@MODULES@@/recharge' ]; then |
al@844 | 168 @@MODULES@@/recharge >&2 |
al@840 | 169 fi |
al@844 | 170 |
al@844 | 171 |
al@844 | 172 |
al@844 | 173 # Get repositories priority using $PKGS_DB/priority. |
al@844 | 174 # In this file undigest repos are called by their names and main mirror by 'main' |
al@844 | 175 |
al@844 | 176 PRIORITY="$( |
al@844 | 177 { |
al@844 | 178 [ -s "$PKGS_DB/priority" ] && cat "$PKGS_DB/priority" |
al@844 | 179 echo 'main' |
al@844 | 180 [ -d "$PKGS_DB/undigest" ] && ls "$PKGS_DB/undigest" |
al@844 | 181 } | awk -vv="$PKGS_DB/undigest/" '{ |
al@844 | 182 if(arr[$0] != 1) { arr[$0]=1; print v $0; } |
al@844 | 183 }' | sed 's|/undigest/main||')" |
al@844 | 184 debug "PRIORITY = '$PRIORITY'" |
al@844 | 185 |
al@844 | 186 |
al@844 | 187 # TazPkg version |
al@844 | 188 export VERSION=$(awk -F$'\t' '$1=="tazpkg"{print $2;exit}' "$PKGS_DB/installed.info") |
al@844 | 189 # User Agent |
al@844 | 190 export UA="TazPkg-${VERSION:-Unknown}" |
al@844 | 191 debug "UA = '$UA'" |
al@844 | 192 |
al@844 | 193 CUR_DIR="$(pwd)" |
al@844 | 194 |
al@844 | 195 debug '-- end getenv --' |