-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from alfonsocv12/feature/runScripts
Feature/run scripts
- Loading branch information
Showing
7 changed files
with
207 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__='0.0.5a' | ||
__version__='0.1.0a' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .missing import ( | ||
MissingFiles, MissingValue, NotSuchScript | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class MissingFiles(Exception): | ||
def __init__(self, file_name): | ||
super().__init__("Missing File: %s" % file_name) | ||
|
||
self.file = file_name | ||
|
||
class MissingValue(Exception): | ||
def __init__(self, file_name, value_name): | ||
super().__init__(f"Missing value on file {file_name}: {value_name}") | ||
|
||
self.file_name = file_name | ||
self.value_name = value_name | ||
|
||
class NotSuchScript(Exception): | ||
def __init__(self, script_name): | ||
super().__init__(f"Not such script: {script_name}") | ||
|
||
self.script_name = script_name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import functools | ||
import json | ||
import re | ||
import os | ||
import logging | ||
import sys | ||
from inspect import getdoc | ||
|
||
from . import signals | ||
from .colors import bcolors | ||
from .docopt_command import DocoptDispatcher | ||
from .docopt_command import get_handler | ||
from .docopt_command import NoSuchCommand | ||
from .formatter import ConsoleWarningFormatter | ||
from .utils import get_version_info | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
console_handler = logging.StreamHandler(sys.stderr) | ||
|
||
|
||
def main(): | ||
try: | ||
command = dispatch() | ||
command() | ||
except (KeyboardInterrupt, signals.ShutdownException): | ||
print("Aborting.") | ||
sys.exit(1) | ||
except NoSuchCommand as e: | ||
TopLevelCommand.pass_to_lib() | ||
except Exception as e: | ||
print(str(e)) | ||
|
||
def dispatch(): | ||
dispatcher = DocoptDispatcher( | ||
TopLevelCommand, { | ||
'options_first': True, | ||
'version': get_version_info('mmp') | ||
} | ||
) | ||
options, handler, command_options = dispatcher.parse(sys.argv[1:]) | ||
return functools.partial(perform_command, options, handler, command_options) | ||
|
||
|
||
def perform_command(options, handler, command_options): | ||
if options['COMMAND'] in ('help', 'version'): | ||
handler(command_options) | ||
return | ||
|
||
command = TopLevelCommand(options=options) | ||
handler(command, command_options) | ||
|
||
|
||
class TopLevelCommand: | ||
'''Module Environment Executor | ||
This is the short for mmp exec, running a command from local modules | ||
usage: | ||
mex [options] [--] [COMMAND] [ARGS...] | ||
mex -h|--help | ||
Options: | ||
-v, --version Shows mmp version | ||
''' | ||
|
||
def __init__(self, options=None): | ||
''' | ||
Constructor function | ||
''' | ||
self.toplevel_options = options or {} | ||
|
||
@staticmethod | ||
def pass_to_lib(): | ||
''' | ||
Function dedicated to pass commands to the virtualenviroment | ||
''' | ||
command_to_find: str = sys.argv[1] | ||
full_command: str = ' '.join(sys.argv[1:]) | ||
try: | ||
if not os.path.isfile(f'pip_modules/bin/{command_to_find}'): | ||
raise NoSuchCommand(command_to_find, 'aol') | ||
|
||
os.system(f'pip_modules/bin/{full_command}') | ||
except Exception as e: | ||
print(str(e)) | ||
return | ||
|
||
@classmethod | ||
def help(cls, options): | ||
""" | ||
Get help on a command. | ||
Usage: help [COMMAND] | ||
""" | ||
if options['COMMAND']: | ||
subject = get_handler(cls, options['COMMAND']) | ||
else: | ||
subject = cls | ||
|
||
print(getdoc(subject)) | ||
|
||
|
||
def set_no_color_if_clicolor(no_color_flag): | ||
return no_color_flag or os.environ.get('CLICOLOR') == "0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import os | ||
from .errors import ( | ||
MissingFiles, MissingValue, NotSuchScript | ||
) | ||
|
||
|
||
class RunScripts: | ||
|
||
@staticmethod | ||
def get_run_script_value(command): | ||
''' | ||
Function dedicated to find the scripts on values | ||
param: command list | ||
''' | ||
try: | ||
import run as run_file | ||
except ImportError as e: | ||
raise Exception(f'Error on run.py: {str(e)}') | ||
except: | ||
raise MissingFiles('run.py') | ||
|
||
if not hasattr(run_file, 'SCRIPTS'): | ||
raise MissingValue('run.py', 'SCRIPTS') | ||
|
||
script_target: str = command[0] | ||
user_script: str = run_file.SCRIPTS.get(script_target, False) | ||
if not user_script: | ||
raise NotSuchScript(script_target) | ||
extra_args = ' '.join(command[1:]) | ||
if extra_args: | ||
user_script = f'{user_script} {extra_args}' | ||
os.system(f'pip_modules/bin/{user_script}') | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,8 @@ | |
|
||
setup( | ||
name='mmp', | ||
version='0.0.5', | ||
authro='Alfonso Villaobos', | ||
version='0.1.0', | ||
author='Alfonso Villaobos', | ||
author_email='[email protected]', | ||
url='https://github.com/alfonsocv12/mmp', | ||
license='MIT', | ||
|
@@ -19,7 +19,10 @@ | |
long_description=open('readme.md', 'r').read(), | ||
long_description_content_type='text/markdown', | ||
entry_points={ | ||
'console_scripts': ['mmp=mmp.cli.main:main'] | ||
'console_scripts': [ | ||
'mmp=mmp.cli.main:main', | ||
'mex=mmp.cli.mex:main' | ||
] | ||
}, | ||
install_requires=[ | ||
'virtualenv>=20.4.4', | ||
|