A collection of classes and commands for automated command line scripting using Python.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.8 KiB

2 years ago
# Use Script Tease With Common Kit
2 years ago
Since the focus of Script Tease is to convert plain text instructions into valid command line statements, it does *not* provide support for executing those statements either locally or remotely. However, the shell component of [python-commonkit](https://docs.develmaycare.com/en/python-commonkit/stable/components/#module-commonkit.shell) *does* provide support for executing commands in local POSIX environments.
2 years ago
Here is an example of how to use these packages together:
```python
from commonkit.shell import Command
2 years ago
from scripttease.exceptions import InvalidInput
2 years ago
from scripttease.lib.factories import command_factory
from scripttease.lib.loaders import INILoader
def execute(step):
command = Command(
step.statement,
comment=step.comment,
path=step.cd,
prefix=step.prefix,
shell=step.shell
)
# Sudo is a different class, but identical in behavior.
command.sudo = step.sudo
if command.run():
print("[success] %s" % step.comment)
else:
print("[failure] %s" % step.comment)
if step.stop:
print("I can't go on: %s" % command.error)
exit(command.code)
2 years ago
ini = INILoader("path/to/steps.ini")
2 years ago
ini.load()
2 years ago
try:
steps = command_factory(ini)
except InvalidInput as e:
print("%s: I can't go on." % e)
exit(1)
2 years ago
# A failure to load results in None.
if steps is None:
print("Failed to load steps. Bummer.")
exit(1)
2 years ago
# Iterate through each step to create a Common Kit command instance.
2 years ago
for step in steps:
# To preview ...
# print(step.get_statement(cd=True))
execute(step)
```
Common Kit is already a dependency of Script Tease, so it is installed by default. The ``execute()`` function provides the interface between Script Tease command instances and Common Kit command instances.