tazpkg view modules/recharge @ rev 955

modules/search: allow search file with dash at start: tazpkg -sf "-spi"
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri Dec 22 00:02:54 2017 +0200 (2017-12-22)
parents ce0db4329d29
children 9d683c983e2e
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 " cp '$mirror$1' ."
38 cp "$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 # skip?
78 [ ! -s "$path/mirror" ] && continue
79 cd "$path"
80 # Mirror URL will have a trailing slash
81 mirror=$(cat mirror); mirror="${mirror%/}/"
83 # Repository name
84 if [ "$path" == "$PKGS_DB" ]; then
85 repo_name='Main'
86 else
87 repo_name="$(_n 'Undigest %s' "$(basename "$path")")"
88 fi
90 title 'Recharging repository "%s"' "$repo_name"
92 # Don't let ID be a symlink when using local repository.
93 if [ -h ID ]; then mv -f ID ID.lnk; cat ID.lnk > ID; rm ID.lnk; fi
94 if [ -h IDs ]; then mv -f IDs IDs.lnk; cat IDs.lnk > IDs; rm IDs.lnk; fi
96 # Compatibility with "old" ID
97 [ -f ID ] && mv ID ID.bak
98 [ -f IDs ] && mv IDs IDs.bak
99 action 'Checking...'
100 get_from_mirror IDs
102 # Compatibility with "old" ID
103 [ -e 'IDs' ] && awk '{print $1}' IDs > ID
104 [ -e 'IDs' -a -z "$quiet" ] && \
105 _ 'Database timestamp: %s' "$(date -d "@$(awk '{print $2}' IDs)" "+%x %R")"
107 # Check if recharging is needed
108 if [ -f 'IDs' ] && [ -f 'packages.info' ] && [ -f 'files.list.lzma' ] && \
109 cmp -s IDs IDs.bak; then
110 footer "$(_ 'Repository "%s" is up to date.' "$repo_name")"
111 rm IDs.bak ID.bak
112 continue
113 fi
114 rm IDs.bak ID.bak 2>/dev/null
116 action 'Creating backup of the last packages list...'
117 for i in packages.desc packages.$SUM packages.txt packages.list \
118 packages.equiv files.list.lzma extra.list mirrors packages.info; do
119 [ -f "$i" ] && mv -f $i $i.bak 2>/dev/null
120 done
121 # Always "[ Done ]"
122 :; status
124 # Download and extract bundle: extra.list, mirrors, files-list.md5,
125 # packages.{info,desc,md5,txt,list,equiv}
126 bundle='bundle.tar.lzma'
127 action 'Getting "%s"...' "$bundle"
128 get_from_mirror "$bundle"
129 if [ -f "$bundle" ]; then
130 tar -xaf "$bundle"; rm "$bundle"
131 else
132 recharging_failed "$path"; continue
133 fi
135 # Download files.list.lzma
136 files_local='files.list.lzma'; files_remote='files-list.lzma'
137 if [ -e "$files_local.bak" ]; then
138 md5sum "$files_local.bak" | awk '{printf $1}' > files-list.md5.bak
139 if cmp -s files-list.md5 files-list.md5.bak; then
140 mv "$files_local.bak" "$files_remote"
141 else
142 action 'Getting "%s"...' "$files_remote"
143 get_from_mirror "$files_remote"
144 fi
145 else
146 action 'Getting "%s"...' "$files_remote"
147 get_from_mirror "$files_remote"
148 fi
150 if [ ! -e "$files_remote" ]; then
151 recharging_failed "$path"; continue
152 fi
153 mv -f "$files_remote" "$files_local"
155 # Remove old database files (but packages.list.bak, extra.list.bak)
156 for i in packages.desc packages.$SUM packages.txt packages.equiv \
157 files.list.lzma mirrors packages.info files-list.md5; do
158 [ -f "$i.bak" ] && rm $i.bak 2>/dev/null
159 done
161 footer "$(_ 'Last database is ready to use.')"
163 # Check diff
164 if [ -f 'packages.list.bak' ]; then
165 diff -u packages.list.bak packages.list | grep ^+[a-z] > packages.diff
166 rm packages.list.bak
167 if [ -f 'extra.list.bak' ]; then
168 if [ -f 'extra.list' ]; then
169 awk -F'|' '{print $1 " (extra)"}' extra.list > extra.list1
170 awk -F'|' '{print $1 " (extra)"}' extra.list.bak > extra.list1.bak
171 diff -u extra.list1.bak extra.list1 | grep ^+[a-z] >> packages.diff
172 rm extra.list.bak extra.list1 extra.list1.bak
173 else
174 mv extra.list.bak extra.list
175 fi
176 fi
177 sed -i s/+// packages.diff
179 new_pkgs=$(wc -l < packages.diff)
180 if [ "$new_pkgs" -gt 0 ]; then
181 title 'Mirrored packages diff'
182 cat packages.diff
183 footer "$(emsg "$(_p \
184 '%s new package on the mirror.' \
185 '%s new packages on the mirror.' "$new_pkgs" \
186 "<c 32>$new_pkgs</c>")")"
187 fi
188 else
189 longline "$(_ "Note that next time you recharge the list, a list of \
190 differences will be displayed to show new and upgradeable packages.")"
191 fi
192 done
193 newline
195 # Close tazpkg-notify
196 echo "quit" > /tmp/tazpkg-notify.fifo