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) 2021 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.
30 import onelib.utils as oneutils
32 # TODO Find better way to suppress trackback on error
33 sys.tracebacklimit = 0
36 def _get_backends_list():
47 The list where `one-profile` finds its backends
48 - `bin` folder where `one-profile` exists
51 NOTE If there are backends of the same name in different places,
52 the closer to the top in the list, the higher the priority.
54 dir_path = os.path.dirname(os.path.realpath(__file__))
58 files = [f for f in glob.glob(dir_path + '/*-profile')]
61 f for f in glob.glob(dir_path + '/../backends/**/*-profile', recursive=True)
63 # TODO find backends in `$PATH`
67 base = ntpath.basename(cand)
68 if not base in backend_set and os.path.isfile(cand) and os.access(cand, os.X_OK):
70 backends_list.append(cand)
75 def _get_parser(backends_list):
76 profile_usage = 'one-profile [-h] [-v] [-C CONFIG] [-b BACKEND] [--] [COMMANDS FOR BACKEND]'
77 parser = argparse.ArgumentParser(
78 description='command line tool for profiling backend model', usage=profile_usage)
80 oneutils.add_default_arg(parser)
82 # get backend list in the directory
83 backends_name = [ntpath.basename(f) for f in backends_list]
85 backends_name_message = '(There is no available backend drivers)'
87 backends_name_message = '(available backend drivers: ' + ', '.join(
89 backend_help_message = 'backend name to use ' + backends_name_message
90 parser.add_argument('-b', '--backend', type=str, help=backend_help_message)
95 def _verify_arg(parser, args):
96 """verify given arguments"""
97 # check if required arguments is given
99 if not oneutils.is_valid_attr(args, 'backend'):
100 missing.append('-b/--backend')
102 parser.error('the following arguments are required: ' + ' '.join(missing))
105 def _parse_arg(parser):
109 argv = copy.deepcopy(sys.argv)
113 args = [list(y) for x, y in itertools.groupby(argv, lambda z: z == '--') if not x]
115 profile_args = parser.parse_args(profile_args)
116 # one-profile has two interfaces
117 # 1. one-profile [-h] [-v] [-C CONFIG] [-b BACKEND] [COMMANDS FOR BACKEND]
119 profile_args = args[0]
120 profile_args, unknown_args = parser.parse_known_args(profile_args)
121 # 2. one-profile [-h] [-v] [-C CONFIG] [-b BACKEND] -- [COMMANDS FOR BACKEND]
123 profile_args = args[0]
124 backend_args = args[1]
125 profile_args = parser.parse_args(profile_args)
127 if len(args) and profile_args.version:
128 oneutils.print_version_and_exit(__file__)
130 return profile_args, backend_args, unknown_args
135 backends_list = _get_backends_list()
138 parser = _get_parser(backends_list)
139 args, backend_args, unknown_args = _parse_arg(parser)
141 # parse configuration file
142 oneutils.parse_cfg(args.config, 'one-profile', args)
145 _verify_arg(parser, args)
147 # make a command to run given backend driver
149 backend_base = getattr(args, 'backend') + '-profile'
150 for cand in backends_list:
151 if ntpath.basename(cand) == backend_base:
154 raise FileNotFoundError(backend_base + ' not found')
155 profile_cmd = [profile_path] + backend_args + unknown_args
156 if oneutils.is_valid_attr(args, 'command'):
157 profile_cmd += getattr(args, 'command').split()
160 oneutils.run(profile_cmd, err_prefix=backend_base)
163 if __name__ == '__main__':