-
Notifications
You must be signed in to change notification settings - Fork 15
/
setup.py
193 lines (144 loc) · 5.45 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
#! /usr/share/env python3
# -*- coding:utf-8 -*-
# This file is a part of IoT-LAB gateway_code
# Copyright (C) 2015 INRIA (Contact: [email protected])
# Contributor(s) : see AUTHORS file
#
# This software is governed by the CeCILL license under French law
# and abiding by the rules of distribution of free software. You can use,
# modify and/ or redistribute the software under the terms of the CeCILL
# license as circulated by CEA, CNRS and INRIA at the following URL
# http://www.cecill.info.
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms.
"""setup.py deployement script.
Install all the `gateway code` on a gateway
python setup.py release
It runs the `install` command and the `post_install` procedure.
Tests commands:
python setup.py nosetests
python setup.py integration
Pylint and pep8 checker:
python setup.py lint
python setup.py pep8
"""
import sys
import os
import subprocess
import shutil
from glob import glob
from distutils.command.install import install # pylint:disable=W4901
from setuptools import setup, Command, find_packages
PACKAGE = 'gateway_code'
# GPL compatible http://www.gnu.org/licenses/license-list.html#CeCILL
LICENSE = 'CeCILL v2.1'
def get_version(package):
"""Extract package version without importing file.
Importing cause issues with coverage,
(modules can be removed from sys.modules to prevent this)
Importing __init__.py triggers importing rest and then requests too
Inspired from pep8 setup.py
"""
version = '-1'
with open(os.path.join(package, '__init__.py')) as init_fd:
for line in init_fd:
if line.startswith('__version__'):
version = eval(line.split('=')[-1]) # pylint:disable=eval-used
break
return version
SCRIPTS = glob('bin/scripts/*')
INSTALL_REQUIRES = ['argparse', 'bottle', 'paste', 'pyserial']
INSTALL_REQUIRES += ['pyelftools']
if sys.version_info[0] < 3:
# Python3 backports of subprocess, support 'timeout' option
INSTALL_REQUIRES += ['subprocess32']
UDEV_RULES = glob('bin/rules.d/*.rules')
def simple_command(function):
"""Return a simple command without options."""
class SimpleCommand(Command):
"""Command without options."""
user_options = []
def initialize_options(self):
"""Initialize options."""
pass # pylint:disable=unnecessary-pass
def finalize_options(self):
"""Finalize options."""
pass # pylint:disable=unnecessary-pass
def run(self):
"""Run function with or without self argument."""
try:
execute(self, function, [self])
except TypeError:
execute(self, function)
return SimpleCommand
def execute(self, function, args=()):
"""Run distutils execute function with args and auto-doc."""
msg = function.__doc__.splitlines()[0]
# pylint:disable=consider-using-f-string
msg = 'running %s: %s' % (function.__name__, msg)
self.execute(function, args, msg)
def post_install(self):
"""System configuration.
* install init.d gateway server daemon script
* install init.d gateway camera streamer daemon script
* install init.d gateway rtl sdr daemon script
* install udev rules files
* Add www-data user to dialout group
"""
execute(self, setup_initd_script, args=('gateway-server-daemon',))
execute(self, udev_rules)
execute(self, add_www_data_to_dialout)
def setup_initd_script(init_script):
"""Setup an init.d script."""
update_rc_d_args = [
'update-rc.d', init_script,
'start', '85', '2', '3', '4', '5', '.',
'stop', '15', '0', '1', '6', '.'
]
shutil.copy('bin/init_script/' + init_script, '/etc/init.d/')
os.chmod('/etc/init.d/' + init_script, 0o755)
subprocess.check_call(update_rc_d_args)
def udev_rules():
"""Install udev rules files."""
for rule in UDEV_RULES:
shutil.copy(rule, '/etc/udev/rules.d/')
subprocess.check_call(['udevadm', 'control', '--reload'])
def add_www_data_to_dialout():
"""Add `www-data` user to `dialout` group."""
subprocess.check_call(['usermod', '-a', '-G', 'dialout', 'www-data'])
class Release(install):
"""Install and do the 'post installation' procedure too.
Meant to be used directly on the gateways
"""
def run(self):
"""Run `install` and `post_install`."""
install.run(self)
execute(self, post_install, [self])
PACKAGE_DATA = {
'static': ['static/*'],
}
setup(name=PACKAGE,
version=get_version(PACKAGE),
description='Linux Gateway code',
long_description="Linux Gateway code",
author='IoT-Lab Team',
author_email='[email protected]',
url='http://www.iot-lab.info',
license=LICENSE,
packages=find_packages(),
scripts=SCRIPTS,
include_package_data=True,
package_data=PACKAGE_DATA,
cmdclass={
'release': Release,
'post_install': simple_command(post_install),
'udev_rules_install': simple_command(udev_rules),
},
install_requires=INSTALL_REQUIRES)