skorokithakis / tbvaccine

A small utility to pretty-print Python tracebacks. ⛺
MIT License
377 stars 14 forks source link

switching on and off using an environment variable #13

Closed anntzer closed 7 years ago

anntzer commented 7 years ago

If tbvaccine provides (e.g.) a tbvaccine.pth file with contents similar to

import os; exec("if os.environ.get('TBVACCINE'):\n    <activation code>")

this would make it much easier to switch its functionality on and off without having to modify the "vaccined" code. You could even load the kwargs to TBVaccine from the environment variable, say as json: see https://github.com/anntzer/mplcursors/blob/master/setup.py for a self-contained example.

skorokithakis commented 7 years ago

Hmm, what does this do? Does it run on interpreter initialization and prettify all tracebacks if the env var is set?

anntzer commented 7 years ago

Yes. See last line of the following paragraph from https://docs.python.org/3.6/library/site.htm:

A path configuration file is a file whose name has the form name.pth and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path. Non-existing items are never added to sys.path, and no check is made that the item refers to a directory rather than a file. No item is added to sys.path more than once. Blank lines and lines beginning with # are skipped. Lines starting with import (followed by space or tab) are executed.

skorokithakis commented 7 years ago

Ah, that's very interesting, thank you. I'd welcome a PR, or I will get to this myself later on.

anntzer commented 7 years ago

See also https://github.com/ionelmc/python-hunter/blob/master/setup.py where I got this trick from, and https://github.com/xolox/python-coloredlogs/blob/master/setup.py for another usage.

An important point is that the site module (which is imported by default) already imports the os module at every interpreter startup (https://docs.python.org/3/library/os.html#os.environ), so the startup cost of the pth is basically 1) byte-compiling a tiny code fragment and 2) checking whether a dict entry is set, so that "should" be acceptable...

skorokithakis commented 7 years ago

I would love it if someone implemented this and issued a PR.

anntzer commented 7 years ago

The merged implementation is rather brittle; for example, it does not support packaging into a wheel

$ pip wheel tbvaccine==0.2 && unzip -l tbvaccine*.whl
Collecting tbvaccine==0.2
  Using cached tbvaccine-0.2.0.tar.gz
Building wheels for collected packages: tbvaccine
  Running setup.py bdist_wheel for tbvaccine ... done
  Stored in directory: /tmp
Successfully built tbvaccine
Archive:  tbvaccine-0.2.0-py3-none-any.whl
  Length      Date    Time    Name
---------  ---------- -----   ----
      110  2017-07-22 12:18   usr/lib/python3.6/site-packages/tbvaccine.pth
      715  2017-07-22 12:07   tbvaccine/tests.py
     7674  2017-07-22 12:07   tbvaccine/tbv.py
       22  2017-07-22 12:32   tbvaccine/version.py
      104  2017-07-22 12:07   tbvaccine/__init__.py
     1104  2017-07-22 12:07   tbvaccine/cli.py
     1918  2017-07-22 12:07   tbvaccine/__main__.py
     2653  2017-07-22 19:05   tbvaccine-0.2.0.dist-info/DESCRIPTION.rst
       50  2017-07-22 19:05   tbvaccine-0.2.0.dist-info/entry_points.txt
     1243  2017-07-22 19:05   tbvaccine-0.2.0.dist-info/metadata.json
       10  2017-07-22 19:05   tbvaccine-0.2.0.dist-info/top_level.txt
       92  2017-07-22 19:05   tbvaccine-0.2.0.dist-info/WHEEL
     3541  2017-07-22 19:05   tbvaccine-0.2.0.dist-info/METADATA
     1162  2017-07-22 19:05   tbvaccine-0.2.0.dist-info/RECORD
---------                     -------
    20398                     14 files

Note the first entry, which should be tbvaccine.pth and not usr/lib/python3.6/site-packages/tbvaccine.pth.

You can really only know where to put the pth file when install.run is being executed, see the link in the first post.

skorokithakis commented 7 years ago

Ah, I see, thank you. I will have another look then.

akittas commented 7 years ago

I've created a new PR using the trick from https://github.com/ionelmc/python-hunter/blob/master/setup.py. I've tested this both with wheel and sdist and it seems to work, hopefully resolves this. It's a pity there is not a consistent and recommended way to do this and we have to resort to hacks.

skorokithakis commented 7 years ago

Fantastic, thank you! Does wheel packaging work properly?

akittas commented 7 years ago
pip wheel . && unzip -l tbvaccine*.whl

Archive:  tbvaccine-0.2.0-py3-none-any.whl
  Length      Date    Time    Name
---------  ---------- -----   ----
      110  2017-07-22 11:45   tbvaccine.pth
     1104  2017-07-25 17:21   tbvaccine/cli.py
     7674  2017-07-25 17:21   tbvaccine/tbv.py
      715  2017-07-25 17:21   tbvaccine/tests.py
       22  2017-07-25 17:20   tbvaccine/version.py
      104  2017-07-25 17:21   tbvaccine/__init__.py
     1918  2017-07-25 17:21   tbvaccine/__main__.py
     2653  2017-07-27 16:14   tbvaccine-0.2.0.dist-info/DESCRIPTION.rst
       50  2017-07-27 16:14   tbvaccine-0.2.0.dist-info/entry_points.txt
     1243  2017-07-27 16:14   tbvaccine-0.2.0.dist-info/metadata.json
       10  2017-07-27 16:14   tbvaccine-0.2.0.dist-info/top_level.txt
       97  2017-07-27 16:14   tbvaccine-0.2.0.dist-info/WHEEL
     3541  2017-07-27 16:14   tbvaccine-0.2.0.dist-info/METADATA
     1130  2017-07-27 16:14   tbvaccine-0.2.0.dist-info/RECORD
---------                     -------
    20371                     14 files

tbvaccine.pth is where it should be, also I tested building the wheel and installing from it and it seems to work fine.