forked from spencershepard/RotorOps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release_script.py
145 lines (102 loc) · 4.65 KB
/
release_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import datetime
import ftplib
import os
import sys
import json
import yaml
from Generator import version
if __name__ == "__main__":
# change working directory to the directory of this
os.chdir(os.path.dirname(os.path.abspath(__file__)))
#get unix epoch seconds string via datetime
timestamp = str(int(datetime.datetime.now().timestamp()))
updatescript_bkup = "updatescript.ini.bkup" + timestamp
versioncheck_bkup = "versioncheck.yaml.bkup" + timestamp
if not os.getenv("FTP_SERVER") or not os.getenv("FTP_USERNAME") or not os.getenv("FTP_PASSWORD"):
print("FTP_SERVER, FTP_USERNAME, and FTP_PASSWORD environment variables must be set.")
sys.exit(1)
# connect to the update server
ftp = ftplib.FTP(os.getenv("FTP_SERVER"))
# login to the update server
try:
ftp.login(os.getenv("FTP_USERNAME"), os.getenv("FTP_PASSWORD"))
except ftplib.error_perm:
print("Login failed. Check your username and password.")
sys.exit(1)
except ftplib.error_temp:
print("Login failed due to temp error. Check your connection settings.")
sys.exit(1)
# list files in the current directory on the update server
print("Files in the current remote directory: ")
files = ftp.nlst()
print(files)
#download the updatescript.ini file
with open("updatescript.ini", "wb") as f:
ftp.retrbinary("RETR updatescript.ini", f.write)
with open(updatescript_bkup, "wb") as f:
ftp.retrbinary("RETR updatescript.ini", f.write)
#download the versioncheck.yaml file
with open("versioncheck.yaml", "wb") as f:
ftp.retrbinary("RETR versioncheck.yaml", f.write)
with open(versioncheck_bkup, "wb") as f:
ftp.retrbinary("RETR versioncheck.yaml", f.write)
# load yaml file and check previous version
with open('versioncheck.yaml') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
# get version from yaml file
version_from_file = data['version']
if version_from_file == version.version_string:
# throw error
print("Version is the same as the current version. Increment the version number in version.py and try again.")
sys.exit(1)
changed_files = json.loads(os.getenv("changed_files"))
if changed_files:
print("Changed files: " + str(changed_files))
# open updatescript.ini for read/write
with open("updatescript.ini", "r") as f:
file_text = f.read()
# get the contents of the first block starting with 'releases{' and ending with '}'
releases_str = file_text[file_text.find('releases{') + 9:file_text.find('}')].strip()
# split the releases into a list of releases
# remove spaces
releases_str = releases_str.replace(" ", "")
releases = releases_str.split('\n')
for r in releases:
if r == version.version_string:
print("Version already exists in updatescript.ini")
sys.exit(1)
with open("updatescript.ini", "w") as f:
#add the newest release to the bottom of the list
releases.append(version.version_string)
# remove text before first '}'
file_text = file_text[file_text.find('}') + 1:]
# write the releases block back to the file
f.write('releases{\n ' + '\n '.join(releases) + '\n}\n')
#write the old releases back to the file
f.write(file_text)
# write the new release to the file
f.write("\nrelease:" + version.version_string + "{")
for file in changed_files:
remote_path = 'continuous/' + file
subdir = None
#if file has a subdir, get the subdir
if file.rfind("/") != -1:
subdir = file[:file.rfind("/")]
file = file[file.rfind("/") + 1:]
f.write("\n DownloadFile:" + remote_path)
f.write("," + subdir + "/")
else:
f.write("\n DownloadFile:" + remote_path)
f.write("\n}")
f.close()
# create new versioncheck.yaml file
with open('versioncheck.yaml', 'w') as f:
f.write("title: \"Update Available\"\n")
f.write("description: \"UPDATE AVAILABLE: Please run the included updater utility (RotorOps_updater.exe) to get the latest version.\"" + "\n")
f.write("version: \"" + version.version_string + "\"\n")
#upload the new files to the update server
files = [updatescript_bkup, versioncheck_bkup, 'updatescript.ini', 'versioncheck.yaml']
for file in files:
ftp.storbinary("STOR " + file, open(file, "rb"))
print("Uploaded " + file)
ftp.quit()