From f532a6dcf4526416fdec1f2aae035f45150887ac Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Sun, 26 Aug 2018 21:28:48 +0100 Subject: [PATCH] config works --- common.py | 24 ++++++++++++++++++------ config.example | 7 ++++--- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/common.py b/common.py index 72035b1..3f635e6 100644 --- a/common.py +++ b/common.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import argparse +import imp import subprocess import os import sys @@ -30,6 +31,7 @@ def get_argparse(**kwargs): """ Return an argument parser with common arguments set. """ + global this default_build_id='default' parser = argparse.ArgumentParser( formatter_class=argparse.RawTextHelpFormatter, @@ -70,7 +72,7 @@ Default: %(default)s '--port-offset', type=int, help="""\ Increase the ports to be used such as for GDB by an offset to run multiple -instances in parallel. +instances in parallel. Equals the run ID if that is an integer. Default: %(default)s """ ) @@ -90,6 +92,11 @@ around when you checkout between branches. '-t', '--gem5-build-type', default='opt', help='gem5 build type, most often used for "debug" builds. Default: %(default)s' ) + # A bit ugly as it actually changes the defaults shown on --help, but we can't do any better + # because it is impossible to check if arguments were given or not... + # - https://stackoverflow.com/questions/30487767/check-if-argparse-optional-argument-is-set-or-not + # - https://stackoverflow.com/questions/3609852/which-is-the-best-way-to-allow-configuration-options-be-overridden-at-the-comman + parser.set_defaults(**this.configs) return parser def setup(parser): @@ -166,7 +173,10 @@ def setup(parser): # Ports. if args.port_offset is None: - args.port_offset = int(args.run_id) + try: + args.port_offset = int(args.run_id) + except ValueError: + args.port_offset = 0 if args.gem5: this.gem5_telnet_port = 3456 + args.port_offset this.gdb_port = 7000 + args.port_offset @@ -204,7 +214,9 @@ sha = subprocess.check_output(['git', '-C', root_dir, 'log', '-1', '--format=%H' # Config file. TODO move to decent python setup. config_file = os.path.join(data_dir, 'config') -if os.path.exists(config_file): - exec(open(config_file).read()) - with open(config_file) as f: - exec(f.read()) +config = imp.load_source('config', config_file) +configs = {x:getattr(config, x) for x in dir(config) if not x.startswith('__')} +# if os.path.exists(config_file): + # exec(open(config_file).read()) + # with open(config_file) as f: + # exec(f.read()) diff --git a/config.example b/config.example index f86fff2..dd8c8d9 100644 --- a/config.example +++ b/config.example @@ -1,3 +1,4 @@ -#!/usr/bin/env bash -common_arch=aarch64 -common_gem5=true +#!/usr/bin/env python +arch = 'aarch64' +gem5 = True +run_id = 'asdf'