sup view sup @ rev 45

Improve update
author Christophe Lincoln <pankso@slitaz.org>
date Fri Mar 10 08:57:55 2017 +0100 (2017-03-10)
parents d86e4e894a97
children 9ff1a3400fb1
line source
1 #!/bin/sh
2 #
3 # SliTaz Users Package - Minimal cmdline tools - README for devel info
4 #
5 # Copyright (C) 2017 SliTaz GNU/Linux - BSD License
6 # Author: Christophe Lincoln <pankso@slitaz.org>
7 #
9 # Get system wide libsup.sh or development version if working in Hg folder
10 if [ -f "/usr/lib/slitaz/libsup.sh" ]; then
11 . /usr/lib/slitaz/libsup.sh
12 fi
13 if [ -f "./libsup.sh" ]; then
14 . ./libsup.sh
15 fi
17 # Sanity check
18 if ! [ -d "/home/$(id -un)" ]; then
19 gettext "Not a valid user:"; colorize 31 " $(id -un)" && exit 1
20 fi
21 mkdir -p ${cache} ${installed} ${downloads}
23 #
24 # Functions
25 #
27 help() {
28 cat << EOT
30 $(boldify $(gettext "Usage:")) $(basename $0) [command|package] [package]
32 $(boldify $(gettext "Commands:"))
33 -i install $(gettext "Install a new package")
34 -u update $(gettext "Update installed package")
35 -r remove $(gettext "Remove an installed package")
36 -c cook $(gettext "Cook a package from the wok")
37 run $(gettext "Run sup_install without installing")
38 -e extract $(gettext "Extract a package to current dir")
39 -n new $(gettext "Create a new SUP package :-)")
40 -l list $(gettext "List packages: installed/wok/db")
41 -s search $(gettext "Search for packages in the database")
42 -d debug $(gettext "Debug info and sup environment")
44 $(boldify $(gettext "Options:"))
45 --install update $(gettext "Install all updates from mirror")
46 --force update $(gettext "Force recharging packages database")
47 --db list $(gettext "List all packages in database")
48 --init cook $(gettext "Initialize sup cook environment")
49 --verbose all $(gettext "Display more output messages")
51 $(colorize 33 Manual) man sup
52 $(colorize 33 Documentation) http://scn.slitaz.org/?d=en/sup
54 EOT
55 exit 0
56 }
58 cook_init() {
59 mkdir -p ${wok} ${cooked}
60 if [ ! -d "$supcook/wok" ]; then
61 ln -s ${wok} ${supcook}/wok
62 fi
63 }
65 # check_pkg_arg "command"
66 check_pkg_arg() {
67 if [ ! "$pkg" ]; then
68 gettext "Missing package name or file path:"
69 boldify " sup $1 [package|file.sup]" && exit 1
70 fi
71 }
73 # new_receip "package"
74 new_receip() {
75 cat > ${wok}/${1}/receip << EOT
76 # SliTaz User Package receip
78 PACKAGE="$1"
79 VERSION=""
80 SHORT_DESC=""
81 MAINTAINER=""
82 LICENSE=""
83 WEB_SITE=""
85 # Sup dependencies will be automatically downloaded but system wide
86 # deps must be manually installed by root.
87 SUP_DEPS=""
88 DEPENDS=""
90 # Here are the optional install rules (download, configure, etc)
91 sup_install() {
92 echo ""
93 }
95 EOT
96 }
98 #
99 # Commands
100 #
102 case "$1" in
104 -c|cook)
105 # Cook in $cache/cook and mv pkg to a visible dir
106 pkg="$2"
107 receip="$wok/$pkg/receip"
108 cache="$cache/cook"
110 # Handle --init
111 if [ "$init" ]; then
112 gettext "Initializing sup cook environment..."
113 cook_init; status; exit 0
114 fi
116 # Sanity check
117 check_pkg_arg "$1"
118 if [ ! -f "$receip" ]; then
119 gettext "Can't find receip in wok for:"; colorize 31 " $pkg"; exit 1
120 fi
122 newline
123 echo -n "$(colorize 33 $(gettext 'Building package:'))"
124 colorize 35 " $pkg"
125 separator
126 rm -rf ${cache} && mkdir -p ${cache} ${cooked}
128 # Check receip
129 [ "$verbose" ] && gettext "Checking receip variables..."
130 . ${receip}
131 for var in PACKAGE VERSION SHORT_DESC MAINTAINER LICENSE WEB_SITE
132 do
133 value="$(. $receip; eval echo \$$var)"
134 case "$var" in
135 PACKAGE|VERSION|SHORT_DESC|MAINTAINER|LICENSE)
136 if [ ! "$value" ]; then
137 gettext "Empty variable:"; colorize 31 " $var=\"\""
138 exit 1
139 fi ;;
140 WEB_SITE)
141 if [ ! "$value" ]; then
142 gettext "Empty variable:"; colorize 31 " $var=\"\""
143 gettext "You can use:"
144 boldify " http://scn.slitaz.org/?sup&pkg=$PACKAGE"
145 exit 1
146 fi ;;
147 esac
148 done
149 [ "$verbose" ] && status
151 # Set $PACKAGE-$VERSION.sup and copy pkg to cache
152 supfile="$PACKAGE-$VERSION.sup"
153 cp -rf ${wok}/${pkg} ${cache}/${supfile%.sup}
154 cd ${cache}/${supfile%.sup}
156 # Move to hidden dir: ~/.local ~/.config ~./icons
157 for dir in local config icons; do
158 [ -d "files/${dir}" ] && mv files/${dir} files/.${dir}
159 done
161 # Add $cook_date
162 sed -i "/^cook_date=/"d receip
163 echo "cook_date=\"$(date "+%Y-%m-%d %H:%M")\"" >> receip
165 gettext "Creating compressed archive..."
166 find files | cpio -o -H newc --quiet | lzma e files.lzma -si
167 rm -rf files
168 find . -print | cpio -o -H newc --quiet > ${cooked}/${supfile}
169 status
171 size="$(du -sh $cooked/$supfile | cut -d " " -f 1)"
172 echo "$(gettext 'Packages:') $(colorize 035 $supfile) ($size)"
173 rm -rf ${cache} && newline ;;
175 run)
176 # Run sup_install() without any installation to help package creation
177 pkg="$2"
178 receip="$wok/$pkg/receip"
179 workdir="$supcook/sup_install"
181 # Sanity check
182 check_pkg_arg "$1"
183 if [ ! -f "$receip" ]; then
184 gettext "Can't find receip in wok for:"; colorize 31 " $pkg"; exit 1
185 fi
187 newline
188 echo "$(colorize 33 $(gettext 'Running') sup_install\(\):) $(colorize 35 $pkg)"
189 separator
190 [ "$clean" ] && rm -rf ${workdir}
191 mkdir -p ${workdir} && cd ${workdir}
192 . ${wok}/${pkg}/receip
193 gettext "Checking package dependencies"
194 deps="$(echo $DEPENDS | wc -w)"
195 in=$((8 + ${deps}))
196 indent $(($(tty_size) - ${in})) "[ $(colorize 033 $deps) ]"
197 check_sys_deps || exit 1
198 sup_install
199 separator
200 gettext "Working folder:"; echo " $workdir"
201 newline ;;
203 -n|new)
204 pkg="$2"
205 if [ ! "$pkg" ]; then
206 newline
207 gettext "New package name:"; echo -n " "; read pkg
208 fi
209 newline
210 [ ! "$pkg" ] && exit 0
211 [ ! -d "$wok" ] && cook_init
212 if [ -d "$wok/$pkg" ]; then
213 gettext "Package already exists in wok:"; colorize 35 " $pkg"
214 newline && exit 0
215 fi
216 gettext "Creating new package files and tree..."
217 mkdir -p ${wok}/${pkg}/files
218 new_receip "$pkg"; status
219 gettext "Executing your favorite editor..."
220 editor ${wok}/${pkg}/receip; status
221 gettext "To cook your package:"; colorize 33 " sup cook $pkg"
222 newline ;;
224 -e|extract)
225 # Extract package.sup to current dir
226 pkg="$2"
228 # Sanity check
229 check_pkg_arg "$1"
230 if [ ! -f "$pkg" ]; then
231 gettext "Can't find package file:"; colorize 31 " $pkg" && exit 1
232 fi
234 newline
235 echo -n "$(colorize 33 $(gettext 'Extract package:'))"
236 colorize 36 " $(basename $pkg)"
237 separator
239 extract_sup "$pkg"
241 echo "$(gettext 'Packages:') $(colorize 036 $pkg) ($size)"
242 newline ;;
244 -i|install)
245 # Download/extract run install() in $cache/install
246 pkg="$2"
247 check_pkg_arg "$1"
248 newline
250 # Sup pkg in current dir
251 if [ -f "$pkg" ]; then
252 install_sup "$pkg" && exit 0
253 fi
255 # Sup cooked from wok
256 if [ -f "$wok/$pkg/receip" ]; then
257 . $wok/$pkg/receip
258 if [ -f "$cooked/$PACKAGE-$VERSION.sup" ]; then
259 install_sup "$cooked/$PACKAGE-$VERSION.sup" && exit 0
260 fi
261 fi
263 # Sup on mirror
264 [ ! -f "${pkgsdb}" ] && sup -u
266 if sqlite3 ${pkgsdb} "SELECT name FROM pkgs WHERE name = '$pkg'" >/dev/null
267 then
268 vers=$(sqlite3 ${pkgsdb} "SELECT version FROM pkgs WHERE name = '$pkg'")
269 mkdir -p ${cache}/wget && cd ${cache}/wget
270 # Download
271 download "$pkg" "${mirror}/${pkg}-${vers}.sup"
272 install_sup "${pkg}-${vers}.sup"
273 rm -rf ${cache}/wget && exit 0
274 fi
275 gettext "Can't find sup package:"; colorize 31 " $pkg" ;;
277 -u|update)
278 # Update packages
279 online=""
280 newline
281 colorize 33 $(gettext "SUP Update")
282 separator
284 # Skip HTTP/1.1 404 Not Found: 2>/dev/null
285 if ! wget -q -T 5 --spider ${mirror}/packages.sql 2>/dev/null; then
286 gettext "Mirror is unreachable"; newline
287 else
288 online=0
289 fi
291 # Newer packages.sql ?
292 gettext "Checking packages.sql..."
293 db_sum=$(wget -q "${server}?sup=dbsum" -O -)
294 if echo "$db_sum $pkgsdb" | md5sum -s -c -; then
295 info 036 "synced"
296 else
297 [ "$online" ] && rm -f ${pkgsdb}
298 info 035 "update"
299 fi
301 # --force
302 [ "$force" ] && rm -f ${pkgsdb}
304 # No packages.sql: get latest!
305 if [ ! -f "$pkgsdb" ] && [ "$online" ]; then
307 download "db" "${mirror}/packages.sql" "${supdb}"
309 timestamp=$(sqlite3 ${pkgsdb} 'SELECT timestamp FROM info' \
310 | awk '{printf $1 " " $2}')
311 gettext "Database timestamp:"
312 indent $(($(tty_size) - 23)) "[ $(colorize 30 $timestamp) ]"
313 log "$(gettext 'Updated packages database:') $timestamp"
314 fi
316 # Exit now if no packages.sql
317 if [ ! -f "$pkgsdb" ]; then
318 gettext "Please try later. Missing packages database"; echo; exit 1
319 fi
321 # Scan installed packages
322 count="$(ls $installed | wc -l)"
323 char="$(echo $count | wc -L)"
324 in=$((7 + ${char}))
326 gettext "Installed packages:"
327 info 033 "$count"; newline
328 echo -n $(boldify $(gettext "Package") $(indent 30 $(gettext "Version")))
329 info 40 $(gettext "Status")
330 separator "-"
332 for pkg in $(ls $installed)
333 do
334 . ${installed}/${pkg}/receip
336 name=$(sqlite3 ${pkgsdb} \
337 "SELECT name FROM pkgs WHERE name = '$pkg'")
338 if [ ! "$name" ]; then
339 #echo -n "$pkg $(indent 30 $VERSION)"
340 #info 036 "$(gettext "local")"
341 continue
342 else
343 db_sum=$(sqlite3 ${pkgsdb} \
344 "SELECT md5sum FROM pkgs WHERE name = '$pkg'")
346 # MD5sum not matching: new version our new build ?
347 if [ "$md5_sum" != "$db_sum" ]; then
348 echo -n "$pkg $(indent 30 $VERSION)"
349 vers=$(sqlite3 ${pkgsdb} \
350 "SELECT version FROM pkgs WHERE name = '$pkg'")
352 if [ "$VERSION" != "$vers" ]; then
353 info 032 "$(gettext "$vers")"
354 else
355 info 035 "$(gettext "rebuilt")"
356 fi
357 echo "$pkg" >> ${cache}/updates
358 fi
359 fi
360 unset name db_sum vers
361 done
363 if [ -f "$cache/updates" ]; then
364 count=$(cat $cache/updates | wc -l)
365 separator "-"
366 gettext "Available updates:"; colorize 32 " $count"
367 newline
368 # --install
369 if [ "$install" ]; then
370 for pkg in $(cat $cache/updates); do
371 sup -i ${pkg}
372 done
373 fi
374 else
375 gettext "Packages are up-to-date"; echo
376 newline
377 fi
378 rm -f ${cache}/updates ;;
380 -r|remove)
381 # Remove a package
382 pkg="$2"
383 check_pkg_arg "$1"
384 if [ -d "$installed/$pkg" ]; then
385 remove_sup "$pkg"
386 else
387 gettext "Can't find installed package:"; colorize 31 " $pkg"
388 exit 1
389 fi ;;
391 -s|search)
392 # TODO: better :-)
393 query="$2"
394 newline
395 echo -n "$(colorize 33 $(gettext 'Searching for:'))"
396 boldify " $query"
397 separator
398 sqlite3 ${pkgsdb} << EOT
399 .headers on
400 .mode column
401 SELECT name, version, short_desc FROM pkgs WHERE name = '$2';
402 EOT
403 newline ;;
405 -l|list)
406 # only --db
407 if [ "$db" ]; then
408 newline
409 echo -n "$(colorize 33 $(gettext 'SUP packages in database:'))"
410 boldify " $(sqlite3 ${pkgsdb} 'SELECT COUNT(name) FROM pkgs')"
411 separator
412 sqlite3 ${pkgsdb} << EOT
413 .mode column
414 SELECT name, short_desc FROM pkgs;
415 EOT
416 newline; exit 0
417 fi
419 installed_nb=$(ls ${installed} | wc -l)
420 newline
421 echo -n "$(colorize 36 $(gettext 'Installed SUP packages:'))"
422 boldify " $installed_nb"
423 separator
424 for pkg in $(ls ${installed}); do
425 . ${installed}/${pkg}/receip
426 echo "$pkg $(indent 26 $VERSION) $(indent 42 $MAINTAINER)"
427 done
428 if [ "$installed_nb" == 0 ]; then
429 gettext "No sup packages installed"; echo
430 fi
431 newline
432 if [ -d "$wok" ]; then
433 echo -n "$(colorize 35 $(gettext 'SUP packages in your wok:'))"
434 boldify " $(ls ${wok} | wc -l)"
435 separator
436 for pkg in $(ls ${wok}); do
437 . ${wok}/${pkg}/receip
438 echo "$pkg $(indent 26 $VERSION) $(indent 42 $MAINTAINER)"
439 done
440 newline
441 fi
442 if [ -d "$cooked" ]; then
443 echo -n "$(colorize 33 $(gettext 'Cooked packages:'))"
444 boldify " $(ls $cooked | wc -l)"
445 fi
446 echo -n "$(colorize 33 $(gettext 'SUP packages in database:'))"
447 boldify " $(sqlite3 ${pkgsdb} 'SELECT COUNT(name) FROM pkgs')"
448 newline ;;
450 -d|debug)
451 # Packaging environment and debugger
452 newline
453 echo "$(colorize 35 $(gettext "Cooked packages:")) $cooked"
454 separator "-"
455 ls -1 ${cooked}; newline
456 echo "$(colorize 35 $(gettext "Downloads::")) $downloads"
457 separator "-"
458 ls -1 ${downloads}; newline ;;
460 ""|-h|help) help ;;
462 *)
463 # Display pkg info if installed
464 pkg="$1"
465 if [ -f "$installed/$pkg/receip" ]; then
466 . ${installed}/${pkg}/receip
467 newline
468 gettext "Installed package:"; colorize 36 " $PACKAGE $VERSION"
469 separator
470 echo "$SHORT_DESC"
471 newline
472 colorize 036 "$(gettext 'Installed files:') ($sup_size)"
473 cat ${installed}/${pkg}/files.list
474 newline
475 else
476 gettext "Can't find installed package:"; colorize 31 " $pkg"
477 fi ;;
479 esac && exit 0