7
7
import re
8
8
9
9
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"
17
17
}
18
+
18
19
def update_readme (summary ):
19
20
writer = pytablewriter .MarkdownTableWriter ()
20
21
writer .headers = ["Tool" , "Apt" , "Yum" , "Packman" , "APK" , "DNF" , "CURL" , "URL" ]
@@ -29,7 +30,7 @@ def update_readme(summary):
29
30
apk = "Yes" if "apk" in installers else "No"
30
31
dnf = "Yes" if "dnf" in installers else "No"
31
32
curl = "Yes" if "curl" in installers else "No"
32
- url = "https://installer.to/" + tool_shortname
33
+ url = "https://installer.to/" + tool_shortname
33
34
value_matrix .append ([name , apt , yum , pacman , apk , dnf , curl , url ])
34
35
35
36
writer .value_matrix = value_matrix
@@ -39,14 +40,14 @@ def update_readme(summary):
39
40
readme = readme_md .read ()
40
41
beggining = "<!-- beginning of tools list -->"
41
42
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 )
44
45
readme_md .seek (0 ) # sets point at the beginning of the file
45
46
readme_md .truncate () # Clear previous content
46
47
readme_md .write (readme )
47
48
readme_md .close ()
48
49
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 )
50
51
51
52
52
53
def update_summary (name , shortname , description , installers ):
@@ -67,43 +68,40 @@ def update_summary(name, shortname, description, installers):
67
68
68
69
update_readme (parsed_summary_toml )
69
70
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 )
71
72
72
73
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
+
78
80
79
81
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' ) \
85
87
.replace ('@error' , 'error' )
86
88
return line
87
89
88
- def generate (path ):
89
- installer_methods = [ ]
90
- installer_toml_path = path + "/installer.toml"
91
- installer_sh_path = path + "/installer.sh"
92
-
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
90
98
- installer_sh . write ( """#!/bin/sh
99
-
91
+ def write_sniffer_commands ( sh_file ):
92
+ sh_file . write ( """
100
93
CURL_CMD=$(which curl)
101
94
YUM_CMD=$(which yum)
102
95
DNF_CMD=$(which dnf)
103
96
APT_GET_CMD=$(which apt-get)
104
97
PACMAN_CMD=$(which pacman)
105
98
APK_CMD=$(which apk)
106
99
GIT_CMD=$(which git)
100
+ """ )
101
+
102
+
103
+ def write_sudo_fix_commands (sh_file ):
104
+ sh_file .write ("""
107
105
SUDO_CMD=$(which sudo)
108
106
109
107
USER="$(id -un 2>/dev/null || true)"
@@ -119,7 +117,11 @@ def generate(path):
119
117
exit 1
120
118
fi
121
119
fi
120
+ """ )
122
121
122
+
123
+ def write_logger_commands (sh_file ):
124
+ sh_file .write ("""
123
125
RESET='\033 [0m'
124
126
RED='\033 [0;31m'
125
127
GREEN='\033 [0;32m'
@@ -139,42 +141,82 @@ def generate(path):
139
141
140
142
""" )
141
143
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
+ installer_sh .write ("""#!/bin/sh
155
+ """ )
156
+ write_sudo_fix_commands (installer_sh )
157
+ write_logger_commands (installer_sh )
158
+ write_installer_commands (installer_sh , lines )
159
+
160
+ except IOError as x :
161
+ if x .errno == errno .EACCES :
162
+ logging .error ('No enough permissions to write to ' + installer_sh_path )
163
+ exit (1 )
164
+ else :
165
+ logging .error ('Something went wrong when trying to write to ' + installer_sh_path , x )
166
+ exit (1 )
167
+
168
+ def generate (path ):
169
+ installer_methods = []
170
+ installer_toml_path = path + "/installer.toml"
171
+ installer_sh_path = path + "/installer.sh"
172
+
173
+ installer_toml = open (installer_toml_path , "r" )
174
+ parsed_toml = toml .loads (installer_toml .read ())
175
+ try :
176
+ with open (installer_sh_path , "w" ) as installer_sh :
177
+
178
+ installer_sh .write ("""#!/bin/sh
179
+
180
+ """ )
181
+ write_sniffer_commands (installer_sh )
182
+ write_sudo_fix_commands (installer_sh )
183
+ write_logger_commands (installer_sh )
184
+
185
+ seperator = "if"
186
+
187
+ for section in parsed_toml :
188
+ if not isinstance (parsed_toml [section ], dict ):
189
+ continue
190
+ if parsed_toml [section ]['sh' ] is "" :
191
+ continue
192
+ installer_methods .append (section )
193
+ lines = parsed_toml [section ]['sh' ]
194
+ installer_sh .write (seperator + " " + get_method_case (section ))
195
+ write_installer_commands (installer_sh , lines , " " )
196
+ generate_individual_installers (section , lines )
197
+ seperator = "elif"
198
+
199
+ installer_sh .write ("""
158
200
else
159
201
echo "Couldn't install package"
160
202
exit 1;
161
203
fi
162
- """ .strip ())
204
+ """ .strip ())
163
205
164
- installer_sh .close ()
165
- update_summary (parsed_toml ['name' ], parsed_toml ['shortname' ], parsed_toml ['description' ], installer_methods )
206
+ installer_sh .close ()
207
+ update_summary (parsed_toml ['name' ], parsed_toml ['shortname' ], parsed_toml ['description' ], installer_methods )
166
208
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 )
209
+ except IOError as x :
210
+ if x .errno == errno .EACCES :
211
+ logging .error ('No enough permissions to write to ' + installer_sh_path )
212
+ exit (1 )
213
+ else :
214
+ logging .error ('Something went wrong when trying to write to ' + installer_sh_path , x )
215
+ exit (1 )
174
216
175
217
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 )
218
+ if os .path .exists (path + '/installer.toml' ):
219
+ logging .info ('Generating installer.sh for ' + path )
220
+ generate (path )
221
+ else :
222
+ logging .warn ('Could not find an installer.toml in ' + path )
0 commit comments