sup view sup @ rev 44

Add support for downloads, tiny debug command and improve README (main doc)
author Christophe Lincoln <pankso@slitaz.org>
date Fri Mar 10 00:00:36 2017 +0100 (2017-03-10)
parents 5fd828b6743f
children 66fa352226c4
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 -c cook $(gettext "Cook a package from the wok")
34 -r run $(gettext "Run sup_install without installing")
35 -e extract $(gettext "Extract a package to current dir")
36 -i install $(gettext "Install a new package")
37 -u update $(gettext "Update installed package")
38 -r remove $(gettext "Remove an installed package")
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 -r|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 indent $(($(tty_size) - 13)) "[ $(colorize 036 synced) ]"
296 else
297 [ "$online" ] && rm -f ${pkgsdb}
298 indent $(($(tty_size) - 13)) "[ $(colorize 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 # Scan installed packages
317 count="$(ls $installed | wc -l)"
318 char="$(echo $count | wc -L)"
319 in=$((7 + ${char}))
321 gettext "Installed packages:"
322 indent $(($(tty_size) - ${in})) "[ $(colorize 033 $count) ]"
323 cat << EOT
325 $(boldify $(gettext "Package") $(indent 30 $(gettext "Version")) \
326 $(indent 60 $(gettext "Status")))
327 $(separator "-")
328 EOT
329 for pkg in $(ls $installed)
330 do
331 . ${installed}/${pkg}/receip
332 db_sum=$(sqlite3 ${pkgsdb} \
333 "SELECT md5sum FROM pkgs WHERE name = '$pkg'")
335 # MD5sum not matching: new version our new build ?
336 if [ "$md5_sum" != "$db_sum" ]; then
338 vers=$(sqlite3 ${pkgsdb} \
339 "SELECT version FROM pkgs WHERE name = '$pkg'")
341 if [ "$VERSION" != "$vers" ]; then
342 status="$(colorize 032 $(gettext "update") )"
343 else
344 status="$(colorize 035 $(gettext "rebuilt"))"
345 fi
346 echo "$pkg $(indent 30 $VERSION) $(indent 60 $status)"
347 echo "$pkg" >> ${cache}/updates
348 fi
350 done
352 if [ -f "$cache/updates" ]; then
353 count=$(cat $cache/updates | wc -l)
354 separator "-"
355 gettext "Available updates:"; colorize 32 " $count"
356 newline
357 # --install
358 if [ "$install" ]; then
359 for pkg in $(cat $cache/updates); do
360 sup -i ${pkg}
361 done
362 fi
363 else
364 gettext "Packages are up-to-date"; echo
365 newline
366 fi
367 rm -f ${cache}/updates ;;
369 -r|remove)
370 # Remove a package
371 pkg="$2"
372 check_pkg_arg "$1"
373 if [ -d "$installed/$pkg" ]; then
374 remove_sup "$pkg"
375 else
376 gettext "Can't find installed package:"; colorize 31 " $pkg"
377 exit 1
378 fi ;;
380 -s|search)
381 # TODO: better :-)
382 query="$2"
383 newline
384 echo -n "$(colorize 33 $(gettext 'Searching for:'))"
385 boldify " $query"
386 separator
387 sqlite3 ${pkgsdb} << EOT
388 .headers on
389 .mode column
390 SELECT name, version, short_desc FROM pkgs WHERE name = '$2';
391 EOT
392 newline ;;
394 -l|list)
395 # only --db
396 if [ "$db" ]; then
397 newline
398 echo -n "$(colorize 33 $(gettext 'SUP packages in database:'))"
399 boldify " $(sqlite3 ${pkgsdb} 'SELECT COUNT(name) FROM pkgs')"
400 separator
401 sqlite3 ${pkgsdb} << EOT
402 .mode column
403 SELECT name, short_desc FROM pkgs;
404 EOT
405 newline; exit 0
406 fi
408 installed_nb=$(ls ${installed} | wc -l)
409 newline
410 echo -n "$(colorize 36 $(gettext 'Installed SUP packages:'))"
411 boldify " $installed_nb"
412 separator
413 for pkg in $(ls ${installed}); do
414 . ${installed}/${pkg}/receip
415 echo "$pkg $(indent 26 $VERSION) $(indent 42 $MAINTAINER)"
416 done
417 if [ "$installed_nb" == 0 ]; then
418 gettext "No sup packages installed"; echo
419 fi
420 newline
421 if [ -d "$wok" ]; then
422 echo -n "$(colorize 35 $(gettext 'SUP packages in your wok:'))"
423 boldify " $(ls ${wok} | wc -l)"
424 separator
425 for pkg in $(ls ${wok}); do
426 . ${wok}/${pkg}/receip
427 echo "$pkg $(indent 26 $VERSION) $(indent 42 $MAINTAINER)"
428 done
429 newline
430 fi
431 if [ -d "$cooked" ]; then
432 echo -n "$(colorize 33 $(gettext 'Cooked packages:'))"
433 boldify " $(ls $cooked | wc -l)"
434 fi
435 echo -n "$(colorize 33 $(gettext 'SUP packages in database:'))"
436 boldify " $(sqlite3 ${pkgsdb} 'SELECT COUNT(name) FROM pkgs')"
437 newline ;;
439 -d|debug)
440 # Packaging environment and debugger
441 newline
442 echo "$(colorize 35 $(gettext "Cooked packages:")) $cooked"
443 separator "-"
444 ls -1 ${cooked}; newline
445 echo "$(colorize 35 $(gettext "Downloads::")) $downloads"
446 separator "-"
447 ls -1 ${downloads}; newline ;;
449 ""|-h|help) help ;;
451 *)
452 # Display pkg info if installed
453 pkg="$1"
454 if [ -f "$installed/$pkg/receip" ]; then
455 . ${installed}/${pkg}/receip
456 newline
457 gettext "Installed package:"; colorize 36 " $PACKAGE $VERSION"
458 separator
459 echo "$SHORT_DESC"
460 newline
461 colorize 036 "$(gettext 'Installed files:') ($sup_size)"
462 cat ${installed}/${pkg}/files.list
463 newline
464 else
465 gettext "Can't find installed package:"; colorize 31 " $pkg"
466 fi ;;
468 esac && exit 0