Enable portage.env_update() to work without globals.
authorZac Medico <zmedico@gentoo.org>
Sat, 29 Apr 2006 02:42:21 +0000 (02:42 -0000)
committerZac Medico <zmedico@gentoo.org>
Sat, 29 Apr 2006 02:42:21 +0000 (02:42 -0000)
svn path=/main/trunk/; revision=3265

pym/portage.py

index cf5b700fc3778b9fe5bd128820e98e5f5dd8d29d..02457d77920e23911c09e515a792e03363ea837d 100644 (file)
@@ -502,13 +502,16 @@ def elog_process(cpv, mysettings):
 
 #parse /etc/env.d and generate /etc/profile.env
 
-def env_update(makelinks=1):
-       global root, mtimedb
-       if not os.path.exists(root+"etc/env.d"):
-               prevmask=os.umask(0)
-               os.makedirs(root+"etc/env.d",0755)
-               os.umask(prevmask)
-       fns=listdir(root+"etc/env.d",EmptyOnError=1)
+def env_update(makelinks=1, target_root=None, prev_mtimes=None):
+       if target_root is None:
+               global root
+               target_root = root
+       if prev_mtimes is None:
+               global mtimedb
+               prev_mtimes = mtimedb["ldpath"]
+       envd_dir = os.path.join(target_root, "etc", "env.d")
+       portage_util.ensure_dirs(envd_dir, mode=0755)
+       fns = listdir(envd_dir, EmptyOnError=1)
        fns.sort()
        pos=0
        while (pos<len(fns)):
@@ -539,9 +542,10 @@ def env_update(makelinks=1):
                # don't process backup files
                if x[-1]=='~' or x[-4:]==".bak":
                        continue
-               myconfig=getconfig(root+"etc/env.d/"+x)
+               file_path = os.path.join(envd_dir, x)
+               myconfig = getconfig(file_path)
                if myconfig is None:
-                       writemsg("!!! Parsing error in "+str(root)+"etc/env.d/"+str(x)+"\n")
+                       writemsg("!!! Parsing error in '%s'\n" % file_path)
                        #parse error
                        continue
                # process PATH, CLASSPATH, LDPATH
@@ -556,8 +560,9 @@ def env_update(makelinks=1):
                for myenv in myconfig.keys():
                        env[myenv]=myconfig[myenv]
 
-       if os.path.exists(root+"etc/ld.so.conf"):
-               myld=open(root+"etc/ld.so.conf")
+       ldsoconf_path = os.path.join(target_root, "etc", "ld.so.conf")
+       try:
+               myld = open(ldsoconf_path)
                myldlines=myld.readlines()
                myld.close()
                oldld=[]
@@ -566,17 +571,17 @@ def env_update(makelinks=1):
                        if x[0]=="#":
                                continue
                        oldld.append(x[:-1])
-       #       os.rename(root+"etc/ld.so.conf",root+"etc/ld.so.conf.bak")
-       # Where is the new ld.so.conf generated? (achim)
-       else:
-               oldld=None
+       except (IOError, OSError), e:
+               if e.errno != errno.ENOENT:
+                       raise
+               oldld = None
 
        ld_cache_update=False
 
        newld=specials["LDPATH"]
        if (oldld!=newld):
                #ld.so.conf needs updating and ldconfig needs to be run
-               myfd = atomic_ofstream(os.path.join(root, "etc", "ld.so.conf"))
+               myfd = atomic_ofstream(ldsoconf_path)
                myfd.write("# ld.so.conf autogenerated by env-update; make all changes to\n")
                myfd.write("# contents of /etc/env.d directory\n")
                for x in specials["LDPATH"]:
@@ -586,7 +591,8 @@ def env_update(makelinks=1):
 
        # Update prelink.conf if we are prelink-enabled
        if prelink_capable:
-               newprelink = atomic_ofstream(os.path.join(root, "etc", "prelink.conf"))
+               newprelink = atomic_ofstream(
+                       os.path.join(target_root, "etc", "prelink.conf"))
                newprelink.write("# prelink.conf autogenerated by env-update; make all changes to\n")
                newprelink.write("# contents of /etc/env.d directory\n")
 
@@ -612,31 +618,28 @@ def env_update(makelinks=1):
                        newprelink.write("-b "+x+"\n")
                newprelink.close()
 
-       if not mtimedb.has_key("ldpath"):
-               mtimedb["ldpath"]={}
-
        for lib_dir in portage_util.unique_array(specials["LDPATH"]+['usr/lib','usr/lib64','usr/lib32','lib','lib64','lib32']):
-               x = os.path.join(root, lib_dir.lstrip(os.sep))
+               x = os.path.join(target_root, lib_dir.lstrip(os.sep))
                try:
                        newldpathtime = os.stat(x)[stat.ST_MTIME]
                except OSError, oe:
                        if oe.errno == errno.ENOENT:
                                try:
-                                       del mtimedb["ldpath"][x]
+                                       del prev_mtimes[x]
                                except KeyError:
                                        pass
                                # ignore this path because it doesn't exist
                                continue
                        raise
                mtime_changed = False
-               if mtimedb["ldpath"].has_key(x):
-                       if mtimedb["ldpath"][x]==newldpathtime:
+               if x in prev_mtimes:
+                       if prev_mtimes[x] == newldpathtime:
                                pass
                        else:
-                               mtimedb["ldpath"][x]=newldpathtime
+                               prev_mtimes[x] = newldpathtime
                                mtime_changed = True
                else:
-                       mtimedb["ldpath"][x]=newldpathtime
+                       prev_mtimes[x] = newldpathtime
                        mtime_changed = True
 
                if mtime_changed:
@@ -650,14 +653,16 @@ def env_update(makelinks=1):
                        # an older package installed ON TOP of a newer version will cause ldconfig
                        # to overwrite the symlinks we just made. -X means no links. After 'clean'
                        # we can safely create links.
-                       writemsg(">>> Regenerating "+str(root)+"etc/ld.so.cache...\n")
+                       writemsg(">>> Regenerating %setc/ld.so.cache...\n" % target_root)
                        if makelinks:
-                               commands.getstatusoutput("cd / ; /sbin/ldconfig -r "+root)
+                               commands.getstatusoutput("cd / ; /sbin/ldconfig -r '%s'" % target_root)
                        else:
-                               commands.getstatusoutput("cd / ; /sbin/ldconfig -X -r "+root)
+                               commands.getstatusoutput("cd / ; /sbin/ldconfig -X -r '%s'" % target_root)
                elif ostype in ("FreeBSD","DragonFly"):
-                       writemsg(">>> Regenerating "+str(root)+"var/run/ld-elf.so.hints...\n")
-                       commands.getstatusoutput("cd / ; /sbin/ldconfig -elf -i -f "+str(root)+"var/run/ld-elf.so.hints "+str(root)+"etc/ld.so.conf")
+                       writemsg(">>> Regenerating %svar/run/ld-elf.so.hints...\n" % target_root)
+                       commands.getstatusoutput(
+                               "cd / ; /sbin/ldconfig -elf -i -f '%svar/run/ld-elf.so.hints' '%setc/ld.so.conf'" % \
+                               (target_root, target_root))
 
        del specials["LDPATH"]
 
@@ -668,7 +673,7 @@ def env_update(makelinks=1):
        cenvnotice += "# GO INTO /etc/csh.cshrc NOT /etc/csh.env\n\n"
 
        #create /etc/profile.env for bash support
-       outfile = atomic_ofstream(os.path.join(root, "etc", "profile.env"))
+       outfile = atomic_ofstream(os.path.join(target_root, "etc", "profile.env"))
        outfile.write(penvnotice)
 
        for path in specials.keys():
@@ -692,7 +697,7 @@ def env_update(makelinks=1):
        outfile.close()
 
        #create /etc/csh.env for (t)csh support
-       outfile = atomic_ofstream(os.path.join(root, "etc", "csh.env"))
+       outfile = atomic_ofstream(os.path.join(target_root, "etc", "csh.env"))
        outfile.write(cenvnotice)
 
        for path in specials.keys():
@@ -6785,7 +6790,8 @@ def load_mtimedb(f):
        if "cur" in d:
                del d["cur"]
 
-       d.setdefault("updates", {})
+       for k in ("ldpath", "updates"):
+               d.setdefault(k, {})
 
        mtimedbkeys = set(("info", "ldpath", "resume", "resume_backup",
                "starttime", "updates", "version"))
@@ -6870,7 +6876,7 @@ try:
        f.close()
        del f
 except (IOError, OSError):
-       mtimedb = {"updates":{}, "version":"", "starttime":0}
+       mtimedb = {"updates":{}, "ldpath":{}, "version":"", "starttime":0}
 
 # ============================================================================
 # COMPATIBILITY