python-virtualenv-execute-shell

Example:

# Execute 'cookiecutter' from within a virtualenv.
- python-virtualenv-execute-shell:
    command: cookiecutter gh:audreyr/cookiecutter-pypackage --no-input
    chdir: /tmp
    virtualenv_path: /home/freckles/.pyenv/versions/cookiecutter

Description

Execute a command inside a virtualenv, the 'command' needs to be available in the virtualenv (/bin/).

You can easily use the 'execute-shell' frecklet instead of this, because for now with this you need to know the path to the virtualenv or system-wide. If you used the 'python-virtualenv' frecklet and the 'python_type' 'pyenv', your virtualenv will be located under /.pyenv/versions/. In the future this will take the same vars that the 'python-virtualenv' frecklet takes, and auto-calculate the path to the virtualenv.

For now, only absolute virtualenv paths are supported, '~' won't work.

Also, this frecklet does not support commands that require user input. For this, you'd have to create a frecklet similar to this, which uses 'expect'. Not hard, just not available yet.

Variables

Name Type Default Description

command

string --

This needs to be an executable that is located in the virtualenv_path/bin/ folder. Required

virtualenv_path

string --

The (absolute) virtualenv (base) path. Required

chdir

string --

The working directory.

environment

dict --

A dictionary of environment variables to add/set.

ignore_error

boolean False

Whether to ignore any potential errors.

no_log

boolean False

Whether to hide the log of this command (because for example the command contains sensitive information).

user

string --

The user to execute this command as.

Examples

Example 1

Execute 'cookiecutter' from within a virtualenv.

Code
- python-virtualenv-execute-shell:
    command: cookiecutter gh:audreyr/cookiecutter-pypackage --no-input
    chdir: /tmp
    virtualenv_path: /home/freckles/.pyenv/versions/cookiecutter
Description

This assumes you installed cookiecutter in an virtualenv before (for example using the 'python_virtualenv' frecklet), using 'pyenv'.

The result will be a project folder at '/tmp/python_boilerplate'. Obviously, this is not that useful becaues of the '--no-input' option we used (this frecklet really only works with 'batch-able' commands).

Code

doc:
  short_help: Executes a command inside a virtualenv.
  help: |
    Execute a command inside a virtualenv, the 'command' needs to be available in the virtualenv (<venv>/bin/<command>).

    You can easily use the 'execute-shell' frecklet instead of this, because for now with this you need to know the path to the virtualenv or system-wide. If you used the 'python-virtualenv' frecklet and the 'python_type' 'pyenv',
    your virtualenv will be located under <user_home>/.pyenv/versions/<venv_name>.
    In the future this will take the same vars that the 'python-virtualenv' frecklet takes, and auto-calculate the
    path to the virtualenv.

    For now, only absolute virtualenv paths are supported, '~' won't work.

    Also, this frecklet does not support commands that require user input. For this, you'd have to create a frecklet
    similar to this, which uses 'expect'. Not hard, just not available yet.
  examples:
  - title: Execute 'cookiecutter' from within a virtualenv.
    desc: |
      This assumes you installed [cookiecutter](https://cookiecutter.readthedocs.io/en/latest/) in an virtualenv before (for example using the 'python_virtualenv' frecklet), using 'pyenv'.

      The result will be a project folder at '/tmp/python_boilerplate'. Obviously, this is not that useful becaues of the '--no-input' option we used (this frecklet really only works with 'batch-able' commands).
    vars:
      command: cookiecutter gh:audreyr/cookiecutter-pypackage --no-input
      chdir: /tmp
      virtualenv_path: /home/freckles/.pyenv/versions/cookiecutter


args:
  _import: execute-shell
  virtualenv_path:
    doc:
      short_help: The (absolute) virtualenv (base) path.
    type: string
    required: true
  command:
    doc:
      short_help: The command to execute.
      help: |
        This needs to be an executable that is located in the ``virtualenv_path``/bin/ folder.
  user:
    doc:
      short_help: The user to execute this command as.
    type: string
    empty: false
    required: false
  ignore_error:
    doc:
      short_help: Whether to ignore any potential errors.
    type: boolean
    required: false
    default: false
  no_log:
    doc:
      short_help: Whether to hide the log of this command (because for example the
        command contains sensitive information).
    type: boolean
    default: false
    required: false
    cli:
      param_decls:
      - --no-log
  environment:
    doc:
      short_help: A dictionary of environment variables to add/set.
    type: dict
    required: false
    keyschema:
      type: string


frecklets:
- execute-shell:
    frecklet::desc:
      short: 'execute command in venv: {{:: command ::}}'
    command: 'source {{:: virtualenv_path ::}}/bin/activate && {{:: command ::}}'
    chdir: '{{:: chdir ::}}'
    become_user: '{{:: user ::}}'
    ignore_error: '{{:: ignore_error ::}}'
    no_log: '{{:: no_log ::}}'
    environment: '{{:: environment ::}}'
    shell_executable: /bin/bash
frecklecute python-virtualenv-execute-shell --help

Usage: frecklecute python-virtualenv-execute-shell [OPTIONS]

  Execute a command inside a virtualenv, the 'command' needs to be available
  in the virtualenv (<venv>/bin/<command>).

  You can easily use the 'execute-shell' frecklet instead of this, because
  for now with this you need to know the path to the virtualenv or system-
  wide. If you used the 'python-virtualenv' frecklet and the 'python_type'
  'pyenv', your virtualenv will be located under
  <user_home>/.pyenv/versions/<venv_name>. In the future this will take the
  same vars that the 'python-virtualenv' frecklet takes, and auto-calculate
  the path to the virtualenv.

  For now, only absolute virtualenv paths are supported, '~' won't work.

  Also, this frecklet does not support commands that require user input. For
  this, you'd have to create a frecklet similar to this, which uses
  'expect'. Not hard, just not available yet.

Options:
  --command COMMAND               The command to execute.  [required]
  --virtualenv-path VIRTUALENV_PATH
                                  The (absolute) virtualenv (base) path.
                                  [required]
  --chdir CHDIR                   The working directory.
  --environment ENVIRONMENT       A dictionary of environment variables to
                                  add/set.
  --ignore-error / --no-ignore-error
                                  Whether to ignore any potential errors.
  --no-log                        Whether to hide the log of this command
                                  (because for example the command contains
                                  sensitive information).
  --user USER                     The user to execute this command as.
  --help                          Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.python_virtualenv_execute_shell.PythonVirtualenvExecuteShell
#


from dataclasses import dataclass
from pyckles import AutoPycklet
from typing import *    # noqa

@dataclass
class PythonVirtualenvExecuteShell(AutoPycklet):
    """Execute a command inside a virtualenv, the 'command' needs to be available in the virtualenv (<venv>/bin/<command>).

     You can easily use the 'execute-shell' frecklet instead of this, because for now with this you need to know the path to the virtualenv or system-wide. If you used the 'python-virtualenv' frecklet and the 'python_type' 'pyenv',
     your virtualenv will be located under <user_home>/.pyenv/versions/<venv_name>.
     In the future this will take the same vars that the 'python-virtualenv' frecklet takes, and auto-calculate the
     path to the virtualenv.

     For now, only absolute virtualenv paths are supported, '~' won't work.

     Also, this frecklet does not support commands that require user input. For this, you'd have to create a frecklet
     similar to this, which uses 'expect'. Not hard, just not available yet.

       Args:
         chdir: The working directory.
         command: The command to execute.
         environment: A dictionary of environment variables to add/set.
         ignore_error: Whether to ignore any potential errors.
         no_log: Whether to hide the log of this command (because for example the command contains sensitive information).
         user: The user to execute this command as.
         virtualenv_path: The (absolute) virtualenv (base) path.

    """

    FRECKLET_ID = "python-virtualenv-execute-shell"

    chdir: str = None
    command: str = None
    environment: Dict = None
    ignore_error: bool = None
    no_log: bool = None
    user: str = None
    virtualenv_path: str = None


    def __post_init__(self):
        super(PythonVirtualenvExecuteShell, self).__init__(var_names=["chdir", "command", "environment", "ignore_error", "no_log", "user", "virtualenv_path"])


frecklet_class = PythonVirtualenvExecuteShell
# -*- coding: utf-8 -*-


#
# module path: pycklets.python_virtualenv_execute_shell.PythonVirtualenvExecuteShell
#


from pyckles import AutoPycklet

class PythonVirtualenvExecuteShell(AutoPycklet):
    """Execute a command inside a virtualenv, the 'command' needs to be available in the virtualenv (<venv>/bin/<command>).

     You can easily use the 'execute-shell' frecklet instead of this, because for now with this you need to know the path to the virtualenv or system-wide. If you used the 'python-virtualenv' frecklet and the 'python_type' 'pyenv',
     your virtualenv will be located under <user_home>/.pyenv/versions/<venv_name>.
     In the future this will take the same vars that the 'python-virtualenv' frecklet takes, and auto-calculate the
     path to the virtualenv.

     For now, only absolute virtualenv paths are supported, '~' won't work.

     Also, this frecklet does not support commands that require user input. For this, you'd have to create a frecklet
     similar to this, which uses 'expect'. Not hard, just not available yet.

       Args:
         chdir: The working directory.
         command: The command to execute.
         environment: A dictionary of environment variables to add/set.
         ignore_error: Whether to ignore any potential errors.
         no_log: Whether to hide the log of this command (because for example the command contains sensitive information).
         user: The user to execute this command as.
         virtualenv_path: The (absolute) virtualenv (base) path.

    """

    FRECKLET_ID = "python-virtualenv-execute-shell"

    def __init__(self, chdir=None, command=None, environment=None, ignore_error=None, no_log=None, user=None, virtualenv_path=None):

        super(PythonVirtualenvExecuteShell, self).__init__(var_names=["chdir", "command", "environment", "ignore_error", "no_log", "user", "virtualenv_path"])
        self._chdir = chdir
        self._command = command
        self._environment = environment
        self._ignore_error = ignore_error
        self._no_log = no_log
        self._user = user
        self._virtualenv_path = virtualenv_path

    @property
    def chdir(self):
        return self._chdir

    @chdir.setter
    def chdir(self, chdir):
        self._chdir = chdir

    @property
    def command(self):
        return self._command

    @command.setter
    def command(self, command):
        self._command = command

    @property
    def environment(self):
        return self._environment

    @environment.setter
    def environment(self, environment):
        self._environment = environment

    @property
    def ignore_error(self):
        return self._ignore_error

    @ignore_error.setter
    def ignore_error(self, ignore_error):
        self._ignore_error = ignore_error

    @property
    def no_log(self):
        return self._no_log

    @no_log.setter
    def no_log(self, no_log):
        self._no_log = no_log

    @property
    def user(self):
        return self._user

    @user.setter
    def user(self, user):
        self._user = user

    @property
    def virtualenv_path(self):
        return self._virtualenv_path

    @virtualenv_path.setter
    def virtualenv_path(self, virtualenv_path):
        self._virtualenv_path = virtualenv_path



frecklet_class = PythonVirtualenvExecuteShell