tazpkg view modules/recharge @ rev 836

Add module "upgrade"; make bullet-proof code to work with "root" ("recharge" and "upgrade" modules); re-make depends/rdepends commands.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Sat Aug 15 17:41:08 2015 +0300 (2015-08-15)
parents f1c82b2d20aa
children a02e36d44d06
line source
1 #!/bin/sh
2 # TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
3 # recharge - TazPkg module
4 # Recharge packages databases from a mirror
7 # Options:
8 # [main|<repository>] Repository name to recharge (all if empty)
10 # Environment variables:
11 # root Root of the packages DB
12 # UA User Agent string ("TazPkg-<version>")
15 # Connect function libraries
16 . /lib/libtaz.sh
21 # Get TazPkg working environment
22 # ------------------------------
24 [ ! -d "$root/etc/slitaz" ] && mkdir -p "$root/etc/slitaz"
25 [ ! -e "$root/etc/slitaz/slitaz.conf" ] && cp /etc/slitaz/slitaz.conf "$root/etc/slitaz"
26 [ ! -e "$root/etc/slitaz/tazpkg.conf" ] && cp /etc/slitaz/tazpkg.conf "$root/etc/slitaz"
27 [ ! -e "$root/etc/slitaz-release" ] && echo 'cooking' > "$root/etc/slitaz-release"
29 # Read configuration
30 if [ -n "$root" ]; then
31 # Patch external conf files to correctly handle --root value
32 slitaz_conf=$(mktemp); cp "$root/etc/slitaz/slitaz.conf" $slitaz_conf
33 tazpkg_conf=$(mktemp); cp "$root/etc/slitaz/tazpkg.conf" $tazpkg_conf
34 sed -i "s| /| $root/|g; s|\"/|\"$root/|g" $slitaz_conf $tazpkg_conf
35 . $slitaz_conf; . $tazpkg_conf; rm $slitaz_conf $tazpkg_conf
36 else
37 . /etc/slitaz/slitaz.conf; . /etc/slitaz/tazpkg.conf
38 fi
40 # Silent make missing files
41 mkdir -p "$PKGS_DB"
42 [ ! -e "$PKGS_DB/mirror" ] && echo "$ONLINE_PKGS" > "$PKGS_DB/mirror"
47 # Functions
48 # ---------
50 # Download a file from specified mirror
52 get_from_mirror() {
53 case "$mirror" in
54 http://* | https://* | ftp://*)
55 busybox wget -c -q -T 30 -U $UA "$mirror$1" 2>/dev/null;;
56 *)
57 ln -sf "$mirror$1" .;;
58 esac
59 status
60 }
63 # When recharging errors occur
65 recharging_failed() {
66 # Restore database from bak files
67 action 'Restoring database files...'
68 [ -e 'ID' -a ! -e 'ID.bak' ] && rm ID
69 [ -e 'IDs' -a ! -e 'IDs.bak' ] && rm IDs
70 for file in $(ls $1/*.bak); do
71 mv -f $file ${file%.bak}
72 done
73 status
75 footer "$(colorize 31 "$(_ 'Recharging failed')")"
76 }
81 REPO="$1"
83 # What to recharge: main, or all, or selected undigest
84 case "$REPO" in
85 main) repo_to_recharge="$PKGS_DB";;
86 '') repo_to_recharge="$PKGS_DB $PKGS_DB/undigest/*";;
87 *) repo_to_recharge="$PKGS_DB/undigest/$REPO"
88 if [ ! -d "$repo_to_recharge" ]; then
89 _ "Repository \"%s\" doesn't exist." "$repo_to_recharge" >&2
90 exit 1
91 fi
92 ;;
93 esac
95 for path in $repo_to_recharge; do
96 [ ! -f $path/mirror ] && continue # skip
97 cd $path
98 # Mirror URL will have a trailing slash
99 mirror="$(cat mirror)"; mirror="${mirror%/}/"
101 # Repository name
102 if [ "$path" == "$PKGS_DB" ]; then
103 repo_name='Main'
104 else
105 repo_name="$(_n 'Undigest %s' "$(basename "$path")")"
106 fi
108 title 'Recharging repository "%s"' "$repo_name"
110 # Don't let ID be a symlink when using local repository.
111 if [ -h ID ]; then mv -f ID ID.lnk; cat ID.lnk > ID; rm ID.lnk; fi
112 if [ -h IDs ]; then mv -f IDs IDs.lnk; cat IDs.lnk > IDs; rm IDs.lnk; fi
114 [ -f ID ] && mv ID ID.bak # Compatibility with "old" ID
115 [ -f IDs ] && mv IDs IDs.bak
116 action 'Checking...'
117 get_from_mirror IDs
119 [ -e 'IDs' ] && awk '{print $1}' IDs > ID # Compatibility with "old" ID
120 [ -e 'IDs' ] && _ 'Database timestamp: %s' "$(date -d "@$(awk '{print $2}' IDs)" "+%x %R")"
122 # Check if recharging is needed
123 if [ -f 'IDs' ] && cmp -s IDs IDs.bak; then
124 footer "$(_ 'Repository "%s" is up to date.' "$repo_name")"
125 rm IDs.bak ID.bak
126 continue
127 fi
128 rm IDs.bak ID.bak 2>/dev/null
130 action 'Creating backup of the last packages list...'
131 for i in packages.desc packages.$SUM packages.txt packages.list \
132 packages.equiv files.list.lzma extra.list mirrors packages.info; do
133 [ -f "$i" ] && mv -f $i $i.bak 2>/dev/null
134 done
135 :; status # Always "[ Done ]"
137 # Download and extract bundle: extra.list, mirrors, files-list.md5,
138 # packages.{info,desc,md5,txt,list,equiv}
139 bundle='bundle.tar.lzma'
140 action 'Getting "%s"...' $bundle
141 get_from_mirror $bundle
142 if [ -f "$bundle" ]; then
143 busybox tar -xaf $bundle; rm $bundle
144 else
145 recharging_failed $path; continue
146 fi
148 # Download files.list.lzma
149 files_local='files.list.lzma'; files_remote='files-list.lzma'
150 if [ -e "$files_local.bak" ]; then
151 md5sum $files_local.bak | awk '{printf $1}' > files-list.md5.bak
152 if cmp -s files-list.md5 files-list.md5.bak; then
153 mv $files_local.bak $files_remote
154 else
155 action 'Getting "%s"...' $files_remote
156 get_from_mirror $files_remote
157 fi
158 else
159 action 'Getting "%s"...' $files_remote
160 get_from_mirror $files_remote
161 fi
163 if [ ! -e "$files_remote" ]; then
164 recharging_failed $path; continue
165 fi
166 mv -f $files_remote $files_local
168 # Remove old database files (but packages.list.bak, extra.list.bak)
169 for i in packages.desc packages.$SUM packages.txt packages.equiv \
170 files.list.lzma mirrors packages.info files-list.md5; do
171 [ -f "$i.bak" ] && rm $i.bak 2>/dev/null
172 done
174 footer "$(_ 'Last database is ready to use.')"
176 # Check diff
177 if [ -f 'packages.list.bak' ]; then
178 diff -u packages.list.bak packages.list | grep ^+[a-z] > packages.diff
179 rm packages.list.bak
180 if [ -f 'extra.list.bak' ]; then
181 if [ -f 'extra.list' ]; then
182 awk -F'|' '{print $1 " (extra)"}' extra.list > extra.list1
183 awk -F'|' '{print $1 " (extra)"}' extra.list.bak > extra.list1.bak
184 diff -u extra.list1.bak extra.list1 | grep ^+[a-z] >> packages.diff
185 rm extra.list.bak extra.list1 extra.list1.bak
186 else
187 mv extra.list.bak extra.list
188 fi
189 fi
190 sed -i s/+// packages.diff
192 new_pkgs=$(wc -l < packages.diff)
193 if [ "$new_pkgs" -gt 0 ]; then
194 title 'Mirrored packages diff'
195 cat packages.diff
196 footer "$(emsg "$(_p \
197 '%s new package on the mirror.' \
198 '%s new packages on the mirror.' $new_pkgs \
199 "<c 32>$new_pkgs</c>")")"
200 fi
201 else
202 longline "$(_ "Note that next time you recharge the list, a list of \
203 differences will be displayed to show new and upgradeable packages.")"
204 fi
205 done
206 newline