-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathtool_base.py
91 lines (70 loc) · 2.8 KB
/
tool_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Copyright (c) Yugabyte, Inc.
import argparse
import os
from yb.common_util import set_env_vars_from_build_root
from overrides import overrides, EnforceOverrides
class YbBuildToolBase(EnforceOverrides):
"""
A base class for command-line tools that are part of YugabyteDB build.
"""
def get_description(self):
raise NotImplementedError()
def get_arg_parser_kwargs(self):
return dict(description=self.get_description())
def __init__(self):
self.arg_parser = None
self.args = None
# Whether to add "standard" arguments needed by most build tools.
self.add_standard_build_args = True
def run(self):
"""
The top-level function used to run the tool.
"""
self.create_arg_parser()
self.parse_args()
self.validate_and_process_args()
self.run_impl()
def parse_args(self):
self.args = self.arg_parser.parse_args()
def validate_and_process_args(self):
if hasattr(self.args, 'build_root'):
if self.args.build_root is None:
raise ValueError('--build_root (or BUILD_ROOT environment variable) not specified')
set_env_vars_from_build_root(self.args.build_root)
def run_impl(self):
"""
The overridable internal implementation of running the tool.
"""
raise NotImplementedError()
def add_command_line_args(self):
"""
Can be overridden to add more command-line arguments to the parser.
"""
pass
def create_arg_parser(self):
# Don't allow to run this function multiple times.
assert self.arg_parser is None
self.arg_parser = argparse.ArgumentParser(**self.get_arg_parser_kwargs())
if self.add_standard_build_args:
self.add_build_root_arg()
self.add_compiler_type_arg()
self.add_thirdparty_dir_arg()
self.add_command_line_args()
# ---------------------------------------------------------------------------------------------
# Functions to add various standard arguments
# ---------------------------------------------------------------------------------------------
def add_build_root_arg(self):
self.arg_parser.add_argument(
'--build_root',
default=os.environ.get('BUILD_ROOT'),
help='YugabyteDB build root directory')
def add_compiler_type_arg(self):
self.arg_parser.add_argument(
'--compiler_type',
default=os.getenv('YB_COMPILER_TYPE'),
help='Compiler type, e.g. gcc or clang')
def add_thirdparty_dir_arg(self):
self.arg_parser.add_argument(
'--thirdparty_dir',
default=os.getenv('YB_THIRDPARTY_DIR'),
help='YugabyteDB third-party dependencies directory')