Skip to content

Commit f1835e1

Browse files
committed
Adding suggestions on the PR
1 parent b121a11 commit f1835e1

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

generate.py

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import toml
22
import sys
33
import os
4+
import logging
5+
import errno
46

57
methods = {
68
"apt": "$APT_GET",
79
"yum": "$YUM",
810
"dnf": "$DNF",
911
"apk": "$APK",
10-
"pacman": "$PACMAN"
12+
"pacman": "$PACMAN",
13+
"git": "$GIT"
1114
}
1215

1316
def get_method_case(method):
14-
return "[ ! -z "+methods[method]+"_CMD ]; then\n"
17+
if method in methods:
18+
return "[ ! -z "+methods[method]+"_CMD ]; then\n"
19+
else:
20+
logging.error('Unpupported method in the TOML file, method: '+method)
21+
exit(1)
1522

1623
def generate(path):
1724

@@ -20,39 +27,49 @@ def generate(path):
2027

2128
installer_toml = open(installer_toml_path, "r")
2229
parsed_toml = toml.loads(installer_toml.read())
30+
try:
31+
with open(installer_sh_path, "w") as installer_sh:
2332

24-
installer_sh = open(installer_sh_path, "w")
33+
installer_sh.write("""#!/bin/sh
34+
35+
YUM_CMD=$(which yum) # yum package manager for RHEL & CentOS
36+
DNF_CMD=$(which dnf) # dnf package manager for new RHEL & CentOS
37+
APT_GET_CMD=$(which apt-get) # apt package manager for Ubuntu & other Debian based distributions
38+
PACMAN_CMD=$(which pacman) # pacman package manager for ArchLinux
39+
APK_CMD=$(which apk) # apk package manager for Alpine
40+
GIT_CMD=$(which git) # to build from source pulling from git
2541
26-
installer_sh.write("""#!/bin/sh
27-
28-
YUM_CMD=$(which yum) # yum package manager for RHEL & CentOS
29-
DNF_CMD=$(which dnf) # dnf package manager for new RHEL & CentOS
30-
APT_GET_CMD=$(which apt-get) # apt package manager for Ubuntu & other Debian based distributions
31-
PACMAN_CMD=$(which pacman) # pacman package manager for ArchLinux
32-
APK_CMD=$(which apk) # apk package manager for Alpine
33-
34-
""")
42+
""")
3543

36-
seperator = "if"
44+
seperator = "if"
3745

38-
for section in parsed_toml:
39-
lines = parsed_toml[section]['sh']
40-
installer_sh.write(seperator+" "+get_method_case(section))
41-
for line in lines.split("\n"):
42-
installer_sh.write(" "+line+"\n")
43-
seperator = "elif"
46+
for section in parsed_toml:
47+
lines = parsed_toml[section]['sh']
48+
installer_sh.write(seperator+" "+get_method_case(section))
49+
for line in lines.split("\n"):
50+
installer_sh.write(" "+line+"\n")
51+
seperator = "elif"
4452

45-
installer_sh.write("""
46-
else
47-
echo "Couldn't install package"
48-
exit 1;
49-
fi
50-
""".strip())
53+
installer_sh.write("""
54+
else
55+
echo "Couldn't install package"
56+
exit 1;
57+
fi
58+
""".strip())
5159

60+
installer_sh.close()
5261

53-
installer_sh.close()
54-
62+
except IOError as x:
63+
if x.errno == errno.EACCES:
64+
logging.error('No enough permissions to write to '+installer_sh_path)
65+
exit(1)
66+
else:
67+
logging.error('Something went wrong when trying to write to '+installer_sh_path)
68+
exit(1)
5569

5670
for path in sys.argv[1:]:
5771
if os.path.exists(path+'/installer.toml'):
72+
logging.info('Generating installer.sh for '+path)
5873
generate(path)
74+
else:
75+
logging.warn('Could not find an installer.toml in '+path)

0 commit comments

Comments
 (0)