Remove unneeded mercurial variables from the ule.
[ule.git] / update-live-ebuilds
blobed19cdbfe5dd296de3fe2e52399468a7f86c65f1
1 #!/bin/bash
2 ## update-live-ebuilds - Program to update ebuilds that pull from various portage-type repositories.
3 ## Copyright (C) 2006-2009 Avuton Olrich <avuton@gmail.com>
4 ##
5 ## This program is free software: you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation, either version 3 of the License, or
8 ## (at your option) any later version.
9 ##
10 ## This program is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
18 function vecho() {
19 [ ${VERBOSE} -eq ${true} ] && echo "$@"
22 function decho() {
23 [ ${DEBUG} -eq ${true} ] && echo "$@"
26 function email_details() {
27 echo "Please email details about this bug to "
28 echo "Avuton Olrich <avuton@gmail.com>, so I "
29 echo "can improve this script."
32 ################################################################################################################################################
33 ## Updates ${ebuild_epoch};
34 ## This is a function since portage has never put the correct epoch time in /var/db/pkg/*/*/COUNTER.
35 ################################################################################################################################################
36 ## Rather than grepping the CONTENTS, I would just use ${dir}/COUNTER, but portage doesn't not use epoch, and I'm not really quite sure
37 ## what it uses for a counter. I know all package managers use epoch in the CONTENTS file.
38 function update_ebuild_epoch() {
39 export ebuild_epoch
40 if [ ${spec} -eq ${portage} ]; then
41 ebuild_epoch=`grep obj ${dir}/CONTENTS | tail -n1 | awk '{sub(/^.* /,"")} 1'`
42 else
43 ebuild_epoch=$(cat ${dir}/COUNTER)
45 CUREXPORTS="${CUREXPORTS} ebuild_epoch"
48 ################################################################################################################################################
49 ## SCM success detector;
50 ## This is a convience function used in conjunction with a SCM's return argument to detect success/failure and report appropriately.
52 ## Returns;
53 ## The true if the first argument is true, otherwise false.
55 ## Argument;
56 ## $1: The argument given by the SCM's execution
57 ################################################################################################################################################
58 function update_check() {
59 [ ${1} -eq ${true} ] && return ${true}
61 echo "Package ${provide} failed to update with error code: ${1}"
62 echo "If it is believed this is a problem with update-live-ebuilds, send me a verbose bug report"
63 email_details
64 UPDATE_FAIL_PACKAGES="${UPDATE_FAIL_PACKAGES} ${provide}"
65 return ${false}
68 ################################################################################################################################################
69 ## Write to the ULE database;
70 ## This simply writes any information that will be needed next time to the update-live-ebuilds database in a format that can be sourced
71 ## for easy retreival.
73 ## Returns;
74 ## None
76 ## Arguments;
77 ## $1: The ULE "key" or "cookie", basically the unique identifier that ULE uses to detect a difference between the last installed
78 ## repository revision and the current revision.
79 ## $2: The ULE database directory.
80 ################################################################################################################################################
81 function write_to_db() {
82 [ ! -d "`dirname ${ule_file}`" ] && mkdir -p "`dirname ${ule_file}`"
83 echo "ULE_COOKIE=\"${1}\"" > "${ule_file}"
86 ################################################################################################################################################
87 ## Extract a variable out of the current ebuild's portage database environment file;
88 ## This function simply automates detecting a variable in the portage database's environment file and extracts it into the current shell
89 ## for use by this script.
91 ## Returns;
92 ## The number of failed variable extractions.
94 ## Arguments;
95 ## $@: An unlimited amount of variables to find in the environment file in the portage database.
96 ################################################################################################################################################
97 function extract_env() {
98 local tempvar
99 local ret=0
100 for num in $@; do
101 tempvar=$(bzip2 -cd "${dir}/environment.bz2" | grep "^${num}=")
102 if [ $? -ne ${true} ]; then
103 tempvar=$(bzip2 -cd "${dir}/environment.bz2" | grep "^export ${num}=")
104 if [ $? -eq ${false} ]; then
105 decho "Could not find variable ${num} for ${provide}"
106 ret=$(($ret+1))
107 continue
109 tempvar="${tempvar/export /}"
112 decho "Found variable ${num} for ${provide}"
113 export "${tempvar//\'}"
114 CUREXPORTS="${CUREXPORTS} ${tempvar//=*}"
115 done
117 return ${ret}
120 ################################################################################################################################################
121 ## Convience wrapper for extract_env() to require no failure;
122 ## This wrapper simply executes the input in extract_env() and provides a failure message in the case that a failure occured.
124 ## Returns;
125 ## Boolean
127 ## Arguments;
128 ## $@: An unlimited amount of variables to find in the environment file in the portage database.
129 ################################################################################################################################################
130 function secure_env() {
131 local ret=0
132 extract_env $@
133 ret=$?
135 [ ${ret} -ne ${true} ] && vecho "${provide} inherits ${type} but doesn't have necessary variables; skipping"
136 return ${ret}
139 ################################################################################################################################################
140 ## Local repository directory detection;
141 ## This detects the local directory for repositories, and if it isn't found this will emit an error
143 ## Returns;
144 ## Boolean
146 ## Arguments;
147 ## $1: The directory to detect
148 ## $2: The name of the package provide
149 ################################################################################################################################################
150 function check_for_found() {
151 local provide="${2}"
152 local dir="${1}"
154 ## This must be called with something beyond the directory, such as an info file or directory.
155 ## This regexp does a basic 'dirname' without forking a shell
156 if [[ "${dir}" =~ '/.*/' ]] && [ ! -d "${BASH_REMATCH[0]%%\/}" ]; then
157 echo -n "Couldn't find the original directory for ${provide}, "
158 [ ${locbase} -eq ${true} ] && echo 'skipping.' && return 1
160 echo 'reemerging.'
161 emerger ${false} || return ${false}
162 export locbase=${true}
165 if [ ! -d "${dir}" ] && [ ! -f "${dir}" ]; then
166 if [ ${ALL} -eq ${false} ] && [ ${BASELINE} -eq ${true} ]; then
167 [ "${BASELINE}" -eq ${true} ] && echo "Cannot find the directory to baseline: ${dir}" && return ${false}
169 [[ "${dir}" =~ '.*/(.*)' ]] ## Basename
170 echo '--'
171 echo "Couldn't find the ${BASH_REMATCH[1]} information directory for ${provide}"
172 echo "Please provide details about where this package stores itself."
173 echo "Use -a to OVERRIDE this warning and reemerge"
174 email_details
175 echo '--'
176 return ${false}
179 emerger ${false} || return ${false}
180 export locbase=${true}
183 return ${true}
186 ################################################################################################################################################
187 ## Portage compatible installer execution;
188 ## This is where all portage compatible installation is executed. It is all done here so everywhere emerge is neede will have uniform
189 ## error detection.
191 ## Returns;
192 ## Boolean
194 ## Arguments;
195 ## $1: Boolean to baseline the package or not.
196 ################################################################################################################################################
197 function emerger() {
198 local locopts
199 [ ${PRETEND} -eq ${true} ] && locopts="${locopts} ${EMERGE_PRETEND[${spec}]}"
201 ## Handle ULE_ASK_FILE
202 if [ -f "${ULE_ASK_FILE}" ]; then
203 grep -Eq "^($pprovide|${provide})$" "${ULE_ASK_FILE}"
204 if [ $? -eq ${true} ]; then
205 vecho "Package (${provide}) found in ULE_ASK, will ask before emerging..."
206 if [ -z "${EMERGE_ASK_OPTS[${spec}]}" ]; then
207 ${EMERGE_BINARY[${spec}]} ${EMERGE_EXT_OPTS[${spec}]} ${EMERGE_PRETEND[${spec}]} ${provide}
209 local tmpopt
210 while true; do
211 echo -n "Would you like to install this package? [yes/no] "
212 read tmpopt
214 [ "${tmpopt}" == "no" ] && return ${true}
215 [ "${tmpopt}" == "yes" ] && break
216 done
217 else
218 locopts="${locopts} ${EMERGE_ASK_OPTS[${spec}]}"
223 ## Since we didn't continue above, we emerge now.
224 ${EMERGE_BINARY[${spec}]} ${locopts} ${EMERGE_EXT_OPTS[${spec}]} ${provide}
226 if [ $? -ne ${true} ]; then
227 echo "It appears emerge of ${provide} failed, try again later..."
228 export FAILED_PACKAGES="${FAILED_PACKAGES} ${provide}"
229 return ${false}
232 ## We need to pull the hash again, to ensure we have the correct hash, in case of PEEK
233 locbase=${true}
234 get_hash
236 if [ ${1} -eq ${true} ] && [ ${PRETEND} -eq ${false} ]; then
237 write_to_db "${newhash}" "${ULE_LOC}/${provide}"
239 ## Add to the success packages
240 export SUCCESS_PACKAGES="${SUCCESS_PACKAGES} ${provide}"
243 return ${true}
246 ################################################################################################################################################
247 ## Hash detection begins;
248 ## This can either go through looking directly at the repository, or we update the local repository to detect updates.
249 ## Of course, local updating is the prefered method, though it is probably more likely to succeed in the objective, as the SCMs command
250 ## line interface may change and this would obviously cause problems, which will cause user notification at the update_check() after the
251 ## case statement completes.
253 ## To developers considering adding another SCM or fixing up an SCM detection, here's a few things to consider.
254 ## 1) Please make a peek mode if the SCM supports it.
255 ## 2) Attempt to use the ebuild's variables as much as possible, as these may change and it would be more accurate to take
256 ## the newer, maintained variables.
257 ## 3) Use ${prescm} and secure_env(), these will find problems before they happen.
258 ## 4) Don't rely on environment variables through sudo or ${prescm}. Sudo by default resets the environment during privelege drop.
259 ## 5) Avoid HASH_PROGRAM use; depend on native 'cookies', if possible.
260 ## 6) newhash needs to be the final calculation so the update_check() at the end of the case statement does it's job.
261 ## 7) When possible, don't use 'cd', use a repository updating argument.
262 ## 8) Be aware secure_env() is a costly function; use it only as necessary.
263 ################################################################################################################################################
264 function get_hash() {
265 ## No need to check the local repository when ALL is selected, but we do need the newhash.
266 [ ${ALL} -eq ${true} ] && local locbase=${true}
268 export newhash
269 local found
271 case "${type}" in
272 'subversion' )
273 secure_env 'ESVN_REPO_URI' || continue
274 extract_env 'ESVN_REVISION'
275 ESVN_REPO_URI="${ESVN_REPO_URI%%/}"
277 ## This is a simple test to test for: svn://svn.blah.org/svn/blah@592 (being the revision)
278 test="${ESVN_REPO_URI##*@}"
279 if [ "${test//[[:digit:]]}" == '' ] && [ -n "${ESVN_REPO_URI}" ]; then
280 newhash="${ESVN_REPO_URI##*@}"
281 decho "Newhash generated from ESVN_REPO_URI."
283 unset test
285 if [ "${ESVN_REVISION}" == '' ] && [ -n "${ESVN_REVISION}" ]; then
286 newhash="${ESVN_REVISION}"
287 decho "Newhash generated from ESVN_REVISION."
290 if [ ${PEEK} -eq ${true} ] && [ ${locbase} -eq ${false} ] && [ -n ${newhash} ]; then
291 newhash=$(svn info ${ESVN_REPO_URI} | grep 'Last Changed Rev' | awk '{sub(/^.* /,"")} 1')
294 ## Subversions' binaries are extremely unreliable. Cannot count on svn info to give something useful.
295 if [ -z "${newhash}" ]; then
296 secure_env 'ESVN_STORE_DIR' 'ESVN_PROJECT' 'ESVN_UPDATE_CMD' 'ESVN_OPTIONS' || continue
298 found="${ESVN_STORE_DIR}/${ESVN_PROJECT}/${ESVN_REPO_URI##*/}"
299 check_for_found "${found}/.svn" "${provide}" || continue
301 if [ ${locbase} -eq ${false} ]; then
302 decho ${prescm} ${ESVN_UPDATE_CMD} ${ESVN_OPTIONS} --quiet --config-dir="${ESVN_STORE_DIR}/.subversion" ${found}
303 ${prescm} ${ESVN_UPDATE_CMD} ${ESVN_OPTIONS} --quiet --config-dir="${ESVN_STORE_DIR}/.subversion" ${found}
304 update_check $? ${provide} || continue
306 newhash=$(svnversion -c ${found} | awk '{sub(/.*:/,"")} 1')
308 ## Check the repository for modification, this is usually a sign of trouble.
309 if [ ${newhash} == *M ]; then
310 log "${found} repository has modifications. This could be a serious problem, as something"
311 log "has modified the repository in your distfiles directory, which should never happen."
313 ## This removes the trailing M, because 'svnversion' output and svn info output would needlessly differ
314 ## in this case
315 newhash="${newhash%M}"
319 'git' )
320 secure_env 'EGIT_REPO_URI' || continue
322 ## This is actually necessary due to newer eclasses which inherit git.eclass but not the EGIT_BRANCH variable.
323 extract_env 'EGIT_BRANCH' || EGIT_BRANCH='master'
327 ## GIT only allows git peek-remote to be done on certain protocols, thus listed at the end.
328 if [ ${PEEK} -eq ${true} ] && [ ${locbase} -eq ${false} ] && [[ "${EGIT_REPO_URI}" =~ ^(git|ssh|file|ssh+git|git+ssh).* ]]; then
329 newhash=$(git peek-remote ${EGIT_REPO_URI} | grep "heads/${EGIT_BRANCH}$" | awk '{sub(/\t.*$/,"")} 1')
330 else
331 secure_env 'EGIT_STORE_DIR' 'EGIT_PROJECT' || continue
333 found="${EGIT_STORE_DIR}/${EGIT_PROJECT}"
334 check_for_found "${found}/info" "${provide}" || continue
336 if [ ${locbase} -eq ${false} ]; then
337 secure_env 'EGIT_UPDATE_CMD' || continue
339 ${prescm} git --git-dir="${found}" fetch origin ${EGIT_BRANCH}:${EGIT_BRANCH}&>/dev/null
340 update_check $? ${provide} || continue
342 newhash="$(git --git-dir="${found}" rev-parse ${EGIT_BRANCH})"
345 'cvs')
346 ## CVS_RSH gets extracted, but is not used anywhere by design.
347 secure_env 'CVS_PASSFILE' 'CVS_RSH' 'ECVS_AUTH' 'ECVS_CVS_COMMAND' \
348 'ECVS_SERVER' 'ECVS_TOP_DIR' 'ECVS_UP_OPTS' 'ECVS_USER' || continue
350 ## The obvious "optional only", these don't exist at all in some ebuilds.
351 extract_env 'ECVS_MODULE' 'ECVS_LOCALNAME' 'ECVS_OPTIONS' 'ECVS_COMPRESS' 'ECVS_PASS' 'T'
353 if [ -n "${ECVS_LOCALNAME}" ]; then
354 found="${ECVS_TOP_DIR}/${ECVS_LOCALNAME}"
355 elif [ -n "${ECVS_MODULE}" ]; then
356 found="${ECVS_TOP_DIR}/${ECVS_MODULE}"
357 else
358 echo "Couldn't find the directory for ${provide}, skipping..."
359 email_details
360 continue
363 [ -z "${T}" ] && T="`dirname ${CVS_PASSFILE}`"
365 check_for_found "${found}/CVS" "${provide}" || continue
367 cd "${found}"
368 if [ ${locbase} -eq ${false} ]; then
369 ## From the cvs.eclass
370 [ -d "${T}" ] || mkdir -p "${T}"
371 touch "${CVS_PASSFILE}"
372 chown ${EMERGE_USER[${spec}]} ${CVS_PASSFILE}
374 ## DON'T login as ${prescm}, it causes all sorts of problems when the sudo environment is reset.
375 decho -d:${ECVS_AUTH}:${ECVS_USER}:${ECVS_PASS}@${ECVS_SERVER} ${ECVS_CVS_COMMAND} -Q login \>/dev/null
376 ${ECVS_CVS_COMMAND} -d:${ECVS_AUTH}:${ECVS_USER}:${ECVS_PASS}@${ECVS_SERVER} -Q login > /dev/null
377 [ $? -ne 0 ] && vecho "Couldn't login to cvs server for ${provide}" && continue
379 decho ${prescm} ${ECVS_CVS_COMMAND} -d:${ECVS_AUTH}:${ECVS_USER}:${ECVS_PASS}@${ECVS_SERVER} -Q update ${ECVS_UP_OPTS} ${ECVS_LOCALNAME}
380 ${prescm} ${ECVS_CVS_COMMAND} -d:${ECVS_AUTH}:${ECVS_USER}:${ECVS_PASS}@${ECVS_SERVER} -Q update ${ECVS_UP_OPTS} ${ECVS_LOCALNAME}
381 update_check $? ${provide} || continue
383 newhash="$(find ${found} -type f -name Entries -exec cat {} \; | ${HASH_PROGRAM} | sed -e 's/ .*//')"
385 'darcs')
386 secure_env 'EDARCS_DARCS_CMD' || continue
388 if [ ${PEEK} -eq ${true} ] && [ ${locbase} -eq ${false} ]; then
389 secure_env 'EDARCS_REPOSITORY' || continue
390 newhash="$(${EDARCS_DARCS_CMD} changes --last 1 --xml-output --repo="${EDARCS_REPOSITORY}" | grep hash | sed "s/^.*hash='\(.*\).gz.*$/\1/")"
391 else
392 secure_env 'EDARCS_TOP_DIR' 'EDARCS_LOCALREPO' 'EDARCS_UPDATE_CMD' 'EDARCS_OPTIONS' || continue
394 found="${EDARCS_TOP_DIR}/${EDARCS_LOCALREPO}"
395 check_for_found "${found}/_darcs" "${provide}" || continue
397 cd "${found}"
398 if [ ${locbase} -eq ${false} ]; then
399 ${prescm} ${EDARCS_DARCS_CMD} ${EDARCS_UPDATE_CMD} --all ${EDARCS_OPTIONS} -q>/dev/null
400 update_check $? ${provide} || continue
402 newhash="$(${prescm} ${EDARCS_DARCS_CMD} changes --last 1 --xml-output | grep hash | sed "s/^.*hash='\(.*\).gz.*$/\1/")"
405 'mercurial')
406 secure_env 'EHG_REPO_URI' || continue
408 if [ ${EHG_STORE_DIR} -eq '']; then
409 EHG_STORE_DIR='/usr/portage/distfiles/hg-src/'
410 EHG_STORE_DIR="${EHG_STORE_DIR}$(echo ${EHG_REPO_URI} | sed 's/\/$//' | sed -r 's/(.*)\/(.+)/\2/')"
413 found="${EHG_STORE_DIR}/${EHG_PROJECT}"
415 ## Takes a little more time, but the mercurial eclass is a little less mature than most of the others,
416 ## so we must search inside the STORE_DIR in the case that it's a module inside that.
417 ## ie. EHG_STORE_DIR=/usr/distfiles/hg-src/alsa-drivers , but .hg is in ..alsa-drivers/alsa-kernel
418 found="`find ${found} -name .hg -type d -printf '%h\n'`"
419 check_for_found "${found}" "${provide}" || continue
421 ## To avoid mercurial warnings prescm is run below.
422 ## Mercurial returns 1 when there is no updates (don't ask me.)
423 if [ ${PEEK} -eq ${true} ]; then
424 ${prescm} hg -R "${found}" incoming>/dev/null
425 if [ $? -eq 0 ]; then
426 newhash='0101' ## fake hash
427 elif [ $? -ne 1 ]; then
428 update_check $? ${provide}
430 else
431 if [ ${locbase} -eq ${false} ]; then
432 ${prescm} ${EHG_PULL_CMD} --repository "${found}">/dev/null
433 [ $? -ne 1 ] && update_check $? ${provide}
437 ## This must be run even with peek in the case that the repository is on an NFS mount.
438 if [ -n "${newhash}" ]; then
439 newhash="$(${prescm} hg --quiet --repository "${found}" tip)"
440 [ $? -ne 1 ] && update_check $? ${provide}
443 'tla')
444 ## These are the workings; it works, but with permissions it fails. If someone can fix the permissions problems, by
445 ## putting ${prescm} before the TLA commands and before the tla commands; the only way it runs successfully now is to run
446 ## without the prescm commands due to the CACHE_DIR permissions problems. Send patches if you want this fixed.
447 secure_env 'ETLA_TOP_DIR' 'ETLA_TLA_CMD' 'ETLA_UPDATE_CMD' 'ETLA_CACHE_DIR' 'ETLA_VERSION' 'ETLA_ARCHIVES' 'WORKDIR' 'P' || continue
448 found="${ETLA_TOP_DIR}/${ETLA_CACHE_DIR}"
449 check_for_found "${found}" "${provide}" || continue
451 if [ ${locbase} -eq ${false} ]; then
452 ETLA_TLA_CMD="${ETLA_TLA_CMD} --dir "${found}""
454 mkdir -p -m 0700 "${WORKDIR}/${P}"
455 chown ${EMERGE_USER[$spec]} "${WORKDIR}/${P}"
456 ${prescm} cp -Rf "$ETLA_TOP_DIR/$ETLA_CACHE_DIR"/* "${WORKDIR}/${P}"
457 for archive in ${ETLA_ARCHIVES}; do
458 ${prescm} ${ETLA_TLA_CMD} register-archive -f ${archive} || echo "Couldn't register ${archive}" && continue
459 done
461 tla_archive=`${prescm} ${ETLA_TLA_CMD} parse-package-name --arch ${ETLA_VERSION}`
462 tla_version=`${prescm} ${ETLA_TLA_CMD} parse-package-name --package-version ${ETLA_VERSION}`
463 oldversion="`${prescm} ${ETLA_TLA_CMD} tree-version`"
464 [ "${tla_archive}/${tla_version}" != "${oldversion}" ] && ${prescm} ${ETLA_TLA_CMD} set-tree-version ${tla_archive}/${tla_version}
466 ${prescm} ${ETLA_TLA_CMD} ${ETLA_UPDATE_CMD} ${ETLA_VERSION}>/dev/null
467 update_check $? ${provide} || continue
470 newhash="$(${HASH_PROGRAM} "${ETLA_TOP_DIR}/${ETLA_CACHE_DIR}"/\{arch\}/tine-treesqls | sed -e 's/ .*//g')"
472 'bazaar')
473 secure_env 'EBAZAAR_REPO_URI' || continue
475 if [ ${PEEK} -eq ${true} ] && [ ${locbase} -eq ${false} ]; then
476 newhash="$(bzr version-info "${EBAZAAR_REPO_URI}" | grep revision-id | awk '{sub(/^.* /,"")} 1')"
477 else
478 secure_env 'EBAZAAR_REGISTER_CMD' 'EBAZAAR_REGISTER_URI' 'EBAZAAR_STORE_DIR' \
479 'EBAZAAR_REGISTER_CMD' 'EBAZAAR_PROJECT' || continue
480 found="${EBAZAAR_STORE_DIR}/${EBAZAAR_PROJECT}/${EBAZAAR_REPO_URI##*/}"
482 check_for_found "${found} ${provide}" || continue
484 decho "${prescm} ${EBAZAAR_REGISTER_CMD} ${EBAZAAR_REGISTER_URI} --dir "${found}">/dev/null"
485 ${prescm} ${EBAZAAR_REGISTER_CMD} ${EBAZAAR_REGISTER_URI} --dir "${found}">/dev/null
486 update_check $? ${provide} || continue
488 decho "${prescm} ${EBAZAAR_UPDATE_CMD} --dir "${found}">/dev/null"
489 ${prescm} ${EBAZAAR_UPDATE_CMD} --dir "${found}">/dev/null
490 update_check $? ${provide} || continue
492 newhash="$(bzr version-info "${found}" | grep revision-id | awk '{sub(/^.* /,"")} 1')"
495 'bzr')
496 if [ ${PEEK} -eq ${true} ] && [ ${locbase} -eq ${false} ]; then
497 secure_env 'EBZR_REPO_URI' 'EBZR_BRANCH' || continue
498 found="${EBZR_REPO_URI}${EBZR_BRANCH}"
499 else
500 secure_env 'EBZR_STORE_DIR' 'EBZR_CACHE_DIR' || continue
501 found="${EBZR_STORE_DIR}/${EBZR_CACHE_DIR}"
503 check_for_found "${found}" || continue
505 newhash="`bzr revno ${found} &>/dev/stdout | tail -n1`"
507 esac
509 return $?
512 ################################################################################################################################################
513 ## Begin ULE;
514 ## This is the actual beginning of update-live-ebuilds, skipping past all the functions. This beginning checks a few prerequisites.
515 ################################################################################################################################################
516 trap 'echo; exit ${USER_INTERRUPT}' TERM INT
517 [ "${BASH_VERSION//.*}" -lt 3 ] && echo "Requires BASH v3 or better" && exit 5
519 if [ ! -f "/etc/ule/ule.conf" ]; then
520 echo -n "This script failed a sanity check, cannot continue without"
521 echo "/etc/ule/ule.conf"
522 exit 2
524 source /etc/ule/ule.conf
526 if [ "${UID}" -ne 0 ]; then
527 echo "${0}: root access required."
528 exit 3
531 ## Variables that don't need to be in the conf
532 CUREXPORTS=''
533 DB_LOC='/var/db/pkg'
534 PACKAGES_SO_FAR=''
536 ## Defaults
537 ALL=${false}
538 BASELINE=${false}
539 DATE_FORMAT="%x %X"
540 PRETEND=${false}
541 REPORT=${false}
542 TOTAL=''
543 USAGE=${false}
544 USER_INTERRUPT=13
546 while getopts "abhpqr" opt; do
547 case "${opt}" in
548 a) ALL=${true} ;;
549 b) BASELINE=${true} ;;
550 h) USAGE=${true} ;;
551 p) PRETEND=${true} ;;
552 q) VERBOSE=${false} ;;
553 r) REPORT=${true} ;;
554 esac
555 done
556 shift $(($OPTIND - 1))
558 if [ ${USAGE} -eq ${true} ]; then
559 cat << USAGE
560 Usage: ${0} [OPTION] [PKGSPEC]
561 Example: ${0} -v net-www/gnash
562 Search for live repository ebuilds that need updating, and update them if necessary.
564 -a Reemerge all packages fetched from various repositories
565 -b Baseline the ebuild (write into the database without updating)
566 -h This help file
567 -p If update is required, only pretend to emerge.
568 -q Show less output
569 -r Report information about packages that are tracked by update-live-ebuilds
571 See man 8 update-live-ebuilds for more information.
572 Report bugs to Avuton Olrich <avuton@gmail.com>
573 USAGE
574 exit 0
576 unset USAGE
578 if [ ${BASELINE} -eq ${true} ] && [ ${PRETEND} -eq ${true} ]; then
579 echo "Baseline and pretend are not compatible."
580 exit 22
583 ################################################################################################################################################
584 ## Update only support;
585 ## The purpose of doing things this way is so we update libraries in the order of:
586 ## 1) Libraries
587 ## 2) Everything but libraries and plugins
588 ## 3) Plugins
589 ## This is done like this to make sure libraries are updated first in the case that the packages (#2 & #3) that depend on the
590 ## libraries have the latest and greatest to link to, if necessary. Plugins are done last as they may depend on the
591 ## packages in #2.
593 ## if;
594 ## This is a bit of mumbo jumbo, mostly for the reason that we don't yet track version numbers, only by provide
595 ## A side effect of not being able to track version numbers means we need to glob for the version in the db's directory
596 ## which may grab two seperate directorys with the same name, but one longer like: foo-1.1 and foobar-2.1. It's not
597 ## a great side effect, but in reality shouldn't hit too many people. A more elegant solution is wanted, but complexity
598 ## is not.
599 ## else;
600 ## Self-explanatory.
601 ################################################################################################################################################
602 if [ -n "${1}" ]; then
603 for ebuild in $@; do
604 [ "${ebuild:0:1}" == '=' ] && ebuild="${ebuild:1}"
606 ebuild="${ebuild//:[[:digit:]]}" # Remove the slot for now
608 for mydir in `[[ ${ebuild} == */* ]] && echo ${DB_LOC}/${ebuild}* || echo ${DB_LOC}/*/${ebuild}*`; do
609 [ ! -d "${mydir}" ] && vecho "Directory doesn't exist: ${mydir}, continuing..." && continue
610 if [[ "${mydir}" == *libs* ]]; then
611 libs="${libs} ${mydir}"
612 elif [[ "${mydir}" == *plugin* ]]; then
613 plugin="${plugin} ${mydir}"
614 else
615 nlibs="${nlibs} ${mydir}"
617 done
618 done
620 [ -z "${nlibs}" ] && [ -z "${plugin}" ] && [ -z "${libs}" ] && echo "No packages found." && exit 5
621 else
622 libs="`find "${DB_LOC}" -mindepth 2 -maxdepth 2 -type d -wholename "*libs*"`"
623 plugin="`find "${DB_LOC}" -mindepth 2 -maxdepth 2 -type d -wholename "*plugin*"`"
624 nlibs="`find "${DB_LOC}" -mindepth 2 -maxdepth 2 -type d -not \( -wholename "*plugin*" -o -wholename "*libs*" \)`"
627 ################################################################################################################################################
628 ## Begin looping for SCM detection and emersion
629 ## Beyond this, each ebuild runs in this for loop. Attempt is made to weed out the non-SCM ebuilds immediately to prevent any unnecessary
630 ## CPU time incurred.
631 ################################################################################################################################################
632 for dir in ${libs} ${nlibs} ${plugin}; do
633 type=${false}
634 locbase=${BASELINE}
635 newhash=${false}
636 prescm=${false}
638 ################################################################################################################################################
639 ## Cleanup;
640 ## This prevents bugs.
641 ################################################################################################################################################
642 unset ${CUREXPORTS} CUREXPORTS
644 ################################################################################################################################################
645 ## Detect INHERITED location;
646 ## This should always go in the first block; the only reason it wouldn't is if the user used pkgcore to install a package, as pkgcore
647 ## doesn't install an INHERITED file.
648 ################################################################################################################################################
649 if [ -f "${dir}/INHERITED" ]; then
650 INHERITED="$(cat ${dir}/INHERITED)"
651 elif [ -f "${dir}"/*.ebuild ]; then
652 extract_env 'INHERITED'
653 else
654 continue ## This really shouldn't happen unless something is trashing the portage database.
657 ## The inherit line is checked here for the various scms defined in the config, and executed below.
658 for scm in ${SCM_SUPPORT[@]}; do
659 [[ "${INHERITED}" = *${scm}* ]] && type="${scm}" && break
660 done
662 ## .. or next file
663 [ "${type}" == ${false} ] && continue
665 if [ ! -f "${dir}/environment.bz2" ]; then
666 echo 'Error: Failed a sanity check.'
667 echo "dir: ${dir} has no environment.bz2 file!"
669 echo 'Correct this condition by manually reemerging.'
670 echo "If you believe this is a script error,"
671 email_details
672 exit 6
675 secure_env 'CATEGORY' 'PN' 'SLOT'
676 if [ $? -ne ${true} ]; then
677 echo "This is an assertion, you probably have a corrupt portage database for dir ${dir}."
678 email_details
679 continue
682 ## Finish this
683 # if [ "${PN}" == ${ebuild/${ebuild%%:[[:digit:]]}:} ]; then
684 # echo blah
685 # fi
687 provide="${CATEGORY}/${PN}:${SLOT}"
688 pprovide="${CATEGORY}/${PN}"
689 ule_file="${ULE_LOC}/${provide/:*}/ULE_INFO.${SLOT}"
690 ULE_DB_FILES="${ULE_DB_FILES} ${ule_file}"
691 decho "${provide} uses the ${scm} SCM".
693 [ -f "${ULE_LOC}/${provide}/ULE_INFO" ] && mv "${ULE_LOC}/${provide}/ULE_INFO" "${ule_file}"
695 if [ -f "${ule_file}" ]; then
696 decho 'Sourcing ULE_INFO'
697 source "${ule_file}"
700 TOTAL="${TOTAL} ${provide}"
702 ################################################################################################################################################
703 ## Begin ebuild masking detection;
704 ## This looks for an ebuild on a line in the ULE_MASK_FILE, if it exists, this will simply begin analyzation of the next ebuild in the
705 ## queue, if applicable.
706 ################################################################################################################################################
707 if [ -f "${ULE_MASK_FILE}" ]; then
708 grep -Eq "^(${pprovide}|${provide})$" "${ULE_MASK_FILE}"
710 if [ $? -eq ${true} ]; then
711 vecho "Package (${provide}) found in ULE_MASK, skipping..."
712 continue
716 ################################################################################################################################################
717 ## Portage compatible installer and privelege dropping autodetection;
718 ## This is where autodetection takes place. This will make it not only automatically detect which portage compatible installer to use,
719 ## but it will also use exactly what was used on the package last time, in the case that one of the portage compatible installers cause
720 ## breakage. This is hacky and can break with changes to the package managers, though it will be fixed, I do hope that portage adds
721 ## PKGMANAGER in the future.
722 ################################################################################################################################################
723 spec="${DEFAULT_EMERGE_PROGRAM}"
724 if [ ${FORCE_DEFAULT_EMERGE_PROGRAM} -eq ${false} ]; then
725 if [ -f "${dir}/PKGMANAGER" ]; then
726 tmp=$(cat "${dir}/PKGMANAGER")
727 if [[ "${tmp}" =~ (paludis)-.* ]] && [ "${BASH_REMATCH[1]}" == paludis ]; then
728 spec=${paludis}
729 ## Versions prior to 22 didn't have the ability to be run as non-superuser.
730 [ -n ${PALVER} ] && export PALVER=`paludis --version | head -n1 | sed -e 's/.* [0-9].\([0-9][0-9]\)..*/\1/g'`
731 if [ ${PALVER} -gt 21 ]; then
732 decho "Paludis version ${tmp} (greater than version 0.22.0) is the portage compatible installer"
733 prescm=${true}
734 else
735 decho "Paludis version ${tmp} (less than version 0.22.0) is the portage compatible installer"
737 elif [[ "${tmp}" =~ (pkgcore)-.* ]] && [ "${BASH_REMATCH[1]}" == pkgcore ]; then
738 decho "Pkgcore is the portage compatible installer"
739 spec=${pkgcore}
740 else
741 ## Problems
742 echo "Your portage compatible installer was not recognized, please report this problem"
743 echo "Continuing using default portage compatible installer ${EMERGE_BINARY[${DEFAULT_EMERGE_PROGRAM}]}"
744 email_details
746 else
747 decho "Emerge is the portage compatible installer"
748 spec=${portage}
752 ################################################################################################################################################
753 ## Begin date detection;
754 ## This looks for a ebuild/date combination for the current ebuild being analyzed, if it exists, this will detect if it has been updated
755 ## since the date in the ebuild/date combination and if it has, it will not attempt to emerge again until that time has expired.
756 ################################################################################################################################################
757 [ ${REPORT} -eq ${false} ] && [ -f "${ULE_DATE_FILE}" ] &&
758 userdays=`grep -E "^(\*\/\*\:${SLOT}|${CATEGORY}\/\*|${pprovide}|${provide}) [0-9]*" "${ULE_DATE_FILE}" | tail -n1`
760 if [ ${ALL} -eq ${false} ] && [ ${BASELINE} -eq ${false} ] && [ -n "${userdays}" ]; then
762 if [[ ${userdays} =~ (\*\/\*\:${SLOT} )(.*) ]] || [[ ${userdays} =~ (${CATEGORY}\/\* )(.*) ]] || \
763 [[ ${userdays} =~ (${pprovide} )(.*) ]] || [[ ${userdays} =~ (${provide} )(.*) ]]; then
765 [ -n ${ebuild_epoch} ] && update_ebuild_epoch
766 curepoch=`date +%s`
767 updateepoch=$(((86400*${BASH_REMATCH[2]})+${ebuild_epoch}))
768 diffepoch=$((${updateepoch} - ${curepoch}))
770 if [ ${diffepoch} -ne 0 ] && [ ${updateepoch} -gt ${curepoch} ]; then
771 if [ ${diffepoch} -gt 86400 ]; then
772 vecho "$((${diffepoch}/86400)) days until scheduled update attempts resume for ${provide}."
773 else
774 vecho "Less than a day until scheduled update attempts resume for ${provide}"
776 continue
780 unset updateepoch curepoch userdays
782 if [ ${REPORT} -eq ${true} ]; then
783 if [ -n ${ebuild_epoch} ]; then
784 update_ebuild_epoch
785 printf "\n"
788 source ${ule_file}
789 printf "${provide}\n"
790 printf "\tRevision: ${ULE_COOKIE}\n"
791 printf "\tEmerged on: `date -d \"1970-01-01 UTC ${ebuild_epoch} sec\" \"+${DATE_FORMAT}\"`\n\n"
792 continue
795 vecho "Attempting to check if ${provide} needs an update."
797 if [ -f /etc/make.conf ] && [ ${spec} -eq ${portage} ] || [ ${spec} -eq ${pkgcore} ]; then
798 grep 'userpriv' /etc/make.conf | grep -v '.*#.*userpriv.*'&>/dev/null
799 [ $? -eq 0 ] && prescm=${true}
802 if [ ${prescm} -eq ${true} ]; then
803 decho "Dropping privileges for ${provide}."
804 prescm="sudo -u ${EMERGE_USER[${spec}]}"
805 else
806 decho "Not dropping privileges for ${provide}."
807 prescm=''
810 get_hash
812 ## As long as 'newhash' was calculated last in the section above, this should be the outcome of it.
813 update_check $? "${provide}"
815 ################################################################################################################################################
816 ## Where the magic happens;
818 ## if;
819 ## We cannot locbase here, because we can't guarantee the repository has only been updated locally, it's basically assurance that
820 ## the cookie that goes into the ule database is the version that's installed.
821 ## elif;
822 ## In this case ALL is in the case that the user wants to reinstall and reupdate the ule database, reguardless of updates. The
823 ## locbase is also showing that we assume we didn't previously use the emerger().
824 ## elif;
825 ## This is the case where the ULE_COOKIE matches the newhash (no updates!), we're going to skip the emerge and simply baseline,
826 ## or we've got an empty newhash, which should absolutely, positively never happen. The only reason it would happen is
827 ## if there aren't enough update_check()s sprinkled above. This can also be caused by the SCM command line interface
828 ## throwing the wrong exit code, for example throwing 0 on failure (see Subversion bug 2414).
829 ## After fi;
830 ## Finally, this will be where 90-95% of all emerger() inits go, we cleanup prior to it then set the emerger to emerge with
831 ## ule db updating.
832 ################################################################################################################################################
833 if [ -z "${ULE_COOKIE}" ] && [ ${locbase} -eq ${false} ]; then
834 vecho "No previous cookie detected, reemerging"
835 elif [ ${ALL} -eq ${true} ] && [ ${locbase} -eq ${false} ]; then
836 vecho "Emerging all, reemerging"
837 elif [ "${ULE_COOKIE}" == "${newhash}" ] || [ ${locbase} -eq ${true} ] || [ -z "${newhash}" ]; then
838 if [ ${locbase} -eq ${true} ]; then
839 vecho "Baselineing ${provide}"
840 write_to_db "${newhash}" "${ULE_LOC}/${provide}"
842 [ -z "${newhash}" ] && vecho "Newhash empty, this should never happen unless errors that wern't caught previously"
843 unset ${CUREXPORTS} CUREXPORTS
844 continue
847 unset ${CUREXPORTS} CUREXPORTS
849 emerger ${true}
851 unset provide newhash type locbase
852 done
854 [ ${REPORT} -eq ${true} ] && exit 0
856 [ ${VERBOSE} -eq ${true} ] && echo 'Update complete'
858 [ -n "${1}" ] && exit 0
860 ## Update-only cannot make it this far; thus this should be fine.
862 ## Cleanup the ULE directory
863 echo "Cleaning ULE database"
864 for file in ${ULE_DB_FILES}; do
865 [ -n "${PACKAGES}" ] && PACKAGES="${PACKAGES} -a"
866 PACKAGES="${PACKAGES} -not -wholename "${file}""
867 done
869 ## Find and remove the ULE_INFO files
870 find ${ULE_LOC} -maxdepth 3 -mindepth 3 ${PACKAGES} -exec rm -v {} \;
872 ## Clean out the empty directories
873 find ${ULE_LOC} -depth -empty -type d -exec rmdir -v {} \;
875 if [ -n "${SUCCESS_PACKAGES}" ] && [ "${VERBOSE}" -eq ${true} ]; then
876 echo "The following packages were updated: ${SUCCESS_PACKAGES}"
877 unset SUCCESS_PACKAGES
880 if [ -n "${FAILED_PACKAGES}" ]; then
881 echo "The following programs failed: ${FAILED_PACKAGES}."
882 echo "You could either rerun this program at a later date, or you could do it on an individual"
883 echo "basis by just using the package spec as a argument: i.e. ${0} net-www/gnash"
884 unset FAILED_PACKAGES
887 if [ -n "${UPDATE_FAIL_PACKAGES}" ]; then
888 echo "The following programs failed when trying to be updated by update-live-ebuilds: ${UPDATE_FAIL_PACKAGES}"
889 unset UPDATE_FAIL_PACKAGES