Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / scripts / helpers / upload_release_asset.py
1 #!/usr/bin/env python3
2
3 #
4 # Copyright (c) 2021 Project CHIP Authors
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19 import argparse
20 import coloredlogs
21 import logging
22 import tarfile
23 import github
24 import os
25
26
27 class BundleBuilder:
28
29   def __init__(self, outputName, outputPrefix, workingDirectory):
30     self.outputName = outputName + '.tar.xz'
31     self.outputPrefix = outputPrefix
32     self.workingDirectory = workingDirectory
33
34     logging.info('Creating bundle "%s":', self.outputName)
35
36     self.output = tarfile.open(self.outputName, 'w:xz')
37
38   def appendFile(self, name):
39     """Appends the specified file in the working directory to the bundle."""
40     logging.info('   Appending %s to the bundle', name)
41
42     current_directory = os.path.realpath(os.curdir)
43     try:
44       os.chdir(self.workingDirectory)
45       self.output.add(name, os.path.join(self.outputPrefix, name))
46     finally:
47       os.chdir(current_directory)
48
49   def close(self):
50     """Closes the bundle and returns the file name of the bundle."""
51     logging.info('   Bundle creation complete.')
52     self.output.close()
53     return self.outputName
54
55
56 def main():
57   """Main task if executed standalone."""
58   parser = argparse.ArgumentParser(
59       description='Uploads an asset bundle file to a github release .')
60   parser.add_argument(
61       '--github-api-token',
62       type=str,
63       help='Github API token to upload the report as a comment')
64   parser.add_argument(
65       '--github-repository', type=str, help='Repository to use for PR comments')
66   parser.add_argument(
67       '--release-tag', type=str, help='Release tag to upload asset to')
68   parser.add_argument(
69       '--bundle-files',
70       type=str,
71       help='A file containing what assets to include')
72   parser.add_argument(
73       '--working-directory',
74       type=str,
75       help='What directory to use as the current directory for uploading')
76   parser.add_argument(
77       '--bundle-name', type=str, help='Prefix to use in the archive file')
78   parser.add_argument(
79       '--log-level',
80       default=logging.INFO,
81       type=lambda x: getattr(logging, x),
82       help='Configure the logging level.')
83   args = parser.parse_args()
84
85   # Ensures somewhat pretty logging of what is going on
86   logging.basicConfig(
87       level=args.log_level,
88       format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
89   coloredlogs.install()
90
91   if not args.github_api_token:
92     logging.error('Required arguments missing: github api token is required')
93     return
94
95   bundle = BundleBuilder(args.bundle_name, args.bundle_name,
96                          args.working_directory)
97
98   with open(args.bundle_files, 'rt') as bundleInputs:
99     for fileName in bundleInputs.readlines():
100       bundle.appendFile(fileName.strip())
101
102   assetPath = bundle.close()
103
104   api = github.Github(args.github_api_token)
105   repo = api.get_repo(args.github_repository)
106
107   logging.info('Connected to github repository')
108
109   release = repo.get_release(args.release_tag)
110   logging.info('Release "%s" found.' % args.release_tag)
111
112   logging.info('Uploading %s', assetPath)
113   release.upload_asset(assetPath)
114   logging.info('Asset upload complete')
115
116
117 if __name__ == '__main__':
118   # execute only if run as a script
119   main()