python-virtualenv

Example:

# Create user 'freckles',  install Python via pyenv, create virtualenv 'freckles', install the 'freckles-cli' package in it.
- python-virtualenv:
    venv_name: freckles
    user: freckles
    python_type: pyenv
    python_version: 3.6.8
    python_packages:
    - freckles-cli

Description

Installs Python either via system packages or pyenv, then use that to create a virtualenv with the specified name and Python version. Also lets you specify (optional) system dependencies as well as packages to install in the virtualenv.

If you choose 'system' as 'python_type', only '2' and '3' are allowed as versions. If you choose 'pyenv', you can select any of the options that can be displayed via pyenv install -l.

Also, in case you choose 'pyenv' as the 'python_type', this will add a piece of code to $HOME/.bashrc to load pyenv when the user logs in.

Variables

Name Type Default Description

venv_name

string --

The name of the virtualenv to set up. Required

group

string --

The group who owns/runs the virtualenv.

pip_extra_args

string --

Extra arguments forwarded to 'pip'.

python_base_path

string --

The base path to install Python into (if using 'pyenv' or 'conda').

python_build_opts

string --

Build options to be forwarded (if supported by 'install_type').

python_packages

list []

All necessary Python packages.

python_type

string pyenv

How to install Python. Defaults to 'pyenv'.

python_version

string latest

The version of python.

system_dependencies

list []

System packages the application depends on.

system_user

boolean False

Whether the user and group should be a system user/group.

uid

integer --

The uid of the user to create (optional).

update

boolean False

Update packages if already installed.

user

string --

The user who owns/runs the virtualenv.

venv_base_path

string --

The path that holds the virtualenv directory.

venv_python_exe

string --

This only works in combination with the 'system' 'python_install_type', if used with another type, this task will fail.

Examples

Example 1

Create user 'freckles', install Python via pyenv, create virtualenv 'freckles', install the 'freckles-cli' package in it.

Code
- python-virtualenv:
    venv_name: freckles
    user: freckles
    python_type: pyenv
    python_version: 3.6.8
    python_packages:
    - freckles-cli
Description

Create a user 'freckles' if it does not exist yet, install Python version '3.6.8' using Pyenv for that user. Then use 'pyenv virtualenv' to create a virtualenv called 'freckles' (which will be located under /home/freckles/.pyenv/versions/freckles).

Finally, install the pip package 'freckles-cli' into that virtualenv.

Code

doc:
  short_help: Create a Python virtualenv and install necessary packages.
  help: |
    Installs Python either via system packages or [pyenv](https://github.com/pyenv/pyenv), then use that to create a virtualenv with the
    specified name and Python version. Also lets you specify (optional) system dependencies as well as packages to
    install in the virtualenv.

    If you choose 'system' as 'python_type', only '2' and '3' are allowed as versions. If you choose 'pyenv', you can select any of the options that can be displayed via ``pyenv install -l``.

    Also, in case you choose 'pyenv' as the 'python_type', this will add a piece of code to ``$HOME/.bashrc`` to load pyenv when the user logs in.
  examples:
  - title: Create user 'freckles',  install Python via pyenv, create virtualenv 'freckles',
      install the 'freckles-cli' package in it.
    desc: |
      Create a user 'freckles' if it does not exist yet, install Python version '3.6.8' using Pyenv for that user. Then use 'pyenv virtualenv' to create a virtualenv called 'freckles' (which will be located under /home/freckles/.pyenv/versions/freckles).

      Finally, install the pip package 'freckles-cli' into that virtualenv.
    vars:
      venv_name: freckles
      user: freckles
      python_type: pyenv
      python_version: 3.6.8
      python_packages:
      - freckles-cli

args:
  _import:
  - python-virtualenv-exists
  - python-packages-in-virtualenv
  system_dependencies:
    doc:
      short_help: System packages the application depends on.
    type: list
    empty: true
    required: false
    default: []
    cli:
      param_decls:
      - --system-dependency
      - -s
  python_packages:
    doc:
      short_help: All necessary Python packages.
    type: list
    empty: true
    required: true
    default: []
    schema:
      type: string
    cli:
      param_decls:
      - --python-pkg
      - -p
  pip_extra_args:
    doc:
      short_help: Extra arguments forwarded to 'pip'.
    type: string
    required: false
  update:
    doc:
      short_help: Update packages if already installed.
    type: boolean
    default: false
    cli:
      param_decls:
      - --update
frecklets:
- packages-installed:
    frecklet::skip: '{{:: system_dependencies | true_if_empty ::}}'
    packages: '{{:: system_dependencies ::}}'
- python-virtualenv-exists:
    group: '{{:: group ::}}'
    system_user: '{{:: system_user ::}}'
    user: '{{:: user ::}}'
    uid: '{{:: uid ::}}'
    python_base_path: '{{:: python_base_path ::}}'
    python_build_opts: '{{:: python_build_opts ::}}'
    python_version: '{{:: python_version ::}}'
    python_type: '{{:: python_type ::}}'
    venv_name: '{{:: venv_name ::}}'
    venv_base_path: '{{:: venv_base_path ::}}'
    venv_python_exe: '{{:: venv_python_exe ::}}'
- python-packages-in-virtualenv:
    frecklet::skip: '{{:: python_packages | true_if_empty ::}}'
    user: '{{:: user ::}}'
    venv_name: '{{:: venv_name ::}}'
    venv_base_path: '{{:: venv_base_path ::}}'
    python_version: '{{:: python_version ::}}'
    python_type: '{{:: python_type ::}}'
    venv_python_exe: '{{:: venv_python_exe ::}}'
    pip_extra_args: '{{:: pip_extra_args ::}}'
    python_packages: '{{:: python_packages ::}}'
    update: '{{:: update ::}}'
frecklecute python-virtualenv --help

Usage: frecklecute python-virtualenv [OPTIONS]

  Installs Python either via system packages or
  [pyenv](https://github.com/pyenv/pyenv), then use that to create a
  virtualenv with the specified name and Python version. Also lets you
  specify (optional) system dependencies as well as packages to install in
  the virtualenv.

  If you choose 'system' as 'python_type', only '2' and '3' are allowed as
  versions. If you choose 'pyenv', you can select any of the options that
  can be displayed via ``pyenv install -l``.

  Also, in case you choose 'pyenv' as the 'python_type', this will add a
  piece of code to ``$HOME/.bashrc`` to load pyenv when the user logs in.

Options:
  --venv-name VENV_NAME           The name of the virtualenv to set up.
                                  [required]
  --group GROUP                   The group who owns/runs the virtualenv.
  --pip-extra-args PIP_EXTRA_ARGS
                                  Extra arguments forwarded to 'pip'.
  --python-base-path PYTHON_BASE_PATH
                                  The base path to install Python into (if
                                  using 'pyenv' or 'conda').
  --python-build-opts PYTHON_BUILD_OPTS
                                  Build options to be forwarded (if supported
                                  by 'install_type').
  -p, --python-pkg PYTHON_PACKAGES
                                  All necessary Python packages.
  --python-type PYTHON_TYPE       How to install Python. Defaults to 'pyenv'.
  --python-version PYTHON_VERSION
                                  The version of python.
  -s, --system-dependency SYSTEM_DEPENDENCIES
                                  System packages the application depends on.
  --system-user / --no-system-user
                                  Whether the user and group should be a
                                  system user/group.
  --uid UID                       The uid of the user to create (optional).
  --update                        Update packages if already installed.
  --user USER                     The user who owns/runs the virtualenv.
  --venv-base-path VENV_BASE_PATH
                                  The path that holds the virtualenv
                                  directory.
  --venv-python-exe VENV_PYTHON_EXE
                                  The (optional) path to an existing Python
                                  executable to be used for the venv.
  --help                          Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.python_virtualenv.PythonVirtualenv
#


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

@dataclass
class PythonVirtualenv(AutoPycklet):
    """Installs Python either via system packages or [pyenv](https://github.com/pyenv/pyenv), then use that to create a virtualenv with the
     specified name and Python version. Also lets you specify (optional) system dependencies as well as packages to
     install in the virtualenv.

     If you choose 'system' as 'python_type', only '2' and '3' are allowed as versions. If you choose 'pyenv', you can select any of the options that can be displayed via ``pyenv install -l``.

     Also, in case you choose 'pyenv' as the 'python_type', this will add a piece of code to ``$HOME/.bashrc`` to load pyenv when the user logs in.

       Args:
         group: The group who owns/runs the virtualenv.
         pip_extra_args: Extra arguments forwarded to 'pip'.
         python_base_path: The base path to install Python into (if using 'pyenv' or 'conda').
         python_build_opts: Build options to be forwarded (if supported by 'install_type').
         python_packages: All necessary Python packages.
         python_type: How to install Python. Defaults to 'pyenv'.
         python_version: The version of python.
         system_dependencies: System packages the application depends on.
         system_user: Whether the user and group should be a system user/group.
         uid: The uid of the user to create (optional).
         update: Update packages if already installed.
         user: The user who owns/runs the virtualenv.
         venv_base_path: The path that holds the virtualenv directory.
         venv_name: The name of the virtualenv to set up.
         venv_python_exe: The (optional) path to an existing Python executable to be used for the venv.

    """

    FRECKLET_ID = "python-virtualenv"

    group: str = None
    pip_extra_args: str = None
    python_base_path: str = None
    python_build_opts: str = None
    python_packages: List = None
    python_type: str = None
    python_version: str = None
    system_dependencies: List = None
    system_user: bool = None
    uid: int = None
    update: bool = None
    user: str = None
    venv_base_path: str = None
    venv_name: str = None
    venv_python_exe: str = None


    def __post_init__(self):
        super(PythonVirtualenv, self).__init__(var_names=["group", "pip_extra_args", "python_base_path", "python_build_opts", "python_packages", "python_type", "python_version", "system_dependencies", "system_user", "uid", "update", "user", "venv_base_path", "venv_name", "venv_python_exe"])


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


#
# module path: pycklets.python_virtualenv.PythonVirtualenv
#


from pyckles import AutoPycklet

class PythonVirtualenv(AutoPycklet):
    """Installs Python either via system packages or [pyenv](https://github.com/pyenv/pyenv), then use that to create a virtualenv with the
     specified name and Python version. Also lets you specify (optional) system dependencies as well as packages to
     install in the virtualenv.

     If you choose 'system' as 'python_type', only '2' and '3' are allowed as versions. If you choose 'pyenv', you can select any of the options that can be displayed via ``pyenv install -l``.

     Also, in case you choose 'pyenv' as the 'python_type', this will add a piece of code to ``$HOME/.bashrc`` to load pyenv when the user logs in.

       Args:
         group: The group who owns/runs the virtualenv.
         pip_extra_args: Extra arguments forwarded to 'pip'.
         python_base_path: The base path to install Python into (if using 'pyenv' or 'conda').
         python_build_opts: Build options to be forwarded (if supported by 'install_type').
         python_packages: All necessary Python packages.
         python_type: How to install Python. Defaults to 'pyenv'.
         python_version: The version of python.
         system_dependencies: System packages the application depends on.
         system_user: Whether the user and group should be a system user/group.
         uid: The uid of the user to create (optional).
         update: Update packages if already installed.
         user: The user who owns/runs the virtualenv.
         venv_base_path: The path that holds the virtualenv directory.
         venv_name: The name of the virtualenv to set up.
         venv_python_exe: The (optional) path to an existing Python executable to be used for the venv.

    """

    FRECKLET_ID = "python-virtualenv"

    def __init__(self, group=None, pip_extra_args=None, python_base_path=None, python_build_opts=None, python_packages=None, python_type="pyenv", python_version="latest", system_dependencies=None, system_user=None, uid=None, update=None, user=None, venv_base_path=None, venv_name=None, venv_python_exe=None):

        super(PythonVirtualenv, self).__init__(var_names=["group", "pip_extra_args", "python_base_path", "python_build_opts", "python_packages", "python_type", "python_version", "system_dependencies", "system_user", "uid", "update", "user", "venv_base_path", "venv_name", "venv_python_exe"])
        self._group = group
        self._pip_extra_args = pip_extra_args
        self._python_base_path = python_base_path
        self._python_build_opts = python_build_opts
        self._python_packages = python_packages
        self._python_type = python_type
        self._python_version = python_version
        self._system_dependencies = system_dependencies
        self._system_user = system_user
        self._uid = uid
        self._update = update
        self._user = user
        self._venv_base_path = venv_base_path
        self._venv_name = venv_name
        self._venv_python_exe = venv_python_exe

    @property
    def group(self):
        return self._group

    @group.setter
    def group(self, group):
        self._group = group

    @property
    def pip_extra_args(self):
        return self._pip_extra_args

    @pip_extra_args.setter
    def pip_extra_args(self, pip_extra_args):
        self._pip_extra_args = pip_extra_args

    @property
    def python_base_path(self):
        return self._python_base_path

    @python_base_path.setter
    def python_base_path(self, python_base_path):
        self._python_base_path = python_base_path

    @property
    def python_build_opts(self):
        return self._python_build_opts

    @python_build_opts.setter
    def python_build_opts(self, python_build_opts):
        self._python_build_opts = python_build_opts

    @property
    def python_packages(self):
        return self._python_packages

    @python_packages.setter
    def python_packages(self, python_packages):
        self._python_packages = python_packages

    @property
    def python_type(self):
        return self._python_type

    @python_type.setter
    def python_type(self, python_type):
        self._python_type = python_type

    @property
    def python_version(self):
        return self._python_version

    @python_version.setter
    def python_version(self, python_version):
        self._python_version = python_version

    @property
    def system_dependencies(self):
        return self._system_dependencies

    @system_dependencies.setter
    def system_dependencies(self, system_dependencies):
        self._system_dependencies = system_dependencies

    @property
    def system_user(self):
        return self._system_user

    @system_user.setter
    def system_user(self, system_user):
        self._system_user = system_user

    @property
    def uid(self):
        return self._uid

    @uid.setter
    def uid(self, uid):
        self._uid = uid

    @property
    def update(self):
        return self._update

    @update.setter
    def update(self, update):
        self._update = update

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

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

    @property
    def venv_base_path(self):
        return self._venv_base_path

    @venv_base_path.setter
    def venv_base_path(self, venv_base_path):
        self._venv_base_path = venv_base_path

    @property
    def venv_name(self):
        return self._venv_name

    @venv_name.setter
    def venv_name(self, venv_name):
        self._venv_name = venv_name

    @property
    def venv_python_exe(self):
        return self._venv_python_exe

    @venv_python_exe.setter
    def venv_python_exe(self, venv_python_exe):
        self._venv_python_exe = venv_python_exe



frecklet_class = PythonVirtualenv