Skip to content

Commit

Permalink
add container support
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsocv12 committed Jul 6, 2024
1 parent 972aca2 commit e1bf9a0
Showing 1 changed file with 64 additions and 29 deletions.
93 changes: 64 additions & 29 deletions mmp/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import functools
import json
import re
import os
import logging
import sys
import subprocess
from inspect import getdoc

from . import signals
Expand All @@ -12,7 +11,6 @@
from .docopt_command import get_handler
from .docopt_command import NoSuchCommand
from .run_scripts import RunScripts
from .formatter import ConsoleWarningFormatter
from .utils import get_version_info


Expand Down Expand Up @@ -105,20 +103,20 @@ def run(self, options=None):
bcolors.printColor('FAIL', 'Missing run.py or file parameter')
return
os.system('pip_modules/bin/python run.py')

def ls(self, options=None):
'''
List modules on project.
Options:
-a List all the modules with their dependencies
usage: ls [options]
'''
if options.get('-a'):
os.system('./pip_modules/bin/pip list')
else:
os.system(f'cat ./requirements.txt')
os.system('cat ./requirements.txt')

def install(self, options=None):
'''
Expand Down Expand Up @@ -151,12 +149,21 @@ def init(self, options=None):
By default, creates the virtualenv in pip_modules dir on the proyect
and a requirements.txt without any line on the requirements file
options:
-d, --docker On Containers, use this flag to avoid the virtualenv
usage: init
'''
bcolors.printColor('HEADER', 'Initializing mmp')
self.__check_virtual_env()
self.__create_git()

if not options.get('--docker'):
self.__check_virtual_env()
self.__create_git()
else:
self.__safe_docker_init()

self.__check_requirements()

bcolors.printColor('OKGREEN', 'Finish init')

def uninstall(self, options=None):
Expand All @@ -171,20 +178,39 @@ def uninstall(self, options=None):
if not options.get('COMMAND', None):
bcolors.printColor('FAIL', 'You need to send the module name')
return
self.__uninstall_pip_module(options.get('COMMAND'))

module_name = options.get('COMMAND')

prefix = ''
if not self.__get_docker_status():
if not os.path.exists("pip_modules/"):
bcolors.printColor('FAIL', 'Can\'t remove module')
return

prefix = 'pip_modules/bin/'

os.system(f'{prefix}pip uninstall {module_name}')

self.__update_requirements(module_name, remove=True)

def clean(self, options=None):
'''
Clean all the libraries and reinstall the requirements.txt
usage: clean
'''
if self.__get_docker_status():
bcolors.printColor('FAIL', 'You are on a container, you can\'t clean')
return

if not os.path.exists('pip_modules'):
bcolors.printColor('FAIL', 'No pip_modules')
return

if not os.path.exists('requirements.txt'):
bcolors.printColor('FAIL', 'No requirements.txt')
return

os.system('rm -rf pip_modules')
self.__check_virtual_env()
os.system('pip_modules/bin/pip install -r requirements.txt')
Expand All @@ -200,31 +226,18 @@ def upgrade(self, options=None):
return
module_name = options.get('COMMAND')
os.system(f'pip_modules/bin/pip install --upgrade {module_name}')

def __create_git(self) -> None:
'''
Function dedicated to create git i
'''
if os.popen("command -v git").read() != ''\
and os.popen("[ -f ./.git ] && echo \"true\"") != '':
if (os.popen("command -v git").read() != '' and os.popen("[ -f ./.git ] && echo \"true\"") != ''):
os.system("git init")

if os.popen("[ -f ./.gitignore ] && echo \"true\"") != '':
file = open('.gitignore', 'w+')
file.write("*.DS_Store\n.vscode/\n\n*.pyc\n\npip_modules/")


def __uninstall_pip_module(self, module_name: str):
'''
This function deletes the module from pip_modules and remove it from
requirements
'''
if not os.path.exists("pip_modules/"):
bcolors.printColor('FAIL', 'Can\'t remove module')
return
os.system(f'pip_modules/bin/pip uninstall {module_name}')
self.__update_requirements(module_name, remove=True)

def __check_virtual_env(self):
'''
Function dedicated to create a new virtual env with the name
Expand All @@ -248,17 +261,25 @@ def __install_specifict_lib(self, command: str):
param: options (dict)
'''
self.__check_virtual_env()
prefix = self.__get_prefix_by_docker_status()

if prefix != '':
self.__check_virtual_env()

self.__check_requirements()

lib: dict = {
'name': command.rstrip('\x00').split('=')[0],
}

v_installed = self.__check_lib_installed(lib['name'])
if v_installed:
bcolors.printColor('WARNING', f'{v_installed} allready satisfied')
return
os.system(f'pip_modules/bin/pip install {command}')
show_result = os.popen(f'pip_modules/bin/pip show {lib["name"]}').read()

os.system(f'{prefix}pip install {command}')
show_result = os.popen(f'{prefix}pip show {lib["name"]}').read()

lib['version'] = show_result.splitlines()[1].split(':')[1].strip(' ')
self.__update_requirements(lib['name'], lib['version'])

Expand Down Expand Up @@ -305,6 +326,20 @@ def __check_lib_installed(self, lib_name: str):
return
return response_str.rstrip('\n')

def __get_docker_status(self):
with open('/.mmpConfig', 'r') as file:
text = file.read()
if 'docker=true' in text:
return True
return False

def __get_prefix_by_docker_status(self):
return '' if self.__get_docker_status() else 'pip_modules/bin/'

def __safe_docker_init(self):
with open('/.mmpConfig', 'w') as file:
file.write('docker=true\n')

@classmethod
def help(cls, options):
"""
Expand Down

0 comments on commit e1bf9a0

Please sign in to comment.