This repository has been archived by the owner on Dec 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
install-cfg
135 lines (127 loc) · 4.04 KB
/
install-cfg
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
134
135
#!/usr/bin/python
# INTENDED TO BE EXECUTED WITH `python install-cfg src dest verbose`
import os
import sys
import curses # Used by the std cfg.
def scan_cfg(src_cfg, dest_cfg):
'''Does `src_cfg` have something new that `dest_cfg` doesn't?'''
for key in src_cfg:
if key not in dest_cfg:
return True
if isinstance(src_cfg[key], dict):
if not isinstance(dest_cfg[key], dict):
return True
if scan_cfg(src_cfg[key], dest_cfg[key]):
return True
# Tuple check doesn't work.
if isinstance(src_cfg[key], tuple):
try:
if len(src_cfg[key]) != len(dest_cfg[key]):
return True
except:
return True
return False
def decide(src, dest, verbose=True):
'''
Decide what needs to be done to install the configuration file
from `src` to `dest`.
Returns
'install' if the `dest` should be overwritten with `src`.
'fail' if it can't be installed automatically.
'skip' if it should not be installed/overwritten.
'''
try:
src_cfg = eval(open(src).read())
except:
sys.stderr.write(
"\nERROR:\n"
"Cannot load the source configuration files.\n"
"\n"
)
return 'fail'
# Install the file if it hasn't already been installed.
try:
dest_file = open(dest)
except IOError:
return 'install'
try:
content = dest_file.read()
except:
sys.stderr.write(
"\nERROR:\n"
"Cannot open the pre-existing configuration file.\n"
"\n"
)
# Install if file has not been modified.
magic = '#@not-modified@\n'
if '\n'+magic in content or content.startswith(magic):
if verbose:
sys.stderr.write(
"\nINFORMATIONAL:\n"
"The already existing configuration file is at its default\n"
"and will therefore be overwritten to install any changes.\n"
"\n"
)
return 'install'
# Check if the pre-existing configuration is usable.
try:
dest_cfg = eval(content)
except:
sys.stderr.write(
"\nERROR:\n"
"Cannot parse the pre-existing configuration.\n"
"\n"
)
return 'fail'
dest_file.close()
if scan_cfg(src_cfg, dest_cfg):
sys.stderr.write(
"\nNOTICE:\n"
"The format of the configuration file has been changed in this\n"
"version. The configuration file must be modified manually.\n"
"\n"
)
return 'fail'
else:
if verbose:
sys.stderr.write(
"\nINFORMATIONAL:\n"
"The altered configuration file should be usable\n"
"by the newly installed version of the program.\n"
"\n"
)
return 'skip'
def main():
if len(sys.argv) != 5:
sys.stderr.write(
"Usage: python install-cfg install-cmd src dest verbose\n"
)
sys.exit(1)
install_cmd, src, dest = sys.argv[1], sys.argv[2], sys.argv[3]
if sys.argv[4] == 'true':
verbose = True
elif sys.argv[4] == 'false':
verbose = False
else:
verbose = True
sys.stderr.write(
'The <verbose> parameter must be "true" or "false".\n'
)
status = decide(src, dest, verbose)
if status == 'fail':
sys.stderr.write("Error: Cannot install automatically.\n")
sys.stderr.write(
'"{0}" is incompatible and must be edited\n'.format(dest)
)
sys.stderr.write(
'Look at "{0}" to know what changes must be made.\n'.format(src)
)
sys.exit(1)
elif status == 'install':
os.system('{0} -m 644 "{1}" "{2}"'.format(install_cmd, src, dest))
elif status == 'skip':
pass
else:
sys.stderr.write('Internal error!\n')
if __name__ == '__main__':
main()