forked from mlcommons/cm4mlops
-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup.py
125 lines (107 loc) · 4.48 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
# setup.py
from setuptools import setup
from setuptools._distutils.dist import Distribution
from setuptools.command.install import install
import subprocess
import sys
import importlib.util
import platform
import os
class CustomInstallCommand(install):
def run(self):
self.get_sys_platform()
self.install_system_packages()
# Call the standard run method
install.run(self)
# Call the custom function
return self.custom_function()
def install_system_packages(self):
# List of packages to install via system package manager
packages = []
git_status = self.command_exists('git')
if not git_status:
packages.append("git")
wget_status = self.command_exists('wget')
if not wget_status:
packages.append("wget")
curl_status = self.command_exists('curl')
if not curl_status:
packages.append("curl")
name='venv'
if name in sys.modules:
pass #nothing needed
elif (spec := importlib.util.find_spec(name)) is not None:
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
#print(f"{name} has been imported")
else:
packages.append("python3-venv")
if packages:
if self.system == 'Linux' or self.system == 'Darwin':
manager, details = self.get_package_manager_details()
if manager:
if manager == "apt-get":
subprocess.check_call(['sudo', 'apt-get', 'update'])
subprocess.check_call(['sudo', 'apt-get', 'install', '-y'] + packages)
elif self.system == 'Windows':
print(f"Please install the following packages manually: {packages}")
def detect_package_manager(self):
package_managers = {
'apt-get': '/usr/bin/apt-get',
'yum': '/usr/bin/yum',
'dnf': '/usr/bin/dnf',
'pacman': '/usr/bin/pacman',
'zypper': '/usr/bin/zypper',
'brew': '/usr/local/bin/brew'
}
for name, path in package_managers.items():
if os.path.exists(path):
return name
return None
def get_package_manager_details(self):
manager = self.detect_package_manager()
if manager:
try:
version_output = subprocess.check_output([manager, '--version'], stderr=subprocess.STDOUT).decode('utf-8')
return manager, version_output.split('\n')[0]
except subprocess.CalledProcessError:
return manager, 'Version information not available'
else:
return None, 'No supported package manager found'
# Checks if command exists(for installing required packages).
# If the command exists, which returns 0, making the function return True.
# If the command does not exist, which returns a non-zero value, making the function return False.
# NOTE: The standard output and standard error streams are redirected to PIPES so that it could be captured in future if needed.
def command_exists(self, command):
if self.system == "Linux" or self.system == 'Darwin':
return subprocess.call(['which', command], stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0
elif self.system == "Windows":
return subprocess.call([command, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) == 0
def custom_function(self):
import cmind
#r = cmind.access({'action':'rm', 'automation':'repo', 'data_uoa':'mlcommons@cm4mlops', 'force': True})
r = cmind.access({'action':'pull', 'automation':'repo', 'artifact':'mlcommons@cm4mlops', 'branch': 'mlperf-inference'})
print(r)
if r['return'] > 0:
return r['return']
def get_sys_platform(self):
self.system = platform.system()
setup(
name='cm4mlops',
version='0.1',
long_description='CM automations and scripts for MLOps',
long_description_content_type='text/x-rst',
packages=[],
install_requires=[
"setuptools>=60",
"wheel",
"cmind",
"giturlparse",
"requests",
"pyyaml"
],
cmdclass={
'install': CustomInstallCommand,
},
)