Merge lp:~harlowja/cloud-init/use-utils-file-funcs into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Joshua Harlow
Status: Merged
Merged at revision: 696
Proposed branch: lp:~harlowja/cloud-init/use-utils-file-funcs
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 177 lines (+32/-31)
6 files modified
cloudinit/distros/__init__.py (+1/-2)
cloudinit/sources/DataSourceAltCloud.py (+8/-11)
cloudinit/sources/DataSourceConfigDrive.py (+9/-11)
cloudinit/sources/DataSourceMAAS.py (+2/-4)
cloudinit/sources/DataSourceOVF.py (+2/-3)
cloudinit/util.py (+10/-0)
To merge this branch: bzr merge lp:~harlowja/cloud-init/use-utils-file-funcs
Reviewer Review Type Date Requested Status
cloud-init Commiters Pending
Review via email: mp+125616@code.launchpad.net

Description of the change

Use util funcs for file actions.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'cloudinit/distros/__init__.py'
2--- cloudinit/distros/__init__.py 2012-09-18 17:27:41 +0000
3+++ cloudinit/distros/__init__.py 2012-09-21 00:59:18 +0000
4@@ -343,8 +343,7 @@
5
6 else:
7 try:
8- with open(sudo_file, 'a') as f:
9- f.write(content)
10+ util.append_file(sudo_file, content)
11 except IOError as e:
12 util.logexc(LOG, "Failed to write %s" % sudo_file, e)
13 raise e
14
15=== modified file 'cloudinit/sources/DataSourceAltCloud.py'
16--- cloudinit/sources/DataSourceAltCloud.py 2012-08-10 19:29:42 +0000
17+++ cloudinit/sources/DataSourceAltCloud.py 2012-09-21 00:59:18 +0000
18@@ -73,13 +73,11 @@
19
20 # First try deltacloud_user_data_file. On failure try user_data_file.
21 try:
22- with open(deltacloud_user_data_file, 'r') as user_data_f:
23- user_data = user_data_f.read().strip()
24- except:
25+ user_data = util.load_file(deltacloud_user_data_file).strip()
26+ except IOError:
27 try:
28- with open(user_data_file, 'r') as user_data_f:
29- user_data = user_data_f.read().strip()
30- except:
31+ user_data = util.load_file(user_data_file).strip()
32+ except IOError:
33 util.logexc(LOG, ('Failed accessing user data file.'))
34 return None
35
36@@ -157,11 +155,10 @@
37
38 if os.path.exists(CLOUD_INFO_FILE):
39 try:
40- cloud_info = open(CLOUD_INFO_FILE)
41- cloud_type = cloud_info.read().strip().upper()
42- cloud_info.close()
43- except:
44- util.logexc(LOG, 'Unable to access cloud info file.')
45+ cloud_type = util.load_file(CLOUD_INFO_FILE).strip().upper()
46+ except IOError:
47+ util.logexc(LOG, 'Unable to access cloud info file at %s.',
48+ CLOUD_INFO_FILE)
49 return False
50 else:
51 cloud_type = self.get_cloud_type()
52
53=== modified file 'cloudinit/sources/DataSourceConfigDrive.py'
54--- cloudinit/sources/DataSourceConfigDrive.py 2012-08-24 21:06:18 +0000
55+++ cloudinit/sources/DataSourceConfigDrive.py 2012-09-21 00:59:18 +0000
56@@ -227,19 +227,19 @@
57 found = False
58 if os.path.isfile(fpath):
59 try:
60- with open(fpath) as fp:
61- data = fp.read()
62- except Exception as exc:
63- raise BrokenConfigDriveDir("failed to read: %s" % fpath)
64+ data = util.load_file(fpath)
65+ except IOError:
66+ raise BrokenConfigDriveDir("Failed to read: %s" % fpath)
67 found = True
68 elif required:
69- raise NonConfigDriveDir("missing mandatory %s" % fpath)
70+ raise NonConfigDriveDir("Missing mandatory path: %s" % fpath)
71
72 if found and process:
73 try:
74 data = process(data)
75 except Exception as exc:
76- raise BrokenConfigDriveDir("failed to process: %s" % fpath)
77+ raise BrokenConfigDriveDir(("Failed to process "
78+ "path: %s") % fpath)
79
80 if found:
81 results[name] = data
82@@ -255,8 +255,7 @@
83 # do not use os.path.join here, as content_path starts with /
84 cpath = os.path.sep.join((source_dir, "openstack",
85 "./%s" % item['content_path']))
86- with open(cpath) as fp:
87- return(fp.read())
88+ return util.load_file(cpath)
89
90 files = {}
91 try:
92@@ -270,7 +269,7 @@
93 if item:
94 results['network_config'] = read_content_path(item)
95 except Exception as exc:
96- raise BrokenConfigDriveDir("failed to read file %s: %s" % (item, exc))
97+ raise BrokenConfigDriveDir("Failed to read file %s: %s" % (item, exc))
98
99 # to openstack, user can specify meta ('nova boot --meta=key=value') and
100 # those will appear under metadata['meta'].
101@@ -385,8 +384,7 @@
102 # hasn't declared itself found.
103 fname = os.path.join(paths.get_cpath('data'), 'instance-id')
104 try:
105- with open(fname) as fp:
106- return fp.read()
107+ return util.load_file(fname)
108 except IOError:
109 return None
110
111
112=== modified file 'cloudinit/sources/DataSourceMAAS.py'
113--- cloudinit/sources/DataSourceMAAS.py 2012-08-06 17:26:21 +0000
114+++ cloudinit/sources/DataSourceMAAS.py 2012-09-21 00:59:18 +0000
115@@ -301,9 +301,7 @@
116 'token_secret': args.tsec, 'consumer_secret': args.csec}
117
118 if args.config:
119- import yaml
120- with open(args.config) as fp:
121- cfg = yaml.safe_load(fp)
122+ cfg = util.read_conf(args.config)
123 if 'datasource' in cfg:
124 cfg = cfg['datasource']['MAAS']
125 for key in creds.keys():
126@@ -312,7 +310,7 @@
127
128 def geturl(url, headers_cb):
129 req = urllib2.Request(url, data=None, headers=headers_cb(url))
130- return(urllib2.urlopen(req).read())
131+ return (urllib2.urlopen(req).read())
132
133 def printurl(url, headers_cb):
134 print "== %s ==\n%s\n" % (url, geturl(url, headers_cb))
135
136=== modified file 'cloudinit/sources/DataSourceOVF.py'
137--- cloudinit/sources/DataSourceOVF.py 2012-07-12 19:20:24 +0000
138+++ cloudinit/sources/DataSourceOVF.py 2012-09-21 00:59:18 +0000
139@@ -204,9 +204,8 @@
140
141 try:
142 # See if we can read anything at all...??
143- with open(fullp, 'rb') as fp:
144- fp.read(512)
145- except:
146+ util.peek_file(fullp, 512)
147+ except IOError:
148 continue
149
150 try:
151
152=== modified file 'cloudinit/util.py'
153--- cloudinit/util.py 2012-08-28 03:51:00 +0000
154+++ cloudinit/util.py 2012-09-21 00:59:18 +0000
155@@ -952,6 +952,12 @@
156 return entries
157
158
159+def peek_file(fname, max_bytes):
160+ LOG.debug("Peeking at %s (max_bytes=%s)", fname, max_bytes)
161+ with open(fname, 'rb') as ifh:
162+ return ifh.read(max_bytes)
163+
164+
165 def load_file(fname, read_cb=None, quiet=False):
166 LOG.debug("Reading from %s (quiet=%s)", fname, quiet)
167 ofh = StringIO()
168@@ -1281,6 +1287,10 @@
169 return uptime_str
170
171
172+def append_file(path, content):
173+ write_file(path, content, omode="ab", mode=None)
174+
175+
176 def ensure_file(path, mode=0644):
177 write_file(path, content='', omode="ab", mode=mode)
178