Browse code

Use the with keyword when dealing with file objects.

Change-Id: I3586b00e5c671707c221347fdc542ec28b4db9a9
Reviewed-on: http://photon-jenkins.eng.vmware.com:8082/4472
Tested-by: gerrit-photon <photon-checkins@vmware.com>
Reviewed-by: Bo Gan <ganb@vmware.com>

xiaolin-vmware authored on 2017/12/07 11:04:06
Showing 2 changed files
... ...
@@ -298,37 +298,35 @@ class Installer(object):
298 298
             self.exit_gracefully(None, None)
299 299
 
300 300
     def update_fstab(self):
301
-        fstab_file = open(os.path.join(self.photon_root, "etc/fstab"), "w")
302
-        fstab_file.write("#system\tmnt-pt\ttype\toptions\tdump\tfsck\n")
301
+        with open(os.path.join(self.photon_root, "etc/fstab"), "w") as fstab_file:
302
+            fstab_file.write("#system\tmnt-pt\ttype\toptions\tdump\tfsck\n")
303 303
 
304
-        for partition in self.install_config['disk']['partitions']:
305
-            options = 'defaults'
306
-            dump = 1
307
-            fsck = 2
304
+            for partition in self.install_config['disk']['partitions']:
305
+                options = 'defaults'
306
+                dump = 1
307
+                fsck = 2
308 308
 
309
-            if 'mountpoint' in partition and partition['mountpoint'] == '/':
310
-                options = options + ',barrier,noatime,noacl,data=ordered'
311
-                fsck = 1
309
+                if 'mountpoint' in partition and partition['mountpoint'] == '/':
310
+                    options = options + ',barrier,noatime,noacl,data=ordered'
311
+                    fsck = 1
312 312
             
313
-            if partition['filesystem'] == 'swap':
314
-                mountpoint = 'swap'
315
-                dump = 0
316
-                fsck = 0
317
-            else:
318
-                mountpoint = partition['mountpoint']
319
-
320
-            fstab_file.write("{}\t{}\t{}\t{}\t{}\t{}\n".format(
321
-                partition['path'],
322
-                mountpoint,
323
-                partition['filesystem'],
324
-                options,
325
-                dump,
326
-                fsck
327
-                ))
328
-        # Add the cdrom entry
329
-        fstab_file.write("/dev/cdrom\t/mnt/cdrom\tiso9660\tro,noauto\t0\t0\n")
330
-
331
-        fstab_file.close()
313
+                if partition['filesystem'] == 'swap':
314
+                    mountpoint = 'swap'
315
+                    dump = 0
316
+                    fsck = 0
317
+                else:
318
+                    mountpoint = partition['mountpoint']
319
+
320
+                fstab_file.write("{}\t{}\t{}\t{}\t{}\t{}\n".format(
321
+                    partition['path'],
322
+                    mountpoint,
323
+                    partition['filesystem'],
324
+                    options,
325
+                    dump,
326
+                    fsck
327
+                    ))
328
+            # Add the cdrom entry
329
+            fstab_file.write("/dev/cdrom\t/mnt/cdrom\tiso9660\tro,noauto\t0\t0\n")
332 330
 
333 331
     def generate_partitions_param(self, reverse = False):
334 332
         if reverse:
... ...
@@ -13,12 +13,11 @@ class JsonWrapper(object):
13 13
         self.filename = filename
14 14
 
15 15
     def read(self):
16
-        json_data = open(self.filename)
17
-        self.data = json.load(json_data, object_pairs_hook=collections.OrderedDict)
18
-        json_data.close()
16
+        with open(self.filename) as json_data:
17
+            self.data = json.load(json_data, object_pairs_hook=collections.OrderedDict)
19 18
         return self.data
20 19
 
21 20
     def write(self,  data):
22 21
         self.data = data
23
-        outfile = open(self.filename,  'wb')
24
-        json.dump(data,  outfile)
22
+        with open(self.filename,  'wb') as outfile:
23
+            json.dump(data,  outfile)