Skip to content

Commit cd6a167

Browse files
committed
Generating platform specific installers
1 parent b735a82 commit cd6a167

File tree

1 file changed

+104
-64
lines changed

1 file changed

+104
-64
lines changed

generate.py

Lines changed: 104 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
import re
88

99
methods = {
10-
"curl": "$CURL",
11-
"apt": "$APT_GET",
12-
"yum": "$YUM",
13-
"dnf": "$DNF",
14-
"apk": "$APK",
15-
"pacman": "$PACMAN",
16-
"git": "$GIT"
10+
"curl": "$CURL",
11+
"apt": "$APT_GET",
12+
"yum": "$YUM",
13+
"dnf": "$DNF",
14+
"apk": "$APK",
15+
"pacman": "$PACMAN",
16+
"git": "$GIT"
1717
}
18+
1819
def update_readme(summary):
1920
writer = pytablewriter.MarkdownTableWriter()
2021
writer.headers = ["Tool", "Apt", "Yum", "Packman", "APK", "DNF", "CURL", "URL"]
@@ -29,7 +30,7 @@ def update_readme(summary):
2930
apk = "Yes" if "apk" in installers else "No"
3031
dnf = "Yes" if "dnf" in installers else "No"
3132
curl = "Yes" if "curl" in installers else "No"
32-
url = "https://installer.to/"+tool_shortname
33+
url = "https://installer.to/" + tool_shortname
3334
value_matrix.append([name, apt, yum, pacman, apk, dnf, curl, url])
3435

3536
writer.value_matrix = value_matrix
@@ -39,14 +40,14 @@ def update_readme(summary):
3940
readme = readme_md.read()
4041
beggining = "<!-- beginning of tools list -->"
4142
end = "<!-- end of tools list -->"
42-
regex = r""+beggining+"\n(.*)\n"+end
43-
readme = re.sub(regex, beggining+"\n"+table_md+"\n"+end, readme, flags=re.S)
43+
regex = r"" + beggining + "\n(.*)\n" + end
44+
readme = re.sub(regex, beggining + "\n" + table_md + "\n" + end, readme, flags=re.S)
4445
readme_md.seek(0) # sets point at the beginning of the file
4546
readme_md.truncate() # Clear previous content
4647
readme_md.write(readme)
4748
readme_md.close()
4849
except Error as e:
49-
logging.error('Error occurred when trying to update README.md, error: '+ e)
50+
logging.error('Error occurred when trying to update README.md, error: ' + e)
5051

5152

5253
def update_summary(name, shortname, description, installers):
@@ -67,43 +68,40 @@ def update_summary(name, shortname, description, installers):
6768

6869
update_readme(parsed_summary_toml)
6970
except IOError as e:
70-
logging.error('Error occurred when trying to update installers.toml, error: '+ e)
71+
logging.error('Error occurred when trying to update installers.toml, error: ' + e)
7172

7273
def get_method_case(method):
73-
if method in methods:
74-
return "[ ! -z "+methods[method]+"_CMD ]; then\n"
75-
else:
76-
logging.error('Unpupported method in the TOML file, method: '+method)
77-
exit(1)
74+
if method in methods:
75+
return "[ ! -z " + methods[method] + "_CMD ]; then\n"
76+
else:
77+
logging.error('Unpupported method in the TOML file, method: ' + method)
78+
exit(1)
79+
7880

7981
def parse_line(line):
80-
line = line\
81-
.replace('@sudo', '$SUDO')\
82-
.replace('@log', 'info')\
83-
.replace('@info', 'info')\
84-
.replace('@warn', 'warn')\
82+
line = line \
83+
.replace('@sudo', '$SUDO') \
84+
.replace('@log', 'info') \
85+
.replace('@info', 'info') \
86+
.replace('@warn', 'warn') \
8587
.replace('@error', 'error')
8688
return line
8789

88-
def generate(path):
89-
installer_methods = [ ]
90-
installer_toml_path = path+"/installer.toml"
91-
installer_sh_path = path+"/installer.sh"
9290

93-
installer_toml = open(installer_toml_path, "r")
94-
parsed_toml = toml.loads(installer_toml.read())
95-
try:
96-
with open(installer_sh_path, "w") as installer_sh:
97-
98-
installer_sh.write("""#!/bin/sh
99-
91+
def write_sniffer_commands(sh_file):
92+
sh_file.write("""
10093
CURL_CMD=$(which curl)
10194
YUM_CMD=$(which yum)
10295
DNF_CMD=$(which dnf)
10396
APT_GET_CMD=$(which apt-get)
10497
PACMAN_CMD=$(which pacman)
10598
APK_CMD=$(which apk)
10699
GIT_CMD=$(which git)
100+
""")
101+
102+
103+
def write_sudo_fix_commands(sh_file):
104+
sh_file.write("""
107105
SUDO_CMD=$(which sudo)
108106
109107
USER="$(id -un 2>/dev/null || true)"
@@ -119,7 +117,11 @@ def generate(path):
119117
exit 1
120118
fi
121119
fi
120+
""")
121+
122122

123+
def write_logger_commands(sh_file):
124+
sh_file.write("""
123125
RESET='\033[0m'
124126
RED='\033[0;31m'
125127
GREEN='\033[0;32m'
@@ -139,42 +141,80 @@ def generate(path):
139141
140142
""")
141143

142-
seperator = "if"
143-
144-
for section in parsed_toml:
145-
if not isinstance(parsed_toml[section], dict):
146-
continue
147-
if parsed_toml[section]['sh'] is "":
148-
continue
149-
installer_methods.append(section)
150-
lines = parsed_toml[section]['sh']
151-
installer_sh.write(seperator+" "+get_method_case(section))
152-
for line in lines.split("\n"):
153-
step = parse_line(line)
154-
installer_sh.write(" "+step+"\n")
155-
seperator = "elif"
156-
157-
installer_sh.write("""
144+
145+
def write_installer_commands(sh_file, lines, indent=""):
146+
for line in lines.split("\n"):
147+
step = parse_line(line)
148+
sh_file.write(indent + step + "\n")
149+
150+
def generate_individual_installers(method, lines):
151+
installer_sh_path = path + "/installer."+method+".sh"
152+
try:
153+
with open(installer_sh_path, "w") as installer_sh:
154+
write_sudo_fix_commands(installer_sh)
155+
write_logger_commands(installer_sh)
156+
write_installer_commands(installer_sh, lines)
157+
158+
except IOError as x:
159+
if x.errno == errno.EACCES:
160+
logging.error('No enough permissions to write to ' + installer_sh_path)
161+
exit(1)
162+
else:
163+
logging.error('Something went wrong when trying to write to ' + installer_sh_path, x)
164+
exit(1)
165+
166+
def generate(path):
167+
installer_methods = []
168+
installer_toml_path = path + "/installer.toml"
169+
installer_sh_path = path + "/installer.sh"
170+
171+
installer_toml = open(installer_toml_path, "r")
172+
parsed_toml = toml.loads(installer_toml.read())
173+
try:
174+
with open(installer_sh_path, "w") as installer_sh:
175+
176+
installer_sh.write("""#!/bin/sh
177+
178+
""")
179+
write_sniffer_commands(installer_sh)
180+
write_sudo_fix_commands(installer_sh)
181+
write_logger_commands(installer_sh)
182+
183+
seperator = "if"
184+
185+
for section in parsed_toml:
186+
if not isinstance(parsed_toml[section], dict):
187+
continue
188+
if parsed_toml[section]['sh'] is "":
189+
continue
190+
installer_methods.append(section)
191+
lines = parsed_toml[section]['sh']
192+
installer_sh.write(seperator + " " + get_method_case(section))
193+
write_installer_commands(installer_sh, lines, " ")
194+
generate_individual_installers(section, lines)
195+
seperator = "elif"
196+
197+
installer_sh.write("""
158198
else
159199
echo "Couldn't install package"
160200
exit 1;
161201
fi
162-
""".strip())
202+
""".strip())
163203

164-
installer_sh.close()
165-
update_summary(parsed_toml['name'], parsed_toml['shortname'], parsed_toml['description'], installer_methods)
204+
installer_sh.close()
205+
update_summary(parsed_toml['name'], parsed_toml['shortname'], parsed_toml['description'], installer_methods)
166206

167-
except IOError as x:
168-
if x.errno == errno.EACCES:
169-
logging.error('No enough permissions to write to '+installer_sh_path)
170-
exit(1)
171-
else:
172-
logging.error('Something went wrong when trying to write to '+installer_sh_path)
173-
exit(1)
207+
except IOError as x:
208+
if x.errno == errno.EACCES:
209+
logging.error('No enough permissions to write to ' + installer_sh_path)
210+
exit(1)
211+
else:
212+
logging.error('Something went wrong when trying to write to ' + installer_sh_path, x)
213+
exit(1)
174214

175215
for path in sys.argv[1:]:
176-
if os.path.exists(path+'/installer.toml'):
177-
logging.info('Generating installer.sh for '+path)
178-
generate(path)
179-
else:
180-
logging.warn('Could not find an installer.toml in '+path)
216+
if os.path.exists(path + '/installer.toml'):
217+
logging.info('Generating installer.sh for ' + path)
218+
generate(path)
219+
else:
220+
logging.warn('Could not find an installer.toml in ' + path)

0 commit comments

Comments
 (0)