Merge lp:~florian-rathgeber/instant/paths-module-fixes into lp:instant

Proposed by Florian Rathgeber
Status: Merged
Merged at revision: 332
Proposed branch: lp:~florian-rathgeber/instant/paths-module-fixes
Merge into: lp:instant
Diff against target: 116 lines (+29/-32)
2 files modified
instant/build.py (+0/-10)
instant/paths.py (+29/-22)
To merge this branch: bzr merge lp:~florian-rathgeber/instant/paths-module-fixes
Reviewer Review Type Date Requested Status
Registry Administrators Pending
Review via email: mp+98065@code.launchpad.net

Description of the change

A few small fixes for the paths utility module:

* Fix docstrings
* Add helper function for checking and creating directories that don't yet exist
* Call instant_error when directory creation fails

To post a comment you must log in.
Revision history for this message
Johan Hake (johan-hake) wrote :

Thanks!

The if test for generating the cache dir might fail for clusters, for
jobs submitted to several nodes. For this purpose there is a function in
build.py called makedirs, which fails alright, but silently so.

If you do not mind can you use this function for the purpose of this
patch? It is also more logical to move makedirs to the paths.py module.
If you could fix that too it would be excellent!

Johan

On 03/17/2012 08:10 PM, Florian Rathgeber wrote:
> Florian Rathgeber has proposed merging
> lp:~florian-rathgeber/instant/paths-module-fixes into lp:instant.
>
> Requested reviews: Instant Core Team (instant-core)
>
> For more details, see:
> https://code.launchpad.net/~florian-rathgeber/instant/paths-module-fixes/+merge/98065
>
> A few small fixes for the paths utility module:
>
> * Fix docstrings * Add helper function for checking and creating
> directories that don't yet exist * Call instant_error when directory
> creation fails

335. By Florian Rathgeber

Move makedirs helper function to paths module and use that instead of _check_or_create

336. By Florian Rathgeber

Use debug log level in paths module test

337. By Florian Rathgeber

Ignore empty string when reading cache/error dir from environment

Revision history for this message
Florian Rathgeber (florian-rathgeber) wrote :

I hadn't seen this function was already available, sorry. The changes I just pushed use it in the way you suggest.

Revision history for this message
Johan Hake (johan-hake) wrote :

Well, it was present in the "wrong" module so not your fault. Thanks again for the patch. It is now applied, tested and pushed.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'instant/build.py'
2--- instant/build.py 2011-11-28 23:57:11 +0000
3+++ instant/build.py 2012-03-19 19:59:16 +0000
4@@ -35,16 +35,6 @@
5 x = x.split()
6 return strip_strings(x)
7
8-def makedirs(path):
9- """
10- Creates a directory (tree). If directory already excists it does nothing.
11- """
12- try:
13- os.makedirs(path)
14- except os.error, e:
15- if e.errno != errno.EEXIST:
16- raise
17-
18 def copy_files(source, dest, files):
19 """Copy a list of files from a source directory to a destination directory.
20 This may seem a bit complicated, but a lot of this code is error checking."""
21
22=== modified file 'instant/paths.py'
23--- instant/paths.py 2011-06-18 05:32:06 +0000
24+++ instant/paths.py 2012-03-19 19:59:16 +0000
25@@ -3,6 +3,7 @@
26 # Utilities for directory handling:
27
28 import os
29+import errno
30 import shutil
31 import tempfile
32 import time
33@@ -13,7 +14,7 @@
34 """Return a temporary directory for the duration of this process.
35
36 Multiple calls in the same process returns the same directory.
37- Remember to all delete_temp_dir() before exiting."""
38+ Remember to call delete_temp_dir() before exiting."""
39 global _tmp_dir
40 if _tmp_dir is None:
41 datestring = "%d-%d-%d-%02d-%02d" % time.localtime()[:5]
42@@ -30,47 +31,53 @@
43 _tmp_dir = None
44
45 def get_instant_dir():
46- "Return a temporary directory for the duration of this process."
47+ "Return the default instant directory, creating it if necessary."
48 # os.path.expanduser works for Windows, Linux, and Mac
49 # In Windows, $HOME is os.environ['HOMEDRIVE'] + os.environ['HOMEPATH']
50 instant_dir = os.path.join(os.path.expanduser("~"), ".instant")
51- if not os.path.isdir(instant_dir):
52- instant_debug("Creating instant directory '%s'." % instant_dir)
53- os.mkdir(instant_dir)
54+ makedirs(instant_dir)
55 return instant_dir
56
57 def get_default_cache_dir():
58 "Return the default cache directory."
59- if "INSTANT_CACHE_DIR" in os.environ:
60- cache_dir = os.environ["INSTANT_CACHE_DIR"]
61- else:
62+ cache_dir = os.environ.get("INSTANT_CACHE_DIR")
63+ # Catches the cases where INSTANT_CACHE_DIR is not set or ''
64+ if not cache_dir:
65 cache_dir = os.path.join(get_instant_dir(), "cache")
66- if not os.path.isdir(cache_dir):
67- instant_debug("Creating cache directory '%s'." % cache_dir)
68- os.mkdir(cache_dir)
69+ makedirs(cache_dir)
70 return cache_dir
71
72 def get_default_error_dir():
73- "Return the default cache directory."
74- if "INSTANT_ERROR_DIR" in os.environ:
75- cache_dir = os.environ["INSTANT_ERROR_DIR"]
76- else:
77- cache_dir = os.path.join(get_instant_dir(), "error")
78- if not os.path.isdir(cache_dir):
79- instant_debug("Creating cache directory '%s'." % cache_dir)
80- os.mkdir(cache_dir)
81- return cache_dir
82+ "Return the default error directory."
83+ error_dir = os.environ.get("INSTANT_ERROR_DIR")
84+ # Catches the cases where INSTANT_ERROR_DIR is not set or ''
85+ if not error_dir:
86+ error_dir = os.path.join(get_instant_dir(), "error")
87+ makedirs(error_dir)
88+ return error_dir
89
90 def validate_cache_dir(cache_dir):
91 if cache_dir is None:
92 return get_default_cache_dir()
93 instant_assert(isinstance(cache_dir, str), "Expecting cache_dir to be a string.")
94 cache_dir = os.path.abspath(cache_dir)
95- if not os.path.isdir(cache_dir):
96- os.mkdir(cache_dir)
97+ makedirs(cache_dir)
98 return cache_dir
99
100+def makedirs(path):
101+ """
102+ Creates a directory (tree). If directory already excists it does nothing.
103+ """
104+ try:
105+ os.makedirs(path)
106+ instant_debug("In instant.makedirs: Creating directory %r" % path)
107+ except os.error, e:
108+ if e.errno != errno.EEXIST:
109+ raise
110+
111 def _test():
112+ from output import set_logging_level
113+ set_logging_level("DEBUG")
114 print "Temp dir:", get_temp_dir()
115 print "Instant dir:", get_instant_dir()
116 print "Default cache dir:", get_default_cache_dir()

Subscribers

People subscribed via source and target branches