Magellan Linux

Annotation of /trunk/mage/usr/lib/mage/convert_databases_smage.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1642 - (hide annotations) (download) (as text)
Fri Jan 13 18:37:52 2012 UTC (12 years, 3 months ago) by niro
File MIME type: application/x-sh
File size: 14168 byte(s)
-honor the PCATEGORIE deprecation and make the scripts work with the latest changes to smage/mage
1 niro 489 #!/bin/bash
2    
3 niro 1642 : ${MAGERC="/etc/mage.rc"}
4    
5 niro 489 source /etc/mage.rc.global
6 niro 1642 source ${MAGERC}
7 niro 489 source ${MLIBDIR}/mage4.functions.sh
8 niro 1642 source ${MLIBDIR}/smage2.functions.sh
9 niro 489 source ${MLIBDIR}/sql_functions.sh
10    
11     die()
12     {
13     echo -e "Exited ${BASH_SOURCE} at line no ${BASH_LINENO}."
14     echo -e "$@"
15     exit 1
16     }
17    
18     sql()
19     {
20     local sqlcmd="$*"
21     [[ -z ${sqlcmd} ]] && die "no sqlcmd given."
22    
23     sqlite3 -nullvalue 'NULL' -list -separator '|' ${DBFILE} << EOF || die "error running '$@'"
24     ${sqlcmd};
25     EOF
26     }
27    
28     create_database_layout()
29     {
30     sql "create table categories(id integer primary key, pcat text unique)"
31     sql "create table packages(id integer primary key,
32     pname text,
33     pver text,
34     pbuild text,
35     pcat_id numeric,
36     state text,
37     provide text,
38     pkgtype text
39     )"
40     sql "create table depends(id integer primary key, pkg_id numeric, relation text, pcat_id numeric, pname text, pver text, pbuild text)"
41     sql "create table sdepends(id integer primary key, pkg_id numeric, relation text, pcat_id numeric, pname text, pver text, pbuild text)"
42     sql "create table packages_info(id integer primary key, pkg_id numeric, arch text, md5 text, mtime numeric, homepage text, description text, size numeric)"
43     #sql "create table packages_content(id integer primary key, pkginfo_id numeric, char text, dirs text, files text, pipes text, symlinks text)"
44     sql "create table packages_content(id integer primary key, pkginfo_id numeric, char blob, dirs blob, files blob, pipes blob, symlinks blob)"
45     # sql "create table packages_routines(id integer primary key,
46     # pkg_id numeric,
47     # mincludes text,
48     # preinstall text,
49     # postinstall text,
50     # preremove text,
51     # postremove text,
52     # extra_functions text,
53     # extra_vars text)"
54     sql "create table packages_routines(id integer primary key,
55     pkg_id numeric,
56     mincludes text,
57     preinstall numeric,
58     postinstall numeric,
59     preremove numeric,
60     postremove numeric,
61     script text)"
62     }
63    
64     convert_smage_db()
65     {
66     local i
67     local smagefile
68 niro 1642 local PCAT
69 niro 489 local PNAME
70     local PVER
71     local PBUILD
72     local pcatid
73     local STATE
74     local pkgid
75     local dep
76     local PKGNAME
77     local DESCRIPTION
78     local HOMEPAGE
79     local DEPEND
80     local SDEPEND
81     local PROVIDE
82     local PKGTYPE
83     local preinstall
84     local postinstall
85     local preremove
86     local postremove
87     local sym
88     local depname
89     local depver
90     local depbuild
91     local depcat
92     local depcatid
93     local md5file
94     local md5sum
95    
96     # create categories
97     cat ${SMAGESCRIPTSDIR}/.known_categories | while read pcat
98     do
99     case ${pcat} in
100     include|profiles) continue;;
101     esac
102    
103     sql "insert into categories (pcat) values('${pcat}')"
104     done
105    
106     # create packages
107     for smagefile in $(find ${SMAGESCRIPTSDIR}/glibc-nptl -type f -name '*'.smage2 | sort)
108     do
109     case ${DEBUG} in
110     1|true) echo "converting ${smagefile}" ;;
111     esac
112    
113 niro 1642 smagesource ${smagefile}
114 niro 489
115     # substitute r from pbuild
116     PBUILD="${PBUILD/r/}"
117    
118 niro 1642 pcatid=$(sql "select id from categories where pcat='${PCAT}'")
119 niro 489 sql "insert into packages(
120     pname,
121     pver,
122     pbuild,
123     pcat_id,
124     state,
125     provide,
126     pkgtype
127     )
128     values(
129     '${PNAME}',
130     '${PVER}',
131     '${PBUILD}',
132     '${pcatid}',
133     '${STATE}',
134     '${PROVIDE}',
135     '${PKGTYPE}'
136     )"
137    
138     pkgid=$(sql "select id from packages where pname='${PNAME}' and pver='${PVER}' and pbuild='${PBUILD}'")
139     # depends
140     while read sym dep
141     do
142     [[ -z ${dep} ]] && continue
143     depcat="$(dep2pcat ${dep})"
144     depcatid=$(sql "select id from categories where pcat='${depcat}'")
145     depname="$(dep2pname ${dep})"
146     depver="$(dep2pver ${dep})"
147     sql "insert into depends(pkg_id,
148     relation,
149     pcat_id,
150     pname,
151     pver,
152     pbuild)
153     values('${pkgid}',
154     '${sym}',
155     '${depcatid}',
156     '${depname}',
157     '${depver}',
158     '${depbuild}')"
159     done << EOF
160     ${DEPEND}
161     EOF
162     # sdepends
163     while read sym dep
164     do
165     [[ -z ${dep} ]] && continue
166     depcat="$(dep2pcat ${dep})"
167     depcatid=$(sql "select id from categories where pcat='${depcat}'")
168     depname="$(dep2pname ${dep})"
169     depver="$(dep2pver ${dep})"
170     sql "insert into sdepends(pkg_id,
171     relation,
172     pcat_id,
173     pname,
174     pver,
175     pbuild)
176     values('${pkgid}',
177     '${sym}',
178     '${depcatid}',
179     '${depname}',
180     '${depver}',
181     '${depbuild}')"
182     done << EOF
183     ${SDEPEND}
184     EOF
185    
186     for arch in i686 x86_64
187     do
188 niro 1642 md5file="${MAGEDIR}/${PCAT}/${PNAME}/md5/${PNAME}-${PVER}-${arch}-r${PBUILD}.md5"
189 niro 489 [[ -f ${md5file} ]] && md5sum="$(cat ${md5file} | cut -d' ' -f1)"
190    
191     # fix descriptions
192     DESCRIPTION="$(echo ${DESCRIPTION} | sed s:\':\'\':g)"
193    
194     sql "insert into packages_info(pkg_id, arch, md5, description, homepage)
195     values('${pkgid}','${arch}','${md5sum}','${DESCRIPTION}','${HOMEPAGE}')"
196     done
197    
198     # using $INHERITS directly
199     #mincludes="$(grep -r 'minclude ' ${magefile} | cut -d: -f2 | sed s:minclude\ ::)"
200    
201     # # #
202     # # # install routines & includes
203     # # #
204     # # # needs special magic: the last line of typeset has no ';'
205     # # # but to eval the function we must append it
206     # # # so this sed deletes the last line of the typeset output
207     # # # and replaces it with an ;} and a newline between
208     # # #
209     # # # but we will not add this fun when no postinstall etc are given
210     # # # -> [[ -n $(typeset -f postinstall ]] &&
211     # # #
212     # # # also the character ' is a problem while importing to sql
213     # # # because we use ' as escape char. so we replace it with ''
214     # # #
215     # # # $([[ -n $(typeset -f postinstall) ]] && echo -e "$(typeset -f postinstall | sed s:\':\'\':g | sed '$d');\n}")
216     # # #
217     # # sql "insert into packages_routines(pkg_id,
218     # # mincludes,
219     # # postinstall,
220     # # preinstall,
221     # # preremove,
222     # # postremove)
223     # # values('${pkgid}',
224     # # '${mincludes}',
225     # # '$([[ -n $(typeset -f postinstall) ]] && echo -e "$(typeset -f postinstall | sed s:\':\'\':g | sed '$d');\n}")',
226     # # '$([[ -n $(typeset -f preinstall) ]] && echo -e "$(typeset -f preinstall | sed s:\':\'\':g | sed '$d');\n}")',
227     # # '$([[ -n $(typeset -f preremove) ]] && echo -e "$(typeset -f preremove | sed s:\':\'\':g | sed '$d');\n}")',
228     # # '$([[ -n $(typeset -f postremove) ]] && echo -e "$(typeset -f postremove | sed s:\':\'\':g | sed '$d');\n}")')"
229    
230     local do_preinst=0
231     local do_postinst=0
232     local do_prerm=0
233     local do_postrm=0
234     local do_script=0
235     local script=""
236    
237     [[ -n $(typeset -f preinstall) ]] && do_preinst=1 && do_script=1
238     [[ -n $(typeset -f postinstall) ]] && do_postinst=1 && do_script=1
239     [[ -n $(typeset -f preremove) ]] && do_prerm=1 && do_script=1
240     [[ -n $(typeset -f postremove) ]] && do_postrm=1 && do_script=1
241    
242     [[ ${do_script} = 1 ]] && script="$(basename ${DBFILE} .sql).routines/${pkgid}/routines.sh"
243    
244     sql "insert into packages_routines(pkg_id,
245     mincludes,
246     postinstall,
247     preinstall,
248     preremove,
249     postremove,
250     script)
251     values('${pkgid}',
252     '${INHERITS}',
253     '${do_postinst}',
254     '${do_pretinst}',
255     '${do_prerm}',
256     '${do_postrm}',
257     '${script}')"
258    
259     # create it only if do_script=1
260     if [[ ${do_script} = 1 ]]
261     then
262     # create a routines script
263     local routines=$(dirname ${DBFILE})/${script}
264     install -d $(dirname ${routines})
265    
266     echo "#!/bin/sh" > ${routines}
267     echo "# routines.sh script for #${pkgid}, ${PNAME}-${PVER}-${PBUILD}" >> ${routines}
268     echo >> ${routines}
269    
270    
271     # special functions and variables
272     if [[ -n ${SPECIAL_VARS} ]]
273     then
274     local i
275     for i in ${SPECIAL_VARS}
276     do
277     # being tricky here :)
278     echo "${i}=\"$(eval echo \$${i})\"" >> ${routines}
279     done
280     echo >> ${routines}
281     fi
282    
283     # add special functions
284     if [[ -n ${SPECIAL_FUNCTIONS} ]]
285     then
286     local i
287     for i in ${SPECIAL_FUNCTIONS}
288     do
289     # add to mage (quotes needed !)
290     typeset -f "${i}" >> ${routines}
291     echo >> ${routines}
292     # unset to be safe (quotes needed !)
293     unset "${i}"
294     done
295     echo >> ${routines}
296     fi
297    
298     # postinstall and co
299     for i in preinstall postinstall preremove postremove
300     do
301     if [[ -n $(typeset -f "${i}") ]]
302     then
303     typeset -f "${i}" >> ${routines}
304     echo >> ${routines}
305     fi
306     done
307    
308     # create start logic
309     echo >> ${routines}
310     echo 'case $1 in' >> ${routines}
311     echo ' preinstall) preinstall ;;' >> ${routines}
312     echo ' postinstall) postinstall ;;' >> ${routines}
313     echo ' preremove) preremove ;;' >> ${routines}
314     echo ' postremove) postremove ;;' >> ${routines}
315     echo 'esac' >> ${routines}
316     echo >> ${routines}
317     fi
318    
319    
320 niro 1642 unset PCAT
321 niro 489 unset PNAME
322     unset PVER
323     unset PBUILD
324     unset STATE
325     unset DESCRIPTION
326     unset HOMEPAGE
327     unset PKGTYPE
328     unset PKGNAME
329     unset DEPEND
330     unset SDEPEND
331     unset PROVIDE
332     unset SPECIAL_VARS
333     unset SPECIAL_FUNCTIONS
334     unset INHERITS
335    
336     # unset these functions
337     unset -f preinstall
338     unset -f postinstall
339     unset -f preremove
340     unset -f postremove
341     done
342     }
343    
344     convert_install_db()
345     {
346     local i
347     local magefile
348     local pcat
349     local pname
350     local pver
351     local pbuild
352     local pcatid
353     local state
354     local preinstall
355     local postinstall
356     local preremove
357     local postremove
358     local pkgid
359     local dep
360     local PKGNAME
361     local STATE
362     local DESCRIPTION
363     local HOMEPAGE
364     local DEPEND
365     local SDEPEND
366     local PROVIDE
367     local PKGTYPE
368     local preinstall
369     local postinstall
370     local preremove
371     local postremove
372     local sym
373     local depname
374     local depver
375     local depbuild
376     local depcat
377     local depcatid
378     local md5file
379     local md5sum
380     local pkginfoid
381     local entrydir
382    
383     # create categories
384     for i in virtual ${INSTALLDB}/*
385     do
386     pcat="$(basename ${i})"
387     case ${pcat} in
388     include|profiles) continue;;
389     esac
390    
391     sql "insert into categories (pcat) values('${pcat}')"
392     done
393    
394     # create packages
395     for magefile in $(find ${INSTALLDB} -type f -name *.mage| sort)
396     do
397     case ${DEBUG} in
398     1|true) echo "converting ${magefile}" ;;
399     esac
400    
401     pcat="$(magename2pcat ${magefile})"
402     pname="$(magename2pname ${magefile})"
403     pver="$(magename2pver ${magefile})"
404     pbuild="$(magename2pbuild ${magefile})"
405     # substitute r from pbuild
406     pbuild="${pbuild/r/}"
407    
408     source ${magefile}
409    
410     pcatid=$(sql "select id from categories where pcat='${pcat}'")
411     sql "insert into packages(
412     pname,
413     pver,
414     pbuild,
415     pcat_id,
416     state,
417     provide,
418     pkgtype
419     )
420     values(
421     '${pname}',
422     '${pver}',
423     '${pbuild}',
424     '${pcatid}',
425     '${STATE}',
426     '${PROVIDE}',
427     '${PKGTYPE}'
428     )"
429    
430     pkgid=$(sql "select id from packages where pname='${pname}' and pver='${pver}' and pbuild='${pbuild}'")
431     # depends
432     while read sym dep
433     do
434     [[ -z ${dep} ]] && continue
435     depcat="$(dep2pcat ${dep})"
436     depcatid=$(sql "select id from categories where pcat='${depcat}'")
437     depname="$(dep2pname ${dep})"
438     depver="$(dep2pver ${dep})"
439     sql "insert into depends(pkg_id,
440     relation,
441     pcat_id,
442     pname,
443     pver,
444     pbuild)
445     values('${pkgid}',
446     '${sym}',
447     '${depcatid}',
448     '${depname}',
449     '${depver}',
450     '${depbuild}')"
451     done << EOF
452     ${DEPEND}
453     EOF
454     # sdepends
455     while read sym dep
456     do
457     [[ -z ${dep} ]] && continue
458     depcat="$(dep2pcat ${dep})"
459     depcatid=$(sql "select id from categories where pcat='${depcat}'")
460     depname="$(dep2pname ${dep})"
461     depver="$(dep2pver ${dep})"
462     sql "insert into sdepends(pkg_id,
463     relation,
464     pcat_id,
465     pname,
466     pver,
467     pbuild)
468     values('${pkgid}',
469     '${sym}',
470     '${depcatid}',
471     '${depname}',
472     '${depver}',
473     '${depbuild}')"
474     done << EOF
475     ${SDEPEND}
476     EOF
477    
478     DESCRIPTION="$(echo ${DESCRIPTION} | sed s:\':\'\':g)"
479     sql "insert into packages_info(pkg_id, arch, md5, description, homepage)
480     values('${pkgid}','${ARCH}','${md5sum}','${DESCRIPTION}','${HOMEPAGE}')"
481    
482    
483     # install routines & includes
484     sql "insert into packages_routines(pkg_id,
485     mincludes,
486     postinstall,
487     preinstall,
488     preremove,
489     postremove)
490     values('${pkgid}',
491     '${mincludes}',
492     '$(typeset -f postinstall | sed s:\':\'\':g)',
493     '$(typeset -f preinstall | sed s:\':\'\':g)',
494     '$(typeset -f preremove | sed s:\':\'\':g)',
495     '$(typeset -f postremove | sed s:\':\'\':g)')"
496    
497     #### ####
498     # record contents #
499     #### ####
500     pkginfoid=$(sql "select id from packages_info where pkg_id='${pkgid}' and arch='${ARCH}'")
501     entrydir="$(dirname ${magefile})"
502     sql "insert into packages_content(pkginfo_id,
503     char,
504     dirs,
505     files,
506     pipes,
507     symlinks)
508     values('${pkginfoid}',
509     '$(basename ${DBFILE} .sql).records/${pkginfoid}/char.bz2',
510     '$(basename ${DBFILE} .sql).records/${pkginfoid}/dirs.bz2',
511     '$(basename ${DBFILE} .sql).records/${pkginfoid}/files.bz2',
512     '$(basename ${DBFILE} .sql).records/${pkginfoid}/pipes.bz2',
513     '$(basename ${DBFILE} .sql).records/${pkginfoid}/symlinks.bz2')"
514     # create compressed content files
515     local entryfile
516     for entryfile in char dirs files pipes symlinks
517     do
518     install -d $(dirname ${DBFILE})/$(basename ${DBFILE} .sql).records/${pkginfoid}
519     cat ${entrydir}/.${entryfile} | bzip2 -9 >> $(dirname ${DBFILE})/$(basename ${DBFILE} .sql).records/${pkginfoid}/${entryfile}.bz2
520     done
521    
522     unset PKGNAME
523     unset DEPEND
524     unset SDEPEND
525     unset PROVIDE
526     unset SPECIAL_VARS
527     unset SPECIAL_FUNCTIONS
528    
529     # unset these functions
530     unset -f preinstall
531     unset -f postinstall
532     unset -f preremove
533     unset -f postremove
534     done
535     }
536    
537     convert_virtual_defaults()
538     {
539     local virtual
540     local package
541     local vcat
542     local vname
543     local pcat
544     local pname
545    
546     sql "create table virtual_defaults(id integer primary key, vcat text,vname text, pcat text, pname text)"
547    
548     while read virtual package
549     do
550     vcat="$(dirname ${virtual})"
551     vname="$(basename ${virtual})"
552     pcat="$(dirname ${package})"
553     pname="$(basename ${package})"
554    
555     sql "insert into virtual_defaults(vcat,vname,pcat,pname) values('${vcat}','${vname}','${pcat}','${pname}')"
556     done < /etc/mage-profile/virtuals.defaults
557     }
558    
559     METHOD="$1"
560     DBFILE="$2"
561    
562     [[ -z ${METHOD} ]] && die "No method given. Use --install, --packages or --virtual for your appropiate task"
563     [[ -z ${DBFILE} ]] && die "No database given."
564    
565     case $1 in
566     --install|-i)
567     create_database_layout
568     convert_install_db
569     ;;
570     --packages|-p)
571     create_database_layout
572     convert_smage_db
573     ;;
574     --virtuals|-v)
575     convert_virtual_defaults
576     ;;
577     *) die "Unkown method." ;;
578     esac

Properties

Name Value
svn:executable *