1 |
commit: 2f8312854d9fac7476f8f972660824941dc4c852 |
2 |
Author: Brian Dolbec <dolsen <AT> gentoo <DOT> org> |
3 |
AuthorDate: Mon Dec 30 21:19:28 2013 +0000 |
4 |
Commit: Brian Dolbec <dolsen <AT> gentoo <DOT> org> |
5 |
CommitDate: Thu Feb 26 20:08:06 2015 +0000 |
6 |
URL: http://sources.gentoo.org/gitweb/?p=proj/catalyst.git;a=commit;h=2f831285 |
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 | 71 +++++++++++++++------------------------------- |
12 |
catalyst/base/resume.py | 26 ++++------------- |
13 |
catalyst/fileops.py | 43 +++++++++++++++++++++++++++- |
14 |
3 files changed, 70 insertions(+), 70 deletions(-) |
15 |
|
16 |
diff --git a/catalyst/base/clearbase.py b/catalyst/base/clearbase.py |
17 |
index 0ebe299..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 |
- print "clearing autoresume ..." |
69 |
- self.clear_autoresume() |
70 |
- |
71 |
- print "clearing chroot ..." |
72 |
- self.clear_chroot() |
73 |
- |
74 |
- if "PURGETMPONLY" not in self.settings: |
75 |
- print "clearing package cache ..." |
76 |
- self.clear_packages() |
77 |
- |
78 |
- print "clearing kerncache ..." |
79 |
- self.clear_kerncache() |
80 |
- |
81 |
- |
82 |
- def clear_dir(self, myemp, mode=0755, chg_flags=False): |
83 |
- '''Universal directory clearing function |
84 |
- ''' |
85 |
- if not myemp: |
86 |
- return False |
87 |
- if os.path.isdir(myemp): |
88 |
- print "Emptying directory" , myemp |
89 |
- """ |
90 |
- stat the dir, delete the dir, recreate the dir and set |
91 |
- the proper perms and ownership |
92 |
- """ |
93 |
- try: |
94 |
- mystat=os.stat(myemp) |
95 |
- """ There's no easy way to change flags recursively in python """ |
96 |
- if chg_flags and os.uname()[0] == "FreeBSD": |
97 |
- os.system("chflags -R noschg " + myemp) |
98 |
- shutil.rmtree(myemp) |
99 |
- ensure_dirs(myemp, mode=mode) |
100 |
- os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) |
101 |
- os.chmod(myemp,mystat[ST_MODE]) |
102 |
- except Exception as e: |
103 |
- print CatalystError("clear_dir(); Exeption: %s" % str(e)) |
104 |
- return False |
105 |
- return True |
106 |
+ if any(k in self.settings["options"] for k in ("purge", |
107 |
+ "purgeonly", "purgetmponly")): |
108 |
+ print "purge(); clearing autoresume ..." |
109 |
+ self.clear_autoresume(remove) |
110 |
+ |
111 |
+ print "purge(); clearing chroot ..." |
112 |
+ self.clear_chroot(remove) |
113 |
+ |
114 |
+ if "purgetmponly" not in self.settings["options"]: |
115 |
+ print "purge(); clearing package cache ..." |
116 |
+ self.clear_packages(remove) |
117 |
+ |
118 |
+ print "purge(); clearing kerncache ..." |
119 |
+ self.clear_kerncache(remove) |
120 |
|
121 |
diff --git a/catalyst/base/resume.py b/catalyst/base/resume.py |
122 |
index e42c7dc..cf495fc 100644 |
123 |
--- a/catalyst/base/resume.py |
124 |
+++ b/catalyst/base/resume.py |
125 |
@@ -16,7 +16,7 @@ import shutil |
126 |
from stat import ST_UID, ST_GID, ST_MODE |
127 |
import traceback |
128 |
|
129 |
-from catalyst.fileops import ensure_dirs, pjoin, listdir_files |
130 |
+from catalyst.fileops import ensure_dirs, pjoin, listdir_files, clear_dir |
131 |
from catalyst.support import touch |
132 |
|
133 |
|
134 |
@@ -139,28 +139,12 @@ class AutoResume(object): |
135 |
return list(self._points) |
136 |
|
137 |
|
138 |
- def clear_all(self): |
139 |
+ def clear_all(self, remove=False): |
140 |
'''Clear all active resume points |
141 |
|
142 |
@return boolean |
143 |
''' |
144 |
- try: |
145 |
- print "Emptying directory---", self.basedir |
146 |
- """ |
147 |
- stat the dir, delete the dir, recreate the dir and set |
148 |
- the proper perms and ownership |
149 |
- """ |
150 |
- mystat=os.stat(self.basedir) |
151 |
- if os.uname()[0] == "FreeBSD": |
152 |
- cmd("chflags -R noschg " + self.basedir,\ |
153 |
- "Could not remove immutable flag for file "\ |
154 |
- + self.basedir) |
155 |
- shutil.rmtree(self.basedir) |
156 |
- ensure_dirs(self.basedir, 0755) |
157 |
- os.chown(self.basedir,mystat[ST_UID],mystat[ST_GID]) |
158 |
- os.chmod(self.basedir,mystat[ST_MODE]) |
159 |
+ if clear_dir(self.basedir, mode=0755, chg_flags=True, remove=remove): |
160 |
self._points = {} |
161 |
- except Exception as e: |
162 |
- print AutoResumeError(str(e)) |
163 |
- return False |
164 |
- return True |
165 |
+ return True |
166 |
+ return False |
167 |
|
168 |
diff --git a/catalyst/fileops.py b/catalyst/fileops.py |
169 |
index e3a4ead..245c83e 100644 |
170 |
--- a/catalyst/fileops.py |
171 |
+++ b/catalyst/fileops.py |
172 |
@@ -10,7 +10,13 @@ ensuring directories exist,... imports snakeoils osutils |
173 |
functions for use throughout catalyst. |
174 |
''' |
175 |
|
176 |
-from snakeoil.osutils import (ensure_dirs as snakeoil_ensure_dirs, normpath, |
177 |
+import os |
178 |
+import shutil |
179 |
+from stat import ST_UID, ST_GID, ST_MODE |
180 |
+ |
181 |
+# NOTE: pjoin and listdir_files are imported here for export |
182 |
+# to other catalyst modules |
183 |
+from snakeoil.osutils import (ensure_dirs as snakeoil_ensure_dirs, |
184 |
pjoin, listdir_files) |
185 |
from catalyst.support import CatalystError |
186 |
|
187 |
@@ -42,3 +48,38 @@ def ensure_dirs(path, gid=-1, uid=-1, mode=0777, minimal=True, |
188 |
raise CatalystError( |
189 |
"Failed to create directory: %s" % path, print_traceback=True) |
190 |
return succeeded |
191 |
+ |
192 |
+ |
193 |
+def clear_dir(target, mode=0755, chg_flags=False, remove=False): |
194 |
+ '''Universal directory clearing function |
195 |
+ ''' |
196 |
+ #print "fileops.clear_dir()" |
197 |
+ if not target: |
198 |
+ #print "fileops.clear_dir(), no target... returning" |
199 |
+ return False |
200 |
+ if os.path.isdir(target): |
201 |
+ print "Emptying directory" , target |
202 |
+ """ |
203 |
+ stat the dir, delete the dir, recreate the dir and set |
204 |
+ the proper perms and ownership |
205 |
+ """ |
206 |
+ try: |
207 |
+ #print "fileops.clear_dir(), os.stat()" |
208 |
+ mystat=os.stat(target) |
209 |
+ """ There's no easy way to change flags recursively in python """ |
210 |
+ if chg_flags and os.uname()[0] == "FreeBSD": |
211 |
+ os.system("chflags -R noschg " + target) |
212 |
+ #print "fileops.clear_dir(), shutil.rmtree()" |
213 |
+ shutil.rmtree(target) |
214 |
+ if not remove: |
215 |
+ #print "fileops.clear_dir(), ensure_dirs()" |
216 |
+ ensure_dirs(target, mode=mode) |
217 |
+ os.chown(target, mystat[ST_UID], mystat[ST_GID]) |
218 |
+ os.chmod(target, mystat[ST_MODE]) |
219 |
+ except Exception as e: |
220 |
+ print CatalystError("clear_dir(); Exeption: %s" % str(e)) |
221 |
+ return False |
222 |
+ else: |
223 |
+ print "fileops.clear_dir(), %s is not a directory" % (target) |
224 |
+ #print "fileops.clear_dir(), DONE, returning True" |
225 |
+ return True |