1
1
import toml
2
2
import sys
3
3
import os
4
+ import logging
5
+ import errno
4
6
5
7
methods = {
6
8
"apt" : "$APT_GET" ,
7
9
"yum" : "$YUM" ,
8
10
"dnf" : "$DNF" ,
9
11
"apk" : "$APK" ,
10
- "pacman" : "$PACMAN"
12
+ "pacman" : "$PACMAN" ,
13
+ "git" : "$GIT"
11
14
}
12
15
13
16
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 )
15
22
16
23
def generate (path ):
17
24
@@ -20,39 +27,49 @@ def generate(path):
20
27
21
28
installer_toml = open (installer_toml_path , "r" )
22
29
parsed_toml = toml .loads (installer_toml .read ())
30
+ try :
31
+ with open (installer_sh_path , "w" ) as installer_sh :
23
32
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
25
41
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
+ """ )
35
43
36
- seperator = "if"
44
+ seperator = "if"
37
45
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"
44
52
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 ())
51
59
60
+ installer_sh .close ()
52
61
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 )
55
69
56
70
for path in sys .argv [1 :]:
57
71
if os .path .exists (path + '/installer.toml' ):
72
+ logging .info ('Generating installer.sh for ' + path )
58
73
generate (path )
74
+ else :
75
+ logging .warn ('Could not find an installer.toml in ' + path )
0 commit comments