From 98d2c83317140022dbf4cd9ad6efaae3ce65e5c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciro=20Santilli=20=E5=85=AD=E5=9B=9B=E4=BA=8B=E4=BB=B6=20?= =?UTF-8?q?=E6=B3=95=E8=BD=AE=E5=8A=9F?= Date: Thu, 25 Oct 2018 00:00:00 +0000 Subject: [PATCH] echo key configs as bash commands --- build-linux | 3 +-- common.py | 26 +++++++++++++++++++------- run | 6 ++---- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/build-linux b/build-linux index 6db57da..2474f65 100755 --- a/build-linux +++ b/build-linux @@ -92,8 +92,7 @@ Configure the kernel, but don't build it. if args.config != []: cli_config_fragment_path = os.path.join(build_dir, 'lkmc_cli_config_fragment') cli_config_str = '\n'.join(map(lambda x: 'CONFIG_' + x, args.config)) - with open(cli_config_fragment_path, 'w') as cli_config_fragment_cli: - cli_config_fragment_cli.write(cli_config_str) + common.write_string_to_file(cli_config_fragment_path, cli_config_str) config_fragments.append(cli_config_fragment_path) common.cp( base_config_file, diff --git a/common.py b/common.py index c803ee0..e09a116 100644 --- a/common.py +++ b/common.py @@ -154,6 +154,17 @@ mkdir are generally omitted since those are obvious. def base64_encode(string): return base64.b64encode(string.encode()).decode() +def write_string_to_file(path, string, mode='w'): + global this_module + if mode == 'a': + redirect = '>>' + else: + redirect = '>' + print_cmd("cat << 'EOF' {} {}\n{}\nEOF".format(redirect, path, string)) + if not this_module.dry_run: + with open(path, 'a') as f: + f.write(string) + def copy_dir_if_update_non_recursive(srcdir, destdir, filter_ext=None): os.makedirs(destdir, exist_ok=True) for basename in os.listdir(srcdir): @@ -441,7 +452,7 @@ def cmd_to_string(cmd, cwd=None, extra_env=None, extra_paths=None): out.append('{}={}'.format(shlex.quote(key), shlex.quote(extra_env[key])) + newline_separator) for arg in cmd: out.append(shlex.quote(arg) + newline_separator) - return ' '.join(out) + ';\n' + return ' '.join(out) + ';' def print_cmd(cmd, cwd=None, cmd_file=None, extra_env=None, extra_paths=None): ''' @@ -450,8 +461,12 @@ def print_cmd(cmd, cwd=None, cmd_file=None, extra_env=None, extra_paths=None): Optionally save the command to cmd_file file, and add extra_env environment variables to the command generated. ''' - cmd_string = cmd_to_string(cmd, cwd=None, extra_env=None, extra_paths=None) - print(this_module.command_prefix + cmd_string, end='') + global dry_run + if type(cmd) is str: + cmd_string = cmd + else: + cmd_string = cmd_to_string(cmd, cwd=None, extra_env=None, extra_paths=None) + print(this_module.command_prefix + cmd_string) if cmd_file is not None: with open(cmd_file, 'w') as f: f.write('#!/usr/bin/env bash\n') @@ -864,7 +879,4 @@ def write_configs(config_path, configs, config_fragments=None): if not this_module.dry_run: for line in config_fragment_file: config_file.write(line) - for config in configs: - print_cmd(['echo', config, '>>', config_path]) - if not this_module.dry_run: - config_file.write(config + '\n') + write_string_to_file(config_path, '\n'.join(configs), mode='a') diff --git a/run b/run index 6c7381a..de06c73 100755 --- a/run +++ b/run @@ -112,16 +112,14 @@ def main(args, extra_args=None): else: if not os.path.exists(common.gem5_fake_iso): os.makedirs(os.path.dirname(common.gem5_fake_iso), exist_ok=True) - with open(common.gem5_fake_iso, 'w') as f: - f.write('a' * 512) + common.write_string_to_file(common.gem5_fake_iso, 'a' * 512) if not os.path.exists(common.image): # This is to run gem5 from a prebuilt download. if (not args.baremetal is None) or (not os.path.exists(common.linux_image)): raise_image_not_found() common.run_cmd([os.path.join(common.extract_vmlinux, common.linux_image)]) os.makedirs(os.path.dirname(common.gem5_readfile), exist_ok=True) - with open(common.gem5_readfile, 'w') as readfile: - readfile.write(args.gem5_readfile) + common.write_string_to_file(common.gem5_readfile, args.gem5_readfile) memory = '{}B'.format(args.memory) gem5_exe_args = shlex.split(args.gem5_exe_args) if do_trace: