From d923c606f391a120501c7239804ed66713e49876 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: Tue, 22 Jan 2019 00:00:00 +0000 Subject: [PATCH] getvar works again --- common.py | 15 ++++++++------- getvar | 37 +++++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/common.py b/common.py index 0da131e..217ae1f 100644 --- a/common.py +++ b/common.py @@ -100,8 +100,9 @@ class LkmcCliFunction(cli_function.CliFunction): * command timing * some common flags, e.g.: --arch, --dry-run, --verbose ''' - def __init__(self, *args, **kwargs): + def __init__(self, *args, do_print_time=True, **kwargs): kwargs['config_file'] = consts['config_file'] + self._do_print_time = do_print_time super().__init__(*args, **kwargs) # Args for all scripts. @@ -659,7 +660,7 @@ to allow overriding configs from the CLI. ret = self.timed_main() if not kwargs['dry_run']: end_time = time.time() - self.print_time(end_time - start_time) + self._print_time(end_time - start_time) return ret def make_build_dirs(self): @@ -685,11 +686,11 @@ to allow overriding configs from the CLI. return True return False - @staticmethod - def print_time(ellapsed_seconds): - hours, rem = divmod(ellapsed_seconds, 3600) - minutes, seconds = divmod(rem, 60) - print("time {:02}:{:02}:{:02}".format(int(hours), int(minutes), int(seconds))) + def _print_time(self, ellapsed_seconds): + if self._do_print_time: + hours, rem = divmod(ellapsed_seconds, 3600) + minutes, seconds = divmod(rem, 60) + print("time {:02}:{:02}:{:02}".format(int(hours), int(minutes), int(seconds))) def raw_to_qcow2(self, prebuilt=False, reverse=False): if prebuilt or not os.path.exists(self.env['qemu_img_executable']): diff --git a/getvar b/getvar index 0f6c04f..da7a86e 100755 --- a/getvar +++ b/getvar @@ -5,13 +5,16 @@ import types import common from shell_helpers import LF -parser = self.get_argparse(argparse_args={ - 'description': '''Print the value of a kwargs['py'] variable. +class Main(common.LkmcCliFunction): + def __init__(self): + super().__init__( + description='''\ +Print the value of a self.env['py'] variable. This is useful to: * give dry commands on the README that don't change when we refactor directory structure -* create simple bash scripts that call use kwargs['py'] variables +* create simple bash scripts that call use self.env['py'] variables For example, to get the Buildroot output directory for an ARM build, use: @@ -24,16 +27,18 @@ List all available variables: .... ./%(prog)s .... -.... -''' -}) -parser.add_argument('variable', nargs='?') -args = self.setup(parser) -if kwargs['variable']: - print(getattr(common, kwargs['variable'])) -else: - for attr in dir(common): - if not attr.startswith('__'): - val = getattr(common, attr) - if not callable(val) and not type(val) is types.ModuleType: - print('{} {}'.format(attr, val)) +''', + do_print_time=False, + ) + self.add_argument('variable', nargs='?') + + def timed_main(self): + variable = self.env['variable'] + if variable: + print(self.env[variable]) + else: + for key in self.env: + print('{}={}'.format(key, self.env[key])) + +if __name__ == '__main__': + Main().cli()