tazpkg view modules/recharge @ rev 884
Module 'check': fix to work with $root; start to add tests
author | Aleksej Bobylev <al.bobylev@gmail.com> |
---|---|
date | Sat Dec 05 16:14:14 2015 +0200 (2015-12-05) |
parents | 8a73a58ed3cb |
children | ce0db4329d29 |
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
18 # Get TazPkg working environment
19 . @@MODULES@@/getenv
24 # Functions
25 # ---------
27 # Download a file from specified mirror
29 get_from_mirror() {
30 debug "get_from_mirror($1)"
31 debug " mirror='$mirror'"
32 case "$mirror" in
33 http://* | https://* | ftp://*)
34 debug " wget -c -q -T 30 -U '$UA' '$mirror$1'"
35 wget -c -q -T 30 -U "$UA" "$mirror$1" 2>/dev/null;;
36 *)
37 debug " ln -sf '$mirror$1' ."
38 ln -sf "$mirror$1" .;;
39 esac
40 status
41 }
44 # When recharging errors occur
46 recharging_failed() {
47 # Restore database from bak files
48 action 'Restoring database files...'
49 [ -e 'ID' -a ! -e 'ID.bak' ] && rm ID
50 [ -e 'IDs' -a ! -e 'IDs.bak' ] && rm IDs
51 for file in $(ls "$1"/*.bak); do
52 mv -f "$file" "${file%.bak}"
53 done
54 status
56 footer "$(colorize 31 "$(_ 'Recharging failed')")"
57 }
62 REPO="$1"
64 # What to recharge: main, or all, or selected undigest
65 case "$REPO" in
66 main) repo_to_recharge="$PKGS_DB";;
67 '') repo_to_recharge="$PKGS_DB $PKGS_DB/undigest/*";;
68 *) repo_to_recharge="$PKGS_DB/undigest/$REPO"
69 if [ ! -d "$repo_to_recharge" ]; then
70 _ "Repository \"%s\" doesn't exist." "$repo_to_recharge" >&2
71 exit 1
72 fi
73 ;;
74 esac
76 for path in $repo_to_recharge; do
77 [ ! -s "$path/mirror" ] && continue # skip
78 cd "$path"
79 # Mirror URL will have a trailing slash
80 mirror=$(cat mirror); mirror="${mirror%/}/"
82 # Repository name
83 if [ "$path" == "$PKGS_DB" ]; then
84 repo_name='Main'
85 else
86 repo_name="$(_n 'Undigest %s' "$(basename "$path")")"
87 fi
89 title 'Recharging repository "%s"' "$repo_name"
91 # Don't let ID be a symlink when using local repository.
92 if [ -h ID ]; then mv -f ID ID.lnk; cat ID.lnk > ID; rm ID.lnk; fi
93 if [ -h IDs ]; then mv -f IDs IDs.lnk; cat IDs.lnk > IDs; rm IDs.lnk; fi
95 [ -f ID ] && mv ID ID.bak # Compatibility with "old" ID
96 [ -f IDs ] && mv IDs IDs.bak
97 action 'Checking...'
98 get_from_mirror IDs
100 [ -e 'IDs' ] && awk '{print $1}' IDs > ID # Compatibility with "old" ID
101 [ -e 'IDs' ] && _ 'Database timestamp: %s' "$(date -d "@$(awk '{print $2}' IDs)" "+%x %R")"
103 # Check if recharging is needed
104 if [ -f 'IDs' ] && [ -f 'packages.info' ] && [ -f 'files.list.lzma' ] && \
105 cmp -s IDs IDs.bak; then
106 footer "$(_ 'Repository "%s" is up to date.' "$repo_name")"
107 rm IDs.bak ID.bak
108 continue
109 fi
110 rm IDs.bak ID.bak 2>/dev/null
112 action 'Creating backup of the last packages list...'
113 for i in packages.desc packages.$SUM packages.txt packages.list \
114 packages.equiv files.list.lzma extra.list mirrors packages.info; do
115 [ -f "$i" ] && mv -f $i $i.bak 2>/dev/null
116 done
117 # Always "[ 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 get_from_mirror "$bundle"
125 if [ -f "$bundle" ]; then
126 tar -xaf "$bundle"; rm "$bundle"
127 else
128 recharging_failed "$path"; continue
129 fi
131 # Download files.list.lzma
132 files_local='files.list.lzma'; files_remote='files-list.lzma'
133 if [ -e "$files_local.bak" ]; then
134 md5sum "$files_local.bak" | awk '{printf $1}' > files-list.md5.bak
135 if cmp -s files-list.md5 files-list.md5.bak; then
136 mv "$files_local.bak" "$files_remote"
137 else
138 action 'Getting "%s"...' "$files_remote"
139 get_from_mirror "$files_remote"
140 fi
141 else
142 action 'Getting "%s"...' "$files_remote"
143 get_from_mirror "$files_remote"
144 fi
146 if [ ! -e "$files_remote" ]; then
147 recharging_failed "$path"; continue
148 fi
149 mv -f "$files_remote" "$files_local"
151 # Remove old database files (but packages.list.bak, extra.list.bak)
152 for i in packages.desc packages.$SUM packages.txt packages.equiv \
153 files.list.lzma mirrors packages.info files-list.md5; do
154 [ -f "$i.bak" ] && rm $i.bak 2>/dev/null
155 done
157 footer "$(_ 'Last database is ready to use.')"
159 # Check diff
160 if [ -f 'packages.list.bak' ]; then
161 diff -u packages.list.bak packages.list | grep ^+[a-z] > packages.diff
162 rm packages.list.bak
163 if [ -f 'extra.list.bak' ]; then
164 if [ -f 'extra.list' ]; then
165 awk -F'|' '{print $1 " (extra)"}' extra.list > extra.list1
166 awk -F'|' '{print $1 " (extra)"}' extra.list.bak > extra.list1.bak
167 diff -u extra.list1.bak extra.list1 | grep ^+[a-z] >> packages.diff
168 rm extra.list.bak extra.list1 extra.list1.bak
169 else
170 mv extra.list.bak extra.list
171 fi
172 fi
173 sed -i s/+// packages.diff
175 new_pkgs=$(wc -l < packages.diff)
176 if [ "$new_pkgs" -gt 0 ]; then
177 title 'Mirrored packages diff'
178 cat packages.diff
179 footer "$(emsg "$(_p \
180 '%s new package on the mirror.' \
181 '%s new packages on the mirror.' "$new_pkgs" \
182 "<c 32>$new_pkgs</c>")")"
183 fi
184 else
185 longline "$(_ "Note that next time you recharge the list, a list of \
186 differences will be displayed to show new and upgradeable packages.")"
187 fi
188 done
189 newline
191 # Close tazpkg-notify
192 echo "quit" > /tmp/tazpkg-notify.fifo