Revision 76e5f8b5
b/lib/utils.py | ||
---|---|---|
365 | 365 |
# as efficient. |
366 | 366 |
if mkdir and err.errno == errno.ENOENT: |
367 | 367 |
# Create directory and try again |
368 |
dirname = os.path.dirname(new) |
|
369 |
try: |
|
370 |
os.makedirs(dirname, mode=mkdir_mode) |
|
371 |
except OSError, err: |
|
372 |
# Ignore EEXIST. This is only handled in os.makedirs as included in |
|
373 |
# Python 2.5 and above. |
|
374 |
if err.errno != errno.EEXIST or not os.path.exists(dirname): |
|
375 |
raise |
|
368 |
Makedirs(os.path.dirname(new)) |
|
376 | 369 |
|
377 | 370 |
return os.rename(old, new) |
378 | 371 |
|
379 | 372 |
raise |
380 | 373 |
|
381 | 374 |
|
375 |
def Makedirs(path, mode=0750): |
|
376 |
"""Super-mkdir; create a leaf directory and all intermediate ones. |
|
377 |
|
|
378 |
This is a wrapper around C{os.makedirs} adding error handling not implemented |
|
379 |
before Python 2.5. |
|
380 |
|
|
381 |
""" |
|
382 |
try: |
|
383 |
os.makedirs(path, mode) |
|
384 |
except OSError, err: |
|
385 |
# Ignore EEXIST. This is only handled in os.makedirs as included in |
|
386 |
# Python 2.5 and above. |
|
387 |
if err.errno != errno.EEXIST or not os.path.exists(path): |
|
388 |
raise |
|
389 |
|
|
390 |
|
|
382 | 391 |
def ResetTempfileModule(): |
383 | 392 |
"""Resets the random name generator of the tempfile module. |
384 | 393 |
|
b/test/ganeti.utils_unittest.py | ||
---|---|---|
1504 | 1504 |
self.assertEqual(validity, (None, None)) |
1505 | 1505 |
|
1506 | 1506 |
|
1507 |
class TestMakedirs(unittest.TestCase): |
|
1508 |
def setUp(self): |
|
1509 |
self.tmpdir = tempfile.mkdtemp() |
|
1510 |
|
|
1511 |
def tearDown(self): |
|
1512 |
shutil.rmtree(self.tmpdir) |
|
1513 |
|
|
1514 |
def testNonExisting(self): |
|
1515 |
path = utils.PathJoin(self.tmpdir, "foo") |
|
1516 |
utils.Makedirs(path) |
|
1517 |
self.assert_(os.path.isdir(path)) |
|
1518 |
|
|
1519 |
def testExisting(self): |
|
1520 |
path = utils.PathJoin(self.tmpdir, "foo") |
|
1521 |
os.mkdir(path) |
|
1522 |
utils.Makedirs(path) |
|
1523 |
self.assert_(os.path.isdir(path)) |
|
1524 |
|
|
1525 |
def testRecursiveNonExisting(self): |
|
1526 |
path = utils.PathJoin(self.tmpdir, "foo/bar/baz") |
|
1527 |
utils.Makedirs(path) |
|
1528 |
self.assert_(os.path.isdir(path)) |
|
1529 |
|
|
1530 |
def testRecursiveExisting(self): |
|
1531 |
path = utils.PathJoin(self.tmpdir, "B/moo/xyz") |
|
1532 |
self.assert_(not os.path.exists(path)) |
|
1533 |
os.mkdir(utils.PathJoin(self.tmpdir, "B")) |
|
1534 |
utils.Makedirs(path) |
|
1535 |
self.assert_(os.path.isdir(path)) |
|
1536 |
|
|
1537 |
|
|
1507 | 1538 |
if __name__ == '__main__': |
1508 | 1539 |
testutils.GanetiTestProgram() |
Also available in: Unified diff