tazpkg view modules/getenv @ rev 898

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