-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.py
187 lines (153 loc) · 5.93 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
# coding=utf-8
# !/usr/bin/env python
from setuptools import setup, Command
import sys
import versioneer
DESCRIPTION = "A daemon for controlling the LEDs of Mr Beam II"
LONG_DESCRIPTION = """mrbeam_ledstrips is a small Python daemon for reading
the machine state of Mr Beam II over a unix domain socket.
"""
EXTRAS_FOLDERS = [
('/etc/mrbeam_ledstrips.conf.d', 0o755),
('/usr/share/mrbeam_ledstrips/png', 0o755)
]
EXTRAS_FILES = [
('/etc/', [('extras/mrbeam_ledstrips.yaml', 'mrbeam_ledstrips.yaml', 0o600)]),
('/usr/share/mrbeam_ledstrips/png/', [('extras/png', 0o644)]),
('/etc/logrotate.d/', [('extras/mrbeam_ledstrips.logrotate', 'mrbeam_ledstrips', 0o644)]),
('/lib/systemd/system/', [('extras/mrbeam_ledstrips.unit', 'mrbeam_ledstrips.service', 0o644)])
]
def get_extra_tuple(entry):
import os
if isinstance(entry, (tuple, list)):
if len(entry) == 2:
path, mode = entry
filename = os.path.basename(path)
elif len(entry) == 3:
path, filename, mode = entry
elif len(entry) == 1:
path = entry[0]
filename = os.path.basename(path)
mode = None
else:
return None
else:
path = entry
filename = os.path.basename(path)
mode = None
return path, filename, mode
class InstallExtrasCommand(Command):
description = "install extras like init scripts and config files"
user_options = [("force", "F", "force overwriting files if they already exist")]
def initialize_options(self):
self.force = None
def finalize_options(self):
if self.force is None:
self.force = False
def run(self):
global EXTRAS_FILES, EXTRAS_FOLDERS
import shutil
import os
# TODO enable service by running "sudo systemctl enable mrbeam_ledstrips.service" or symlinking
for folder, mode in EXTRAS_FOLDERS:
try:
if os.path.exists(folder):
os.chmod(folder, mode)
else:
os.mkdir(folder, mode)
except Exception as e:
print(("Error while creating %s (%s), aborting" % (folder, e.message)))
sys.exit(-1)
for target, files in EXTRAS_FILES:
for entry in files:
extra_tuple = get_extra_tuple(entry)
if extra_tuple is None:
print(("Can't parse entry for target %s, skipping it: %r" % (target, entry)))
continue
path, filename, mode = extra_tuple
target_path = os.path.join(target, filename)
path_exists = os.path.exists(target_path)
if path_exists and not self.force:
print(("Skipping copying %s to %s as it already exists, use --force to overwrite" % (
path, target_path)))
continue
try:
shutil.copy(path, target_path)
if mode:
os.chmod(target_path, mode)
print(("Copied %s to %s and changed mode to %o" % (path, target_path, mode)))
else:
print(("Copied %s to %s" % (path, target_path)))
except Exception as e:
if not path_exists and os.path.exists(target_path):
# we'll try to clean up again
try:
os.remove(target_path)
except:
pass
print(("Error while copying %s to %s (%s), aborting" % (path, target_path, e.message)))
sys.exit(-1)
class UninstallExtrasCommand(Command):
description = "uninstall extras like init scripts and config files"
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
global EXTRAS_FILES, EXTRAS_FOLDERS
import os
for target, files in EXTRAS_FILES:
for entry in files:
extra_tuple = get_extra_tuple(entry)
if extra_tuple is None:
print(("Can't parse entry for target %s, skipping it: %r" % (target, entry)))
path, filename, mode = extra_tuple
target_path = os.path.join(target, filename)
try:
os.remove(target_path)
print(("Removed %s" % target_path))
except Exception as e:
print(("Error while deleting %s from %s (%s), please remove manually" % (
filename, target, e.message)))
for folder, mode in EXTRAS_FOLDERS[::-1]:
try:
os.rmdir(folder)
except Exception as e:
print(("Error while removing %s (%s), please remove manually" % (folder, e.message)))
def get_cmdclass():
"""
get the versioneer cmd class and extra commands
Returns:
cmdclass
"""
cmdclass = versioneer.get_cmdclass()
cmdclass.update({
'install_extras': InstallExtrasCommand,
'uninstall_extras': UninstallExtrasCommand
})
return cmdclass
install_requires = ["PyYaml", ]
if sys.version_info >= (3, 0):
install_requires += ["rpi-ws281x; platform_machine=='armv7l'", ]
setup(
name="mrbeam_ledstrips",
version=versioneer.get_version(),
cmdclass=get_cmdclass(),
description=DESCRIPTION,
long_description=LONG_DESCRIPTION,
author="Teja Philipp",
author_email="teja@mr-beam.org",
url="http://github.com/mrbeam/mrbeam_ledstrips",
license="GPLV3",
packages=["mrbeam_ledstrips"],
zip_safe=False,
dependency_links=[],
install_requires=install_requires,
entry_points={
"console_scripts": {
"mrbeam_ledstrips = mrbeam_ledstrips:server",
"mrbeam_ledstrips_cli = mrbeam_ledstrips:client"
}
},
)