From d018f0d8ea29d0326299b40f92e42f8c0fa7a66c Mon Sep 17 00:00:00 2001 From: Brian Dolbec Date: Sun, 20 Jan 2013 00:10:03 -0800 Subject: [PATCH] Begin splitting up generic_stage_target into smaller code blocks. This so snapshot_target does not need to import it since most of it was not used or initialized properly. --- catalyst/targets/clearbase.py | 115 +++++++++++++ catalyst/targets/genbase.py | 58 +++++++ catalyst/targets/generic_stage_target.py | 152 +----------------- catalyst/targets/snapshot_target.py | 10 +- .../{generic_target.py => targetbase.py} | 6 +- 5 files changed, 190 insertions(+), 151 deletions(-) create mode 100644 catalyst/targets/clearbase.py create mode 100644 catalyst/targets/genbase.py rename catalyst/targets/{generic_target.py => targetbase.py} (60%) diff --git a/catalyst/targets/clearbase.py b/catalyst/targets/clearbase.py new file mode 100644 index 00000000..8519accc --- /dev/null +++ b/catalyst/targets/clearbase.py @@ -0,0 +1,115 @@ + +import os +import shutil +from stat import ST_UID, ST_GID, ST_MODE + + +from catalyst.support import cmd, countdown + + +class ClearBase(object): + """ + This class does all of clearing after task completion + """ + def __init__(self, myspec): + self.settings = myspec + + + + def clear_autoresume(self): + """ Clean resume points since they are no longer needed """ + if "autoresume" in self.settings["options"]: + print "Removing AutoResume Points: ..." + myemp=self.settings["autoresume_path"] + if os.path.isdir(myemp): + if "autoresume" in self.settings["options"]: + print "Emptying directory",myemp + """ + stat the dir, delete the dir, recreate the dir and set + the proper perms and ownership + """ + mystat=os.stat(myemp) + if os.uname()[0] == "FreeBSD": + cmd("chflags -R noschg "+myemp,\ + "Could not remove immutable flag for file "\ + +myemp) + #cmd("rm -rf "+myemp, "Could not remove existing file: "+myemp,env-self.env) + shutil.rmtree(myemp) + os.makedirs(myemp,0755) + os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) + os.chmod(myemp,mystat[ST_MODE]) + + + def clear_chroot(self): + myemp=self.settings["chroot_path"] + if os.path.isdir(myemp): + print "Emptying directory",myemp + """ + stat the dir, delete the dir, recreate the dir and set + the proper perms and ownership + """ + mystat=os.stat(myemp) + #cmd("rm -rf "+myemp, "Could not remove existing file: "+myemp,env=self.env) + """ There's no easy way to change flags recursively in python """ + if os.uname()[0] == "FreeBSD": + os.system("chflags -R noschg "+myemp) + shutil.rmtree(myemp) + os.makedirs(myemp,0755) + os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) + os.chmod(myemp,mystat[ST_MODE]) + + + def clear_packages(self): + if "pkgcache" in self.settings["options"]: + print "purging the pkgcache ..." + + myemp=self.settings["pkgcache_path"] + if os.path.isdir(myemp): + print "Emptying directory",myemp + """ + stat the dir, delete the dir, recreate the dir and set + the proper perms and ownership + """ + mystat=os.stat(myemp) + #cmd("rm -rf "+myemp, "Could not remove existing file: "+myemp,env=self.env) + shutil.rmtree(myemp) + os.makedirs(myemp,0755) + os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) + os.chmod(myemp,mystat[ST_MODE]) + + + def clear_kerncache(self): + if "kerncache" in self.settings["options"]: + print "purging the kerncache ..." + + myemp=self.settings["kerncache_path"] + if os.path.isdir(myemp): + print "Emptying directory",myemp + """ + stat the dir, delete the dir, recreate the dir and set + the proper perms and ownership + """ + mystat=os.stat(myemp) + #cmd("rm -rf "+myemp, "Could not remove existing file: "+myemp,env=self.env) + shutil.rmtree(myemp) + os.makedirs(myemp,0755) + os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) + os.chmod(myemp,mystat[ST_MODE]) + + + def purge(self): + countdown(10,"Purging Caches ...") + if any(k in self.settings["options"] for k in ("purge","purgeonly","purgetmponly")): + print "clearing autoresume ..." + self.clear_autoresume() + + print "clearing chroot ..." + self.clear_chroot() + + if "PURGETMPONLY" not in self.settings: + print "clearing package cache ..." + self.clear_packages() + + print "clearing kerncache ..." + self.clear_kerncache() + diff --git a/catalyst/targets/genbase.py b/catalyst/targets/genbase.py new file mode 100644 index 00000000..e818781e --- /dev/null +++ b/catalyst/targets/genbase.py @@ -0,0 +1,58 @@ + + +import os + + +class GenBase(object): + """ + This class does generation of the contents and digests files. + """ + def __init__(self,myspec): + self.settings = myspec + + + def gen_contents_file(self,file): + if os.path.exists(file+".CONTENTS"): + os.remove(file+".CONTENTS") + if "contents" in self.settings: + contents_map = self.settings["contents_map"] + if os.path.exists(file): + myf=open(file+".CONTENTS","w") + keys={} + for i in self.settings["contents"].split(): + keys[i]=1 + array=keys.keys() + array.sort() + for j in array: + contents = contents_map.generate_contents(file, j, + verbose="VERBOSE" in self.settings) + if contents: + myf.write(contents) + myf.close() + + def gen_digest_file(self,file): + if os.path.exists(file+".DIGESTS"): + os.remove(file+".DIGESTS") + if "digests" in self.settings: + hash_map = self.settings["hash_map"] + if os.path.exists(file): + myf=open(file+".DIGESTS","w") + keys={} + for i in self.settings["digests"].split(): + keys[i]=1 + array=keys.keys() + array.sort() + for f in [file, file+'.CONTENTS']: + if os.path.exists(f): + if "all" in array: + for k in list(hash_map.hash_map): + hash = hash_map.generate_hash(f,hash_=k, + verbose = "VERBOSE" in self.settings) + myf.write(hash) + else: + for j in array: + hash = hash_map.generate_hash(f,hash_=j, + verbose = "VERBOSE" in self.settings) + myf.write(hash) + myf.close() + diff --git a/catalyst/targets/generic_stage_target.py b/catalyst/targets/generic_stage_target.py index e66b4308..18e6278c 100644 --- a/catalyst/targets/generic_stage_target.py +++ b/catalyst/targets/generic_stage_target.py @@ -9,14 +9,14 @@ from stat import ST_UID, ST_GID, ST_MODE from catalyst.support import (CatalystError, msg, file_locate, normpath, - touch, cmd, warn, list_bashify, countdown, read_makeconf, read_from_clst, - ismount) - -from generic_target import generic_target + touch, cmd, warn, list_bashify, read_makeconf, read_from_clst, ismount) +from catalyst.targets.targetbase import TargetBase +from catalyst.targets.clearbase import ClearBase +from catalyst.targets.genbase import GenBase from catalyst.lock import LockDir -class generic_stage_target(generic_target): +class generic_stage_target(TargetBase, ClearBase, GenBase): """ This class does all of the chroot setup, copying of files, etc. It is the driver class for pretty much everything that Catalyst does. @@ -31,7 +31,9 @@ class generic_stage_target(generic_target): "distcc_hosts","makeopts","pkgcache_path","kerncache_path"]) self.set_valid_build_kernel_vars(addlargs) - generic_target.__init__(self,myspec,addlargs) + TargetBase.__init__(self, myspec, addlargs) + GenBase.__init__(self, myspec) + ClearBase.__init__(self, myspec) """ The semantics of subarchmap and machinemap changed a bit in 2.0.3 to @@ -1569,142 +1571,4 @@ class generic_stage_target(generic_target): self.unbind() raise CatalystError("build aborting due to livecd_update error.") - def clear_chroot(self): - myemp=self.settings["chroot_path"] - if os.path.isdir(myemp): - print "Emptying directory",myemp - """ - stat the dir, delete the dir, recreate the dir and set - the proper perms and ownership - """ - mystat=os.stat(myemp) - #cmd("rm -rf "+myemp, "Could not remove existing file: "+myemp,env=self.env) - """ There's no easy way to change flags recursively in python """ - if os.uname()[0] == "FreeBSD": - os.system("chflags -R noschg "+myemp) - shutil.rmtree(myemp) - os.makedirs(myemp,0755) - os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) - os.chmod(myemp,mystat[ST_MODE]) - - def clear_packages(self): - if "pkgcache" in self.settings["options"]: - print "purging the pkgcache ..." - - myemp=self.settings["pkgcache_path"] - if os.path.isdir(myemp): - print "Emptying directory",myemp - """ - stat the dir, delete the dir, recreate the dir and set - the proper perms and ownership - """ - mystat=os.stat(myemp) - #cmd("rm -rf "+myemp, "Could not remove existing file: "+myemp,env=self.env) - shutil.rmtree(myemp) - os.makedirs(myemp,0755) - os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) - os.chmod(myemp,mystat[ST_MODE]) - - def clear_kerncache(self): - if "kerncache" in self.settings["options"]: - print "purging the kerncache ..." - - myemp=self.settings["kerncache_path"] - if os.path.isdir(myemp): - print "Emptying directory",myemp - """ - stat the dir, delete the dir, recreate the dir and set - the proper perms and ownership - """ - mystat=os.stat(myemp) - #cmd("rm -rf "+myemp, "Could not remove existing file: "+myemp,env=self.env) - shutil.rmtree(myemp) - os.makedirs(myemp,0755) - os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) - os.chmod(myemp,mystat[ST_MODE]) - - def clear_autoresume(self): - """ Clean resume points since they are no longer needed """ - if "autoresume" in self.settings["options"]: - print "Removing AutoResume Points: ..." - myemp=self.settings["autoresume_path"] - if os.path.isdir(myemp): - if "autoresume" in self.settings["options"]: - print "Emptying directory",myemp - """ - stat the dir, delete the dir, recreate the dir and set - the proper perms and ownership - """ - mystat=os.stat(myemp) - if os.uname()[0] == "FreeBSD": - cmd("chflags -R noschg "+myemp,\ - "Could not remove immutable flag for file "\ - +myemp) - #cmd("rm -rf "+myemp, "Could not remove existing file: "+myemp,env-self.env) - shutil.rmtree(myemp) - os.makedirs(myemp,0755) - os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) - os.chmod(myemp,mystat[ST_MODE]) - - def gen_contents_file(self,file): - if os.path.exists(file+".CONTENTS"): - os.remove(file+".CONTENTS") - if "contents" in self.settings: - contents_map = self.settings["contents_map"] - if os.path.exists(file): - myf=open(file+".CONTENTS","w") - keys={} - for i in self.settings["contents"].split(): - keys[i]=1 - array=keys.keys() - array.sort() - for j in array: - contents = contents_map.generate_contents(file, j, - verbose="VERBOSE" in self.settings) - if contents: - myf.write(contents) - myf.close() - - def gen_digest_file(self,file): - if os.path.exists(file+".DIGESTS"): - os.remove(file+".DIGESTS") - if "digests" in self.settings: - hash_map = self.settings["hash_map"] - if os.path.exists(file): - myf=open(file+".DIGESTS","w") - keys={} - for i in self.settings["digests"].split(): - keys[i]=1 - array=keys.keys() - array.sort() - for f in [file, file+'.CONTENTS']: - if os.path.exists(f): - if "all" in array: - for k in list(hash_map.hash_map): - hash = hash_map.generate_hash(f,hash_=k, - verbose = "VERBOSE" in self.settings) - myf.write(hash) - else: - for j in array: - hash = hash_map.generate_hash(f,hash_=j, - verbose = "VERBOSE" in self.settings) - myf.write(hash) - myf.close() - - def purge(self): - countdown(10,"Purging Caches ...") - if any(k in self.settings["options"] for k in ("purge","purgeonly","purgetmponly")): - print "clearing autoresume ..." - self.clear_autoresume() - - print "clearing chroot ..." - self.clear_chroot() - - if "PURGETMPONLY" not in self.settings: - print "clearing package cache ..." - self.clear_packages() - - print "clearing kerncache ..." - self.clear_kerncache() - # vim: ts=4 sw=4 sta et sts=4 ai diff --git a/catalyst/targets/snapshot_target.py b/catalyst/targets/snapshot_target.py index e6668376..3289bbde 100644 --- a/catalyst/targets/snapshot_target.py +++ b/catalyst/targets/snapshot_target.py @@ -8,9 +8,10 @@ from stat import ST_UID, ST_GID, ST_MODE from catalyst.support import normpath, cmd -from generic_stage_target import generic_stage_target +from catalyst.targets.targetbase import TargetBase +from catalyst.targets.genbase import GenBase -class snapshot_target(generic_stage_target): +class snapshot_target(TargetBase, GenBase): """ Builder class for snapshots. """ @@ -18,8 +19,9 @@ class snapshot_target(generic_stage_target): self.required_values=["version_stamp","target"] self.valid_values=["version_stamp","target"] - generic_stage_target.__init__(self,myspec,addlargs) - self.settings=myspec + TargetBase.__init__(self, myspec, addlargs) + GenBase.__init__(self,myspec) + #self.settings=myspec self.settings["target_subpath"]="portage" st=self.settings["storedir"] self.settings["snapshot_path"] = normpath(st + "/snapshots/" diff --git a/catalyst/targets/generic_target.py b/catalyst/targets/targetbase.py similarity index 60% rename from catalyst/targets/generic_target.py rename to catalyst/targets/targetbase.py index d3b9511e..b7a7428a 100644 --- a/catalyst/targets/generic_target.py +++ b/catalyst/targets/targetbase.py @@ -2,11 +2,11 @@ from catalyst.support import addl_arg_parse -class generic_target: +class TargetBase(object): """ - The toplevel class for generic_stage_target. This is about as generic as we get. + The toplevel class for all targets. This is about as generic as we get. """ - def __init__(self,myspec,addlargs): + def __init__(self, myspec, addlargs): addl_arg_parse(myspec,addlargs,self.required_values,self.valid_values) self.settings=myspec self.env={} -- 2.26.2