Debian Codegraph @

build/common/upload-assets.py

raw
1 # Copyright (C) 2019 The Android Open Source Project
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from github import Github
16 import os, sys
17
18 def print_usage():
19 print('Upload assets to a Filament GitHub release.')
20 print()
21 print('Usage:')
22 print(' upload-assets.py <tag> <asset>...')
23 print()
24 print('Notes:')
25 print(' The GitHub release must already be created prior to running this script. This is typically done')
26 print(' through the GitHub web UI.')
27 print()
28 print(' <tag> is the Git tag for the desired release to attach assets to, for example, "v1.4.2".')
29 print()
30 print(' <asset> is a path to the asset file to upload. The file name will be used as the name of the')
31 print(' asset.')
32 print()
33 print(' The GITHUB_API_KEY environment variable must be set to a valid authentication token for a')
34 print(' collaborator account of the Filament repository.')
35
36 # The first argument is the path to this script.
37 if len(sys.argv) < 3:
38 print_usage()
39 sys.exit(1)
40
41 tag = sys.argv[1]
42 assets = sys.argv[2:]
43
44 authentication_token = os.environ.get('GITHUB_API_KEY')
45 if not authentication_token:
46 sys.stderr.write('Error: the GITHUB_API_KEY is not set.\n')
47 sys.exit(1)
48
49 g = Github(authentication_token)
50
51 FILAMENT_REPO = "google/filament"
52 filament = g.get_repo(FILAMENT_REPO)
53
54 def find_release_from_tag(repo, tag):
55 """ Find a release in the repo for the given Git tag string. """
56 releases = repo.get_releases()
57 for r in releases:
58 if r.tag_name == tag:
59 return r
60 return None
61
62 release = find_release_from_tag(filament, tag)
63 if not release:
64 sys.stderr.write(f"Error: Could not find release with tag '{tag}'.\n")
65 sys.exit(1)
66
67 print(f"Found release with tag '{tag}'.")
68
69 for asset_path in assets:
70 sys.stdout.write(f'Uploding asset: {asset_path}... ')
71 asset_name = os.path.basename(asset_path)
72 asset = release.upload_asset(asset_path, name=asset_name)
73 if asset:
74 sys.stdout.write('Success!\n')