From fbbb246d809bd8d6f72ae129867475e952806e4d Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Fri, 21 Jul 2006 07:19:10 +0000 Subject: [PATCH] Move update_config_files from the core portage module to portage_update. svn path=/main/trunk/; revision=3980 --- pym/portage.py | 72 +----------------------------------- pym/portage_update.py | 85 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 74 deletions(-) diff --git a/pym/portage.py b/pym/portage.py index 15d191269..84a66d399 100644 --- a/pym/portage.py +++ b/pym/portage.py @@ -91,7 +91,8 @@ try: from portage_checksum import perform_md5,perform_checksum,prelink_capable import eclass_cache from portage_localization import _ - from portage_update import fixdbentries, grab_updates, parse_updates, update_dbentries + from portage_update import fixdbentries, grab_updates, parse_updates, \ + update_config_files, update_dbentries # Need these functions directly in portage namespace to not break every external tool in existence from portage_versions import ververify,vercmp,catsplit,catpkgsplit,pkgsplit,pkgcmp @@ -6680,75 +6681,6 @@ def portageexit(): atexit_register(portageexit) -def update_config_files(config_root, protect, protect_mask, update_iter): - """Perform global updates on /etc/portage/package.* and the world file. - config_root - location of files to update - protect - list of paths from CONFIG_PROTECT - protect_mask - list of paths from CONFIG_PROTECT_MASK - update_iter - list of update commands as returned from parse_updates()""" - update_files={} - file_contents={} - myxfiles = ["package.mask","package.unmask","package.keywords","package.use"] - myxfiles.extend(prefix_array(myxfiles, "profile/")) - abs_user_config = os.path.join(config_root, - USER_CONFIG_PATH.lstrip(os.path.sep)) - recursivefiles = [] - for x in myxfiles: - config_file = os.path.join(abs_user_config, x) - if os.path.isdir(config_file): - recursivefiles.extend([os.path.join(x, y) \ - for y in listdir(config_file, filesonly=1, recursive=1)]) - else: - recursivefiles.append(x) - myxfiles = recursivefiles - for x in myxfiles: - try: - myfile = open(os.path.join(abs_user_config, x),"r") - file_contents[x] = myfile.readlines() - myfile.close() - except IOError: - if file_contents.has_key(x): - del file_contents[x] - continue - worldlist = grabfile(os.path.join(config_root, WORLD_FILE)) - - for update_cmd in update_iter: - if update_cmd[0] == "move": - old_value, new_value = update_cmd[1], update_cmd[2] - #update world entries: - for x in range(0,len(worldlist)): - #update world entries, if any. - worldlist[x] = dep_transform(worldlist[x], old_value, new_value) - - #update /etc/portage/packages.* - for x in file_contents: - for mypos in range(0,len(file_contents[x])): - line = file_contents[x][mypos] - if line[0] == "#" or string.strip(line) == "": - continue - key = dep_getkey(line.split()[0]) - if key == old_value: - file_contents[x][mypos] = string.replace(line, old_value, new_value) - update_files[x] = 1 - sys.stdout.write("p") - sys.stdout.flush() - - write_atomic(os.path.join(config_root, WORLD_FILE), "\n".join(worldlist)) - - protect_obj = portage_util.ConfigProtect( - config_root, protect, protect_mask) - for x in update_files: - updating_file = os.path.join(abs_user_config, x) - if protect_obj.isprotected(updating_file): - updating_file = new_protect_filename(updating_file)[0] - try: - write_atomic(updating_file, "".join(file_contents[x])) - except portage_exception.PortageException, e: - writemsg("\n!!! %s\n" % str(e), noiselevel=-1) - writemsg("!!! An error occured while updating a config file:" + \ - " '%s'\n" % updating_file, noiselevel=-1) - continue - def global_updates(mysettings, trees, prev_mtimes): """Perform new global updates if they exist in $PORTDIR/profiles/updates/.""" # only do this if we're root and not running repoman/ebuild digest diff --git a/pym/portage_update.py b/pym/portage_update.py index 000514bf4..40f05f795 100644 --- a/pym/portage_update.py +++ b/pym/portage_update.py @@ -2,11 +2,13 @@ # Distributed under the terms of the GNU General Public License v2 # $Header: $ -import errno, os, re +import errno, os, re, sys -from portage_util import write_atomic -from portage_exception import DirectoryNotFound -from portage_dep import isvalidatom, isjustname +from portage_util import ConfigProtect, grabfile, new_protect_filename, \ + normalize_path, write_atomic, writemsg +from portage_exception import DirectoryNotFound, PortageException +from portage_dep import dep_getkey, dep_transform, isvalidatom, isjustname +from portage_const import USER_CONFIG_PATH, WORLD_FILE ignored_dbentries = ("CONTENTS", "environment.bz2") @@ -119,3 +121,78 @@ def parse_updates(mycontent): # The list of valid updates is filtered by continue statements above. myupd.append(mysplit) return myupd, errors + +def update_config_files(config_root, protect, protect_mask, update_iter): + """Perform global updates on /etc/portage/package.* and the world file. + config_root - location of files to update + protect - list of paths from CONFIG_PROTECT + protect_mask - list of paths from CONFIG_PROTECT_MASK + update_iter - list of update commands as returned from parse_updates()""" + config_root = normalize_path(config_root) + update_files = {} + file_contents = {} + myxfiles = ["package.mask", "package.unmask", \ + "package.keywords", "package.use"] + myxfiles += [os.path.join("profile", x) for x in myxfiles] + abs_user_config = os.path.join(config_root, + USER_CONFIG_PATH.lstrip(os.path.sep)) + recursivefiles = [] + for x in myxfiles: + config_file = os.path.join(abs_user_config, x) + if os.path.isdir(config_file): + for parent, dirs, files in os.walk(config_file): + for y in files: + recursivefiles.append( + os.path.join(parent, y)[len(abs_user_config) + 1:]) + else: + recursivefiles.append(x) + myxfiles = recursivefiles + for x in myxfiles: + try: + myfile = open(os.path.join(abs_user_config, x),"r") + file_contents[x] = myfile.readlines() + myfile.close() + except IOError: + if file_contents.has_key(x): + del file_contents[x] + continue + worldlist = grabfile(os.path.join(config_root, WORLD_FILE)) + + for update_cmd in update_iter: + if update_cmd[0] == "move": + old_value, new_value = update_cmd[1], update_cmd[2] + #update world entries: + for x in range(0,len(worldlist)): + #update world entries, if any. + worldlist[x] = \ + dep_transform(worldlist[x], old_value, new_value) + + #update /etc/portage/packages.* + for x in file_contents: + for mypos in range(0,len(file_contents[x])): + line = file_contents[x][mypos] + if line[0] == "#" or not line.strip(): + continue + key = dep_getkey(line.split()[0]) + if key == old_value: + file_contents[x][mypos] = \ + line.replace(old_value, new_value) + update_files[x] = 1 + sys.stdout.write("p") + sys.stdout.flush() + + write_atomic(os.path.join(config_root, WORLD_FILE), "\n".join(worldlist)) + + protect_obj = ConfigProtect( + config_root, protect, protect_mask) + for x in update_files: + updating_file = os.path.join(abs_user_config, x) + if protect_obj.isprotected(updating_file): + updating_file = new_protect_filename(updating_file)[0] + try: + write_atomic(updating_file, "".join(file_contents[x])) + except PortageException, e: + writemsg("\n!!! %s\n" % str(e), noiselevel=-1) + writemsg("!!! An error occured while updating a config file:" + \ + " '%s'\n" % updating_file, noiselevel=-1) + continue -- 2.26.2