aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild/scripts
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
parent7830cb61c39fbaf57933ac403dcdf5007667d87d (diff)
downloadmeson-4c71695e41a50dda3199d26ed7aedbaaf3150768.zip
meson-4c71695e41a50dda3199d26ed7aedbaaf3150768.tar.gz
meson-4c71695e41a50dda3199d26ed7aedbaaf3150768.tar.bz2
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')
-rw-r--r--mesonbuild/scripts/meson_benchmark.py43
-rw-r--r--mesonbuild/scripts/meson_exe.py3
-rw-r--r--mesonbuild/scripts/meson_install.py11
-rw-r--r--mesonbuild/scripts/meson_test.py45
-rw-r--r--mesonbuild/scripts/regen_checker.py6
-rw-r--r--mesonbuild/scripts/symbolextractor.py9
-rw-r--r--mesonbuild/scripts/vcstagger.py13
7 files changed, 76 insertions, 54 deletions
diff --git a/mesonbuild/scripts/meson_benchmark.py b/mesonbuild/scripts/meson_benchmark.py
index d1107b6..6d138b0 100644
--- a/mesonbuild/scripts/meson_benchmark.py
+++ b/mesonbuild/scripts/meson_benchmark.py
@@ -52,33 +52,34 @@ def run_benchmarks(options, datafile):
failed_tests = 0
logfile_base = 'meson-logs/benchmarklog'
jsonlogfilename = logfile_base+ '.json'
- jsonlogfile = open(jsonlogfilename, 'w')
- tests = pickle.load(open(datafile, 'rb'))
+ with open(datafile, 'rb') as f:
+ tests = pickle.load(f)
num_tests = len(tests)
if num_tests == 0:
print('No benchmarks defined.')
return 0
iteration_count = 5
wrap = [] # Benchmarks on cross builds are pointless so don't support them.
- for i, test in enumerate(tests):
- runs = []
- durations = []
- failed = False
- for _ in range(iteration_count):
- res = meson_test.run_single_test(wrap, test)
- runs.append(res)
- durations.append(res.duration)
- if res.returncode != 0:
- failed = True
- mean = statistics.mean(durations)
- stddev = statistics.stdev(durations)
- if failed:
- resultstr = 'FAIL'
- failed_tests += 1
- else:
- resultstr = 'OK'
- print_stats(3, num_tests, test.name, resultstr, i, mean, stddev)
- print_json_log(jsonlogfile, runs, test.name, i)
+ with open(jsonlogfilename, 'w') as jsonlogfile:
+ for i, test in enumerate(tests):
+ runs = []
+ durations = []
+ failed = False
+ for _ in range(iteration_count):
+ res = meson_test.run_single_test(wrap, test)
+ runs.append(res)
+ durations.append(res.duration)
+ if res.returncode != 0:
+ failed = True
+ mean = statistics.mean(durations)
+ stddev = statistics.stdev(durations)
+ if failed:
+ resultstr = 'FAIL'
+ failed_tests += 1
+ else:
+ resultstr = 'OK'
+ print_stats(3, num_tests, test.name, resultstr, i, mean, stddev)
+ print_json_log(jsonlogfile, runs, test.name, i)
print('\nFull log written to meson-logs/benchmarklog.json.')
return failed_tests
diff --git a/mesonbuild/scripts/meson_exe.py b/mesonbuild/scripts/meson_exe.py
index 1a0fcda..d2ae357 100644
--- a/mesonbuild/scripts/meson_exe.py
+++ b/mesonbuild/scripts/meson_exe.py
@@ -74,7 +74,8 @@ def run(args):
print('Test runner for Meson. Do not run on your own, mmm\'kay?')
print(sys.argv[0] + ' [data file]')
exe_data_file = options.args[0]
- exe = pickle.load(open(exe_data_file, 'rb'))
+ with open(exe_data_file, 'rb') as f:
+ exe = pickle.load(f)
return run_exe(exe)
if __name__ == '__main__':
diff --git a/mesonbuild/scripts/meson_install.py b/mesonbuild/scripts/meson_install.py
index 1924b95..6e877cf 100644
--- a/mesonbuild/scripts/meson_install.py
+++ b/mesonbuild/scripts/meson_install.py
@@ -38,8 +38,8 @@ def do_copy(from_file, to_file):
append_to_log(to_file)
def do_install(datafilename):
- ifile = open(datafilename, 'rb')
- d = pickle.load(ifile)
+ with open(datafilename, 'rb') as ifile:
+ d = pickle.load(ifile)
d.destdir = os.environ.get('DESTDIR', '')
d.fullprefix = destdir_join(d.destdir, d.prefix)
@@ -112,7 +112,9 @@ def install_man(d):
os.makedirs(outdir, exist_ok=True)
print('Installing %s to %s.' % (full_source_filename, outdir))
if outfilename.endswith('.gz') and not full_source_filename.endswith('.gz'):
- open(outfilename, 'wb').write(gzip.compress(open(full_source_filename, 'rb').read()))
+ with open(outfilename, 'wb') as of:
+ with open(full_source_filename, 'rb') as sf:
+ of.write(gzip.compress(sf.read()))
shutil.copystat(full_source_filename, outfilename)
append_to_log(outfilename)
else:
@@ -142,7 +144,8 @@ def run_install_script(d):
print('Running custom install script %s' % script)
suffix = os.path.splitext(script)[1].lower()
if platform.system().lower() == 'windows' and suffix != '.bat':
- first_line = open(script, encoding='latin_1', errors='ignore').readline().strip()
+ with open(script, encoding='latin_1', errors='ignore') as f:
+ first_line = f.readline().strip()
if first_line.startswith('#!'):
if shutil.which(first_line[2:]):
commands = [first_line[2:]]
diff --git a/mesonbuild/scripts/meson_test.py b/mesonbuild/scripts/meson_test.py
index 33b6165..951fb85 100644
--- a/mesonbuild/scripts/meson_test.py
+++ b/mesonbuild/scripts/meson_test.py
@@ -202,10 +202,8 @@ def run_tests(datafilename):
wrap = [options.wrapper]
logfilename = logfile_base + '-' + options.wrapper.replace(' ', '_') + '.txt'
jsonlogfilename = logfile_base + '-' + options.wrapper.replace(' ', '_') + '.json'
- logfile = open(logfilename, 'w')
- jsonlogfile = open(jsonlogfilename, 'w')
- logfile.write('Log of Meson test suite run on %s.\n\n' % datetime.datetime.now().isoformat())
- tests = pickle.load(open(datafilename, 'rb'))
+ with open(datafilename, 'rb') as f:
+ tests = pickle.load(f)
if len(tests) == 0:
print('No tests defined.')
return
@@ -222,24 +220,31 @@ def run_tests(datafilename):
executor = conc.ThreadPoolExecutor(max_workers=num_workers)
futures = []
filtered_tests = filter_tests(options.suite, tests)
- for i, test in enumerate(filtered_tests):
- if test.suite[0] == '':
- visible_name = test.name
- else:
- if options.suite is not None:
- visible_name = options.suite + ' / ' + test.name
+
+ with open(jsonlogfilename, 'w') as jsonlogfile, \
+ open(logfilename, 'w') as logfile:
+ logfile.write('Log of Meson test suite run on %s.\n\n' %
+ datetime.datetime.now().isoformat())
+ for i, test in enumerate(filtered_tests):
+ if test.suite[0] == '':
+ visible_name = test.name
else:
- visible_name = test.suite[0] + ' / ' + test.name
+ if options.suite is not None:
+ visible_name = options.suite + ' / ' + test.name
+ else:
+ visible_name = test.suite[0] + ' / ' + test.name
- if not test.is_parallel:
- drain_futures(futures)
- futures = []
- res = run_single_test(wrap, test)
- print_stats(numlen, filtered_tests, visible_name, res, i, logfile, jsonlogfile)
- else:
- f = executor.submit(run_single_test, wrap, test)
- futures.append((f, numlen, filtered_tests, visible_name, i, logfile, jsonlogfile))
- drain_futures(futures)
+ if not test.is_parallel:
+ drain_futures(futures)
+ futures = []
+ res = run_single_test(wrap, test)
+ print_stats(numlen, filtered_tests, visible_name, res, i,
+ logfile, jsonlogfile)
+ else:
+ f = executor.submit(run_single_test, wrap, test)
+ futures.append((f, numlen, filtered_tests, visible_name, i,
+ logfile, jsonlogfile))
+ drain_futures(futures)
return logfilename
def run(args):
diff --git a/mesonbuild/scripts/regen_checker.py b/mesonbuild/scripts/regen_checker.py
index f65f3bd..ddf4943 100644
--- a/mesonbuild/scripts/regen_checker.py
+++ b/mesonbuild/scripts/regen_checker.py
@@ -48,8 +48,10 @@ def run(args):
private_dir = args[0]
dumpfile = os.path.join(private_dir, 'regeninfo.dump')
coredata = os.path.join(private_dir, 'coredata.dat')
- regeninfo = pickle.load(open(dumpfile, 'rb'))
- coredata = pickle.load(open(coredata, 'rb'))
+ with open(dumpfile, 'rb') as f:
+ regeninfo = pickle.load(f)
+ with open(coredata, 'rb') as f:
+ coredata = pickle.load(f)
mesonscript = coredata.meson_script_file
backend = coredata.get_builtin_option('backend')
regen_timestamp = os.stat(dumpfile).st_mtime
diff --git a/mesonbuild/scripts/symbolextractor.py b/mesonbuild/scripts/symbolextractor.py
index 79c1264..c117301 100644
--- a/mesonbuild/scripts/symbolextractor.py
+++ b/mesonbuild/scripts/symbolextractor.py
@@ -34,16 +34,19 @@ parser.add_argument('args', nargs='+')
def dummy_syms(outfilename):
"""Just touch it so relinking happens always."""
- open(outfilename, 'w').close()
+ with open(outfilename, 'w'):
+ pass
def write_if_changed(text, outfilename):
try:
- oldtext = open(outfilename, 'r').read()
+ with open(outfilename, 'r') as f:
+ oldtext = f.read()
if text == oldtext:
return
except FileNotFoundError:
pass
- open(outfilename, 'w').write(text)
+ with open(outfilename, 'w') as f:
+ f.write(text)
def linux_syms(libfilename, outfilename):
pe = subprocess.Popen(['readelf', '-d', libfilename], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
diff --git a/mesonbuild/scripts/vcstagger.py b/mesonbuild/scripts/vcstagger.py
index 390e37a..3f36e0a 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]