4 # Copyright (c) 2021 Project CHIP Authors
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
29 def __init__(self, outputName, outputPrefix, workingDirectory):
30 self.outputName = outputName + '.tar.xz'
31 self.outputPrefix = outputPrefix
32 self.workingDirectory = workingDirectory
34 logging.info('Creating bundle "%s":', self.outputName)
36 self.output = tarfile.open(self.outputName, 'w:xz')
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)
42 current_directory = os.path.realpath(os.curdir)
44 os.chdir(self.workingDirectory)
45 self.output.add(name, os.path.join(self.outputPrefix, name))
47 os.chdir(current_directory)
50 """Closes the bundle and returns the file name of the bundle."""
51 logging.info(' Bundle creation complete.')
53 return self.outputName
57 """Main task if executed standalone."""
58 parser = argparse.ArgumentParser(
59 description='Uploads an asset bundle file to a github release .')
63 help='Github API token to upload the report as a comment')
65 '--github-repository', type=str, help='Repository to use for PR comments')
67 '--release-tag', type=str, help='Release tag to upload asset to')
71 help='A file containing what assets to include')
73 '--working-directory',
75 help='What directory to use as the current directory for uploading')
77 '--bundle-name', type=str, help='Prefix to use in the archive file')
81 type=lambda x: getattr(logging, x),
82 help='Configure the logging level.')
83 args = parser.parse_args()
85 # Ensures somewhat pretty logging of what is going on
88 format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
91 if not args.github_api_token:
92 logging.error('Required arguments missing: github api token is required')
95 bundle = BundleBuilder(args.bundle_name, args.bundle_name,
96 args.working_directory)
98 with open(args.bundle_files, 'rt') as bundleInputs:
99 for fileName in bundleInputs.readlines():
100 bundle.appendFile(fileName.strip())
102 assetPath = bundle.close()
104 api = github.Github(args.github_api_token)
105 repo = api.get_repo(args.github_repository)
107 logging.info('Connected to github repository')
109 release = repo.get_release(args.release_tag)
110 logging.info('Release "%s" found.' % args.release_tag)
112 logging.info('Uploading %s', assetPath)
113 release.upload_asset(assetPath)
114 logging.info('Asset upload complete')
117 if __name__ == '__main__':
118 # execute only if run as a script