-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinstall.py
111 lines (91 loc) · 3.65 KB
/
install.py
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
#coding: utf-8
#!/usr/bin/env python
import os, sys
import argparse
import platform
import shutil
import subprocess
import shelve
install_log = "install.log"
local_path = os.path.dirname(os.path.abspath(__file__))
package_path = os.path.join(local_path, "osms")
class Install(object):
def install(self, dst=None):
self.install_dir = dst
self.check_platform()
self.check_requires()
if not os.path.exists(package_path):
print "Error: main package does not exist!"
sys.exit(1)
if not os.path.exists(self.install_dir):
print "Notice: %s does not exist, will be created." % self.install_dir
try:
os.makedirs(self.install_dir)
except:
print "Error: error occured while mkdir: %s , install program quit now." % self.install_dir
sys.exit(1)
shutil.rmtree(self.install_dir, onerror=self.raise_error)
shutil.copytree(local_path, self.install_dir)
self.record_log(self.install_dir, action="put")
def uninstall(self):
install_dir = self.record_log(action="get")
shutil.rmtree(install_dir, onerror=self.raise_error)
print "osms has been removed from your system."
def raise_error(self, listdir_obj, path, exc_info):
raise exc_info[1]
def check_platform(self):
if not sys.platform.startswith("linux"):
print "Error: osms can only run in linux platform."
sys.exit(1)
dist = platform.dist()[0]
if not dist in ("Ubuntu"):
print "Notice: there may be some problem in %s" % dist
def check_requires(self):
pass
def execute_command(self, command):
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
process.wait()
returncode = process.returncode
if returncode == 1:
return False
return True
def record_log(self, content=None, action=None):
try:
db = shelve.open(install_log, flag='c')
except:
print "error ocurred while create log file."
return False
if action == "put" and len(content) > 0:
try:
db['install'] = content
db.close()
except:
print "error ocurred while store log."
return False
else:
return True
elif action == "get":
try:
return db['install']
except:
print "error ocurred while get log."
return False
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="osms install script")
exclusive_group = parser.add_mutually_exclusive_group(required=False)
exclusive_group.add_argument('--install', action="store_true",
dest='install', default=False, help="install osms to your system")
exclusive_group.add_argument('--uninstall', action='store_true',
dest='uninstall', default=False, help="uninstall osms from your system")
parser.add_argument('--install-dir', action='store', default="/usr/local/osms",
dest='install_dir', help="install directory, default is /usr/local/osms")
sysargs = sys.argv[1:]
args = parser.parse_args(args=sysargs)
if len(sysargs) < 1:
parser.print_help()
else:
install_handler = Install()
if args.install:
install_handler.install(args.install_dir)
elif args.uninstall:
install_handler.uninstall()