forked from rbonghi/jetson_stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
245 lines (218 loc) · 9.41 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# -*- coding: UTF-8 -*-
# This file is part of the jetson_stats package (https://github.com/rbonghi/jetson_stats or http://rnext.it).
# Copyright (c) 2019 Raffaello Bonghi.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Reference:
# 1. https://packaging.python.org
# 2. https://julien.danjou.info/starting-your-first-python-project/
# 3. https://medium.com/@trstringer/the-easy-and-nice-way-to-do-cli-apps-in-python-5d9964dc950d
# 4. https://chriswarrick.com/blog/2014/09/15/python-apps-the-right-way-entry_points-and-scripts/
# 5. https://python-packaging.readthedocs.io
# 6. https://github.com/pypa/sampleproject
# 7. https://pypi.org/classifiers/
# 8. https://stackoverflow.com/questions/20288711/post-install-script-with-python-setuptools
# Always prefer setuptools over distutils
from setuptools import setup, find_packages
from setuptools.command.develop import develop
from setuptools.command.install import install
from jtop.service import import_jetson_variables
# io.open is needed for projects that support Python 2.7
# It ensures open() defaults to text mode with universal newlines,
# and accepts an argument to specify the text encoding
# Python 3 only projects can skip this import
from io import open
# Launch command
import subprocess as sp
import shlex
import os
from shutil import copyfile
import sys
import re
import logging
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
log = logging.getLogger()
# https://stackoverflow.com/questions/1871549/determine-if-python-is-running-inside-virtualenv
def is_virtualenv():
if os.path.exists(os.path.join(sys.prefix, 'conda-meta')):
# Conda virtual environments
return True
if hasattr(sys, 'real_prefix'):
return True
if hasattr(sys, 'base_prefix'):
return sys.prefix != sys.base_prefix
return False
def is_superuser():
return os.getuid() == 0
def list_scripts():
JETSONS = import_jetson_variables()
# Load scripts to install
scripts = ['scripts/jetson_swap', 'scripts/jetson_release', 'scripts/jetson_config']
# If jetpack lower than 32 install also jetson_docker
l4t_release = JETSONS['JETSON_L4T_RELEASE']
if l4t_release.isdigit():
if int(l4t_release) < 32:
scripts += ['scripts/jetson_docker']
return scripts
def list_services():
return ["services/{file}".format(file=f) for f in os.listdir("services") if os.path.isfile(os.path.join("services", f))]
here = os.path.abspath(os.path.dirname(__file__))
project_homepage = "https://github.com/rbonghi/jetson_stats"
documentation_homepage = "https://rbonghi.github.io/jetson_stats"
# Get the long description from the README file
with open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
# Load version package
with open(os.path.join(here, "jtop", "__init__.py")) as fp:
VERSION = (
re.compile(r""".*__version__ = ["'](.*?)['"]""", re.S).match(fp.read()).group(1)
)
# Store version package
version = VERSION
def install_services(copy=False):
"""
This function install all services in a proper folder and setup the deamons
"""
print("System prefix {prefix}".format(prefix=sys.prefix))
# Make jetson stats folder
root = sys.prefix + "/local/jetson_stats/"
if not os.path.exists(root):
os.makedirs(root)
# Copy all files
for f_service in list_services():
folder, _ = os.path.split(__file__)
path = root + os.path.basename(f_service)
# remove if exist file
if os.path.exists(path):
os.remove(path)
# Copy or link file
if copy:
type_service = "Copying"
copyfile(folder + "/" + f_service, path)
else:
type_service = "Linking"
os.symlink(folder + "/" + f_service, path)
# Prompt message
print("{type} {file} -> {path}".format(type=type_service, file=os.path.basename(f_service), path=path))
def pre_installer(installer, obj, copy):
js_is_active = os.system('systemctl is-active --quiet jetson_stats') == 0
# Run the uninstaller before to copy all scripts
if not is_virtualenv():
if is_superuser():
sp.call(shlex.split('./scripts/jetson_config --uninstall'))
# Install services (linking only for develop)
install_services(copy=copy)
else:
print("----------------------------------------")
print("Install on your host using superuser permission, like:")
print("sudo -H pip install -U jetson-stats")
sys.exit(1)
else:
if is_superuser():
print("Skip uninstall on virtual environment")
elif not js_is_active:
print("----------------------------------------")
print("Please, before install in your virtual environment, install jetson-stats on your host with superuser permission, like:")
print("sudo -H pip install -U jetson-stats")
sys.exit(1)
# Run the default installation script
installer.run(obj)
# Run the restart all services before to close the installer
if not is_virtualenv() and is_superuser():
sp.call(shlex.split('./scripts/jetson_config --install'))
else:
print("Skip install on virtual environment")
class PostInstallCommand(install):
"""Installation mode."""
def run(self):
# Run the uninstaller before to copy all scripts
pre_installer(install, self, True)
class PostDevelopCommand(develop):
"""Post-installation for development mode."""
def run(self):
# Run the uninstaller before to copy all scripts
# Install services (linking)
pre_installer(develop, self, False)
# Configuration setup module
setup(
name="jetson-stats",
version=version,
author="Raffaello Bonghi",
author_email="raffaello@rnext.it",
description="Interactive system-monitor and process viewer for all NVIDIA Jetson [Xavier NX, Nano, AGX Xavier, TX1, TX2]",
license='AGPL-3.0',
long_description=long_description,
long_description_content_type="text/markdown",
url=project_homepage,
download_url=(project_homepage + "/archive/master.zip"),
project_urls={
"How To": documentation_homepage,
"Examples": (project_homepage + "/tree/master/examples"),
"Bug Reports": (project_homepage + "/issues"),
"Source": (project_homepage + "/tree/master")
},
packages=find_packages(exclude=['examples', 'scripts', 'tests', 'jtop.tests']), # Required
# Load jetson_variables
package_data={"jtop": ["jetson_variables", "jetson_libraries"]},
# Define research keywords
keywords=("jetson_stats jtop python system-monitor docker \
nvidia Jetson XavierNX Nano Xavier TX2 TX1 process viewer"
),
classifiers=[
"Development Status :: 5 - Production/Stable",
# Audience and topics
"Intended Audience :: Developers",
"Topic :: Software Development :: Embedded Systems",
"Topic :: Software Development :: Debuggers",
"Topic :: Software Development :: Libraries",
"Topic :: Software Development :: User Interfaces",
"Topic :: System :: Hardware",
"Topic :: System :: Logging",
"Topic :: System :: Monitoring",
"Topic :: System :: Operating System",
"Topic :: System :: Operating System Kernels",
"Topic :: System :: Shells",
"Topic :: System :: Systems Administration",
"Topic :: Terminals",
# License
"License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)",
# Programming and Operative system
"Programming Language :: Unix Shell",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Operating System :: POSIX :: Linux"],
# Requisites
# https://packaging.python.org/guides/distributing-packages-using-setuptools/#python-requires
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4',
platforms=["linux", "linux2", "darwin"],
# Zip safe configuration
# https://setuptools.readthedocs.io/en/latest/setuptools.html#setting-the-zip-safe-flag
zip_safe=False,
# Add jetson_variables in /opt/jetson_stats
# http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files
data_files=[('jetson_stats', list_services())],
# Install extra scripts
scripts=list_scripts(),
cmdclass={'develop': PostDevelopCommand,
'install': PostInstallCommand},
# The following provide a command called `jtop`
entry_points={'console_scripts': ['jtop=jtop.__main__:main']},
)
# EOF