Skip to content

Commit

Permalink
Added "cm init" command prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
gfursin committed Aug 12, 2024
1 parent 951a002 commit 78190a9
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cm/cmind/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Written by Grigori Fursin

__version__ = "2.3.4"
__version__ = "2.3.4.1"

from cmind.core import access
from cmind.core import error
Expand Down
3 changes: 3 additions & 0 deletions cm/cmind/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ def access(self, i, out = None):
elif action == '' and automation == '' and i.get('version',False):
action = 'version'
automation = 'core'
elif action == 'init' and automation == '':
automation = 'core'


# Print basic help if action == ''
extra_help = True if action == 'help' and automation == '' else False
Expand Down
134 changes: 134 additions & 0 deletions cm/cmind/repo/automation/core/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,118 @@
#sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
#from cm_60cb625a46b38610 import misc

class CMInit():
def run(self):
print ('Checking platform information ...')
self.get_sys_platform()

print ('')
print ('Checking system dependencies ...')
self.install_system_packages()

print ('')
print ('Obtaining default automation repository ...')

print ('')
r = self.pull_repo()
return r

def install_system_packages(self):

import sys
import subprocess
import importlib.util

# 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):
import subprocess

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):
import subprocess

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 pull_repo(self):
import cmind

r = cmind.access({'action':'pull', 'automation':'repo', 'artifact':'mlcommons@cm4mlops', 'branch': 'mlperf-inference', 'out':'con'})
return r

def get_sys_platform(self):
import platform

self.system = platform.system()

class CAutomation(Automation):
"""
CM "core" automation actions
Expand Down Expand Up @@ -42,3 +154,25 @@ def uid(self, i):

return utils.call_internal_module(self, __file__, 'module_misc', 'uid', i)

############################################################
def init(self, i):
"""
Init CM
Args:
(CM input dict):
(out) (str): if 'con', output to console
Returns:
(CM return dict):
* return (int): return code == 0 if no error and >0 if error
* (error) (str): error string if return>0
"""

cm_init = CMInit()

return cm_init.run()

0 comments on commit 78190a9

Please sign in to comment.