tazpkg view modules/recharge @ rev 834

Remove all --options from positional parameters; make "recharge" module; re-make "up" and "search" commands.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri Aug 14 16:55:14 2015 +0300 (2015-08-14)
parents
children 4fbdffec7f48
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
17 # Get TazPkg configuration variables
18 . "$root/etc/slitaz/slitaz.conf"
19 . "$root/etc/slitaz/tazpkg.conf"
20 PKGS_DB="$root$PKGS_DB"
22 # Fallback actions
23 mkdir -p "$PKGS_DB"
24 [ ! -e "$PKGS_DB/mirror" ] && echo "$ONLINE_PKGS" > "$PKGS_DB/mirror"
27 # Functions
28 # ---------
30 # Download a file from specified mirror
32 download_from() {
33 # input: "<mirror_url>+" "file_name"
34 local i
35 for i in $1; do
36 case "$i" in
37 # Mirror URL can have a trailing slash or not.
38 http://* | https://* | ftp://*)
39 busybox wget -c -q -T 30 -U $UA ${i%/}/$2 2>/dev/null && break ;;
40 *)
41 ln -sf ${i%/}/$2 . && break ;;
42 esac
43 done
44 }
47 # When recharging errors occur
49 recharging_failed() {
50 # Restore database from bak files
51 action 'Restoring database files...'
52 [ -e 'ID' -a ! -e 'ID.bak' ] && rm ID
53 [ -e 'IDs' -a ! -e 'IDs.bak' ] && rm IDs
54 for file in $(ls $1/*.bak); do
55 mv -f $file ${file%.bak}
56 done
57 status
59 footer "$(colorize 31 "$(_ 'Recharging failed')")"
60 }
65 REPO="$1"
67 # What to recharge: main, or all, or selected undigest
68 case "$REPO" in
69 main) repo_to_recharge="$PKGS_DB";;
70 '') repo_to_recharge="$PKGS_DB $PKGS_DB/undigest/*";;
71 *) repo_to_recharge="$PKGS_DB/undigest/$REPO"
72 if [ ! -d "$repo_to_recharge" ]; then
73 _ "Repository \"%s\" doesn't exist." "$repo_to_recharge" >&2
74 exit 1
75 fi
76 ;;
77 esac
79 for path in $repo_to_recharge; do
80 [ ! -f $path/mirror ] && continue # skip
81 cd $path
82 mirror="$(cat mirror)"
84 # Repository name
85 if [ "$path" == "$PKGS_DB" ]; then
86 repo_name='Main'
87 else
88 repo_name="$(_n 'Undigest %s' "$(basename "$path")")"
89 fi
91 title 'Recharging repository "%s"' "$repo_name"
93 # Don't let ID be a symlink when using local repository.
94 if [ -h ID ]; then mv -f ID ID.lnk; cat ID.lnk > ID; rm ID.lnk; fi
95 if [ -h IDs ]; then mv -f IDs IDs.lnk; cat IDs.lnk > IDs; rm IDs.lnk; fi
97 [ -f ID ] && mv ID ID.bak # Compatibility with "old" ID
98 [ -f IDs ] && mv IDs IDs.bak
99 download_from "$mirror" IDs
100 [ -e 'IDs' ] && awk '{print $1}' IDs > ID # Compatibility with "old" ID
102 # Check if recharging is needed
103 if [ -f 'IDs' ] && cmp -s IDs IDs.bak; then
104 action 'Checking...'; status # "Fake" message
105 footer "$(_ 'Repository "%s" is up to date.' "$repo_name")"
106 rm IDs.bak ID.bak
107 continue
108 fi
109 rm IDs.bak ID.bak 2>/dev/null
111 [ -e 'IDs' ] && _ 'Database timestamp: %s' "$(date -d "@$(awk '{print $2}' IDs)" "+%x %R")"
113 action 'Creating backup of the last packages list...'
114 for i in packages.desc packages.$SUM packages.txt packages.list \
115 packages.equiv files.list.lzma extra.list mirrors packages.info; do
116 [ -f "$i" ] && mv -f $i $i.bak 2>/dev/null
117 done
118 :; status
120 # Download and extract bundle: extra.list, mirrors, files-list.md5,
121 # packages.{info,desc,md5,txt,list,equiv}
122 bundle='bundle.tar.lzma'
123 action 'Getting "%s"...' $bundle
124 download_from "$mirror" $bundle
125 status
126 if [ -f "$bundle" ]; then
127 busybox tar -xaf $bundle; rm $bundle
128 else
129 recharging_failed $path; continue
130 fi
132 # Download files.list.lzma
133 files_local='files.list.lzma'; files_remote='files-list.lzma'
134 if [ -e "$files_local.bak" ]; then
135 md5sum $files_local.bak | awk '{printf $1}' > files-list.md5.bak
136 if cmp -s files-list.md5 files-list.md5.bak; then
137 mv $files_local.bak $files_remote
138 else
139 action 'Getting "%s"...' $files_remote
140 download_from "$mirror" $files_remote
141 status
142 fi
143 else
144 action 'Getting "%s"...' $files_remote
145 download_from "$mirror" $files_remote
146 status
147 fi
149 if [ ! -e "$files_remote" ]; then
150 recharging_failed $path; continue
151 fi
152 mv -f $files_remote $files_local
154 # Remove old database files (but packages.list.bak, extra.list.bak)
155 for i in packages.desc packages.$SUM packages.txt packages.equiv \
156 files.list.lzma mirrors packages.info files-list.md5; do
157 [ -f "$i.bak" ] && rm $i.bak 2>/dev/null
158 done
160 footer "$(_ 'Last database is ready to use.')"
162 # Check diff
163 if [ -f 'packages.list.bak' ]; then
164 diff -u packages.list.bak packages.list | grep ^+[a-z] > packages.diff
165 rm packages.list.bak
166 if [ -f 'extra.list.bak' ]; then
167 if [ -f 'extra.list' ]; then
168 awk -F'|' '{print $1 " (extra)"}' extra.list > extra.list1
169 awk -F'|' '{print $1 " (extra)"}' extra.list.bak > extra.list1.bak
170 diff -u extra.list1.bak extra.list1 | grep ^+[a-z] >> packages.diff
171 rm extra.list.bak extra.list1 extra.list1.bak
172 else
173 mv extra.list.bak extra.list
174 fi
175 fi
176 sed -i s/+// packages.diff
178 new_pkgs=$(wc -l < packages.diff)
179 if [ "$new_pkgs" -gt 0 ]; then
180 title 'Mirrored packages diff'
181 cat packages.diff
182 footer "$(emsg "$(_p \
183 '%s new package on the mirror.' \
184 '%s new packages on the mirror.' $new_pkgs \
185 "<c 32>$new_pkgs</c>")")"
186 fi
187 else
188 longline "$(_ "Note that next time you recharge the list, a list of \
189 differences will be displayed to show new and upgradeable packages.")"
190 fi
191 done
192 newline