Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
240 lines (203 loc) · 8.76 KB
/
Copy pathsetup.py
File metadata and controls
240 lines (203 loc) · 8.76 KB
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
#!/usr/bin/env python3
# -*- 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-2026 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/>.
"""
Minimal setup.py for backward compatibility and custom install commands.
Main configuration is in pyproject.toml (PEP 517/518/621 compliant).
"""
from setuptools import setup
from setuptools.command.develop import develop
from setuptools.command.install import install
from setuptools.command.build_py import build_py
import os
import sys
import shutil
import subprocess as sp_mod
import logging
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
log = logging.getLogger()
def is_virtualenv():
# Check if in virtual environment
has_real_prefix = hasattr(sys, 'real_prefix')
has_base_prefix = (
hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix
)
return bool(has_real_prefix or has_base_prefix)
def is_docker():
# Check common container marker files
if os.path.exists('/.dockerenv'):
return True
if os.path.exists('/run/.containerenv'):
return True
# Check container environment variable (set by various runtimes)
if os.environ.get('container'):
return True
# Check cgroup v1: look for docker/buildkit/containerd in cgroup paths
path = '/proc/self/cgroup'
if os.path.isfile(path):
with open(path) as f:
for line in f:
if 'docker' in line or 'buildkit' in line or 'containerd' in line:
return True
# Check cgroup v2 + overlay/BuildKit: inspect mountinfo for container signals
mountinfo = '/proc/self/mountinfo'
if os.path.isfile(mountinfo):
with open(mountinfo) as f:
for line in f:
if '/docker/' in line or '/buildkit/' in line or '/containerd/' in line:
return True
# Fallback: if systemctl is not available, treat as container-like environment
if not shutil.which('systemctl'):
return True
return False
def is_superuser():
return os.getuid() == 0
def _systemctl(*args):
"""Run systemctl if available, otherwise log and skip."""
if shutil.which('systemctl'):
return sp_mod.call(['systemctl'] + list(args))
log.warning("systemctl not found, skipping: systemctl %s", ' '.join(args))
return 1
def _run_service_install(source_folder):
"""Install jtop system service and config files using only stdlib.
This is used by the build_py override so it works inside isolated
PEP 517 build environments where jtop's runtime dependencies
(smbus2, distro, …) are *not* available.
"""
service_src = os.path.join(source_folder, 'services', 'jtop.service')
service_dst = '/etc/systemd/system/jtop.service'
env_src = os.path.join(source_folder, 'scripts', 'jtop_env.sh')
env_dst = '/etc/profile.d/jtop_env.sh'
pipe_path = '/run/jtop.sock'
# --- Uninstall previous service ---
if os.path.isfile(service_dst) or os.path.islink(service_dst):
_systemctl('stop', 'jtop.service')
_systemctl('disable', 'jtop.service')
os.remove(service_dst)
_systemctl('daemon-reload')
if os.path.isdir(pipe_path):
shutil.rmtree(pipe_path)
elif os.path.exists(pipe_path):
os.remove(pipe_path)
if os.path.isfile(env_dst):
os.remove(env_dst)
# --- Install service file ---
if os.path.isfile(service_src):
shutil.copy2(service_src, service_dst)
log.info("Installed %s -> %s", service_src, service_dst)
_systemctl('daemon-reload')
_systemctl('enable', 'jtop.service')
_systemctl('start', 'jtop.service')
# --- Install env script ---
if os.path.isfile(env_src):
shutil.copy2(env_src, env_dst)
log.info("Installed %s -> %s", env_src, env_dst)
# --- Set permissions ---
user = os.environ.get('SUDO_USER', '') or 'root'
sp_mod.call(['groupadd', 'jtop'])
sp_mod.call(['usermod', '-a', '-G', 'jtop', user])
class JTOPBuildPy(build_py):
"""Extend build_py to install system service during PEP 517 builds.
Modern pip always builds a wheel and then installs it, so the legacy
``install`` cmdclass never fires. By hooking into ``build_py`` (which
*is* executed during ``bdist_wheel``), we can install the systemd
service, env script, and group as part of ``sudo pip install``.
Note: we intentionally do NOT check ``is_virtualenv()`` here because
pip's PEP 517 isolated build environment is itself a temporary venv.
The ``is_superuser()`` gate is sufficient — if the user ran ``sudo``,
they want a system-level install.
"""
def run(self):
build_py.run(self)
if is_superuser() and not is_docker():
folder = os.path.dirname(os.path.realpath(__file__))
log.info("Installing jtop system service …")
_run_service_install(folder)
def pypi_installer(installer, obj, copy):
"""Main installation function for jtop services."""
# Import here to avoid import errors during build
try:
from jtop.service import status_service, remove_service_pipe, uninstall_service, set_service_permission, install_service
from jtop.core.jetson_variables import uninstall_variables, install_variables
from jtop.terminal_colors import bcolors
except ImportError:
# If imports fail, we're likely in build phase, so skip custom installation
installer.run(obj)
return
log.info("Install status:")
log.info(f" - [{'X' if is_superuser() else ' '}] super_user")
log.info(f" - [{'X' if is_virtualenv() else ' '}] virtualenv")
log.info(f" - [{'X' if is_docker() else ' '}] docker")
# Run the uninstaller before to copy all scripts
if not is_virtualenv() and not is_docker():
if is_superuser():
# remove service jtop.service
uninstall_service()
# Remove service path
remove_service_pipe()
# Uninstall variables
uninstall_variables()
else:
log.info("----------------------------------------")
log.info("Install on your host using superuser permission, like:")
log.info(bcolors.bold("sudo pip3 install -U jetson-stats"))
sys.exit(1)
elif is_docker():
log.info("Skip uninstall in docker")
else:
if is_superuser():
log.info("Skip uninstall on virtual environment")
elif not status_service():
log.info("----------------------------------------")
log.info("Please, before install in your virtual environment, install jetson-stats on your host with superuser permission, like:")
log.info(bcolors.bold("sudo pip3 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 not is_docker() and is_superuser():
folder, _ = os.path.split(os.path.realpath(__file__)) # This folder
# Install variables
install_variables(folder, copy=copy)
# Set service permissions
set_service_permission()
# Install service (linking only for develop)
install_service(folder, copy=copy)
else:
log.info("Skip install service")
class JTOPInstallCommand(install):
"""Custom installation command for production install."""
def run(self):
# Run the custom installer
pypi_installer(install, self, True)
class JTOPDevelopCommand(develop):
"""Custom installation command for development mode."""
def run(self):
# Run the custom installer with linking
pypi_installer(develop, self, False)
# Minimal setup() call - most configuration is in pyproject.toml
if __name__ == '__main__':
setup(
# Custom commands for backward compatibility
cmdclass={
'build_py': JTOPBuildPy,
'develop': JTOPDevelopCommand,
'install': JTOPInstallCommand,
},
# Include data files that need special installation
data_files=[('jetson_stats', ['services/jtop.service', 'scripts/jtop_env.sh'])],
)