From b14f667e022812a69b1354d1d9612cf36e7ff1a7 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Fri, 21 Jul 2006 06:25:16 +0000 Subject: Move new_protect_filename from the core portage module to portage_util. svn path=/main/trunk/; revision=3978 --- pym/portage_util.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'pym/portage_util.py') diff --git a/pym/portage_util.py b/pym/portage_util.py index 8fa62c5a2..cf115d1e9 100644 --- a/pym/portage_util.py +++ b/pym/portage_util.py @@ -6,6 +6,7 @@ from portage_exception import PortageException, FileNotFound, \ OperationNotPermitted, PermissionDenied, ReadOnlyFileSystem import portage_exception from portage_dep import isvalidatom +import portage_checksum import sys,string,shlex,os,errno try: @@ -853,3 +854,53 @@ class ConfigProtect(object): #skip, it's in the mask masked = len(pmpath) return protected > masked + +def new_protect_filename(mydest, newmd5=None): + """Resolves a config-protect filename for merging, optionally + using the last filename if the md5 matches. + (dest,md5) ==> 'string' --- path_to_target_filename + (dest) ==> ('next', 'highest') --- next_target and most-recent_target + """ + + # config protection filename format: + # ._cfg0000_foo + # 0123456789012 + prot_num = -1 + last_pfile = "" + + if len(mydest) == 0: + raise ValueError, "Empty path provided where a filename is required" + if mydest[-1] == os.path.sep: # XXX add better directory checking + raise ValueError, "Directory provided but this function requires a filename" + if not os.path.exists(mydest): + return mydest + + real_filename = os.path.basename(mydest) + real_dirname = os.path.dirname(mydest) + for pfile in os.listdir(real_dirname): + if pfile[0:5] != "._cfg": + continue + if pfile[10:] != real_filename: + continue + try: + new_prot_num = int(pfile[5:9]) + if new_prot_num > prot_num: + prot_num = new_prot_num + last_pfile = pfile + except ValueError: + continue + prot_num = prot_num + 1 + + new_pfile = normalize_path(os.path.join(real_dirname, + "._cfg" + str(prot_num).zfill(4) + "_" + real_filename)) + old_pfile = normalize_path(os.path.join(real_dirname, last_pfile)) + if last_pfile and newmd5: + if portage_checksum.perform_md5( + os.path.join(real_dirname, last_pfile)) == newmd5: + return old_pfile + else: + return new_pfile + elif newmd5: + return new_pfile + else: + return (new_pfile, old_pfile) -- cgit v1.2.3-1-g7c22