2 ''''export SCRIPT_PATH="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)" # '''
3 ''''export PY_PATH=${SCRIPT_PATH}/venv/bin/python # '''
4 ''''test -f ${PY_PATH} && exec ${PY_PATH} "$0" "$@" # '''
5 ''''echo "Error: Virtual environment not found. Please run 'one-prepare-venv' command." # '''
8 # Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
10 # Licensed under the Apache License, Version 2.0 (the "License");
11 # you may not use this file except in compliance with the License.
12 # You may obtain a copy of the License at
14 # http://www.apache.org/licenses/LICENSE-2.0
16 # Unless required by applicable law or agreed to in writing, software
17 # distributed under the License is distributed on an "AS IS" BASIS,
18 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 # See the License for the specific language governing permissions and
20 # limitations under the License.
31 import onelib.utils as oneutils
33 # TODO Find better way to suppress trackback on error
34 sys.tracebacklimit = 0
37 def _get_backends_list():
48 The list where `one-codegen` finds its backends
49 - `bin` folder where `one-codegen` exists
53 NOTE If there are backends of the same name in different places,
54 the closer to the top in the list, the higher the priority.
56 dir_path = os.path.dirname(os.path.realpath(__file__))
60 files = [f for f in glob.glob(dir_path + '/*-compile')]
63 f for f in glob.glob(dir_path + '/../backends/**/*-compile', recursive=True)
65 # TODO find backends in `$PATH`
69 base = ntpath.basename(cand)
70 if not base in backend_set and os.path.isfile(cand) and os.access(cand, os.X_OK):
72 backends_list.append(cand)
77 def _get_parser(backends_list):
78 codegen_usage = 'one-codegen [-h] [-v] [-C CONFIG] [-b BACKEND] [--] [COMMANDS FOR BACKEND]'
79 parser = argparse.ArgumentParser(
80 description='command line tool for code generation', usage=codegen_usage)
82 oneutils.add_default_arg(parser)
84 # get backend list in the directory
85 backends_name = [ntpath.basename(f) for f in backends_list]
87 backends_name_message = '(There is no available backend drivers)'
89 backends_name_message = '(available backend drivers: ' + ', '.join(
91 backend_help_message = 'backend name to use ' + backends_name_message
92 parser.add_argument('-b', '--backend', type=str, help=backend_help_message)
97 def _verify_arg(parser, args):
98 """verify given arguments"""
99 # check if required arguments is given
101 if not oneutils.is_valid_attr(args, 'backend'):
102 missing.append('-b/--backend')
104 parser.error('the following arguments are required: ' + ' '.join(missing))
107 def _parse_arg(parser):
111 argv = copy.deepcopy(sys.argv)
115 args = [list(y) for x, y in itertools.groupby(argv, lambda z: z == '--') if not x]
117 codegen_args = parser.parse_args(codegen_args)
118 # one-codegen has two interfaces
119 # 1. one-codegen [-h] [-v] [-C CONFIG] [-b BACKEND] [COMMANDS FOR BACKEND]
121 codegen_args = args[0]
122 codegen_args, unknown_args = parser.parse_known_args(codegen_args)
123 # 2. one-codegen [-h] [-v] [-C CONFIG] [-b BACKEND] -- [COMMANDS FOR BACKEND]
125 codegen_args = args[0]
126 backend_args = args[1]
127 codegen_args = parser.parse_args(codegen_args)
129 if len(args) and codegen_args.version:
130 oneutils.print_version_and_exit(__file__)
132 return codegen_args, backend_args, unknown_args
137 backends_list = _get_backends_list()
140 parser = _get_parser(backends_list)
141 args, backend_args, unknown_args = _parse_arg(parser)
143 # parse configuration file
144 oneutils.parse_cfg(args.config, 'one-codegen', args)
147 _verify_arg(parser, args)
149 # make a command to run given backend driver
151 backend_base = getattr(args, 'backend') + '-compile'
152 for cand in backends_list:
153 if ntpath.basename(cand) == backend_base:
156 # Find backend from system path
157 codegen_path = shutil.which(backend_base)
160 raise FileNotFoundError(backend_base + ' not found')
161 codegen_cmd = [codegen_path] + backend_args + unknown_args
162 if oneutils.is_valid_attr(args, 'command'):
163 codegen_cmd += getattr(args, 'command').split()
166 oneutils.run(codegen_cmd, err_prefix=backend_base)
169 if __name__ == '__main__':
170 oneutils.safemain(main, __file__)