1 |
commit: f98b415f062b624bde02d48768372c9848abe175 |
2 |
Author: Brian Dolbec <dolsen <AT> gentoo <DOT> org> |
3 |
AuthorDate: Fri May 31 16:11:30 2013 +0000 |
4 |
Commit: Brian Dolbec <dolsen <AT> gentoo <DOT> org> |
5 |
CommitDate: Thu Jan 1 05:58:07 2015 +0000 |
6 |
URL: http://sources.gentoo.org/gitweb/?p=proj/catalyst.git;a=commit;h=f98b415f |
7 |
|
8 |
Initial creation of fileops.py |
9 |
|
10 |
Migrate to using ensure_dirs() |
11 |
|
12 |
--- |
13 |
catalyst/base/clearbase.py | 10 ++++----- |
14 |
catalyst/base/stagebase.py | 32 +++++++++++----------------- |
15 |
catalyst/fileops.py | 45 +++++++++++++++++++++++++++++++++++++++ |
16 |
catalyst/targets/grp.py | 5 ++--- |
17 |
catalyst/targets/livecd_stage1.py | 4 ++-- |
18 |
catalyst/targets/livecd_stage2.py | 13 ++++------- |
19 |
catalyst/targets/netboot2.py | 6 +++--- |
20 |
catalyst/targets/snapshot.py | 10 ++++----- |
21 |
catalyst/targets/stage1.py | 10 ++++----- |
22 |
9 files changed, 81 insertions(+), 54 deletions(-) |
23 |
|
24 |
diff --git a/catalyst/base/clearbase.py b/catalyst/base/clearbase.py |
25 |
index 8519acc..585d4f2 100644 |
26 |
--- a/catalyst/base/clearbase.py |
27 |
+++ b/catalyst/base/clearbase.py |
28 |
@@ -5,7 +5,7 @@ from stat import ST_UID, ST_GID, ST_MODE |
29 |
|
30 |
|
31 |
from catalyst.support import cmd, countdown |
32 |
- |
33 |
+from catalyst.fileops import ensure_dirs |
34 |
|
35 |
class ClearBase(object): |
36 |
""" |
37 |
@@ -35,7 +35,7 @@ class ClearBase(object): |
38 |
+myemp) |
39 |
#cmd("rm -rf "+myemp, "Could not remove existing file: "+myemp,env-self.env) |
40 |
shutil.rmtree(myemp) |
41 |
- os.makedirs(myemp,0755) |
42 |
+ ensure_dirs(myemp, 0755) |
43 |
os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) |
44 |
os.chmod(myemp,mystat[ST_MODE]) |
45 |
|
46 |
@@ -54,7 +54,7 @@ class ClearBase(object): |
47 |
if os.uname()[0] == "FreeBSD": |
48 |
os.system("chflags -R noschg "+myemp) |
49 |
shutil.rmtree(myemp) |
50 |
- os.makedirs(myemp,0755) |
51 |
+ ensure_dirs(myemp, mode=0755) |
52 |
os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) |
53 |
os.chmod(myemp,mystat[ST_MODE]) |
54 |
|
55 |
@@ -73,7 +73,7 @@ class ClearBase(object): |
56 |
mystat=os.stat(myemp) |
57 |
#cmd("rm -rf "+myemp, "Could not remove existing file: "+myemp,env=self.env) |
58 |
shutil.rmtree(myemp) |
59 |
- os.makedirs(myemp,0755) |
60 |
+ ensure_dirs(myemp, mode=0755) |
61 |
os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) |
62 |
os.chmod(myemp,mystat[ST_MODE]) |
63 |
|
64 |
@@ -92,7 +92,7 @@ class ClearBase(object): |
65 |
mystat=os.stat(myemp) |
66 |
#cmd("rm -rf "+myemp, "Could not remove existing file: "+myemp,env=self.env) |
67 |
shutil.rmtree(myemp) |
68 |
- os.makedirs(myemp,0755) |
69 |
+ ensure_dirs(myemp, mode=0755) |
70 |
os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) |
71 |
os.chmod(myemp,mystat[ST_MODE]) |
72 |
|
73 |
|
74 |
diff --git a/catalyst/base/stagebase.py b/catalyst/base/stagebase.py |
75 |
index 3056ce2..40c7362 100644 |
76 |
--- a/catalyst/base/stagebase.py |
77 |
+++ b/catalyst/base/stagebase.py |
78 |
@@ -18,6 +18,7 @@ from catalyst.base.targetbase import TargetBase |
79 |
from catalyst.base.clearbase import ClearBase |
80 |
from catalyst.base.genbase import GenBase |
81 |
from catalyst.lock import LockDir |
82 |
+from catalyst.fileops import ensure_dirs, pjoin |
83 |
|
84 |
|
85 |
class StageBase(TargetBase, ClearBase, GenBase): |
86 |
@@ -339,8 +340,7 @@ class StageBase(TargetBase, ClearBase, GenBase): |
87 |
# +self.settings["target_path"],env=self.env) |
88 |
touch(setup_target_path_resume) |
89 |
|
90 |
- if not os.path.exists(self.settings["storedir"]+"/builds/"): |
91 |
- os.makedirs(self.settings["storedir"]+"/builds/") |
92 |
+ ensure_dirs(self.settings["storedir"] + "/builds/") |
93 |
|
94 |
def set_fsscript(self): |
95 |
if self.settings["spec_prefix"]+"/fsscript" in self.settings: |
96 |
@@ -486,8 +486,7 @@ class StageBase(TargetBase, ClearBase, GenBase): |
97 |
)) |
98 |
if "autoresume" in self.settings["options"]: |
99 |
print "The autoresume path is " + self.settings["autoresume_path"] |
100 |
- if not os.path.exists(self.settings["autoresume_path"]): |
101 |
- os.makedirs(self.settings["autoresume_path"],0755) |
102 |
+ ensure_dirs(self.settings["autoresume_path"], mode=0755) |
103 |
|
104 |
def set_controller_file(self): |
105 |
self.settings["controller_file"]=normpath(self.settings["sharedir"]+\ |
106 |
@@ -769,19 +768,15 @@ class StageBase(TargetBase, ClearBase, GenBase): |
107 |
self.clear_autoresume() |
108 |
self.clear_chroot() |
109 |
|
110 |
- if not os.path.exists(self.settings["chroot_path"]): |
111 |
- os.makedirs(self.settings["chroot_path"]) |
112 |
+ ensure_dirs(self.settings["chroot_path"]) |
113 |
|
114 |
- if not os.path.exists(self.settings["chroot_path"]+"/tmp"): |
115 |
- os.makedirs(self.settings["chroot_path"]+"/tmp",1777) |
116 |
+ ensure_dirs(self.settings["chroot_path"]+"/tmp",mode=1777) |
117 |
|
118 |
if "pkgcache" in self.settings["options"]: |
119 |
- if not os.path.exists(self.settings["pkgcache_path"]): |
120 |
- os.makedirs(self.settings["pkgcache_path"],0755) |
121 |
+ ensure_dirs(self.settings["pkgcache_path"],mode=0755) |
122 |
|
123 |
if "kerncache" in self.settings["options"]: |
124 |
- if not os.path.exists(self.settings["kerncache_path"]): |
125 |
- os.makedirs(self.settings["kerncache_path"],0755) |
126 |
+ ensure_dirs(self.settings["kerncache_path"],mode=0755) |
127 |
|
128 |
print display_msg |
129 |
cmd(unpack_cmd,error_msg,env=self.env) |
130 |
@@ -849,8 +844,7 @@ class StageBase(TargetBase, ClearBase, GenBase): |
131 |
print cleanup_msg |
132 |
cleanup_cmd="rm -rf "+destdir |
133 |
cmd(cleanup_cmd,cleanup_errmsg,env=self.env) |
134 |
- if not os.path.exists(destdir): |
135 |
- os.makedirs(destdir,0755) |
136 |
+ ensure_dirs(destdir,mode=0755) |
137 |
|
138 |
print "Unpacking portage tree (This can take a long time) ..." |
139 |
cmd(unpack_cmd,unpack_errmsg,env=self.env) |
140 |
@@ -935,12 +929,11 @@ class StageBase(TargetBase, ClearBase, GenBase): |
141 |
for x in self.mounts: |
142 |
#print "bind(); x =", x |
143 |
target = normpath(self.settings["chroot_path"] + self.target_mounts[x]) |
144 |
- if not os.path.exists(target): |
145 |
- os.makedirs(target, 0755) |
146 |
+ ensure_dirs(target, mode=0755) |
147 |
|
148 |
if not os.path.exists(self.mountmap[x]): |
149 |
if self.mountmap[x] not in ["tmpfs", "shmfs"]: |
150 |
- os.makedirs(self.mountmap[x], 0755) |
151 |
+ ensure_dirs(self.mountmap[x], mode=0755) |
152 |
|
153 |
src=self.mountmap[x] |
154 |
#print "bind(); src =", src |
155 |
@@ -1210,7 +1203,7 @@ class StageBase(TargetBase, ClearBase, GenBase): |
156 |
""" |
157 |
mystat=os.stat(myemp) |
158 |
shutil.rmtree(myemp) |
159 |
- os.makedirs(myemp,0755) |
160 |
+ ensure_dirs(myemp, mode=0755) |
161 |
os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) |
162 |
os.chmod(myemp,mystat[ST_MODE]) |
163 |
touch(empty_resume) |
164 |
@@ -1266,8 +1259,7 @@ class StageBase(TargetBase, ClearBase, GenBase): |
165 |
mypath=string.join(mypath[:-1],"/") |
166 |
|
167 |
""" Now make sure path exists """ |
168 |
- if not os.path.exists(mypath): |
169 |
- os.makedirs(mypath) |
170 |
+ ensure_dirs(mypath) |
171 |
|
172 |
print "Creating stage tarball..." |
173 |
|
174 |
|
175 |
diff --git a/catalyst/fileops.py b/catalyst/fileops.py |
176 |
new file mode 100644 |
177 |
index 0000000..009bf1c |
178 |
--- /dev/null |
179 |
+++ b/catalyst/fileops.py |
180 |
@@ -0,0 +1,45 @@ |
181 |
+#!/usr/bin/python |
182 |
+ |
183 |
+# Maintained in full by: |
184 |
+# Catalyst Team <catalyst@g.o> |
185 |
+# Release Engineering Team <releng@g.o> |
186 |
+ |
187 |
+'''fileops.py |
188 |
+ |
189 |
+Performs file operations such as pack/unpack, |
190 |
+ensuring directories exist,... imports snakeoils osutils |
191 |
+functions for use throughout catalyst. |
192 |
+''' |
193 |
+ |
194 |
+from snakeoil.osutils import (ensure_dirs as snakeoil_ensure_dirs, normpath, |
195 |
+ pjoin, listdir_files) |
196 |
+from catalyst.support import CatalystError |
197 |
+ |
198 |
+ |
199 |
+def ensure_dirs(path, gid=-1, uid=-1, mode=0777, minimal=True, |
200 |
+ failback=None, fatal=False): |
201 |
+ '''Wrapper to snakeoil.osutil's ensure_dirs() |
202 |
+ This additionally allows for failures to run |
203 |
+ cleanup or other code and/or raise fatal errors. |
204 |
+ |
205 |
+ :param path: directory to ensure exists on disk |
206 |
+ :param gid: a valid GID to set any created directories to |
207 |
+ :param uid: a valid UID to set any created directories to |
208 |
+ :param mode: permissions to set any created directories to |
209 |
+ :param minimal: boolean controlling whether or not the specified mode |
210 |
+ must be enforced, or is the minimal permissions necessary. For example, |
211 |
+ if mode=0755, minimal=True, and a directory exists with mode 0707, |
212 |
+ this will restore the missing group perms resulting in 757. |
213 |
+ :param failback: function to run in the event of a failed attemp |
214 |
+ to create the directory. |
215 |
+ :return: True if the directory could be created/ensured to have those |
216 |
+ permissions, False if not. |
217 |
+ ''' |
218 |
+ succeeded = snakeoil_ensure_dirs(path, gid=-1, uid=-1, mode=0777, minimal=True) |
219 |
+ if not succeeded: |
220 |
+ if failback: |
221 |
+ failback() |
222 |
+ if fatal: |
223 |
+ raise CatalystError( |
224 |
+ "Failed to create directory: %s" % path, print_traceback=True) |
225 |
+ return succeeded |
226 |
|
227 |
diff --git a/catalyst/targets/grp.py b/catalyst/targets/grp.py |
228 |
index 93d30fc..a17f9bd 100644 |
229 |
--- a/catalyst/targets/grp.py |
230 |
+++ b/catalyst/targets/grp.py |
231 |
@@ -10,7 +10,7 @@ import glob |
232 |
|
233 |
from catalyst.support import (CatalystError, normpath, |
234 |
touch, cmd, list_bashify) |
235 |
- |
236 |
+from catalyst.fileops import ensure_dirs |
237 |
from catalyst.base.stagebase import StageBase |
238 |
|
239 |
|
240 |
@@ -51,8 +51,7 @@ class grp(StageBase): |
241 |
#if os.path.isdir(self.settings["target_path"]): |
242 |
#cmd("rm -rf "+self.settings["target_path"], |
243 |
#"Could not remove existing directory: "+self.settings["target_path"],env=self.env) |
244 |
- if not os.path.exists(self.settings["target_path"]): |
245 |
- os.makedirs(self.settings["target_path"]) |
246 |
+ ensure_dirs(self.settings["target_path"]) |
247 |
|
248 |
touch(self.settings["autoresume_path"]+"setup_target_path") |
249 |
|
250 |
|
251 |
diff --git a/catalyst/targets/livecd_stage1.py b/catalyst/targets/livecd_stage1.py |
252 |
index 3b19c3f..668960b 100644 |
253 |
--- a/catalyst/targets/livecd_stage1.py |
254 |
+++ b/catalyst/targets/livecd_stage1.py |
255 |
@@ -10,6 +10,7 @@ import string |
256 |
|
257 |
from catalyst.support import (normpath, |
258 |
touch, cmd) |
259 |
+from catalyst.fileops import ensure_dirs |
260 |
|
261 |
from catalyst.base.stagebase import StageBase |
262 |
|
263 |
@@ -43,8 +44,7 @@ class livecd_stage1(StageBase): |
264 |
"Could not remove existing directory: "+self.settings["target_path"],env=self.env) |
265 |
touch(self.settings["autoresume_path"]+"setup_target_path") |
266 |
|
267 |
- if not os.path.exists(self.settings["target_path"]): |
268 |
- os.makedirs(self.settings["target_path"]) |
269 |
+ ensure_dirs(self.settings["target_path"]) |
270 |
|
271 |
def set_spec_prefix(self): |
272 |
self.settings["spec_prefix"]="livecd" |
273 |
|
274 |
diff --git a/catalyst/targets/livecd_stage2.py b/catalyst/targets/livecd_stage2.py |
275 |
index b1cf6b2..94da670 100644 |
276 |
--- a/catalyst/targets/livecd_stage2.py |
277 |
+++ b/catalyst/targets/livecd_stage2.py |
278 |
@@ -7,6 +7,7 @@ import os |
279 |
|
280 |
from catalyst.support import (normpath, file_locate, CatalystError, cmd, |
281 |
read_from_clst, touch) |
282 |
+from catalyst.fileops import ensure_dirs |
283 |
from catalyst.base.stagebase import StageBase |
284 |
|
285 |
|
286 |
@@ -62,8 +63,7 @@ class livecd_stage2(StageBase): |
287 |
cmd("rm -rf "+self.settings["target_path"], |
288 |
"Could not remove existing directory: "+self.settings["target_path"],env=self.env) |
289 |
touch(self.settings["autoresume_path"]+"setup_target_path") |
290 |
- if not os.path.exists(self.settings["target_path"]): |
291 |
- os.makedirs(self.settings["target_path"]) |
292 |
+ ensure_dirs(self.settings["target_path"]) |
293 |
|
294 |
def run_local(self): |
295 |
# what modules do we want to blacklist? |
296 |
@@ -116,15 +116,10 @@ class livecd_stage2(StageBase): |
297 |
self.clear_chroot() |
298 |
#self.dir_setup() |
299 |
|
300 |
- if not os.path.exists(self.settings["chroot_path"]): |
301 |
- os.makedirs(self.settings["chroot_path"]) |
302 |
- |
303 |
- if not os.path.exists(self.settings["chroot_path"]+"/tmp"): |
304 |
- os.makedirs(self.settings["chroot_path"]+"/tmp",1777) |
305 |
+ ensure_dirs(self.settings["chroot_path"]+"/tmp", mode=1777) |
306 |
|
307 |
if "pkgcache" in self.settings["options"]: |
308 |
- if not os.path.exists(self.settings["pkgcache_path"]): |
309 |
- os.makedirs(self.settings["pkgcache_path"],0755) |
310 |
+ ensure_dirs(self.settings["pkgcache_path"], mode=0755) |
311 |
|
312 |
if not display_msg: |
313 |
raise CatalystError("Could not find appropriate source.\n" |
314 |
|
315 |
diff --git a/catalyst/targets/netboot2.py b/catalyst/targets/netboot2.py |
316 |
index e9e2625..7edfc1f 100644 |
317 |
--- a/catalyst/targets/netboot2.py |
318 |
+++ b/catalyst/targets/netboot2.py |
319 |
@@ -10,6 +10,7 @@ from stat import ST_UID, ST_GID, ST_MODE |
320 |
|
321 |
from catalyst.support import (CatalystError, normpath, |
322 |
touch, cmd, list_bashify) |
323 |
+from catalyst.fileops import ensure_dirs |
324 |
|
325 |
from catalyst.base.stagebase import StageBase |
326 |
|
327 |
@@ -62,8 +63,7 @@ class netboot2(StageBase): |
328 |
"Could not remove existing file: "+self.settings["target_path"],env=self.env) |
329 |
touch(self.settings["autoresume_path"]+"setup_target_path") |
330 |
|
331 |
- if not os.path.exists(self.settings["storedir"]+"/builds/"): |
332 |
- os.makedirs(self.settings["storedir"]+"/builds/") |
333 |
+ ensure_dirs(self.settings["storedir"]+"/builds/") |
334 |
|
335 |
def copy_files_to_image(self): |
336 |
# copies specific files from the buildroot to merge_path |
337 |
@@ -158,7 +158,7 @@ class netboot2(StageBase): |
338 |
# the proper perms and ownership |
339 |
mystat=os.stat(myemp) |
340 |
shutil.rmtree(myemp) |
341 |
- os.makedirs(myemp,0755) |
342 |
+ ensure_dirs(myemp, mode=0755) |
343 |
os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) |
344 |
os.chmod(myemp,mystat[ST_MODE]) |
345 |
touch(self.settings["autoresume_path"]+"empty") |
346 |
|
347 |
diff --git a/catalyst/targets/snapshot.py b/catalyst/targets/snapshot.py |
348 |
index 6c2396e..1887110 100644 |
349 |
--- a/catalyst/targets/snapshot.py |
350 |
+++ b/catalyst/targets/snapshot.py |
351 |
@@ -10,7 +10,7 @@ from stat import ST_UID, ST_GID, ST_MODE |
352 |
from catalyst.support import normpath, cmd |
353 |
from catalyst.base.targetbase import TargetBase |
354 |
from catalyst.base.genbase import GenBase |
355 |
- |
356 |
+from catalyst.fileops import ensure_dirs |
357 |
class snapshot(TargetBase, GenBase): |
358 |
""" |
359 |
Builder class for snapshots. |
360 |
@@ -31,8 +31,7 @@ class snapshot(TargetBase, GenBase): |
361 |
|
362 |
def setup(self): |
363 |
x=normpath(self.settings["storedir"]+"/snapshots") |
364 |
- if not os.path.exists(x): |
365 |
- os.makedirs(x) |
366 |
+ ensure_dirs(x) |
367 |
|
368 |
def mount_safety_check(self): |
369 |
pass |
370 |
@@ -50,8 +49,7 @@ class snapshot(TargetBase, GenBase): |
371 |
" from "+self.settings["portdir"]+"..." |
372 |
|
373 |
mytmp=self.settings["tmp_path"] |
374 |
- if not os.path.exists(mytmp): |
375 |
- os.makedirs(mytmp) |
376 |
+ ensure_dirs(mytmp) |
377 |
|
378 |
cmd("rsync -a --delete --exclude /packages/ --exclude /distfiles/ " + |
379 |
"--exclude /local/ --exclude CVS/ --exclude .svn --filter=H_**/files/digest-* " + |
380 |
@@ -88,6 +86,6 @@ class snapshot(TargetBase, GenBase): |
381 |
if os.uname()[0] == "FreeBSD": |
382 |
os.system("chflags -R noschg "+myemp) |
383 |
shutil.rmtree(myemp) |
384 |
- os.makedirs(myemp,0755) |
385 |
+ ensure_dirs(myemp, mode=0755) |
386 |
os.chown(myemp,mystat[ST_UID],mystat[ST_GID]) |
387 |
os.chmod(myemp,mystat[ST_MODE]) |
388 |
|
389 |
diff --git a/catalyst/targets/stage1.py b/catalyst/targets/stage1.py |
390 |
index e72cbcc..3d3b9e9 100644 |
391 |
--- a/catalyst/targets/stage1.py |
392 |
+++ b/catalyst/targets/stage1.py |
393 |
@@ -6,6 +6,7 @@ stage1 target |
394 |
import os |
395 |
|
396 |
from catalyst.support import normpath, list_to_string |
397 |
+from catalyst.fileops import ensure_dirs |
398 |
from catalyst.base.stagebase import StageBase |
399 |
|
400 |
|
401 |
@@ -64,10 +65,8 @@ class stage1(StageBase): |
402 |
# baselayout no longer creates the .keep files in proc and dev for FreeBSD as it |
403 |
# would create them too late...we need them earlier before bind mounting filesystems |
404 |
# since proc and dev are not writeable, so...create them here |
405 |
- if not os.path.exists(self.settings["stage_path"]+"/proc"): |
406 |
- os.makedirs(self.settings["stage_path"]+"/proc") |
407 |
- if not os.path.exists(self.settings["stage_path"]+"/dev"): |
408 |
- os.makedirs(self.settings["stage_path"]+"/dev") |
409 |
+ ensure_dirs(self.settings["stage_path"]+"/proc") |
410 |
+ ensure_dirs(self.settings["stage_path"]+"/dev") |
411 |
if not os.path.isfile(self.settings["stage_path"]+"/proc/.keep"): |
412 |
try: |
413 |
proc_keepfile = open(self.settings["stage_path"]+"/proc/.keep","w") |
414 |
@@ -87,8 +86,7 @@ class stage1(StageBase): |
415 |
|
416 |
def set_mounts(self): |
417 |
# stage_path/proc probably doesn't exist yet, so create it |
418 |
- if not os.path.exists(self.settings["stage_path"]+"/proc"): |
419 |
- os.makedirs(self.settings["stage_path"]+"/proc") |
420 |
+ ensure_dirs(self.settings["stage_path"]+"/proc") |
421 |
|
422 |
# alter the mount mappings to bind mount proc onto it |
423 |
self.mounts.append("stage1root/proc") |