Gentoo Archives: gentoo-commits

From: Brian Dolbec <brian.dolbec@×××××.com>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/catalyst:3.0 commit in: catalyst/, catalyst/base/
Date: Tue, 31 Dec 2013 10:48:44
Message-Id: 1388484018.d3f06c64221059f8eec4bec99072b9f71ea23dfe.dol-sen@gentoo
1 commit: d3f06c64221059f8eec4bec99072b9f71ea23dfe
2 Author: Brian Dolbec <dolsen <AT> gentoo <DOT> org>
3 AuthorDate: Mon Dec 30 21:19:28 2013 +0000
4 Commit: Brian Dolbec <brian.dolbec <AT> gmail <DOT> com>
5 CommitDate: Tue Dec 31 10:00:18 2013 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/catalyst.git;a=commit;h=d3f06c64
7
8 Move some duplicate code to fileops, extend it's capability to not re-make the directory.
9
10 ---
11 catalyst/base/clearbase.py | 55 +++++++++++++---------------------------------
12 catalyst/base/resume.py | 26 +++++-----------------
13 catalyst/fileops.py | 43 +++++++++++++++++++++++++++++++++++-
14 3 files changed, 62 insertions(+), 62 deletions(-)
15
16 diff --git a/catalyst/base/clearbase.py b/catalyst/base/clearbase.py
17 index 50cbc35..e38b1a8 100644
18 --- a/catalyst/base/clearbase.py
19 +++ b/catalyst/base/clearbase.py
20 @@ -5,7 +5,7 @@ from stat import ST_UID, ST_GID, ST_MODE
21
22
23 from catalyst.support import cmd, countdown
24 -from catalyst.fileops import ensure_dirs
25 +from catalyst.fileops import ensure_dirs, clear_dir
26
27 class ClearBase(object):
28 """
29 @@ -16,68 +16,43 @@ class ClearBase(object):
30 self.resume = None
31
32
33 - def clear_autoresume(self):
34 + def clear_autoresume(self, remove=False):
35 """ Clean resume points since they are no longer needed """
36 if "autoresume" in self.settings["options"]:
37 print "Removing AutoResume Points: ..."
38 self.resume.clear_all()
39
40
41 - def clear_chroot(self):
42 + def clear_chroot(self, remove=False):
43 print 'Clearing the chroot path ...'
44 - self.clear_dir(self.settings["chroot_path"], 0755, True)
45 + clear_dir(self.settings["chroot_path"], 0755, True, remove)
46
47
48 - def clear_packages(self):
49 + def clear_packages(self, remove=False):
50 if "pkgcache" in self.settings["options"]:
51 print "purging the pkgcache ..."
52 - self.clear_dir(self.settings["pkgcache_path"])
53 + clear_dir(self.settings["pkgcache_path"], remove=remove)
54
55
56 - def clear_kerncache(self):
57 + def clear_kerncache(self, remove=False):
58 if "kerncache" in self.settings["options"]:
59 print "purging the kerncache ..."
60 - self.clear_dir(self.settings["kerncache_path"])
61 + clear_dir(self.settings["kerncache_path"], remove=remove)
62
63
64 - def purge(self):
65 + def purge(self, remove=False):
66 countdown(10,"Purging Caches ...")
67 - if any(k in self.settings["options"] for k in ("purge","purgeonly","purgetmponly")):
68 + if any(k in self.settings["options"] for k in ("purge",
69 + "purgeonly", "purgetmponly")):
70 print "purge(); clearing autoresume ..."
71 - self.clear_autoresume()
72 + self.clear_autoresume(remove)
73
74 print "purge(); clearing chroot ..."
75 - self.clear_chroot()
76 + self.clear_chroot(remove)
77
78 if "purgetmponly" not in self.settings["options"]:
79 print "purge(); clearing package cache ..."
80 - self.clear_packages()
81 + self.clear_packages(remove)
82
83 print "purge(); clearing kerncache ..."
84 - self.clear_kerncache()
85 -
86 -
87 - def clear_dir(self, myemp, mode=0755, chg_flags=False):
88 - '''Universal directory clearing function
89 - '''
90 - if not myemp:
91 - return False
92 - if os.path.isdir(myemp):
93 - print "Emptying directory" , myemp
94 - """
95 - stat the dir, delete the dir, recreate the dir and set
96 - the proper perms and ownership
97 - """
98 - try:
99 - mystat=os.stat(myemp)
100 - """ There's no easy way to change flags recursively in python """
101 - if chg_flags and os.uname()[0] == "FreeBSD":
102 - os.system("chflags -R noschg " + myemp)
103 - shutil.rmtree(myemp)
104 - ensure_dirs(myemp, mode=mode)
105 - os.chown(myemp,mystat[ST_UID],mystat[ST_GID])
106 - os.chmod(myemp,mystat[ST_MODE])
107 - except Exception as e:
108 - print CatalystError("clear_dir(); Exeption: %s" % str(e))
109 - return False
110 - return True
111 + self.clear_kerncache(remove)
112
113 diff --git a/catalyst/base/resume.py b/catalyst/base/resume.py
114 index e42c7dc..cf495fc 100644
115 --- a/catalyst/base/resume.py
116 +++ b/catalyst/base/resume.py
117 @@ -16,7 +16,7 @@ import shutil
118 from stat import ST_UID, ST_GID, ST_MODE
119 import traceback
120
121 -from catalyst.fileops import ensure_dirs, pjoin, listdir_files
122 +from catalyst.fileops import ensure_dirs, pjoin, listdir_files, clear_dir
123 from catalyst.support import touch
124
125
126 @@ -139,28 +139,12 @@ class AutoResume(object):
127 return list(self._points)
128
129
130 - def clear_all(self):
131 + def clear_all(self, remove=False):
132 '''Clear all active resume points
133
134 @return boolean
135 '''
136 - try:
137 - print "Emptying directory---", self.basedir
138 - """
139 - stat the dir, delete the dir, recreate the dir and set
140 - the proper perms and ownership
141 - """
142 - mystat=os.stat(self.basedir)
143 - if os.uname()[0] == "FreeBSD":
144 - cmd("chflags -R noschg " + self.basedir,\
145 - "Could not remove immutable flag for file "\
146 - + self.basedir)
147 - shutil.rmtree(self.basedir)
148 - ensure_dirs(self.basedir, 0755)
149 - os.chown(self.basedir,mystat[ST_UID],mystat[ST_GID])
150 - os.chmod(self.basedir,mystat[ST_MODE])
151 + if clear_dir(self.basedir, mode=0755, chg_flags=True, remove=remove):
152 self._points = {}
153 - except Exception as e:
154 - print AutoResumeError(str(e))
155 - return False
156 - return True
157 + return True
158 + return False
159
160 diff --git a/catalyst/fileops.py b/catalyst/fileops.py
161 index e3a4ead..245c83e 100644
162 --- a/catalyst/fileops.py
163 +++ b/catalyst/fileops.py
164 @@ -10,7 +10,13 @@ ensuring directories exist,... imports snakeoils osutils
165 functions for use throughout catalyst.
166 '''
167
168 -from snakeoil.osutils import (ensure_dirs as snakeoil_ensure_dirs, normpath,
169 +import os
170 +import shutil
171 +from stat import ST_UID, ST_GID, ST_MODE
172 +
173 +# NOTE: pjoin and listdir_files are imported here for export
174 +# to other catalyst modules
175 +from snakeoil.osutils import (ensure_dirs as snakeoil_ensure_dirs,
176 pjoin, listdir_files)
177 from catalyst.support import CatalystError
178
179 @@ -42,3 +48,38 @@ def ensure_dirs(path, gid=-1, uid=-1, mode=0777, minimal=True,
180 raise CatalystError(
181 "Failed to create directory: %s" % path, print_traceback=True)
182 return succeeded
183 +
184 +
185 +def clear_dir(target, mode=0755, chg_flags=False, remove=False):
186 + '''Universal directory clearing function
187 + '''
188 + #print "fileops.clear_dir()"
189 + if not target:
190 + #print "fileops.clear_dir(), no target... returning"
191 + return False
192 + if os.path.isdir(target):
193 + print "Emptying directory" , target
194 + """
195 + stat the dir, delete the dir, recreate the dir and set
196 + the proper perms and ownership
197 + """
198 + try:
199 + #print "fileops.clear_dir(), os.stat()"
200 + mystat=os.stat(target)
201 + """ There's no easy way to change flags recursively in python """
202 + if chg_flags and os.uname()[0] == "FreeBSD":
203 + os.system("chflags -R noschg " + target)
204 + #print "fileops.clear_dir(), shutil.rmtree()"
205 + shutil.rmtree(target)
206 + if not remove:
207 + #print "fileops.clear_dir(), ensure_dirs()"
208 + ensure_dirs(target, mode=mode)
209 + os.chown(target, mystat[ST_UID], mystat[ST_GID])
210 + os.chmod(target, mystat[ST_MODE])
211 + except Exception as e:
212 + print CatalystError("clear_dir(); Exeption: %s" % str(e))
213 + return False
214 + else:
215 + print "fileops.clear_dir(), %s is not a directory" % (target)
216 + #print "fileops.clear_dir(), DONE, returning True"
217 + return True