upload_release_asset.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) 2021 Project CHIP Authors
  4. #
  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
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  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.
  16. #
  17. import argparse
  18. import logging
  19. import os
  20. import tarfile
  21. import coloredlogs
  22. import github
  23. class BundleBuilder:
  24. def __init__(self, outputName, outputPrefix, workingDirectory):
  25. self.outputName = outputName + '.tar.xz'
  26. self.outputPrefix = outputPrefix
  27. self.workingDirectory = workingDirectory
  28. logging.info('Creating bundle "%s":', self.outputName)
  29. self.output = tarfile.open(self.outputName, 'w:xz')
  30. def appendFile(self, name):
  31. """Appends the specified file in the working directory to the bundle."""
  32. logging.info(' Appending %s to the bundle', name)
  33. current_directory = os.path.realpath(os.curdir)
  34. try:
  35. os.chdir(self.workingDirectory)
  36. self.output.add(name, os.path.join(self.outputPrefix, name))
  37. finally:
  38. os.chdir(current_directory)
  39. def close(self):
  40. """Closes the bundle and returns the file name of the bundle."""
  41. logging.info(' Bundle creation complete.')
  42. self.output.close()
  43. return self.outputName
  44. def main():
  45. """Main task if executed standalone."""
  46. parser = argparse.ArgumentParser(
  47. description='Uploads an asset bundle file to a github release .')
  48. parser.add_argument(
  49. '--github-api-token',
  50. type=str,
  51. help='Github API token to upload the report as a comment')
  52. parser.add_argument(
  53. '--github-repository', type=str, help='Repository to use for PR comments')
  54. parser.add_argument(
  55. '--release-tag', type=str, help='Release tag to upload asset to')
  56. parser.add_argument(
  57. '--bundle-files',
  58. type=str,
  59. help='A file containing what assets to include')
  60. parser.add_argument(
  61. '--working-directory',
  62. type=str,
  63. help='What directory to use as the current directory for uploading')
  64. parser.add_argument(
  65. '--bundle-name', type=str, help='Prefix to use in the archive file')
  66. parser.add_argument(
  67. '--log-level',
  68. default=logging.INFO,
  69. type=lambda x: getattr(logging, x),
  70. help='Configure the logging level.')
  71. args = parser.parse_args()
  72. # Ensures somewhat pretty logging of what is going on
  73. logging.basicConfig(
  74. level=args.log_level,
  75. format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  76. coloredlogs.install()
  77. if not args.github_api_token:
  78. logging.error(
  79. 'Required arguments missing: github api token is required')
  80. return
  81. bundle = BundleBuilder(args.bundle_name, args.bundle_name,
  82. args.working_directory)
  83. with open(args.bundle_files, 'rt') as bundleInputs:
  84. for fileName in bundleInputs.readlines():
  85. bundle.appendFile(fileName.strip())
  86. assetPath = bundle.close()
  87. api = github.Github(args.github_api_token)
  88. repo = api.get_repo(args.github_repository)
  89. logging.info('Connected to github repository')
  90. release = repo.get_release(args.release_tag)
  91. logging.info('Release "%s" found.' % args.release_tag)
  92. logging.info('Uploading %s', assetPath)
  93. release.upload_asset(assetPath)
  94. logging.info('Asset upload complete')
  95. if __name__ == '__main__':
  96. # execute only if run as a script
  97. main()