Add CLI to execute Actions on the fly

This commit is contained in:
jpic 2020-05-30 23:52:18 +02:00
parent d68fdf8d5d
commit 3eb0f22ef9
3 changed files with 58 additions and 50 deletions

View File

@ -24,7 +24,7 @@ setup(
python_requires='>=3', python_requires='>=3',
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
'shlax = shlax.cli:cli', 'shlax = shlax.cli:cli.entry_point',
], ],
}, },
) )

View File

@ -14,55 +14,67 @@ import os
import sys import sys
class ConsoleScript(cli2.ConsoleScript): class Group(cli2.Group):
def __call__(self): def __init__(self, *args, **kwargs):
repo = os.path.join(os.path.dirname(__file__), 'repo') super().__init__(*args, **kwargs)
self.cmdclass = Command
if len(self.argv) > 1:
repofile = os.path.join(repo, sys.argv[1] + '.py')
if os.path.isfile(self.argv[1]):
self.argv = sys.argv[1:]
self.load_shlaxfile(sys.argv[1])
elif os.path.isfile(repofile):
self.argv = sys.argv[1:]
self.load_shlaxfile(repofile)
else:
raise Exception('File not found ' + sys.argv[1])
else:
available = glob.glob(os.path.join(repo, '*.py'))
return super().__call__()
def load_shlaxfile(self, path): class Command(cli2.Command):
with open(path) as f: def call(self, *args, **kwargs):
src = f.read() return self.shlax_target(self.target)
tree = ast.parse(src)
members = [] def __call__(self, *argv):
for node in tree.body: from shlax.targets.base import Target
if not isinstance(node, ast.Assign): self.shlax_target = Target()
result = super().__call__(*argv)
self.shlax_target.output.results(self.shlax_target)
return result
class ActionCommand(Command):
def call(self, *args, **kwargs):
self.target = self.target(*args, **kwargs)
return super().call(*args, **kwargs)
class ConsoleScript(Group):
def __call__(self, *argv):
self.load_actions()
#self.load_shlaxfiles() # wip
return super().__call__(*argv)
def load_shlaxfiles(self):
filesdir = os.path.dirname(__file__) + '/shlaxfiles/'
for filename in os.listdir(filesdir):
filepath = filesdir + filename
if not os.path.isfile(filepath):
continue continue
if not isinstance(node.value, ast.Call):
with open(filepath, 'r') as f:
tree = ast.parse(f.read())
group = self.group(filename[:-3])
main = Group(doc=__doc__).load(shlax)
def load_actions(self):
actionsdir = os.path.dirname(__file__) + '/actions/'
for filename in os.listdir(actionsdir):
filepath = actionsdir + filename
if not os.path.isfile(filepath):
continue continue
members.append(node.targets[0].id) with open(filepath, 'r') as f:
tree = ast.parse(f.read())
cls = [
node
for node in tree.body
if isinstance(node, ast.ClassDef)
]
if not cls:
continue
mod = importlib.import_module('shlax.actions.' + filename[:-3])
cls = getattr(mod, cls[0].name)
self.add(cls, name=filename[:-3], cmdclass=ActionCommand)
spec = importlib.util.spec_from_file_location('shlaxfile', sys.argv[1])
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
for member in members:
subject = getattr(mod, member)
if callable(subject):
self[member] = cli2.Callable(member, subject)
else:
importable = cli2.Importable(member, subject)
self[member] = cli2.Group(member)
for cb in importable.get_callables():
self[member][cb.name] = cb
def call(self, command): cli = ConsoleScript(doc=__doc__)
if command.name == 'help':
return super().call(command)
from shlax.targets.localhost import Localhost
asyncio.run(Localhost()(command.target))
cli = ConsoleScript(__doc__)

View File

@ -58,10 +58,6 @@ class Image:
if not self.tags: if not self.tags:
self.tags = ['latest'] self.tags = ['latest']
async def __call__(self, action, *args, **kwargs):
args = list(args)
return await action.exec(*args, **self.kwargs)
def __str__(self): def __str__(self):
return f'{self.repository}:{self.tags[-1]}' return f'{self.repository}:{self.tags[-1]}'