1#!/usr/bin/env python3 2 3import os 4import sys 5import subprocess 6import datetime 7import shutil 8import glob 9 10gparent = os.path.expandvars('$GDRIVE_DIR') 11grefresh_token = os.path.expandvars('$GDRIVE_REFRESH_TOKEN') 12 13branch = os.path.expandvars('$GITHUB_REF') 14commit = os.path.expandvars('$GITHUB_SHA') 15build_number = os.path.expandvars('$GITHUB_RUN_NUMBER') 16 17if sys.platform.lower().startswith('darwin'): 18 origfilename = './bin/Sigil.tar.xz' 19 newfilename = './bin/Sigil-{}-{}-build_num-{}.tar.xz'.format(branch.split('/')[-1], commit[:7], build_number) 20else: 21 names = glob.glob('.\\installer\\Sigil-*-Setup.exe') 22 if not names: 23 exit(1) 24 origfilename = names[0] 25 newfilename = '.\\installer\\Sigil-{}-{}-build_num-{}-Setup.exe'.format(branch.split('/')[-1], commit[:7], build_number) 26 27shutil.copy2(origfilename, newfilename) 28 29folder_name = datetime.date.today() 30list_command = ['gdrive', 31 '--refresh-token', 32 '{}'.format(grefresh_token), 33 'list', 34 '--no-header', 35 '--query', 36 'trashed = false and mimeType = \'application/vnd.google-apps.folder\' and \'{}\' in parents and name = \'{}\''.format(gparent, folder_name), 37 ] 38list_proc = subprocess.run(list_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) 39 40if list_proc.returncode == 0 and len(list_proc.stdout): 41 gparent = list_proc.stdout.split()[0] 42else: 43 mk_command = ['gdrive', 44 '--refresh-token', 45 '{}'.format(grefresh_token), 46 'mkdir', 47 '--parent', 48 '{}'.format(gparent), 49 '{}'.format(folder_name), 50 ] 51 mk_proc = subprocess.run(mk_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) 52 53 if mk_proc.returncode == 0 and mk_proc.stdout.strip(): 54 print('Created new \'{}\' folder'.format(folder_name)) 55 gparent = mk_proc.stdout.split()[1] 56 57up_command = ['gdrive', 58 '--refresh-token', 59 '{}'.format(grefresh_token), 60 'upload', 61 '--parent', 62 '{}'.format(gparent), 63 '{}'.format(newfilename) 64 ] 65up_proc = subprocess.run(up_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) 66 67info = None 68if up_proc.returncode == 0: 69 print('Uploaded {} to \'{}\' folder'.format(newfilename, folder_name)) 70 info = up_proc.stdout.splitlines()[1].split()[1] 71 72if info is not None: 73 inf_command = ['gdrive', 74 '--refresh-token', 75 '{}'.format(grefresh_token), 76 'info', 77 '{}'.format(info), 78 ] 79 inf_proc = subprocess.run(inf_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) 80 if inf_proc.returncode == 0: 81 inf_dic = {k.split(':')[0].strip():k.split(': ')[1] for k in inf_proc.stdout.splitlines()} 82 print('Download {} from {}'.format(newfilename, inf_dic["DownloadUrl"])) 83 os.remove(newfilename) 84else: 85 os.remove(newfilename) 86 exit(1) 87 88