forked from Rimco/OSPy
-
Notifications
You must be signed in to change notification settings - Fork 5
/
setup.py
213 lines (173 loc) · 7.94 KB
/
setup.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'Rimco'
import sys
import os
import subprocess
import shutil
from ospy import i18n
from ospy.helpers import print_report
def yes_no(msg):
response = u''
while response.lower().strip() not in ['y', 'yes', 'n', 'no']:
response = input('{} [y/yes/n/no]\n'.format(msg))
return response.lower().strip() in ['y', 'yes']
def install_package(module, easy_install=None, package=None, pip=None, git=None, git_execs=None, zipfile=None, zip_cwd=None, zip_execs=None):
from ospy.helpers import del_rw
try:
print_report('setup.py', _('Checking {}').format(module))
__import__(module)
print_report('setup.py', _('{} is available').format(module))
return
except Exception:
if not yes_no(_('{} not available, do you want to install it?').format(module)):
return
done = False
if not done and easy_install is not None:
try:
subprocess.check_call([sys.executable, '-m', 'easy_install', easy_install])
done = True
except Exception as err:
print_report('setup.py', _('Failed to use easy_install:') + '{}'.format(err))
if sys.platform.startswith('linux'):
if not done and package is not None:
try:
subprocess.check_call(['apt-get', 'install', package])
done = True
except Exception as err:
print_report('setup.py', _('Failed to use apt-get:') + u'{}'.format(err))
if not done and package is not None:
try:
subprocess.check_call(['yum', 'install', package])
done = True
except Exception as err:
print_report('setup.py', _('Failed to use yum:') + '{}'.format(err))
if not done and pip is not None:
try:
subprocess.check_call([sys.executable, '-m', 'pip', 'install', pip])
done = True
except Exception as err:
print_report('setup.py', _('Failed to use pip:') + '{}'.format(err))
if not done and git is not None and git_execs is not None:
try:
shutil.rmtree('tmp', onerror=del_rw)
subprocess.check_call(['git', 'clone', git, 'tmp'])
for exc in git_execs:
subprocess.check_call(exc, cwd='tmp')
shutil.rmtree('tmp', onerror=del_rw)
done = True
except Exception as err:
print_report('setup.py', _('Failed to use git:') + '{}'.format(err))
if not done and zipfile is not None and zip_cwd is not None and zip_execs is not None:
try:
del_rw(None, 'tmp.zip', None)
shutil.rmtree('tmp', onerror=del_rw)
from urllib.request import urlretrieve
urlretrieve(zipfile, 'tmp.zip')
import zipfile
zipfile.ZipFile('tmp.zip').extractall('tmp')
del_rw(None, 'tmp.zip', None)
for exc in zip_execs:
subprocess.check_call(exc, cwd=os.path.join('tmp', zip_cwd))
shutil.rmtree('tmp', onerror=del_rw)
done = True
except Exception as err:
print_report('setup.py', _('Failed to use zip file:') + u'{}'.format(err))
if not done:
print_report('setup.py', _('Failed to install {}.').format(module))
def install_service():
my_dir = os.path.dirname(os.path.abspath(__file__))
path = os.path.join('/etc', 'init.d', 'ospy')
if sys.platform.startswith('linux'):
if yes_no(_('Do you want to install OSPy as a service?')):
with open(os.path.join(my_dir, 'service', 'ospy.sh')) as source:
with open(path, 'w') as target:
target.write(source.read().replace('{{OSPY_DIR}}', my_dir))
os.chmod(path, 0o755)
subprocess.check_call(['update-rc.d', 'ospy', 'defaults'])
print_report('setup.py', _('Done installing service.'))
else:
print_report('setup.py', _('Service installation is only possible on unix systems.'))
def uninstall_service():
if sys.platform.startswith('linux'):
if os.path.exists(os.path.join('/var', 'run', 'ospy.pid')):
try:
subprocess.Popen(['service', 'ospy', 'stop'])
except Exception:
print_report('setup.py', _('Could not stop service.'))
else:
print_report('setup.py', _('OSPy was not running.'))
try:
subprocess.Popen(['systemctl', 'daemon-reload'])
except Exception:
print_report('setup.py', _('Could not reload systemctl daemon.'))
try:
subprocess.check_call(['update-rc.d', '-f', 'ospy', 'remove'])
except Exception:
print_report('setup.py', _('Could not remove service using update-rc.d'))
import glob
old_paths = glob.glob(os.path.join('/etc', 'init.d', '*ospy*'))
for old_path in old_paths:
os.unlink(old_path)
if old_paths:
print_report('setup.py', _('Service removed.'))
else:
print_report('setup.py', _('Service not found.'))
else:
print_report('setup.py', _('Service uninstall is only possible on unix systems.'))
def start():
if yes_no(_('Do you want to start OSPy now?')):
if sys.platform.startswith('linux'):
try:
subprocess.Popen(['service', 'ospy', 'restart'])
except Exception:
subprocess.Popen([sys.executable, 'run.py'])
else:
subprocess.check_call(['cmd.exe', '/c', 'start', sys.executable, 'run.py'])
if __name__ == '__main__':
# On unix systems we need to be root:
if sys.platform.startswith('linux'):
if not os.getuid() == 0:
sys.exit(_('This script needs to be run as root.'))
if len(sys.argv) == 2 and sys.argv[1] == 'install':
pkg = False
try:
import setuptools
pkg = True
except ImportError:
if yes_no(_('Could not find setuptools which is needed to install packages, do you want to install it now?')):
from urllib.request import urlretrieve
from ospy.helpers import del_rw
shutil.rmtree('tmp', onerror=del_rw)
os.mkdir('tmp')
urlretrieve('https://bootstrap.pypa.io/ez_setup.py', 'tmp/ez_setup.py')
import subprocess
subprocess.check_call([sys.executable, 'ez_setup.py'], cwd='tmp')
shutil.rmtree('tmp', onerror=del_rw)
pkg = True
if not pkg:
print_report('setup.py', _('Cannot install packages without setuptools.'))
else:
# Check if packages are available:
# module, easy_install=None, package=None, pip=None,
# git=None, git_execs=None, zipfile=None, zip_cwd=None, zip_execs=None
install_package('web', 'web.py==0.62', 'python-webpy', 'web.py==0.62',
'https://github.com/webpy/webpy.git',
[[sys.executable, 'setup.py', 'install']],
'https://github.com/webpy/webpy/archive/master.zip', 'webpy-master',
[[sys.executable, 'setup.py', 'install']])
# https://pypi.org/project/cmarkgfm/
install_package('cmarkgfm', None, None, pip='cmarkgfm')
# https://pypi.org/project/requests/
install_package('requests', None, None, pip='requests')
# https://pypi.org/project/paho-mqtt/
install_package('paho-mqtt', None, None, pip='paho-mqtt')
install_service()
start()
print_report('setup.py', _('Done.'))
elif len(sys.argv) == 2 and sys.argv[1] == 'uninstall':
uninstall_service()
else:
sys.exit("Usage:\n"
"setup.py install: Interactive install.\n"
"setup.py uninstall: Removes the service if installed.")