--- branches/mage-sql/usr/lib/mage/convert_databases_smage.sh 2010/07/14 17:38:51 1112 +++ branches/mage-sql/usr/lib/mage/convert_databases_smage.sh 2010/07/17 10:39:06 1113 @@ -5,7 +5,7 @@ #### fixups #### -MLIBDIR=/home/tjoke/alx-cvs/magellan-src/mage/usr/lib/mage +MLIBDIR=/home/tjoke/svn/magellan-source/branches/mage-sql/usr/lib/mage INSTALLDB=/var/db/mage ################# @@ -29,6 +29,150 @@ EOF } +split_save_variables() +{ + export SAVED_PNAME="${PNAME}" + export SAVED_PVER="${PVER}" + export SAVED_PBUILD="${PBUILD}" + export SAVED_PCATEGORIE="${PCATEGORIE}" + export SAVED_DESCRIPTION="${DESCRIPTION}" + export SAVED_HOMEPAGE="${HOMEPAGE}" + export SAVED_SPECIAL_VARS="${SPECIAL_VARS}" + export SAVED_STATE="${STATE}" + export SAVED_PKGTYPE="${PKGTYPE}" + export SAVED_INHERITS="${INHERITS}" + export SAVED_DEPEND="${DEPEND}" + export SAVED_SDEPEND="${SDEPEND}" + export SAVED_PROVIDE="${PROVIDE}" + export SAVED_NOPKGBUILD="${NOPKGBUILD}" + + # bindir too + export SAVED_BINDIR="${BINDIR}" + + # export the SPLIT_PACKAGE_BASE + export SPLIT_PACKAGE_BASE="${SAVED_PNAME}" + + # functions + if [[ ! -z $(typeset -f preinstall) ]] + then + # rename the old one + local saved_preinstall + saved_preinstall=SAVED_$(typeset -f preinstall) + eval "${saved_preinstall}" + export -f SAVED_preinstall + fi + + if [[ ! -z $(typeset -f postinstall) ]] + then + # rename the old one + local saved_postinstall + saved_postinstall=SAVED_$(typeset -f postinstall) + eval "${saved_postinstall}" + export -f SAVED_postinstall + fi + + if [[ ! -z $(typeset -f preremove) ]] + then + # rename the old one + local saved_preremove + saved_preremove=SAVED_$(typeset -f preremove) + eval "${saved_preremove}" + export -f SAVED_preremove + fi + + if [[ ! -z $(typeset -f postremove) ]] + then + # rename the old one + local saved_postremove + saved_postremove=SAVED_$(typeset -f postremove) + eval "${saved_postremove}" + export -f SAVED_postremove + fi +} + +split_restore_variables() +{ + export PNAME="${SAVED_PNAME}" + export PVER="${SAVED_PVER}" + export PBUILD="${SAVED_PBUILD}" + export PCATEGORIE="${SAVED_PCATEGORIE}" + export DESCRIPTION="${SAVED_DESCRIPTION}" + export HOMEPAGE="${SAVED_HOMEPAGE}" + export SPECIAL_VARS="${SAVED_SPECIAL_VARS}" + export STATE="${SAVED_STATE}" + export PKGTYPE="${SAVED_PKGTYPE}" + export INHERITS="${SAVED_INHERITS}" + export DEPEND="${SAVED_DEPEND}" + export SDEPEND="${SAVED_SDEPEND}" + export PROVIDE="${SAVED_PROVIDE}" + export NOPKGBUILD="${SAVED_NOPKGBUILD}" + + # bindir too + export BINDIR="${SAVED_BINDIR}" + + # functions + if [[ ! -z $(typeset -f SAVED_preinstall) ]] + then + # rename the old one + local saved_preinstall + saved_preinstall=$(typeset -f SAVED_preinstall) + eval "${saved_preinstall/SAVED_/}" + export -f preinstall + fi + + if [[ ! -z $(typeset -f SAVED_postinstall) ]] + then + # rename the old one + local saved_postinstall + saved_postinstall=$(typeset -f SAVED_postinstall) + eval "${saved_postinstall/SAVED_/}" + export -f postinstall + fi + + if [[ ! -z $(typeset -f SAVED_preremove) ]] + then + # rename the old one + local saved_preremove + saved_preremove=$(typeset -f SAVED_preremove) + eval "${saved_preremove/SAVED_/}" + export -f preremove + fi + + if [[ ! -z $(typeset -f SAVED_postremove) ]] + then + # rename the old one + local saved_postremove + saved_postremove=$(typeset -f SAVED_postremove) + eval "${saved_postremove/SAVED_/}" + export -f postremove + fi +} + +split_unset_variables() +{ + # unset saved vars; not needed anymore + unset SAVED_PNAME + unset SAVED_PVER + unset SAVED_PBUILD + unset SAVED_PCATEGORIE + unset SAVED_DESCRIPTION + unset SAVED_HOMEPAGE + unset SAVED_SPECIAL_VARS + unset SAVED_STATE + unset SAVED_PKGTYPE + unset SAVED_INHERITS + unset SAVED_DEPEND + unset SAVED_SDEPEND + unset SAVED_PROVIDE + unset SAVED_BINDIR + unset SAVED_NOPKGBUILD + unset SPLIT_PACKAGE_BASE + unset -f SAVED_preinstall + unset -f SAVED_postinstall + unset -f SAVED_preremove + unset -f SAVED_postremove +} + create_database_layout() { sql "create table categories(id integer primary key, pcat text unique)" @@ -65,29 +209,12 @@ script text)" } -convert_smage_db() +smage2sqlite() { local i - local smagefile - local PCATEGORIE - local PNAME - local PVER - local PBUILD local pcatid - local STATE local pkgid local dep - local PKGNAME - local DESCRIPTION - local HOMEPAGE - local DEPEND - local SDEPEND - local PROVIDE - local PKGTYPE - local preinstall - local postinstall - local preremove - local postremove local sym local depname local depver @@ -97,229 +224,330 @@ local md5file local md5sum - # create categories - cat ${SMAGESCRIPTSDIR}/.known_categories | while read pcat - do - case ${pcat} in - include|profiles) continue;; - esac + pcatid=$(sql "select id from categories where pcat='${PCATEGORIE}'") - sql "insert into categories (pcat) values('${pcat}')" - done + sql "insert into packages( + pname, + pver, + pbuild, + pcat_id, + state, + provide, + pkgtype + ) + values( + '${PNAME}', + '${PVER}', + '${PBUILD}', + '${pcatid}', + '${STATE}', + '${PROVIDE}', + '${PKGTYPE}' + )" - # create packages - for smagefile in $(find ${SMAGESCRIPTSDIR}/glibc-nptl -type f -name '*'.smage2 | sort) + pkgid=$(sql "select id from packages where pname='${PNAME}' and pver='${PVER}' and pbuild='${PBUILD}'") + + # depends + while read sym dep do - case ${DEBUG} in - 1|true) echo "converting ${smagefile}" ;; + # ignore scripts atm -FIXME!!! + case ${sym} in + \$\(*) continue ;; esac - source ${smagefile} + [[ -z ${dep} ]] && continue + depcat="$(dep2pcat ${dep})" + depcatid=$(sql "select id from categories where pcat='${depcat}'") + depname="$(dep2pname ${dep})" + depver="$(dep2pver ${dep})" - # substitute r from pbuild - PBUILD="${PBUILD/r/}" + if [[ -z ${depcatid} ]] + then + die "Error: '${PNAME}' -> depcat '${depcat}' has no depcatid, it does no exist in database!" + fi - pcatid=$(sql "select id from categories where pcat='${PCATEGORIE}'") - sql "insert into packages( + sql "insert into depends(pkg_id, + relation, + pcat_id, pname, pver, - pbuild, - pcat_id, - state, - provide, - pkgtype - ) - values( - '${PNAME}', - '${PVER}', - '${PBUILD}', - '${pcatid}', - '${STATE}', - '${PROVIDE}', - '${PKGTYPE}' - )" - - pkgid=$(sql "select id from packages where pname='${PNAME}' and pver='${PVER}' and pbuild='${PBUILD}'") - # depends - while read sym dep - do - [[ -z ${dep} ]] && continue - depcat="$(dep2pcat ${dep})" - depcatid=$(sql "select id from categories where pcat='${depcat}'") - depname="$(dep2pname ${dep})" - depver="$(dep2pver ${dep})" - sql "insert into depends(pkg_id, - relation, - pcat_id, - pname, - pver, - pbuild) - values('${pkgid}', - '${sym}', - '${depcatid}', - '${depname}', - '${depver}', - '${depbuild}')" - done << EOF + pbuild) + values('${pkgid}', + '${sym}', + '${depcatid}', + '${depname}', + '${depver}', + '${depbuild}')" + done << EOF ${DEPEND} EOF - # sdepends - while read sym dep - do - [[ -z ${dep} ]] && continue - depcat="$(dep2pcat ${dep})" - depcatid=$(sql "select id from categories where pcat='${depcat}'") - depname="$(dep2pname ${dep})" - depver="$(dep2pver ${dep})" - sql "insert into sdepends(pkg_id, - relation, - pcat_id, - pname, - pver, - pbuild) - values('${pkgid}', - '${sym}', - '${depcatid}', - '${depname}', - '${depver}', - '${depbuild}')" - done << EOF + # sdepends + while read sym dep + do + [[ -z ${dep} ]] && continue + depcat="$(dep2pcat ${dep})" + depcatid=$(sql "select id from categories where pcat='${depcat}'") + depname="$(dep2pname ${dep})" + depver="$(dep2pver ${dep})" + sql "insert into sdepends(pkg_id, + relation, + pcat_id, + pname, + pver, + pbuild) + values('${pkgid}', + '${sym}', + '${depcatid}', + '${depname}', + '${depver}', + '${depbuild}')" + done << EOF ${SDEPEND} EOF - for arch in i686 x86_64 - do - md5file="${MAGEDIR}/${PCATEGORIE}/${PNAME}/md5/${PNAME}-${PVER}-${arch}-r${PBUILD}.md5" - [[ -f ${md5file} ]] && md5sum="$(cat ${md5file} | cut -d' ' -f1)" - - # fix descriptions - DESCRIPTION="$(echo ${DESCRIPTION} | sed s:\':\'\':g)" + for arch in i686 x86_64 + do + md5file="${MAGEDIR}/${PCATEGORIE}/${PNAME}/md5/${PNAME}-${PVER}-${arch}-r${PBUILD}.md5" + [[ -f ${md5file} ]] && md5sum="$(cat ${md5file} | cut -d' ' -f1)" - sql "insert into packages_info(pkg_id, arch, md5, description, homepage) - values('${pkgid}','${arch}','${md5sum}','${DESCRIPTION}','${HOMEPAGE}')" - done + # fix descriptions + DESCRIPTION="$(echo ${DESCRIPTION} | sed s:\':\'\':g)" - # using $INHERITS directly - #mincludes="$(grep -r 'minclude ' ${magefile} | cut -d: -f2 | sed s:minclude\ ::)" + sql "insert into packages_info(pkg_id, arch, md5, description, homepage) + values('${pkgid}','${arch}','${md5sum}','${DESCRIPTION}','${HOMEPAGE}')" + done -# # # -# # # install routines & includes -# # # -# # # needs special magic: the last line of typeset has no ';' -# # # but to eval the function we must append it -# # # so this sed deletes the last line of the typeset output -# # # and replaces it with an ;} and a newline between -# # # -# # # but we will not add this fun when no postinstall etc are given -# # # -> [[ -n $(typeset -f postinstall ]] && -# # # -# # # also the character ' is a problem while importing to sql -# # # because we use ' as escape char. so we replace it with '' -# # # -# # # $([[ -n $(typeset -f postinstall) ]] && echo -e "$(typeset -f postinstall | sed s:\':\'\':g | sed '$d');\n}") -# # # -# # sql "insert into packages_routines(pkg_id, -# # mincludes, -# # postinstall, -# # preinstall, -# # preremove, -# # postremove) -# # values('${pkgid}', -# # '${mincludes}', -# # '$([[ -n $(typeset -f postinstall) ]] && echo -e "$(typeset -f postinstall | sed s:\':\'\':g | sed '$d');\n}")', -# # '$([[ -n $(typeset -f preinstall) ]] && echo -e "$(typeset -f preinstall | sed s:\':\'\':g | sed '$d');\n}")', -# # '$([[ -n $(typeset -f preremove) ]] && echo -e "$(typeset -f preremove | sed s:\':\'\':g | sed '$d');\n}")', -# # '$([[ -n $(typeset -f postremove) ]] && echo -e "$(typeset -f postremove | sed s:\':\'\':g | sed '$d');\n}")')" - - local do_preinst=0 - local do_postinst=0 - local do_prerm=0 - local do_postrm=0 - local do_script=0 - local script="" - - [[ -n $(typeset -f preinstall) ]] && do_preinst=1 && do_script=1 - [[ -n $(typeset -f postinstall) ]] && do_postinst=1 && do_script=1 - [[ -n $(typeset -f preremove) ]] && do_prerm=1 && do_script=1 - [[ -n $(typeset -f postremove) ]] && do_postrm=1 && do_script=1 + # using $INHERITS directly + #mincludes="$(grep -r 'minclude ' ${magefile} | cut -d: -f2 | sed s:minclude\ ::)" - [[ ${do_script} = 1 ]] && script="$(basename ${DBFILE} .sql).routines/${pkgid}/routines.sh" - - sql "insert into packages_routines(pkg_id, - mincludes, - postinstall, - preinstall, - preremove, - postremove, - script) - values('${pkgid}', - '${INHERITS}', - '${do_postinst}', - '${do_pretinst}', - '${do_prerm}', - '${do_postrm}', - '${script}')" +# # # +# # # install routines & includes +# # # +# # # needs special magic: the last line of typeset has no ';' +# # # but to eval the function we must append it +# # # so this sed deletes the last line of the typeset output +# # # and replaces it with an ;} and a newline between +# # # +# # # but we will not add this fun when no postinstall etc are given +# # # -> [[ -n $(typeset -f postinstall ]] && +# # # +# # # also the character ' is a problem while importing to sql +# # # because we use ' as escape char. so we replace it with '' +# # # +# # # $([[ -n $(typeset -f postinstall) ]] && echo -e "$(typeset -f postinstall | sed s:\':\'\':g | sed '$d');\n}") +# # # +# # sql "insert into packages_routines(pkg_id, +# # mincludes, +# # postinstall, +# # preinstall, +# # preremove, +# # postremove) +# # values('${pkgid}', +# # '${mincludes}', +# # '$([[ -n $(typeset -f postinstall) ]] && echo -e "$(typeset -f postinstall | sed s:\':\'\':g | sed '$d');\n}")', +# # '$([[ -n $(typeset -f preinstall) ]] && echo -e "$(typeset -f preinstall | sed s:\':\'\':g | sed '$d');\n}")', +# # '$([[ -n $(typeset -f preremove) ]] && echo -e "$(typeset -f preremove | sed s:\':\'\':g | sed '$d');\n}")', +# # '$([[ -n $(typeset -f postremove) ]] && echo -e "$(typeset -f postremove | sed s:\':\'\':g | sed '$d');\n}")')" + + local do_preinst=0 + local do_postinst=0 + local do_prerm=0 + local do_postrm=0 + local do_script=0 + local script="" + + [[ -n $(typeset -f preinstall) ]] && do_preinst=1 && do_script=1 + [[ -n $(typeset -f postinstall) ]] && do_postinst=1 && do_script=1 + [[ -n $(typeset -f preremove) ]] && do_prerm=1 && do_script=1 + [[ -n $(typeset -f postremove) ]] && do_postrm=1 && do_script=1 + + [[ ${do_script} = 1 ]] && script="$(basename ${DBFILE} .sql).routines/${pkgid}/routines.sh" + + sql "insert into packages_routines(pkg_id, + mincludes, + postinstall, + preinstall, + preremove, + postremove, + script) + values('${pkgid}', + '${INHERITS}', + '${do_postinst}', + '${do_pretinst}', + '${do_prerm}', + '${do_postrm}', + '${script}')" # create it only if do_script=1 if [[ ${do_script} = 1 ]] then - # create a routines script - local routines=$(dirname ${DBFILE})/${script} - install -d $(dirname ${routines}) + # create a routines script + local routines=$(dirname ${DBFILE})/${script} + install -d $(dirname ${routines}) + + echo '#!/bin/sh' > ${routines} + echo "# routines.sh script for ${pkgid}, ${PNAME}-${PVER}-${PBUILD}" >> ${routines} + echo >> ${routines} + - echo "#!/bin/sh" > ${routines} - echo "# routines.sh script for #${pkgid}, ${PNAME}-${PVER}-${PBUILD}" >> ${routines} - echo >> ${routines} + # special functions and variables + if [[ -n ${SPECIAL_VARS} ]] + then + local i + for i in ${SPECIAL_VARS} + do + # being tricky here :) + echo "${i}=\"$(eval echo \$${i})\"" >> ${routines} + done + echo >> ${routines} + fi + # add special functions + if [[ -n ${SPECIAL_FUNCTIONS} ]] + then + local i + for i in ${SPECIAL_FUNCTIONS} + do + # add to mage (quotes needed !) + typeset -f "${i}" >> ${routines} + echo >> ${routines} + # unset to be safe (quotes needed !) + unset "${i}" + done + echo >> ${routines} + fi - # special functions and variables - if [[ -n ${SPECIAL_VARS} ]] + # postinstall and co + for i in preinstall postinstall preremove postremove + do + if [[ -n $(typeset -f "${i}") ]] then - local i - for i in ${SPECIAL_VARS} - do - # being tricky here :) - echo "${i}=\"$(eval echo \$${i})\"" >> ${routines} - done - echo >> ${routines} + typeset -f "${i}" >> ${routines} + echo >> ${routines} + fi + done + + # create start logic + echo >> ${routines} + echo 'case $1 in' >> ${routines} + echo ' preinstall) preinstall ;;' >> ${routines} + echo ' postinstall) postinstall ;;' >> ${routines} + echo ' preremove) preremove ;;' >> ${routines} + echo ' postremove) postremove ;;' >> ${routines} + echo 'esac' >> ${routines} + echo >> ${routines} +fi + + unset PCATEGORIE + unset PNAME + unset PVER + unset PBUILD + unset STATE + unset DESCRIPTION + unset HOMEPAGE + unset PKGTYPE + unset PKGNAME + unset DEPEND + unset SDEPEND + unset PROVIDE + unset SPECIAL_VARS + unset SPECIAL_FUNCTIONS + unset INHERITS + unset SPLIT_PACKAGES + + # unset these functions + unset -f preinstall + unset -f postinstall + unset -f preremove + unset -f postremove +} + +categories2sqlite() +{ + local file="$1" + + if [[ ! -f ${file} ]] + then + echo "${file} does not exist, ignoring..." + return 0 + fi + + echo "using categories from ${file}..." + # create categories + cat ${file} | while read pcat + do + case ${pcat} in + include|profiles) continue;; + \#*|"") continue ;; + esac + + # "upsert" + if [[ -z $(sql "select pcat from categories where pcat='${pcat}'") ]] + then + sql "insert into categories (pcat) values('${pcat}')" fi + done +} + +convert_smage_db() +{ + local smagefile - # add special functions - if [[ -n ${SPECIAL_FUNCTIONS} ]] + local PCATEGORIE + local PNAME + local PVER + local PBUILD + local STATE + local PKGNAME + local DESCRIPTION + local HOMEPAGE + local DEPEND + local SDEPEND + local PROVIDE + local PKGTYPE + local SPLIT_PACKAGES + local preinstall + local postinstall + local preremove + local postremove + + # create categories - global + categories2sqlite "${SMAGESCRIPTSDIR}/.known_categories" + + # create categories - repo specific + categories2sqlite "${SMAGESCRIPTSDIR}/${REPO}/.known_categories" + + # create packages + for smagefile in $(find ${SMAGESCRIPTSDIR}/${REPO} -type f -name \*.smage2 | sort) + do + case ${DEBUG} in + 1|true) echo "converting ${smagefile}" ;; + esac + + source ${smagefile} + + # build several subpackages + if [[ -n ${SPLIT_PACKAGES} ]] then - local i - for i in ${SPECIAL_FUNCTIONS} + split_save_variables + for subpackage in ${SPLIT_PACKAGES} do - # add to mage (quotes needed !) - typeset -f "${i}" >> ${routines} - echo >> ${routines} - # unset to be safe (quotes needed !) - unset "${i}" - done - echo >> ${routines} - fi + echo "Running for subpackage '${subpackage}'..." - # postinstall and co - for i in preinstall postinstall preremove postremove - do - if [[ -n $(typeset -f "${i}") ]] - then - typeset -f "${i}" >> ${routines} - echo >> ${routines} - fi - done + # get the right variables for the split + export PNAME="${subpackage}" + split_info_${subpackage} - # create start logic - echo >> ${routines} - echo 'case $1 in' >> ${routines} - echo ' preinstall) preinstall ;;' >> ${routines} - echo ' postinstall) postinstall ;;' >> ${routines} - echo ' preremove) preremove ;;' >> ${routines} - echo ' postremove) postremove ;;' >> ${routines} - echo 'esac' >> ${routines} - echo >> ${routines} -fi + smage2sqlite + # restore smage environment + split_restore_variables + done + # unset all saved smage variables + split_unset_variables + else + smage2sqlite + fi unset PCATEGORIE unset PNAME @@ -336,6 +564,7 @@ unset SPECIAL_VARS unset SPECIAL_FUNCTIONS unset INHERITS + unset SPLIT_PACKAGES # unset these functions unset -f preinstall @@ -384,6 +613,9 @@ local pkginfoid local entrydir + # first of all add virtual categorie + sql "insert into categories(pcat) values('virtual')" + # create categories for i in virtual ${INSTALLDB}/* do @@ -406,8 +638,6 @@ pname="$(magename2pname ${magefile})" pver="$(magename2pver ${magefile})" pbuild="$(magename2pbuild ${magefile})" - # substitute r from pbuild - pbuild="${pbuild/r/}" source ${magefile} @@ -562,9 +792,11 @@ METHOD="$1" DBFILE="$2" +REPO="$3" [[ -z ${METHOD} ]] && die "No method given. Use --install, --packages or --virtual for your appropiate task" [[ -z ${DBFILE} ]] && die "No database given." +[[ -z ${REPO} ]] && die "No repository given." case $1 in --install|-i) @@ -572,11 +804,12 @@ convert_install_db ;; --packages|-p) + convert_virtual_defaults create_database_layout convert_smage_db ;; - --virtuals|-v) - convert_virtual_defaults - ;; +# --virtuals|-v) +# convert_virtual_defaults +# ;; *) die "Unkown method." ;; esac