From dd52cb9c4df6f3f21388bbbda691547081e9820e Mon Sep 17 00:00:00 2001 From: Andrew Gaffney Date: Sat, 12 Sep 2009 11:08:52 -0500 Subject: [PATCH] Create catalyst.util.empty_tree() helper function and use it all over --- ChangeLog | 7 ++ modules/catalyst/target/generic_stage.py | 82 +++++------------------- modules/catalyst/target/livecd_stage1.py | 2 +- modules/catalyst/target/netboot.py | 8 +-- modules/catalyst/target/snapshot.py | 22 +------ modules/catalyst/util.py | 14 +++- 6 files changed, 40 insertions(+), 95 deletions(-) diff --git a/ChangeLog b/ChangeLog index b9e2b60d..9bf50227 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,13 @@ # Distributed under the GPL v2 # $Id$ + 12 Sep 2009; Andrew Gaffney + modules/catalyst/target/generic_stage.py, + modules/catalyst/target/livecd_stage1.py, + modules/catalyst/target/netboot.py, modules/catalyst/target/snapshot.py, + modules/catalyst/util.py: + Create catalyst.util.empty_tree() helper function and use it all over + 12 Sep 2009; Andrew Gaffney TODO, modules/catalyst/target/generic.py: Add ability to invalidate all further autoresume points diff --git a/modules/catalyst/target/generic_stage.py b/modules/catalyst/target/generic_stage.py index 1ce41a2c..0f4a9fe8 100644 --- a/modules/catalyst/target/generic_stage.py +++ b/modules/catalyst/target/generic_stage.py @@ -4,8 +4,7 @@ This class does all of the chroot setup, copying of files, etc. It is the driver class for pretty much everything that Catalyst does. """ -import os, string, types, shutil -from stat import * +import os, types import catalyst from catalyst.output import * from catalyst.spawn import cmd @@ -224,7 +223,7 @@ class generic_stage_target(generic_target): if "install_mask" in self.settings: if type(self.settings["install_mask"])!=types.StringType: self.settings["install_mask"]=\ - string.join(self.settings["install_mask"]) + " ".join(self.settings["install_mask"]) def set_target_profile(self): self.settings["target_profile"]=self.settings["profile"] @@ -243,7 +242,7 @@ class generic_stage_target(generic_target): if "pkgcache_path" in self.settings: if type(self.settings["pkgcache_path"])!=types.StringType: self.settings["pkgcache_path"]=\ - catalyst.util.normpath(string.join(self.settings["pkgcache_path"])) + catalyst.util.normpath(" ".join(self.settings["pkgcache_path"])) else: self.settings["pkgcache_path"]=\ catalyst.util.normpath(self.settings["storedir"]+"/packages/"+\ @@ -253,7 +252,7 @@ class generic_stage_target(generic_target): if "kerncache_path" in self.settings: if type(self.settings["kerncache_path"])!=types.StringType: self.settings["kerncache_path"]=\ - catalyst.util.normpath(string.join(self.settings["kerncache_path"])) + catalyst.util.normpath(" ".join(self.settings["kerncache_path"])) else: self.settings["kerncache_path"]=catalyst.util.normpath(self.settings["storedir"]+\ "/kerncache/"+self.settings["target_subpath"]+"/") @@ -907,7 +906,7 @@ class generic_stage_target(generic_target): if myusevars: myf.write("# These are the USE flags that were used in addition to what is provided by the\n# profile used for building.\n") myusevars = sorted(set(myusevars)) - myf.write('USE="'+string.join(myusevars)+'"\n') + myf.write('USE="' + " ".join(myusevars)+'"\n') if '-*' in myusevars: msg("\nWarning!!! ") msg("\tThe use of -* in " + "use will cause portage to ignore") @@ -984,15 +983,7 @@ class generic_stage_target(generic_target): msg(x + " not a directory or does not exist, skipping 'empty' operation.") continue msg("Emptying directory" + x) - """ - stat the dir, delete the dir, recreate the dir and set - the proper perms and ownership - """ - mystat=os.stat(myemp) - shutil.rmtree(myemp) - os.makedirs(myemp,0755) - os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) - os.chmod(myemp,mystat[ST_MODE]) + catalyst.util.empty_dir(myemp) self.set_autoresume("empty") def remove(self): @@ -1033,7 +1024,7 @@ class generic_stage_target(generic_target): """ Capture target in a tarball """ mypath=self.settings["target_path"].split("/") """ Remove filename from path """ - mypath=string.join(mypath[:-1],"/") + mypath = " ".join(mypath[:-1],"/") """ Now make sure path exists """ if not os.path.exists(mypath): @@ -1078,8 +1069,8 @@ class generic_stage_target(generic_target): #os.environ[varname]=self.settings[x] self.env[varname]=self.settings[x] elif type(self.settings[x])==types.ListType: - #os.environ[varname]=string.join(self.settings[x]) - self.env[varname]=string.join(self.settings[x]) + #os.environ[varname] = " ".join(self.settings[x]) + self.env[varname] = " ".join(self.settings[x]) elif type(self.settings[x])==types.BooleanType: if self.settings[x]: self.env[varname]="true" @@ -1136,7 +1127,7 @@ class generic_stage_target(generic_target): things like "<" to remain intact """ myunmerge[x]="'"+myunmerge[x]+"'" - myunmerge=string.join(myunmerge) + myunmerge = " ".join(myunmerge) """ Before cleaning, unmerge stuff """ try: @@ -1248,7 +1239,7 @@ class generic_stage_target(generic_target): "/kernelopts"] if type(myopts) != types.StringType: - myopts = string.join(myopts) + myopts = " ".join(myopts) self.env[kname+"_kernelopts"]=myopts else: @@ -1336,19 +1327,7 @@ class generic_stage_target(generic_target): myemp=self.settings["chroot_path"] if os.path.isdir(myemp): msg("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]) + catalyst.util.empty_dir(myemp) def clear_packages(self): if "PKGCACHE" in self.settings: @@ -1357,16 +1336,7 @@ class generic_stage_target(generic_target): myemp=self.settings["pkgcache_path"] if os.path.isdir(myemp): msg("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]) + catalyst.util.empty_dir(myemp) def clear_kerncache(self): if "KERNCACHE" in self.settings: @@ -1375,16 +1345,7 @@ class generic_stage_target(generic_target): myemp=self.settings["kerncache_path"] if os.path.isdir(myemp): msg("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]) + catalyst.util.empty_dir(myemp) def clear_autoresume(self): """ Clean resume points since they are no longer needed """ @@ -1393,20 +1354,7 @@ class generic_stage_target(generic_target): myemp=self.settings["autoresume_path"] if os.path.isdir(myemp): msg("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]) + catalyst.util.empty_dir(myemp) def purge(self): catalyst.util.countdown(10, "Purging Caches ...") diff --git a/modules/catalyst/target/livecd_stage1.py b/modules/catalyst/target/livecd_stage1.py index bb640408..7c066d92 100644 --- a/modules/catalyst/target/livecd_stage1.py +++ b/modules/catalyst/target/livecd_stage1.py @@ -60,7 +60,7 @@ class livecd_stage1_target(generic_stage_target): def set_pkgcache_path(self): if "pkgcache_path" in self.settings: if type(self.settings["pkgcache_path"]) != types.StringType: - self.settings["pkgcache_path"]=catalyst.util.normpath(string.join(self.settings["pkgcache_path"])) + self.settings["pkgcache_path"] = catalyst.util.normpath(" ".join(self.settings["pkgcache_path"])) else: generic_stage_target.set_pkgcache_path(self) diff --git a/modules/catalyst/target/netboot.py b/modules/catalyst/target/netboot.py index 5838a620..81af14f6 100644 --- a/modules/catalyst/target/netboot.py +++ b/modules/catalyst/target/netboot.py @@ -138,13 +138,7 @@ class netboot_target(generic_stage_target): msg(x + " not a directory or does not exist, skipping 'empty' operation.") continue msg("Emptying directory " + x) - # stat the dir, delete the dir, recreate the dir and set - # the proper perms and ownership - mystat=os.stat(myemp) - shutil.rmtree(myemp) - os.makedirs(myemp,0755) - os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) - os.chmod(myemp,mystat[ST_MODE]) + catalyst.util.empty_dir(x) self.set_autoresume("empty") def set_action_sequence(self): diff --git a/modules/catalyst/target/snapshot.py b/modules/catalyst/target/snapshot.py index d10a5a3d..9f3de982 100644 --- a/modules/catalyst/target/snapshot.py +++ b/modules/catalyst/target/snapshot.py @@ -3,7 +3,7 @@ Builder class for snapshots. """ -import os, shutil, stat +import os import catalyst from catalyst.target.generic import generic_target from catalyst.spawn import cmd @@ -28,9 +28,6 @@ class snapshot_target(catalyst.target.generic.generic_target): if not os.path.exists(x): os.makedirs(x) - def mount_safety_check(self): - pass - def run(self): if "PURGEONLY" in self.settings: self.purge() @@ -60,28 +57,15 @@ class snapshot_target(catalyst.target.generic.generic_target): self.cleanup() msg("snapshot: complete!") - def kill_chroot_pids(self): - pass - def cleanup(self): + # What is the point of this? msg("Cleaning up...") def purge(self): myemp=self.settings["tmp_path"] if os.path.isdir(myemp): msg("Emptying directory " + myemp) - """ - stat the dir, delete the dir, recreate the dir and set - the proper perms and ownership - """ - mystat=os.stat(myemp) - """ 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[stat.ST_UID],mystat[stat.ST_GID]) - os.chmod(myemp,mystat[stat.ST_MODE]) + catalyst.util.empty_dir(myemp) __target_map = {"snapshot":snapshot_target} diff --git a/modules/catalyst/util.py b/modules/catalyst/util.py index 078cbfb7..1d3e38e3 100644 --- a/modules/catalyst/util.py +++ b/modules/catalyst/util.py @@ -2,7 +2,7 @@ Collection of utility functions for catalyst """ -import sys, traceback, os, os.path, re, time +import sys, traceback, os, os.path, re, time, shutil import catalyst from catalyst.error import * from catalyst.output import * @@ -195,4 +195,16 @@ def addl_arg_parse(myspec,addlargs,requiredspec,validspec): if not x in myspec: raise CatalystError, "Required argument \""+x+"\" not specified." +def empty_dir(path): + mystat = os.stat(path) + if os.uname()[0] == "FreeBSD": + cmd("chflags -R noschg " + path, \ + "Could not remove immutable flag for file " \ + + path) + shutil.rmtree(path) + os.makedirs(path, 0755) + os.chown(path, mystat[stat.ST_UID], mystat[stat.ST_GID]) + os.chmod(path, mystat[stat.ST_MODE]) + + # vim: ts=4 sw=4 sta noet sts=4 ai -- 2.26.2