[mm-common] Python files: Use the 'with' statement when files are opened



commit ebb514cb10848eb7f2685bf7eacda74812491e38
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Wed Sep 2 10:32:25 2020 +0200

    Python files: Use the 'with' statement when files are opened

 util/build_scripts/dist-changelog.py   | 8 +++-----
 util/build_scripts/generate-binding.py | 6 ++----
 util/meson_aux/extra-dist-cmd.py       | 5 ++---
 util/meson_aux/skeletonmm-tarball.py   | 9 ++++-----
 4 files changed, 11 insertions(+), 17 deletions(-)
---
diff --git a/util/build_scripts/dist-changelog.py b/util/build_scripts/dist-changelog.py
index c38fde3..4a11a5f 100755
--- a/util/build_scripts/dist-changelog.py
+++ b/util/build_scripts/dist-changelog.py
@@ -2,7 +2,7 @@
 
 # External command, intended to be called with meson.add_dist_script() in meson.build
 
-#                      sys.argv[1]
+#                       argv[1]
 # dist-changelog.py <root_source_dir>
 
 import os
@@ -20,7 +20,5 @@ cmd = [
   '--max-count=200',
   '--pretty=tformat:%cd  %an  <%ae>%n%n  %s%n%w(0,0,2)%+b',
 ]
-logfile = open(os.path.join(os.getenv('MESON_DIST_ROOT'), 'ChangeLog'), mode='w')
-result = subprocess.run(cmd, stdout=logfile)
-logfile.close()
-sys.exit(result.returncode)
+with open(os.path.join(os.getenv('MESON_DIST_ROOT'), 'ChangeLog'), mode='w') as logfile:
+  sys.exit(subprocess.run(cmd, stdout=logfile).returncode)
diff --git a/util/build_scripts/generate-binding.py b/util/build_scripts/generate-binding.py
index a1244a8..b412521 100755
--- a/util/build_scripts/generate-binding.py
+++ b/util/build_scripts/generate-binding.py
@@ -33,10 +33,8 @@ def generate_wrap_init():
     '--namespace=' + namespace,
     '--parent_dir=' + parent_dir,
   ] + sys.argv[5:]
-  output_file_obj = open(output_file, mode='w')
-  result = subprocess.run(cmd, stdout=output_file_obj)
-  output_file_obj.close()
-  return result.returncode
+  with open(output_file, mode='w') as output_file_obj:
+    return subprocess.run(cmd, stdout=output_file_obj).returncode
 
 # Invoked from custom_target() in meson.build.
 def gmmproc():
diff --git a/util/meson_aux/extra-dist-cmd.py b/util/meson_aux/extra-dist-cmd.py
index 5b04f06..484f4e9 100755
--- a/util/meson_aux/extra-dist-cmd.py
+++ b/util/meson_aux/extra-dist-cmd.py
@@ -27,9 +27,8 @@ cmd = [
   '--max-count=200',
   '--pretty=tformat:%cd  %an  <%ae>%n%n  %s%n%w(0,0,2)%+b',
 ]
-logfile = open(os.path.join(os.getenv('MESON_DIST_ROOT'), 'ChangeLog'), mode='w')
-result = subprocess.run(cmd, stdout=logfile)
-logfile.close()
+with open(os.path.join(os.getenv('MESON_DIST_ROOT'), 'ChangeLog'), mode='w') as logfile:
+  result = subprocess.run(cmd, stdout=logfile)
 
 # Distribute the libstdc++.tag file in addition to the files in the local git clone.
 # shutil.copy2() copies timestamps and some other file metadata.
diff --git a/util/meson_aux/skeletonmm-tarball.py b/util/meson_aux/skeletonmm-tarball.py
index 576b522..db9e650 100755
--- a/util/meson_aux/skeletonmm-tarball.py
+++ b/util/meson_aux/skeletonmm-tarball.py
@@ -39,11 +39,10 @@ elif output_file.endswith('.gz'):
 else:
   mode = 'w'
 
-tar_file = tarfile.open(output_file, mode=mode)
-os.chdir(source_dir) # Input filenames are relative to source_dir.
-for file in sys.argv[3:]:
-  tar_file.add(file)
-tar_file.close()
+with tarfile.open(output_file, mode=mode) as tar_file:
+  os.chdir(source_dir) # Input filenames are relative to source_dir.
+  for file in sys.argv[3:]:
+    tar_file.add(file)
 # Errors raise exceptions. If an exception is raised, Meson+ninja will notice
 # that the command failed, despite exit(0).
 sys.exit(0)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]