-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_pySIMsalabim.py
133 lines (114 loc) · 4.46 KB
/
get_pySIMsalabim.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
import sys, subprocess, shutil, os, requests, zipfile, io
from pathlib import Path
######### Parameter Definitions ###################################################################
cwd = Path.cwd()
folder_name = 'kostergroup-pySIMsalabim-'
###########Function Definitions ###################################################################
def cmd_yes_no_question(question, default = "yes"):
"""_summary_
Parameters
----------
question : str
question to ask the user
default : str, optional
default answer, by default "yes"
Returns
-------
bool
whether the default answer is valid or not
Raises
------
ValueError
If the default answer is not valid
"""
# valid answers (yes/no)
valid = {'yes' : True, 'y': True, 'ye': True, 'no': False, 'n': False}
# Set the default answer.
if default is None:
prompt = " [y/n] "
elif default == 'yes':
prompt = " [Y/n] "
elif default == 'no':
prompt = " [y/N] "
else:
raise ValueError(f"Invalid default answer: {default}\n")
while True:
# Capture user input
sys.stdout.write(question + prompt)
choice = input()
# convert the input to lowercase
choice = choice.lower()
if default is not None and choice == "":
# Use default value
return valid[default]
elif choice in valid:
# Use user input
return valid[choice]
else:
# Incorrect input
sys.stdout.write('Please respond with "y" or "n"\n')
def get_pySIMsalabim_source(cwd, folder_name='kostergroup-pySIMsalabim-',verbose=False):
""" Get the latest release from the Kostergroup Github
Parameters
----------
cwd : string
Current working directory
folder_name : string, optional
Name of the folder to download, by default 'kostergroup-pySIMsalabim-'
verbose : bool, optional
Print verbose output, by default False
Returns
-------
int
0 : Success
2 : Failed
3 : Failed
"""
if verbose:
print("Getting the latest release from the Kostergroup Github")
# Get the SIMsalabim source code.
if os.path.exists(os.path.join(cwd, 'pySIMsalabim')):
# Pop out dialog box to confirm overwriting
result = cmd_yes_no_question("Do you want to replace the existing pySIMsalabim folder?")
if result == True:
# Remove folder
shutil.rmtree(os.path.join(cwd, 'pySIMsalabim'))
# # Get the files from the latest release
url = 'https://api.github.com/repos/kostergroup/pySIMsalabim/zipball'
response = requests.get(url)
# Open the zip file
z = zipfile.ZipFile(io.BytesIO(response.content))
# Extract all the files
z.extractall(path=cwd)
for dirpath, dirnames, files in os.walk(cwd):
for dirname in dirnames:
if dirname.startswith(folder_name):
# Rename folder
shutil.move(os.path.join(cwd, dirname, 'pySIMsalabim'), os.path.join(cwd, 'pySIMsalabim'))
shutil.rmtree(os.path.join(cwd, dirname))
print("\nGot the latest release of pySIMsalabim")
return 0
else:
print('We keep the current pySIMsalabim version')
return 0
else:
# # Get the files from the latest release
url = 'https://api.github.com/repos/kostergroup/pySIMsalabim/zipball'
response = requests.get(url)
# Open the zip file
z = zipfile.ZipFile(io.BytesIO(response.content))
# Extract all the files
z.extractall(path=cwd)
for dirpath, dirnames, files in os.walk(cwd):
for dirname in dirnames:
if dirname.startswith(folder_name):
# print(f"Found a folder named {dirname}")
# Rename folder
shutil.move(os.path.join(cwd, dirname,'pySIMsalabim'), os.path.join(cwd, 'pySIMsalabim'))
shutil.rmtree(os.path.join(cwd, dirname))
print("\nGot the latest release of pySIMsalabim")
return 0
######### Script ##################################################################################
# Get pySIMsalabim source code
result_pySIMsalabim = get_pySIMsalabim_source(cwd, folder_name)
sys.exit(result_pySIMsalabim)