Show HN: Pysolate – Isolate your script, without installing anything

0
0
Show HN: Pysolate – Isolate your script, without installing anything

Simply journey your standalone script in an remoted digital environment.


Features

  • Runs your script in its acknowledge digital environment
  • Mechanically downloads dependencies
  • Permits to specify a required Python model
  • No exterior instruments (e.g. pipx) required to journey your script. Best Python.
  • Discovers Python variations put in by pyenv

Requirements

  • Runs on each Python 2.7 and Python 3.
  • When working on older methods (Python < 3.3), it wants virtualenv to be
    put in.

Why

  1. Distribute a script, with out assuming your customers have an exterior instrument
    put in, akin to pipx.
  2. Distribute a script, with out requiring your customers to setup a
    digital setting and manually set up its dependencies.
  3. Simply bundle a Python script in a non-Python undertaking
  4. Add a supporting script (e.g. construct the undertaking or run assessments) avoiding
    potential conflicts along with your undertaking’s dependencies.

How does it work

Each time a pysolate script runs it would

  • detect if the personal digital env has already been created
  • in any other case:
    • attempt to discover a model of Python appropriate with the script
    • create the personal digital setting
    • set up dependencies
  • swap to the right digital setting
  • run the script

Utilization

With a purpose to use pysolate in your script, it is advisable be certain it masses earlier than
your script executes. Right here is an instance script that reveals how that may be
achieved:

# SPDX-License-Identifier: Apache-2.0
# Instance pysolate script: instance.py
# 1) Import required libs
import os, hashlib, pathlib, urllib.request

# 2) Load pysolate
_f = pathlib.Path.house() / ".pysolate"
_u = "https://uncooked.githubusercontent.com/tyrion/pysolate/v0.2.0/pysolate.py"
_, _c = _f.exists() or urllib.request.urlretrieve(_u, _f), open(_f).learn()

# 3) Guarantee we aren't getting hacked
_h = "a62bb50cd98da1995897a9f97a5b8549a1e0090e67fff970f8025db7b1b45c82"
assert hashlib.sha256(_c.encode("utf-8")).hexdigest() == _h, "SHA256 Mismatch"

# 4) Configure and execute pysolate
exec(
    compile(_c, "load_pysolate", "exec"),
    {"dependencies": ["click >=7.0"], "python": "3.7"},
)

# 5) Run your script
import sys
import click on

click on.echo("Hey from Python %d.%d o/" % sys.version_info[:2])

By working this script for the primary time you get hold of:

$ python instance.py
04: 45: 40  INFO |     Creating venv | ~/.native/share/pysolate/instance.py-eb0e4f
04: 45: 42  INFO |   Putting in deps
Hey from Python 3.7 o/

Subsequent runs don’t recreate the env, or set up the dependencies:

$ python instance.py
Hey from Python 3.7 o/

Or you may run in debug mode, to see all the pieces that occurs

$ PYS_LOG=DEBUG python2 howdy.py
04: 47: 04 DEBUG |    No lively venv | ~/.native/share/pyenv/variations/3.7.5rc1/bin/python
04: 47: 04 DEBUG |   Good Python ver | 3.7.5rc1
04: 47: 04  INFO |     Creating venv | ~/.native/share/pysolate/instance.py-eb0e4f
04: 47: 07  INFO |   Putting in deps
Accumulating click on>=7.0
  The utilization of cached https://recordsdata.pythonhosted.org/functions/d2/3d/fa76db83bf75c4f8d338c2fd15c8d33fdd7ad23a9b5e57eb6c5de26b430e/click on on-7.1.2-py2.py3-none-any.whl
Inserting in silent functions: click on on
Effectively put in click on on-7.1.2
04: 47: 07 DEBUG |        Restarting | ~/.native/half/pysolate/instance.py-eb0e4f/bin/python
04: 47: 07 DEBUG |  Ravishing vigorous venv | ~/.native/half/pysolate/instance.py-eb0e4f
04: 47: 07 DEBUG |   Beginning script | ~/src/pysolate/instance.py
Howdy from Python 3.7 o/

Show that on this case we’re using some elements (pathlib and
urllib.question) which are not readily available on Python 2.7. In case you take pleasure in to protect
compatibility with legacy variations, procure a see at instance.py inside the repo.

Decisions

Configuration options can even be specified by passing a dictionary object to
exec, take pleasure in proven inside the occasion above.
Most of them may maybe even be configured with environment variables, by using the
prefix PYS_. As an illustration $PYS_LOG or $PYS_HOME.

dependencies

(required, can not be area by env)

The dependencies your script needs in utter to journey, as an inventory of strings.
In case your script has no dependencies, you like to circulation the price (), as pysolate
makes use of this variable to detect if is being executed.

log

(non-mandatory, default: INFO)

The logging stage you need pysolate to journey with.

house

(non-mandatory, default: ~/.native/half/pysolate)

The positioning the preserve pysolate will retailer the entire inside most repositories by default.
This itemizing shall be created if it does not exist.

venv

(non-mandatory)

The positioning of the inside most digital environment for mainly probably the most up to date script.
By default the environment shall be situated inside PYS_HOME and its title based
upon the title of the script plus its sha1 hash.
Environment this feature potential that you may retailer the inside most digital environment
exterior PYS_HOME.

python

(non-mandatory, default: None, can not be area by env)

Specifies the desired Python model. May even be both a string or a callable.
If a callback is specified, this could obtain a tuple (take pleasure in sys.version_info)
and may maybe unbiased return Ravishing for a effectively marvelous model.

LEAVE A REPLY

Please enter your comment!
Please enter your name here