3 # Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
26 def __init__(self, token=None, timeout=None):
28 self.headers = {"Authorization": "Bearer {}".format(token)}
31 self.timeout = timeout or 5
33 def make_request(self, url):
35 response = requests.get(url, headers=self.headers, timeout=self.timeout)
36 response.raise_for_status()
38 except requests.RequestException as e:
39 raise SystemExit('[onecc-docker] error: {}'.format(e))
42 # 5 sec timeout is set based on github.com/Samsung/ONE/issues/11134
43 def _request_recent_version(token=None):
44 response = RequestHandler(
46 timeout=5).make_request(url="https://api.github.com/repos/Samsung/ONE/releases")
47 versions = [release_item["tag_name"] for release_item in response.json()]
49 for version in versions:
50 # Return the latest version with the given format
51 # to filter out such as 'onert-micro-0.1.0' release
52 # which doesn't contain onecc package.
53 if bool(re.match(r'^\d+\.\d+\.\d+$', version)):
56 raise SystemExit('[onecc-docker] Failed to get latest onecc version')
59 # 10 sec timeout is set based on github.com/Samsung/ONE/issues/11134
60 def _run(cmd, is_shell=False, timeout=10):
61 result = subprocess.Popen(
62 cmd, shell=is_shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
64 stdout, stderr = result.communicate(timeout=timeout)
65 stdout = stdout.decode('utf-8')
66 stderr = stderr.decode('utf-8')
70 exit(result.returncode)
75 def _image_exists(name):
76 cmd = ['docker', 'images', '-q', name]
77 lines = _run(cmd).splitlines()
82 script_path = os.path.dirname(os.path.realpath(__file__))
83 dockerfile_path = os.path.join(script_path, 'docker')
85 onecc_docker_usage = 'onecc-docker [-h] [-t TOKEN] [COMMAND <args>]'
86 onecc_docker_desc = 'Run onecc via docker'
87 parser = argparse.ArgumentParser(
88 usage=onecc_docker_usage, description=onecc_docker_desc)
93 "Token for authentication to GitHub. This is a workaround for Rate limit exceeded error"
96 args, onecc_arguments = parser.parse_known_args()
97 authorization_token = args.token
99 recent_version = _request_recent_version(authorization_token)
100 image_name = f"onecc:{recent_version}"
101 build_arg = f"VERSION={recent_version}"
103 if not _image_exists(image_name):
105 "docker", "build", "-t", image_name, "--build-arg", build_arg, dockerfile_path
107 print('[onecc-docker] Build docker image ...')
108 _run(build_cmd, timeout=30)
109 print('[onecc-docker] Docker image is built successfully.')
111 contianer_name = f"onecc_{recent_version.replace('.','_')}"
112 user_cmd = ' '.join(onecc_arguments)
115 "docker", "run", "--rm", "-u", "$(id -u):$(id -g)", "--name", contianer_name,
116 "-v", "${HOME}:${HOME}", "-e", "HOME=${HOME}", "-w", "${PWD}", image_name,
120 cmd = ' '.join(run_cmd)
121 output = _run(cmd, is_shell=True)
122 print(output, end='')
125 if __name__ == "__main__":
128 except Exception as e:
129 prog_name = os.path.basename(__file__)
130 print(f"{prog_name}: {type(e).__name__}: " + str(e), file=sys.stderr)