Skip to content

Commit 12722e1

Browse files
committed
Delete release assets if they already exist
1 parent 3a36015 commit 12722e1

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

upload_release.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import requests
44
import json
5+
import os
56
from pprint import pprint
67
from glob import glob
78

@@ -27,23 +28,43 @@ def main():
2728

2829
password = getpass.getpass('Password:')
2930

31+
zip_files = glob('*.zip')
32+
file_names = set([os.path.basename(zip_file) for zip_file in zip_files])
33+
3034
if req.status_code == 200:
31-
print('Found release:', version)
35+
print('\nFound release:', version)
36+
3237
json_data = json.loads(req.text)
3338
tag = json_data.get('tag_name', '')
39+
3440
cur_ver = Version(tag[1:-1])
3541
new_ver = Version(version[1:-1])
42+
3643
if new_ver <= cur_ver:
3744
update = True
3845
rel_id = json_data['id']
46+
assets = json_data.get('assets', [])
47+
48+
for asset in assets:
49+
if asset['name'] in file_names:
50+
print('Found existing asset {}. Deleting...'.format(asset['name']))
51+
req = requests.delete(asset['url'], auth=(github_user, password))
52+
if req.status_code == 204:
53+
print('Delete success!')
54+
else:
55+
print('Delete failed!')
56+
print('Error:', req.text)
57+
3958
upload_url = json_data['upload_url'].replace('{?name,label}', '')
4059

4160
if not update:
42-
print('Creating release:', version)
61+
print('\nCreating release:', version)
4362
data = {'tag_name': version,
4463
'target_commitish': 'master',
4564
'name': 'Web2Executable ' + version}
65+
4666
post_res = requests.post(base_url, data=json.dumps(data), auth=(github_user, password))
67+
4768
if post_res.status_code == 201:
4869
json_data = json.loads(post_res.text)
4970
upload_url = json_data['upload_url'].replace('{?name,label}', '')
@@ -52,18 +73,27 @@ def main():
5273
print('Authentication failed!')
5374

5475
if rel_id:
55-
zip_files = glob('*.zip')
5676
for zip_file in zip_files:
5777
with open(zip_file, 'rb') as zipf:
5878
file_data = zipf.read()
59-
print('Uploading file {}...'.format(zip_file))
79+
80+
print('\nUploading file {}...'.format(zip_file))
81+
6082
data = {'name': zip_file}
6183
headers = {'Content-Type': 'application/zip'}
62-
r = requests.post(upload_url, params=data, data=file_data, headers=headers, auth=(github_user, password))
63-
if r.status_code == 201:
84+
85+
req = requests.post(
86+
upload_url,
87+
params=data,
88+
data=file_data,
89+
headers=headers,
90+
auth=(github_user, password)
91+
)
92+
93+
if req.status_code == 201:
6494
print('Success!')
6595
else:
66-
print('Error:', r.text)
96+
print('Error:', req.text)
6797

6898

6999
if __name__ == '__main__':

0 commit comments

Comments
 (0)