← Back to team overview

cloud-init-dev team mailing list archive

[Merge] lp:~harlowja/cloud-init/use-utils-file-funcs into lp:cloud-init

 

Joshua Harlow has proposed merging lp:~harlowja/cloud-init/use-utils-file-funcs into lp:cloud-init.

Requested reviews:
  cloud init development team (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~harlowja/cloud-init/use-utils-file-funcs/+merge/125616

Use util funcs for file actions.
-- 
https://code.launchpad.net/~harlowja/cloud-init/use-utils-file-funcs/+merge/125616
Your team cloud init development team is requested to review the proposed merge of lp:~harlowja/cloud-init/use-utils-file-funcs into lp:cloud-init.
=== modified file 'cloudinit/distros/__init__.py'
--- cloudinit/distros/__init__.py	2012-09-18 17:27:41 +0000
+++ cloudinit/distros/__init__.py	2012-09-21 00:59:18 +0000
@@ -343,8 +343,7 @@
 
         else:
             try:
-                with open(sudo_file, 'a') as f:
-                    f.write(content)
+                util.append_file(sudo_file, content)
             except IOError as e:
                 util.logexc(LOG, "Failed to write %s" % sudo_file, e)
                 raise e

=== modified file 'cloudinit/sources/DataSourceAltCloud.py'
--- cloudinit/sources/DataSourceAltCloud.py	2012-08-10 19:29:42 +0000
+++ cloudinit/sources/DataSourceAltCloud.py	2012-09-21 00:59:18 +0000
@@ -73,13 +73,11 @@
 
     # First try deltacloud_user_data_file. On failure try user_data_file.
     try:
-        with open(deltacloud_user_data_file, 'r') as user_data_f:
-            user_data = user_data_f.read().strip()
-    except:
+        user_data = util.load_file(deltacloud_user_data_file).strip()
+    except IOError:
         try:
-            with open(user_data_file, 'r') as user_data_f:
-                user_data = user_data_f.read().strip()
-        except:
+            user_data = util.load_file(user_data_file).strip()
+        except IOError:
             util.logexc(LOG, ('Failed accessing user data file.'))
             return None
 
@@ -157,11 +155,10 @@
 
         if os.path.exists(CLOUD_INFO_FILE):
             try:
-                cloud_info = open(CLOUD_INFO_FILE)
-                cloud_type = cloud_info.read().strip().upper()
-                cloud_info.close()
-            except:
-                util.logexc(LOG, 'Unable to access cloud info file.')
+                cloud_type = util.load_file(CLOUD_INFO_FILE).strip().upper()
+            except IOError:
+                util.logexc(LOG, 'Unable to access cloud info file at %s.',
+                            CLOUD_INFO_FILE)
                 return False
         else:
             cloud_type = self.get_cloud_type()

=== modified file 'cloudinit/sources/DataSourceConfigDrive.py'
--- cloudinit/sources/DataSourceConfigDrive.py	2012-08-24 21:06:18 +0000
+++ cloudinit/sources/DataSourceConfigDrive.py	2012-09-21 00:59:18 +0000
@@ -227,19 +227,19 @@
         found = False
         if os.path.isfile(fpath):
             try:
-                with open(fpath) as fp:
-                    data = fp.read()
-            except Exception as exc:
-                raise BrokenConfigDriveDir("failed to read: %s" % fpath)
+                data = util.load_file(fpath)
+            except IOError:
+                raise BrokenConfigDriveDir("Failed to read: %s" % fpath)
             found = True
         elif required:
-            raise NonConfigDriveDir("missing mandatory %s" % fpath)
+            raise NonConfigDriveDir("Missing mandatory path: %s" % fpath)
 
         if found and process:
             try:
                 data = process(data)
             except Exception as exc:
-                raise BrokenConfigDriveDir("failed to process: %s" % fpath)
+                raise BrokenConfigDriveDir(("Failed to process "
+                                            "path: %s") % fpath)
 
         if found:
             results[name] = data
@@ -255,8 +255,7 @@
         # do not use os.path.join here, as content_path starts with /
         cpath = os.path.sep.join((source_dir, "openstack",
                                   "./%s" % item['content_path']))
-        with open(cpath) as fp:
-            return(fp.read())
+        return util.load_file(cpath)
 
     files = {}
     try:
@@ -270,7 +269,7 @@
         if item:
             results['network_config'] = read_content_path(item)
     except Exception as exc:
-        raise BrokenConfigDriveDir("failed to read file %s: %s" % (item, exc))
+        raise BrokenConfigDriveDir("Failed to read file %s: %s" % (item, exc))
 
     # to openstack, user can specify meta ('nova boot --meta=key=value') and
     # those will appear under metadata['meta'].
@@ -385,8 +384,7 @@
     # hasn't declared itself found.
     fname = os.path.join(paths.get_cpath('data'), 'instance-id')
     try:
-        with open(fname) as fp:
-            return fp.read()
+        return util.load_file(fname)
     except IOError:
         return None
 

=== modified file 'cloudinit/sources/DataSourceMAAS.py'
--- cloudinit/sources/DataSourceMAAS.py	2012-08-06 17:26:21 +0000
+++ cloudinit/sources/DataSourceMAAS.py	2012-09-21 00:59:18 +0000
@@ -301,9 +301,7 @@
             'token_secret': args.tsec, 'consumer_secret': args.csec}
 
         if args.config:
-            import yaml
-            with open(args.config) as fp:
-                cfg = yaml.safe_load(fp)
+            cfg = util.read_conf(args.config)
             if 'datasource' in cfg:
                 cfg = cfg['datasource']['MAAS']
             for key in creds.keys():
@@ -312,7 +310,7 @@
 
         def geturl(url, headers_cb):
             req = urllib2.Request(url, data=None, headers=headers_cb(url))
-            return(urllib2.urlopen(req).read())
+            return (urllib2.urlopen(req).read())
 
         def printurl(url, headers_cb):
             print "== %s ==\n%s\n" % (url, geturl(url, headers_cb))

=== modified file 'cloudinit/sources/DataSourceOVF.py'
--- cloudinit/sources/DataSourceOVF.py	2012-07-12 19:20:24 +0000
+++ cloudinit/sources/DataSourceOVF.py	2012-09-21 00:59:18 +0000
@@ -204,9 +204,8 @@
 
         try:
             # See if we can read anything at all...??
-            with open(fullp, 'rb') as fp:
-                fp.read(512)
-        except:
+            util.peek_file(fullp, 512)
+        except IOError:
             continue
 
         try:

=== modified file 'cloudinit/util.py'
--- cloudinit/util.py	2012-08-28 03:51:00 +0000
+++ cloudinit/util.py	2012-09-21 00:59:18 +0000
@@ -952,6 +952,12 @@
     return entries
 
 
+def peek_file(fname, max_bytes):
+    LOG.debug("Peeking at %s (max_bytes=%s)", fname, max_bytes)
+    with open(fname, 'rb') as ifh:
+        return ifh.read(max_bytes)
+
+
 def load_file(fname, read_cb=None, quiet=False):
     LOG.debug("Reading from %s (quiet=%s)", fname, quiet)
     ofh = StringIO()
@@ -1281,6 +1287,10 @@
     return uptime_str
 
 
+def append_file(path, content):
+    write_file(path, content, omode="ab", mode=None)
+
+
 def ensure_file(path, mode=0644):
     write_file(path, content='', omode="ab", mode=mode)
 


Follow ups