summaryrefslogtreecommitdiff
path: root/mesonbuild/scripts/vcstagger.py
diff options
context:
space:
mode:
authorElliott Sales de Andrade <quantum.analyst@gmail.com>2016-08-24 19:29:11 -0400
committerElliott Sales de Andrade <quantum.analyst@gmail.com>2016-08-27 18:29:55 -0400
commit4c71695e41a50dda3199d26ed7aedbaaf3150768 (patch)
tree8bd499a2a113c3da5c1dee8ed29f4b3c69d27dd7 /mesonbuild/scripts/vcstagger.py
parent7830cb61c39fbaf57933ac403dcdf5007667d87d (diff)
downloadmeson-4c71695e41a50dda3199d26ed7aedbaaf3150768.tar.gz
Use context manager for file I/O.
There are a few cases where a context manager cannot be used, such as the logger.
Diffstat (limited to 'mesonbuild/scripts/vcstagger.py')
-rw-r--r--mesonbuild/scripts/vcstagger.py13
1 files changed, 10 insertions, 3 deletions
diff --git a/mesonbuild/scripts/vcstagger.py b/mesonbuild/scripts/vcstagger.py
index 390e37a8d..3f36e0af4 100644
--- a/mesonbuild/scripts/vcstagger.py
+++ b/mesonbuild/scripts/vcstagger.py
@@ -23,9 +23,16 @@ def config_vcs_tag(infile, outfile, fallback, source_dir, replace_string, regex_
except Exception:
new_string = fallback
- new_data = open(infile).read().replace(replace_string, new_string)
- if (not os.path.exists(outfile)) or (open(outfile).read() != new_data):
- open(outfile, 'w').write(new_data)
+ with open(infile) as f:
+ new_data = f.read().replace(replace_string, new_string)
+ if os.path.exists(outfile):
+ with open(outfile) as f:
+ needs_update = (f.read() != new_data)
+ else:
+ needs_update = True
+ if needs_update:
+ with open(outfile, 'w') as f:
+ f.write(new_data)
def run(args):
infile, outfile, fallback, source_dir, replace_string, regex_selector = args[0:6]