Package: venvctl
venvctlvenvctlis a CLI tool allowing the creation of fullyportable Python virtual environments.Synopsisvenvctlhelps to buildfully portablePython virtual environments, inbulk, orsinglemode, keeping the state inconfig files. Each virtual environment comes with a detailed markdown report, to overview the integrity of its state, broken dependencies, errors, and warnings that occurred in the build process. Eventually, the folders ready for distributions get packed in tarballs.LimitationsIt is evident that the portability is limited to systems sharing the same kernel,do not expectto build a virtual environment on aDebianbased system, and deploy it on aRHELhost, to mention one of the many cases;It is possible shipping a virtual environment with a different version of python other than the one installed on the destination system, yet again, do not assume to run python3 based venvs on a system solely configured with python 2.x.Requirementsvenvctlrelies on a few packages to explicate its core functionalities:piphyperd==1.9.10piphyperd, a wrapper aroundpipto manage installations and audits.markd==0.1.20markd, a Python package that facilitates the generation of markdown flavored files.virtualenv==20.4.7  # Virtual Python Environment builder.
click8==8.0.1  # Composable command line interface toolkit.
binaryornot==0.4.4  # Ultra-lightweight pure Python package to check if a file is binary or text.Installationvenvctlis currently distributed only through PyPi.orgpipinstall--uservenvctlVisit theproject pagefor further information about the package status and releases.DocumentationFor the detailed instructions and a full API walkthroug, refer to theOfficial Documenation.You can leveragevenvctlboth programmatically, or calling the CLI, as shown in the examples below:Build virtual environments inbulk programmatically"""Python"""fromvenvctlimportVenvCtl# Build virtualenvs in bulkVenvCtl(config_file=/path/to/config.json,python_binary=/usr/bin/python{version},output_dir=/my/output/dir).run()Build virtual environments inbulk leveraging the CLI#!/bin/bash# Build virtualenvs in bulkvenvctlgenerate\--config~/path/to/your/config/venvs.json\--out./venvsBuild asingle virtual environment programmatically"""Python"""fromvenvctlimportVenvCtlname="test-venv"packages=["Click==7.0","binaryornot==0.4.4"]# Build a single virtual env;# It will generate the config file for you.VenvCtl.create_venv(name=name,packages=packages_list,output_dir=/my/output/dir)Build asingle virtual environment leveraging the CLI#!/bin/bash# Build a single virtual env;# It will generate the config file for you.venvctlcreate--namemy_venv--packages'["tox", "docker"]'--out/my/output/dirConfiguration FileA config file follows the json structure:[{"name":"base","packages":["asn1crypto==1.3.0","dnspython==1.16.0","enum34==1.1.10","ipaddress==1.0.23","jmespath==0.9.5","lxml==4.5.0","paramiko==2.7.1","psutil==5.7.0","pycrypto==2.6.1","pyopenssl==19.1.0","python-ldap==3.2.0","python-memcached==1.59"]},{"name":"base_networking","parent":"base","packages":["f5-sdk==3.0.21","bigsuds==1.0.6","netaddr==0.7.19","cloudshell-networking-cisco-iosxr==4.0.6"]},{"name":"ansible_2_9","parent":"base","packages":["ansible==2.9.6"]},{"name":"ansible_2_9_networking","parent":"base_networking","packages":["ansible==2.9.6"]}]The build process follows an inheritance pattern, in the example above, the environment namedbaseis the core for the rest;ansible_2_9inherits its packages;ansible_2_9_toxadds modules on the top of itsparent,ansible_2_9.With this logic, the build process in bulk can be quite fast, even when deploying complex virtual environments.Run it with ContainersIt is possible to take advantage of acontainer image, built to shipvenvctland the whole toolchain to create virtual environments leveraging bothPython 2andPython 3.
Here to follow, two examples runningDocker.Build virtual environments in bulk, shipping Python 3.6.8:dockerrun-it--rm-v$(pwd)/conf.json:/opt/conf.json\-v$(pwd)/venvs:/opt/venvs:rw\eu.gcr.io/hyperd-containers/venv-builder:latestvenvctlgenerate\--config/opt/conf.json\--out/opt/venvs\Build virtual environments in bulk, shipping Python 2.7.16:dockerrun-it--rm-v$(pwd)/conf.json:/opt/conf.json\-v$(pwd)/venvs:/opt/venvs:rw\eu.gcr.io/hyperd-containers/venv-builder:latestvenvctlgenerate\--config/opt/conf.json\--out/opt/venvs\--python/usr/bin/python2Containers limitationsCurrently, the container image available is based onCentOS 8. It fits the purpose of anyRHELbased deployment; however, it won't be useful in other scenarios.
More kernels will be added, stay tuned, or feel free to build your own and poke me.LicenseGNU General Public License v3 (GPLv3)Report a VulnerabilityIf you believe you have found a security vulnerability invenvctl, drop me a line, I will coordinate the vunerability response and disclosure.About the authorFrancesco CosentinoI'm a surfer, a crypto trader, and a DevSecOps Engineer with 15 years of experience designing highly-available distributed production environments and developing cloud-native apps in public and private clouds.