├── envdir ├── version.py ├── __main__.py ├── __init__.py ├── env.py ├── runner.py └── test_envdir.py ├── MANIFEST.in ├── .gitignore ├── AUTHORS.rst ├── .github └── main.workflow ├── docs ├── index.rst ├── usage.rst ├── installation.rst ├── changelog.rst ├── api.rst ├── Makefile ├── make.bat └── conf.py ├── setup.cfg ├── Makefile ├── .travis.yml ├── tox.ini ├── appveyor.yml ├── LICENSE ├── setup.py └── README.rst /envdir/version.py: -------------------------------------------------------------------------------- 1 | __version__ = "1.0.2" # noqa 2 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include README.rst AUTHORS.rst tox.ini 2 | recursive-include docs * 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__ 2 | *.egg-info 3 | .tox 4 | *.egg 5 | build/ 6 | htmlcov 7 | .coverage 8 | docs/_build 9 | dist/ 10 | -------------------------------------------------------------------------------- /envdir/__main__.py: -------------------------------------------------------------------------------- 1 | from .runner import go, runner 2 | 3 | if __name__ == "__main__": 4 | go(runner.run) # pragma: no cover 5 | -------------------------------------------------------------------------------- /AUTHORS.rst: -------------------------------------------------------------------------------- 1 | Authors 2 | ======= 3 | 4 | - Daniel Hahler 5 | - George Yoshida 6 | - Horst Gutmann 7 | - Jannis Leidel 8 | - Kuba Janoszek 9 | - Nicolas Delaby 10 | -------------------------------------------------------------------------------- /.github/main.workflow: -------------------------------------------------------------------------------- 1 | workflow "Run Black" { 2 | on = "push" 3 | resolves = ["Run black"] 4 | } 5 | 6 | action "Run black" { 7 | uses = "lgeiger/black-action@master" 8 | args = ". --check" 9 | } 10 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../README.rst 2 | 3 | More documentation 4 | ------------------ 5 | 6 | .. toctree:: 7 | :maxdepth: 3 8 | 9 | installation 10 | usage 11 | api 12 | changelog 13 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [wheel] 2 | universal = 1 3 | 4 | [flake8] 5 | exclude = .tox,*.egg 6 | ignore = E501,W503 7 | 8 | [tool:pytest] 9 | flakes-ignore = UnusedImport 10 | addopts = --strict --quiet --tb=short 11 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: pyz dist upload 2 | 3 | pyz: 4 | pyzzer.pyz -o build/envdir-$(shell python setup.py --version).pyz -m envdir:run -r envdir 5 | 6 | dist: 7 | python setup.py sdist bdist_wheel 8 | 9 | upload: 10 | twine upload -s dist/* 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: xenial 2 | language: python 3 | python: 4 | - 2.7 5 | - 3.4 6 | - 3.5 7 | - 3.6 8 | - 3.7 9 | install: 10 | - pip install tox-travis 11 | script: 12 | - tox 13 | after_success: 14 | - bash <(curl -s https://codecov.io/bash) 15 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = py{27,34,35,36,37},flake8 3 | 4 | [testenv] 5 | deps = 6 | pytest 7 | coverage 8 | commands = 9 | coverage run --source=envdir/ -m pytest [] 10 | coverage report -m --omit=envdir/test_envdir.py 11 | 12 | [testenv:flake8] 13 | basepython = python3.7 14 | deps = flake8 15 | commands = flake8 envdir 16 | -------------------------------------------------------------------------------- /envdir/__init__.py: -------------------------------------------------------------------------------- 1 | from .runner import runner, go 2 | from .env import Env # noqa 3 | from .version import __version__ # noqa 4 | 5 | open = runner.open 6 | 7 | 8 | # for backward compatibility 9 | def read(path=None): 10 | return open(path, stacklevel=2) 11 | 12 | 13 | def run(*args): 14 | go(runner.run, *args) 15 | 16 | 17 | def shell(*args): 18 | go(runner.shell, *args) 19 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | build: false 2 | environment: 3 | matrix: 4 | - TOXENV: "py27" 5 | - TOXENV: "py33" 6 | - TOXENV: "py34" 7 | - TOXENV: "py35" 8 | - TOXENV: "py36" 9 | - TOXENV: "py37" 10 | - TOXENV: "flake8" 11 | init: 12 | - "ECHO %TOXENV%" 13 | - ps: "ls C:\\Python*" 14 | install: 15 | - ps: Invoke-WebRequest "https://bootstrap.pypa.io/get-pip.py" -OutFile "c:\\get-pip.py" 16 | - "c:\\python27\\python c:\\get-pip.py" 17 | - "c:\\python27\\Scripts\\pip install tox virtualenv" 18 | test_script: 19 | - "c:\\python27\\Scripts\\tox --version" 20 | - "c:\\python27\\Scripts\\virtualenv --version" 21 | - "c:\\python27\\Scripts\\pip --version" 22 | - "c:\\python27\\Scripts\\tox" 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Jannis Leidel and contributors 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a 4 | copy of this software and associated documentation files (the "Software"), 5 | to deal in the Software without restriction, including without limitation 6 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 7 | and/or sell copies of the Software, and to permit persons to whom the 8 | Software is furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included 11 | in all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 14 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | IN THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import ast 2 | import codecs 3 | import os 4 | 5 | from setuptools import setup 6 | 7 | 8 | here = os.path.abspath(os.path.dirname(__file__)) 9 | 10 | 11 | class VersionFinder(ast.NodeVisitor): 12 | def __init__(self): 13 | self.version = None 14 | 15 | def visit_Assign(self, node): 16 | if node.targets[0].id == "__version__": 17 | self.version = node.value.s 18 | 19 | 20 | def read(*parts): 21 | return codecs.open(os.path.join(here, *parts), "r", encoding="utf8").read() 22 | 23 | 24 | def find_version(*parts): 25 | finder = VersionFinder() 26 | finder.visit(ast.parse(read(*parts))) 27 | return finder.version 28 | 29 | 30 | setup( 31 | name="envdir", 32 | version=find_version("envdir", "version.py"), 33 | classifiers=[ 34 | "Development Status :: 4 - Beta", 35 | "Intended Audience :: Developers", 36 | "License :: OSI Approved :: MIT License", 37 | "Topic :: Software Development :: Build Tools", 38 | "Programming Language :: Python :: 2", 39 | "Programming Language :: Python :: 2.7", 40 | "Programming Language :: Python :: 3", 41 | "Programming Language :: Python :: 3.4", 42 | "Programming Language :: Python :: 3.5", 43 | "Programming Language :: Python :: 3.6", 44 | "Programming Language :: Python :: 3.7", 45 | ], 46 | description="A Python port of daemontools' envdir.", 47 | long_description=( 48 | read("README.rst") + "\n\n" + read(os.path.join("docs", "changelog.rst")) 49 | ), 50 | author="Jannis Leidel", 51 | author_email="jannis@leidel.info", 52 | url="https://envdir.readthedocs.io/", 53 | license="MIT", 54 | packages=["envdir"], 55 | entry_points=dict(console_scripts=["envdir=envdir:run", "envshell=envdir:shell"]), 56 | zip_safe=False, 57 | ) 58 | -------------------------------------------------------------------------------- /docs/usage.rst: -------------------------------------------------------------------------------- 1 | Usage 2 | ===== 3 | 4 | Command line 5 | ------------ 6 | 7 | From the original envdir_ documentation: 8 | 9 | envdir runs another program with environment modified according to files 10 | in a specified directory. 11 | 12 | Interface:: 13 | 14 | envdir d child 15 | 16 | ``d`` is a single argument. ``child`` consists of one or more arguments. 17 | 18 | envdir sets various environment variables as specified by files in the 19 | directory named ``d``. It then runs ``child``. 20 | 21 | If ``d`` contains a file named ``s`` whose first line is ``t``, envdir 22 | removes an environment variable named ``s`` if one exists, and then adds 23 | an environment variable named ``s`` with value ``t``. The name ``s`` must 24 | not contain ``=``. Spaces and tabs at the end of ``t`` are removed. 25 | Nulls in ``t`` are changed to newlines in the environment variable. 26 | 27 | If the file ``s`` is completely empty (0 bytes long), envdir removes an 28 | environment variable named ``s`` if one exists, without adding a new 29 | variable. 30 | 31 | envdir exits ``111`` if it has trouble reading ``d``, if it runs out of 32 | memory for environment variables, or if it cannot run child. Otherwise 33 | its exit code is the same as that of child. 34 | 35 | Alternatively you can also use the ``python -m envdir`` form to call envdir. 36 | 37 | Isolated shell 38 | -------------- 39 | 40 | envdir also includes an optional CLI tool called ``envshell`` which launches 41 | a subshell using the given directory. It basically allows you to make a set 42 | of environment variable stick to your current shell session if you happen to 43 | use envdir a lot outside of simple script use. 44 | 45 | For example: 46 | 47 | .. code-block:: console 48 | 49 | $ envshell ~/mysite/envs/prod/ 50 | Launching envshell for /home/jezdez/mysite/envs/prod. Type 'exit' or 'Ctrl+D' to return. 51 | $ python manage.py runserver 52 | .. 53 | 54 | To leave the subshell, simply use the ``exit`` command or press ``Ctrl+D``. 55 | 56 | .. _envdir: http://cr.yp.to/daemontools/envdir.html 57 | 58 | 59 | Setup an empty environment variable 60 | ----------------------------------- 61 | 62 | Use an empty line to setup an empty environment variable (in contrast to an 63 | empty file, which would unset the environment variable): 64 | 65 | .. code-block:: console 66 | 67 | $ echo > envdir/EMPTY_ENV 68 | $ envdir envdir env | grep EMPTY_ENV 69 | EMPTY_ENV= 70 | -------------------------------------------------------------------------------- /docs/installation.rst: -------------------------------------------------------------------------------- 1 | Installation 2 | ============ 3 | 4 | As Python package 5 | ----------------- 6 | 7 | .. highlight:: console 8 | 9 | :: 10 | 11 | $ pip install envdir 12 | 13 | or: 14 | 15 | :: 16 | 17 | $ easy_install envdir 18 | 19 | As standalone script 20 | -------------------- 21 | 22 | Alternatively you can also download a standalone executable that follows 23 | Python's `PEP 441`_ and works with the Python Launcher for Windows (`PEP 397`_). 24 | Simply install the launcher from its site_ (downloads_) and you're ready to 25 | follow the rest of the instructions below. 26 | 27 | Windows 28 | ^^^^^^^ 29 | 30 | .. note:: 31 | 32 | The Python Launcher for Windows also provides other useful features like 33 | being able to correctly launch Python when double clicking a file with 34 | the .py file extension, a ``py`` command line tool to easily launch the 35 | interactive Python shell when you're working on the command line. See 36 | the `Python Launcher for Windows documentation`_ for more infos. 37 | 38 | Next step is downloading the actual standalone script. On Windows this entails 39 | using your web browser to download the following URL: 40 | 41 | .. parsed-literal:: 42 | 43 | \https://github.com/jezdez/envdir/releases/download/|release|/envdir-|release|.pyz 44 | 45 | Or simply run this on the command line to trigger the download with your 46 | default web browser: 47 | 48 | .. parsed-literal:: 49 | 50 | C:\\Windows\Explorer.exe \https://github.com/jezdez/envdir/releases/download/|release|/envdir-|release|.pyz 51 | 52 | Then -- from the location you downloaded the file to -- run the envdir script 53 | like you would any other script: 54 | 55 | .. parsed-literal:: 56 | 57 | C:\\Users\\jezdez\\Desktop>.\\envdir-|release|.pyz .. 58 | 59 | Linux, Mac OS, others 60 | ^^^^^^^^^^^^^^^^^^^^^ 61 | 62 | On Linux, Mac OS and other platforms with a shell like bash simply download 63 | the standalone file from Github: 64 | 65 | .. parsed-literal:: 66 | 67 | $ curl -LO \https://github.com/jezdez/envdir/releases/download/|release|/envdir-|release|.pyz 68 | 69 | and then run the file like you would do when running the script installed by 70 | the envdir package (see above): 71 | 72 | .. parsed-literal:: 73 | 74 | $ ./envdir-|release|.pyz .. 75 | 76 | .. _`PEP 441`: http://www.python.org/dev/peps/pep-0441/ 77 | .. _`PEP 397`: http://www.python.org/dev/peps/pep-0397/ 78 | .. _site: https://bitbucket.org/pypa/pylauncher/ 79 | .. _downloads: https://bitbucket.org/pypa/pylauncher/downloads 80 | .. _`Python Launcher for Windows documentation`: https://bitbucket.org/pypa/pylauncher/src/tip/Doc/launcher.rst 81 | -------------------------------------------------------------------------------- /docs/changelog.rst: -------------------------------------------------------------------------------- 1 | Changelog 2 | --------- 3 | 4 | 5 | 1.0.0 (26/03/2018) 6 | ^^^^^^^^^^^^^^^^^^ 7 | 8 | * Drop python 2.6, 3.2 and 3.3 9 | 10 | * Add explicit support for python 3.6 11 | 12 | * Add support for symlinks 13 | 14 | * Improved support for windows 15 | 16 | 0.7 (08/10/2014) 17 | ^^^^^^^^^^^^^^^^ 18 | 19 | * Use `exec` (`os.execvpe`) to replace the envdir process with the child 20 | process (fixes #20). 21 | 22 | * Change `isenvvar()` to only check for `=` in var names. 23 | 24 | 0.6.1 (12/23/2013) 25 | ^^^^^^^^^^^^^^^^^^ 26 | 27 | * Fixed handling SIGTERM signals to make sure all children of the forked 28 | process are killed, too. Thanks to Horst Gutmann for the report and 29 | help fixing it. 30 | 31 | 0.6 (12/03/2013) 32 | ^^^^^^^^^^^^^^^^ 33 | 34 | * Rewrote tests with pytest. 35 | 36 | * Vastly extended Python API. 37 | 38 | * Added Sphinx based docs: https://envdir.readthedocs.io/ 39 | 40 | * Fixed killing child process when capturing keyboard interrupt. 41 | 42 | * Added standalone script based on PEPs 441 and 397, compatible with 43 | Python Launcher for Windows. See the installation instructions for more 44 | info. 45 | 46 | 0.5 (09/22/2013) 47 | ^^^^^^^^^^^^^^^^ 48 | 49 | * Added check if the the provided path is a directory and throw an error if 50 | not. This adds compatibility to the daemontools' envdir. 51 | 52 | * Make sure to convert Nulls (``\0``) to newlines as done so in daemontools' 53 | envdir. 54 | 55 | 0.4.1 (08/21/2013) 56 | ^^^^^^^^^^^^^^^^^^ 57 | 58 | * Fixed ``envdir.read()`` to actually work with already existing environment 59 | variables. Extended docs to test Python use. 60 | 61 | 0.4 (08/09/2013) 62 | ^^^^^^^^^^^^^^^^ 63 | 64 | * Added ``envshell`` command which launches a subshell using the environment 65 | as defined in the given envdir. Example:: 66 | 67 | $ envshell ~/mysite/envs/prod/ 68 | Launching envshell for /home/jezdez/mysite/envs/prod. Type 'exit' or 'Ctrl+D' to return. 69 | $ python manage.py runserver 70 | .. 71 | 72 | 0.3 (07/30/2013) 73 | ^^^^^^^^^^^^^^^^ 74 | 75 | * Catch ``KeyboardInterrupt`` exceptions to not show a traceback from envdir 76 | but the repsonse from the called command. 77 | 78 | * Allow multiline environment variables. Thanks to Horst Gutmann for the 79 | suggestion. This is a departure from daemontools' standard which only 80 | allows the first line of the environment variable file. 81 | 82 | 0.2.1 (07/11/2013) 83 | ^^^^^^^^^^^^^^^^^^ 84 | 85 | * Fixed ``python -m envdir`` 86 | * Extended README to better describe the purpose 87 | 88 | 0.2 (07/10/2013) 89 | ^^^^^^^^^^^^^^^^ 90 | 91 | * Added ability to use envdir from Python. 92 | 93 | 0.1 (07/10/2013) 94 | ^^^^^^^^^^^^^^^^ 95 | 96 | * Initial release. 97 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | envdir (Python port) 2 | ==================== 3 | 4 | .. image:: https://api.travis-ci.org/jezdez/envdir.svg 5 | :alt: Linux Build Status 6 | :target: https://travis-ci.org/jezdez/envdir 7 | 8 | .. image:: https://ci.appveyor.com/api/projects/status/0fh77wei6cj5hei5?svg=true 9 | :alt: Windows Build Status 10 | :target: https://ci.appveyor.com/project/jezdez/envdir 11 | 12 | This is a Python port of daemontools_' tool envdir_. It works on Windows and 13 | other systems which can run Python. It's well tested and doesn't need a 14 | compiler to be installed. 15 | 16 | envdir runs another program with a modified environment according to files 17 | in a specified directory. 18 | 19 | So for example, imagine a software you want to run on a server but don't 20 | want to leave certain configuration variables embedded in the program's source 21 | code. A common pattern to solve this problem is to use environment variables 22 | to separate configuration from code. 23 | 24 | envdir allows you to set a series of environment variables at once to simplify 25 | maintaining complicated environments, for example in which you have multiple sets 26 | of those configuration variables depending on the infrastructure you run your 27 | program on (e.g. Windows vs. Linux, Staging vs. Production, Old system vs. 28 | New system etc). 29 | 30 | Let's have a look at a typical envdir: 31 | 32 | .. code-block:: console 33 | 34 | $ tree envs/prod/ 35 | envs/prod/ 36 | ├── DJANGO_SETTINGS_MODULE 37 | ├── MYSITE_DEBUG 38 | ├── MYSITE_DEPLOY_DIR 39 | ├── MYSITE_SECRET_KEY 40 | └── PYTHONSTARTUP 41 | 42 | 0 directories, 3 files 43 | $ cat envs/prod/DJANGO_SETTINGS_MODULE 44 | mysite.settings 45 | $ 46 | 47 | As you can see each file has a capitalized name and contains the value of the 48 | environment variable to set when running your program. To use it, simply 49 | prefix the call to your program with envdir: 50 | 51 | .. code-block:: console 52 | 53 | $ envdir envs/prod/ python manage.py runserver 54 | 55 | That's it, nothing more and nothing less. The way you structure your envdir 56 | is left to you but can easily match your configuration requirements and 57 | integrate with other configuration systems. envdirs contain just files after 58 | all. 59 | 60 | An interesting summary about why it's good to store configuration values in 61 | environment variables can be found on the 12factor_ site. 62 | 63 | .. note:: 64 | 65 | This Python port behaves different for multi line environment variables. 66 | It will not only read the first line of the file but the whole file. Take 67 | care with big files! 68 | 69 | .. tip:: 70 | 71 | Feel free to open tickets at https://github.com/jezdez/envdir/issues. 72 | 73 | .. _12factor: http://12factor.net/config 74 | .. _daemontools: http://cr.yp.to/daemontools.html 75 | .. _envdir: http://cr.yp.to/daemontools/envdir.html 76 | -------------------------------------------------------------------------------- /docs/api.rst: -------------------------------------------------------------------------------- 1 | Python API 2 | ========== 3 | 4 | .. function:: envdir.open([path]) 5 | 6 | To use an envdir in a Python file (e.g. Django's ``manage.py``) simply call 7 | the ``open`` function of the envdir module: 8 | 9 | .. code-block:: python 10 | 11 | import envdir 12 | envdir.open() 13 | 14 | envdir will try to find an :file:`envdir` directory next to the file you modified. 15 | 16 | It's also possible to explicitly pass the path to the envdir: 17 | 18 | .. code-block:: python 19 | 20 | import envdir 21 | 22 | envdir.open('/home/jezdez/mysite/envs/prod') 23 | 24 | Calling ``open`` will automatically apply all environment variables to the 25 | current instance of ``os.environ``. 26 | 27 | If you want to implement more advanced access to envdirs there is also an 28 | own dict-like :class:`~envdir.Env` object to work with. The above example 29 | could also be written like this: 30 | 31 | .. code-block:: python 32 | 33 | import envdir 34 | 35 | env = envdir.open('/home/jezdez/mysite/envs/prod') 36 | 37 | The returned :class:`~envdir.Env` instance has a dict-like interface but also 38 | features a :meth:`~envdir.Env.clear` method to reset the current instance of 39 | :data:`os.environ` to the value it had before the envdir was opened: 40 | 41 | .. code-block:: python 42 | 43 | import envdir 44 | 45 | env = envdir.open('/home/jezdez/mysite/envs/prod') 46 | 47 | # do something 48 | 49 | env.clear() 50 | 51 | Since calling the clear method should be done in a transparent manner 52 | you can also use it as a context manager: 53 | 54 | .. code-block:: python 55 | 56 | import envdir 57 | 58 | with envdir.open('/home/jezdez/mysite/envs/prod') as env: 59 | # do something 60 | 61 | Outside the context manager block the environ is reset back automatically. 62 | 63 | To access and write values you can also use the dict-like interface: 64 | 65 | .. code-block:: python 66 | 67 | import envdir 68 | 69 | with envdir.open() as env: 70 | env['DATABASE_URL'] = 'sqlite://:memory:' 71 | assert 'DATABASE_URL' in env 72 | assert env.items() == [('DATABASE_URL', 'sqlite://:memory:')] 73 | 74 | .. note:: 75 | 76 | Additions to the envdir done inside the context manager block are 77 | persisted to disk and will be available the next time your open the 78 | envdir again. 79 | 80 | Of course you can also directly interact with :class:`~envdir.Env` instances, 81 | e.g.: 82 | 83 | .. code-block:: python 84 | 85 | import envdir 86 | 87 | with envdir.Env('/home/jezdez/mysite/envs/prod') as env: 88 | # do something here 89 | 90 | The difference between instantiating an :class:`~envdir.Env` yourself to 91 | using :func:`envdir.open` is that you'll lose the automatic discovery of 92 | the ``envdir`` directory. 93 | 94 | See the API docs below for a full list of methods available in the 95 | :class:`~envdir.Env` object. 96 | 97 | .. autoclass:: envdir.Env 98 | :members: 99 | :undoc-members: 100 | :special-members: 101 | :inherited-members: 102 | -------------------------------------------------------------------------------- /envdir/env.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | try: 4 | from UserDict import IterableUserDict as UserDict 5 | except ImportError: 6 | from collections import UserDict 7 | 8 | 9 | def isenvvar(name): 10 | root, name = os.path.split(name) 11 | return "=" not in name 12 | 13 | 14 | class _EmptyFile(Exception): 15 | pass 16 | 17 | 18 | try: 19 | FileNotFoundError 20 | except NameError: # " % self.path 41 | 42 | def __enter__(self): 43 | return self 44 | 45 | def __exit__(self, type, value, traceback): 46 | self.clear() 47 | 48 | def __getitem__(self, name, default=_sentinel): 49 | try: 50 | return self._get(name, default=default) 51 | except (_EmptyFile, FileNotFoundError): 52 | if default is _sentinel: 53 | raise KeyError(name) 54 | return default 55 | 56 | def __setitem__(self, name, value): 57 | self._write(**{name: value}) 58 | self._set(name, value) 59 | self.created[name] = value 60 | 61 | def __delitem__(self, name): 62 | os.remove(os.path.join(self.path, name)) 63 | self._delete(name) 64 | 65 | def __contains__(self, name): 66 | return name in self.data or os.path.exists(os.path.join(self.path, name)) 67 | 68 | def _load(self): 69 | for _, _, files in os.walk(self.path, followlinks=True): 70 | for path in filter(isenvvar, files): 71 | root, name = os.path.split(path) 72 | try: 73 | value = self._get(name) 74 | except _EmptyFile: 75 | self._delete(name) 76 | else: 77 | self._set(name, value) 78 | 79 | def _open(self, name, mode="r"): 80 | return open(os.path.join(self.path, name), mode) 81 | 82 | def _get(self, name, default=_sentinel): 83 | path = os.path.join(self.path, name) 84 | if os.stat(path).st_size == 0: 85 | raise _EmptyFile 86 | if not os.path.exists(path): 87 | return default 88 | with self._open(name) as var: 89 | return var.read().strip("\n").replace("\x00", "\n") 90 | 91 | def _set(self, name, value): 92 | if name in os.environ: 93 | self.originals[name] = os.environ[name] 94 | self.data[name] = value 95 | os.environ[name] = value 96 | 97 | def _delete(self, name): 98 | if name in self.originals: 99 | os.environ[name] = self.originals[name] 100 | elif name in os.environ: 101 | del os.environ[name] 102 | if name in self.data: 103 | del self.data[name] 104 | 105 | def _write(self, **values): 106 | for name, value in values.items(): 107 | with self._open(name, "w") as env: 108 | env.write(value) 109 | 110 | def clear(self): 111 | """ 112 | Clears the envdir by resetting the os.environ items to the 113 | values it had before opening this envdir (or removing them 114 | if they didn't exist). Doesn't delete the envdir files. 115 | """ 116 | for name in list(self.data.keys()): 117 | self._delete(name) 118 | -------------------------------------------------------------------------------- /envdir/runner.py: -------------------------------------------------------------------------------- 1 | import optparse 2 | import os 3 | import subprocess 4 | import sys 5 | 6 | from .env import Env 7 | from .version import __version__ 8 | 9 | 10 | class Response(Exception): 11 | def __init__(self, message="", status=0): 12 | self.message = message 13 | self.status = status 14 | 15 | 16 | class Runner(object): 17 | envdir_usage = "usage: %prog [--help] [--version] dir child" 18 | envshell_usage = "usage: %prog [--help] [--version] dir" 19 | 20 | def __init__(self): 21 | self.parser = optparse.OptionParser(version=__version__) 22 | self.parser.disable_interspersed_args() 23 | self.parser.prog = "envdir" 24 | 25 | def path(self, path): 26 | real_path = os.path.realpath(os.path.expanduser(path)) 27 | if not os.path.exists(real_path): 28 | # use 111 error code to adher to envdir's standard 29 | raise Response("envdir %r does not exist" % path, 111) 30 | if not os.path.isdir(real_path): 31 | # use 111 error code to adher to envdir's standard 32 | raise Response("envdir %r not a directory" % path, 111) 33 | return real_path 34 | 35 | def open(self, path=None, stacklevel=1): 36 | if path is None: 37 | frame = sys._getframe() 38 | 39 | def get_parent(frame): 40 | return frame.f_back 41 | 42 | for _ in range(stacklevel): 43 | frame = get_parent(frame) 44 | if frame is not None: 45 | callerdir = os.path.dirname(frame.f_code.co_filename) 46 | path = os.path.join(callerdir, "envdir") 47 | else: 48 | # last holdout, assume cwd 49 | path = "envdir" 50 | return Env(self.path(path)) 51 | 52 | def shell(self, name, *args): 53 | self.parser.set_usage(self.envshell_usage) 54 | self.parser.prog = "envshell" 55 | options, args = self.parser.parse_args(list(args)) 56 | 57 | if len(args) == 0: 58 | raise Response( 59 | "%s\nError: incorrect number of arguments\n" 60 | % (self.parser.get_usage()), 61 | 2, 62 | ) 63 | 64 | sys.stdout.write( 65 | "Launching envshell for %s. " 66 | "Type 'exit' or 'Ctrl+D' to return.\n" % self.path(args[0]) 67 | ) 68 | sys.stdout.flush() 69 | self.open(args[0], 2) 70 | 71 | if "SHELL" in os.environ: 72 | shell = os.environ["SHELL"] 73 | elif "COMSPEC" in os.environ: 74 | shell = os.environ["COMSPEC"] 75 | else: 76 | raise Response("Unable to detect current environment shell") 77 | 78 | try: 79 | subprocess.call([shell]) 80 | except OSError as err: 81 | if err.errno == 2: 82 | raise Response("Unable to find shell %s" % shell, status=err.errno) 83 | else: 84 | raise Response("An error occurred: %s" % err, status=err.errno) 85 | 86 | raise Response() 87 | 88 | def run(self, name, *args): 89 | self.parser.set_usage(self.envdir_usage) 90 | self.parser.prog = "envdir" 91 | options, args = self.parser.parse_args(list(args)) 92 | 93 | if len(args) < 2: 94 | raise Response( 95 | "%s\nError: incorrect number of arguments\n" 96 | % (self.parser.get_usage()), 97 | 2, 98 | ) 99 | 100 | self.open(args[0], 2) 101 | 102 | # the args to call later 103 | args = args[1:] 104 | 105 | # in case someone passes in -- for any reason to separate the commands 106 | if args[0] == "--": 107 | args = args[1:] 108 | 109 | try: 110 | os.execvpe(args[0], args, os.environ) 111 | except OSError as err: 112 | raise Response( 113 | "Unable to run command %s: %s" % (args[0], err), status=err.errno 114 | ) 115 | 116 | raise Response() 117 | 118 | 119 | def go(caller, *args): 120 | if not args: 121 | args = sys.argv 122 | try: 123 | caller(args[0], *args[1:]) 124 | except Response as response: 125 | if response.message: 126 | sys.stderr.write(response.message) 127 | sys.exit(response.status or 0) 128 | else: 129 | sys.exit(0) 130 | 131 | 132 | runner = Runner() 133 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # User-friendly check for sphinx-build 11 | ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) 12 | $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) 13 | endif 14 | 15 | # Internal variables. 16 | PAPEROPT_a4 = -D latex_paper_size=a4 17 | PAPEROPT_letter = -D latex_paper_size=letter 18 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 19 | # the i18n builder cannot share the environment and doctrees with the others 20 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 21 | 22 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext 23 | 24 | help: 25 | @echo "Please use \`make ' where is one of" 26 | @echo " html to make standalone HTML files" 27 | @echo " dirhtml to make HTML files named index.html in directories" 28 | @echo " singlehtml to make a single large HTML file" 29 | @echo " pickle to make pickle files" 30 | @echo " json to make JSON files" 31 | @echo " htmlhelp to make HTML files and a HTML help project" 32 | @echo " qthelp to make HTML files and a qthelp project" 33 | @echo " devhelp to make HTML files and a Devhelp project" 34 | @echo " epub to make an epub" 35 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 36 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 37 | @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" 38 | @echo " text to make text files" 39 | @echo " man to make manual pages" 40 | @echo " texinfo to make Texinfo files" 41 | @echo " info to make Texinfo files and run them through makeinfo" 42 | @echo " gettext to make PO message catalogs" 43 | @echo " changes to make an overview of all changed/added/deprecated items" 44 | @echo " xml to make Docutils-native XML files" 45 | @echo " pseudoxml to make pseudoxml-XML files for display purposes" 46 | @echo " linkcheck to check all external links for integrity" 47 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 48 | 49 | clean: 50 | rm -rf $(BUILDDIR)/* 51 | 52 | html: 53 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 54 | @echo 55 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 56 | 57 | dirhtml: 58 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 59 | @echo 60 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 61 | 62 | singlehtml: 63 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 64 | @echo 65 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 66 | 67 | pickle: 68 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 69 | @echo 70 | @echo "Build finished; now you can process the pickle files." 71 | 72 | json: 73 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 74 | @echo 75 | @echo "Build finished; now you can process the JSON files." 76 | 77 | htmlhelp: 78 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 79 | @echo 80 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 81 | ".hhp project file in $(BUILDDIR)/htmlhelp." 82 | 83 | qthelp: 84 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 85 | @echo 86 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 87 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 88 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/envdir.qhcp" 89 | @echo "To view the help file:" 90 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/envdir.qhc" 91 | 92 | devhelp: 93 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 94 | @echo 95 | @echo "Build finished." 96 | @echo "To view the help file:" 97 | @echo "# mkdir -p $$HOME/.local/share/devhelp/envdir" 98 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/envdir" 99 | @echo "# devhelp" 100 | 101 | epub: 102 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 103 | @echo 104 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 105 | 106 | latex: 107 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 108 | @echo 109 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 110 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 111 | "(use \`make latexpdf' here to do that automatically)." 112 | 113 | latexpdf: 114 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 115 | @echo "Running LaTeX files through pdflatex..." 116 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 117 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 118 | 119 | latexpdfja: 120 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 121 | @echo "Running LaTeX files through platex and dvipdfmx..." 122 | $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja 123 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 124 | 125 | text: 126 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 127 | @echo 128 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 129 | 130 | man: 131 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 132 | @echo 133 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 134 | 135 | texinfo: 136 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 137 | @echo 138 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 139 | @echo "Run \`make' in that directory to run these through makeinfo" \ 140 | "(use \`make info' here to do that automatically)." 141 | 142 | info: 143 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 144 | @echo "Running Texinfo files through makeinfo..." 145 | make -C $(BUILDDIR)/texinfo info 146 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 147 | 148 | gettext: 149 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 150 | @echo 151 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 152 | 153 | changes: 154 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 155 | @echo 156 | @echo "The overview file is in $(BUILDDIR)/changes." 157 | 158 | linkcheck: 159 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 160 | @echo 161 | @echo "Link check complete; look for any errors in the above output " \ 162 | "or in $(BUILDDIR)/linkcheck/output.txt." 163 | 164 | doctest: 165 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 166 | @echo "Testing of doctests in the sources finished, look at the " \ 167 | "results in $(BUILDDIR)/doctest/output.txt." 168 | 169 | xml: 170 | $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml 171 | @echo 172 | @echo "Build finished. The XML files are in $(BUILDDIR)/xml." 173 | 174 | pseudoxml: 175 | $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml 176 | @echo 177 | @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." 178 | -------------------------------------------------------------------------------- /docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | REM Command file for Sphinx documentation 4 | 5 | if "%SPHINXBUILD%" == "" ( 6 | set SPHINXBUILD=sphinx-build 7 | ) 8 | set BUILDDIR=_build 9 | set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . 10 | set I18NSPHINXOPTS=%SPHINXOPTS% . 11 | if NOT "%PAPER%" == "" ( 12 | set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% 13 | set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% 14 | ) 15 | 16 | if "%1" == "" goto help 17 | 18 | if "%1" == "help" ( 19 | :help 20 | echo.Please use `make ^` where ^ is one of 21 | echo. html to make standalone HTML files 22 | echo. dirhtml to make HTML files named index.html in directories 23 | echo. singlehtml to make a single large HTML file 24 | echo. pickle to make pickle files 25 | echo. json to make JSON files 26 | echo. htmlhelp to make HTML files and a HTML help project 27 | echo. qthelp to make HTML files and a qthelp project 28 | echo. devhelp to make HTML files and a Devhelp project 29 | echo. epub to make an epub 30 | echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter 31 | echo. text to make text files 32 | echo. man to make manual pages 33 | echo. texinfo to make Texinfo files 34 | echo. gettext to make PO message catalogs 35 | echo. changes to make an overview over all changed/added/deprecated items 36 | echo. xml to make Docutils-native XML files 37 | echo. pseudoxml to make pseudoxml-XML files for display purposes 38 | echo. linkcheck to check all external links for integrity 39 | echo. doctest to run all doctests embedded in the documentation if enabled 40 | goto end 41 | ) 42 | 43 | if "%1" == "clean" ( 44 | for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i 45 | del /q /s %BUILDDIR%\* 46 | goto end 47 | ) 48 | 49 | 50 | %SPHINXBUILD% 2> nul 51 | if errorlevel 9009 ( 52 | echo. 53 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 54 | echo.installed, then set the SPHINXBUILD environment variable to point 55 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 56 | echo.may add the Sphinx directory to PATH. 57 | echo. 58 | echo.If you don't have Sphinx installed, grab it from 59 | echo.http://sphinx-doc.org/ 60 | exit /b 1 61 | ) 62 | 63 | if "%1" == "html" ( 64 | %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html 65 | if errorlevel 1 exit /b 1 66 | echo. 67 | echo.Build finished. The HTML pages are in %BUILDDIR%/html. 68 | goto end 69 | ) 70 | 71 | if "%1" == "dirhtml" ( 72 | %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml 73 | if errorlevel 1 exit /b 1 74 | echo. 75 | echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. 76 | goto end 77 | ) 78 | 79 | if "%1" == "singlehtml" ( 80 | %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml 81 | if errorlevel 1 exit /b 1 82 | echo. 83 | echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. 84 | goto end 85 | ) 86 | 87 | if "%1" == "pickle" ( 88 | %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle 89 | if errorlevel 1 exit /b 1 90 | echo. 91 | echo.Build finished; now you can process the pickle files. 92 | goto end 93 | ) 94 | 95 | if "%1" == "json" ( 96 | %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json 97 | if errorlevel 1 exit /b 1 98 | echo. 99 | echo.Build finished; now you can process the JSON files. 100 | goto end 101 | ) 102 | 103 | if "%1" == "htmlhelp" ( 104 | %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp 105 | if errorlevel 1 exit /b 1 106 | echo. 107 | echo.Build finished; now you can run HTML Help Workshop with the ^ 108 | .hhp project file in %BUILDDIR%/htmlhelp. 109 | goto end 110 | ) 111 | 112 | if "%1" == "qthelp" ( 113 | %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp 114 | if errorlevel 1 exit /b 1 115 | echo. 116 | echo.Build finished; now you can run "qcollectiongenerator" with the ^ 117 | .qhcp project file in %BUILDDIR%/qthelp, like this: 118 | echo.^> qcollectiongenerator %BUILDDIR%\qthelp\envdir.qhcp 119 | echo.To view the help file: 120 | echo.^> assistant -collectionFile %BUILDDIR%\qthelp\envdir.ghc 121 | goto end 122 | ) 123 | 124 | if "%1" == "devhelp" ( 125 | %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp 126 | if errorlevel 1 exit /b 1 127 | echo. 128 | echo.Build finished. 129 | goto end 130 | ) 131 | 132 | if "%1" == "epub" ( 133 | %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub 134 | if errorlevel 1 exit /b 1 135 | echo. 136 | echo.Build finished. The epub file is in %BUILDDIR%/epub. 137 | goto end 138 | ) 139 | 140 | if "%1" == "latex" ( 141 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 142 | if errorlevel 1 exit /b 1 143 | echo. 144 | echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. 145 | goto end 146 | ) 147 | 148 | if "%1" == "latexpdf" ( 149 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 150 | cd %BUILDDIR%/latex 151 | make all-pdf 152 | cd %BUILDDIR%/.. 153 | echo. 154 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 155 | goto end 156 | ) 157 | 158 | if "%1" == "latexpdfja" ( 159 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 160 | cd %BUILDDIR%/latex 161 | make all-pdf-ja 162 | cd %BUILDDIR%/.. 163 | echo. 164 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 165 | goto end 166 | ) 167 | 168 | if "%1" == "text" ( 169 | %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text 170 | if errorlevel 1 exit /b 1 171 | echo. 172 | echo.Build finished. The text files are in %BUILDDIR%/text. 173 | goto end 174 | ) 175 | 176 | if "%1" == "man" ( 177 | %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man 178 | if errorlevel 1 exit /b 1 179 | echo. 180 | echo.Build finished. The manual pages are in %BUILDDIR%/man. 181 | goto end 182 | ) 183 | 184 | if "%1" == "texinfo" ( 185 | %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo 186 | if errorlevel 1 exit /b 1 187 | echo. 188 | echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. 189 | goto end 190 | ) 191 | 192 | if "%1" == "gettext" ( 193 | %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale 194 | if errorlevel 1 exit /b 1 195 | echo. 196 | echo.Build finished. The message catalogs are in %BUILDDIR%/locale. 197 | goto end 198 | ) 199 | 200 | if "%1" == "changes" ( 201 | %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes 202 | if errorlevel 1 exit /b 1 203 | echo. 204 | echo.The overview file is in %BUILDDIR%/changes. 205 | goto end 206 | ) 207 | 208 | if "%1" == "linkcheck" ( 209 | %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck 210 | if errorlevel 1 exit /b 1 211 | echo. 212 | echo.Link check complete; look for any errors in the above output ^ 213 | or in %BUILDDIR%/linkcheck/output.txt. 214 | goto end 215 | ) 216 | 217 | if "%1" == "doctest" ( 218 | %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest 219 | if errorlevel 1 exit /b 1 220 | echo. 221 | echo.Testing of doctests in the sources finished, look at the ^ 222 | results in %BUILDDIR%/doctest/output.txt. 223 | goto end 224 | ) 225 | 226 | if "%1" == "xml" ( 227 | %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml 228 | if errorlevel 1 exit /b 1 229 | echo. 230 | echo.Build finished. The XML files are in %BUILDDIR%/xml. 231 | goto end 232 | ) 233 | 234 | if "%1" == "pseudoxml" ( 235 | %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml 236 | if errorlevel 1 exit /b 1 237 | echo. 238 | echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. 239 | goto end 240 | ) 241 | 242 | :end 243 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # envdir documentation build configuration file, created by 4 | # sphinx-quickstart on Mon Nov 11 13:23:54 2013. 5 | # 6 | # This file is execfile()d with the current directory set to its containing dir. 7 | # 8 | # Note that not all possible configuration values are present in this 9 | # autogenerated file. 10 | # 11 | # All configuration values have a default; values that are commented out 12 | # serve to show the default. 13 | 14 | import sys, os 15 | 16 | # If extensions (or modules to document with autodoc) are in another directory, 17 | # add these directories to sys.path here. If the directory is relative to the 18 | # documentation root, use os.path.abspath to make it absolute, like shown here. 19 | sys.path.insert(0, os.path.abspath("..")) 20 | 21 | # -- General configuration ----------------------------------------------------- 22 | 23 | # If your documentation needs a minimal Sphinx version, state it here. 24 | # needs_sphinx = '1.0' 25 | 26 | # Add any Sphinx extension module names here, as strings. They can be extensions 27 | # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 28 | extensions = ["sphinx.ext.autodoc", "sphinx.ext.intersphinx", "sphinx.ext.viewcode"] 29 | 30 | # Add any paths that contain templates here, relative to this directory. 31 | templates_path = ["_templates"] 32 | 33 | # The suffix of source filenames. 34 | source_suffix = ".rst" 35 | 36 | # The encoding of source files. 37 | # source_encoding = 'utf-8-sig' 38 | 39 | # The master toctree document. 40 | master_doc = "index" 41 | 42 | # General information about the project. 43 | project = u"envdir" 44 | copyright = u"2013, Jannis Leidel and contributors" 45 | 46 | # The version info for the project you're documenting, acts as replacement for 47 | # |version| and |release|, also used in various other places throughout the 48 | # built documents. 49 | # 50 | # The short X.Y version. 51 | version = "0.7" 52 | # The full version, including alpha/beta/rc tags. 53 | release = "0.7" 54 | 55 | # The language for content autogenerated by Sphinx. Refer to documentation 56 | # for a list of supported languages. 57 | # language = None 58 | 59 | # There are two options for replacing |today|: either, you set today to some 60 | # non-false value, then it is used: 61 | # today = '' 62 | # Else, today_fmt is used as the format for a strftime call. 63 | # today_fmt = '%B %d, %Y' 64 | 65 | # List of patterns, relative to source directory, that match files and 66 | # directories to ignore when looking for source files. 67 | exclude_patterns = ["_build"] 68 | 69 | # The reST default role (used for this markup: `text`) to use for all documents. 70 | # default_role = None 71 | 72 | # If true, '()' will be appended to :func: etc. cross-reference text. 73 | # add_function_parentheses = True 74 | 75 | # If true, the current module name will be prepended to all description 76 | # unit titles (such as .. function::). 77 | # add_module_names = True 78 | 79 | # If true, sectionauthor and moduleauthor directives will be shown in the 80 | # output. They are ignored by default. 81 | # show_authors = False 82 | 83 | # The name of the Pygments (syntax highlighting) style to use. 84 | pygments_style = "sphinx" 85 | 86 | # A list of ignored prefixes for module index sorting. 87 | # modindex_common_prefix = [] 88 | 89 | # If true, keep warnings as "system message" paragraphs in the built documents. 90 | # keep_warnings = False 91 | 92 | 93 | # -- Options for HTML output --------------------------------------------------- 94 | 95 | # The theme to use for HTML and HTML Help pages. See the documentation for 96 | # a list of builtin themes. 97 | html_theme = "default" 98 | 99 | # Theme options are theme-specific and customize the look and feel of a theme 100 | # further. For a list of options available for each theme, see the 101 | # documentation. 102 | # html_theme_options = {} 103 | 104 | # Add any paths that contain custom themes here, relative to this directory. 105 | # html_theme_path = [] 106 | 107 | # The name for this set of Sphinx documents. If None, it defaults to 108 | # " v documentation". 109 | # html_title = None 110 | 111 | # A shorter title for the navigation bar. Default is the same as html_title. 112 | # html_short_title = None 113 | 114 | # The name of an image file (relative to this directory) to place at the top 115 | # of the sidebar. 116 | # html_logo = None 117 | 118 | # The name of an image file (within the static path) to use as favicon of the 119 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 120 | # pixels large. 121 | # html_favicon = None 122 | 123 | # Add any paths that contain custom static files (such as style sheets) here, 124 | # relative to this directory. They are copied after the builtin static files, 125 | # so a file named "default.css" will overwrite the builtin "default.css". 126 | html_static_path = ["_static"] 127 | 128 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 129 | # using the given strftime format. 130 | # html_last_updated_fmt = '%b %d, %Y' 131 | 132 | # If true, SmartyPants will be used to convert quotes and dashes to 133 | # typographically correct entities. 134 | # html_use_smartypants = True 135 | 136 | # Custom sidebar templates, maps document names to template names. 137 | # html_sidebars = {} 138 | 139 | # Additional templates that should be rendered to pages, maps page names to 140 | # template names. 141 | # html_additional_pages = {} 142 | 143 | # If false, no module index is generated. 144 | # html_domain_indices = True 145 | 146 | # If false, no index is generated. 147 | # html_use_index = True 148 | 149 | # If true, the index is split into individual pages for each letter. 150 | # html_split_index = False 151 | 152 | # If true, links to the reST sources are added to the pages. 153 | # html_show_sourcelink = True 154 | 155 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 156 | # html_show_sphinx = True 157 | 158 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 159 | # html_show_copyright = True 160 | 161 | # If true, an OpenSearch description file will be output, and all pages will 162 | # contain a tag referring to it. The value of this option must be the 163 | # base URL from which the finished HTML is served. 164 | # html_use_opensearch = '' 165 | 166 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 167 | # html_file_suffix = None 168 | 169 | # Output file base name for HTML help builder. 170 | htmlhelp_basename = "envdirdoc" 171 | 172 | 173 | # -- Options for LaTeX output -------------------------------------------------- 174 | 175 | latex_elements = { 176 | # The paper size ('letterpaper' or 'a4paper'). 177 | #'papersize': 'letterpaper', 178 | # The font size ('10pt', '11pt' or '12pt'). 179 | #'pointsize': '10pt', 180 | # Additional stuff for the LaTeX preamble. 181 | #'preamble': '', 182 | } 183 | 184 | # Grouping the document tree into LaTeX files. List of tuples 185 | # (source start file, target name, title, author, documentclass [howto/manual]). 186 | latex_documents = [ 187 | ( 188 | "index", 189 | "envdir.tex", 190 | u"envdir Documentation", 191 | u"Jannis Leidel and contributors", 192 | "manual", 193 | ) 194 | ] 195 | 196 | # The name of an image file (relative to this directory) to place at the top of 197 | # the title page. 198 | # latex_logo = None 199 | 200 | # For "manual" documents, if this is true, then toplevel headings are parts, 201 | # not chapters. 202 | # latex_use_parts = False 203 | 204 | # If true, show page references after internal links. 205 | # latex_show_pagerefs = False 206 | 207 | # If true, show URL addresses after external links. 208 | # latex_show_urls = False 209 | 210 | # Documents to append as an appendix to all manuals. 211 | # latex_appendices = [] 212 | 213 | # If false, no module index is generated. 214 | # latex_domain_indices = True 215 | 216 | 217 | # -- Options for manual page output -------------------------------------------- 218 | 219 | # One entry per manual page. List of tuples 220 | # (source start file, name, description, authors, manual section). 221 | man_pages = [ 222 | ("index", "envdir", u"envdir Documentation", [u"Jannis Leidel and contributors"], 1) 223 | ] 224 | 225 | # If true, show URL addresses after external links. 226 | # man_show_urls = False 227 | 228 | 229 | # -- Options for Texinfo output ------------------------------------------------ 230 | 231 | # Grouping the document tree into Texinfo files. List of tuples 232 | # (source start file, target name, title, author, 233 | # dir menu entry, description, category) 234 | texinfo_documents = [ 235 | ( 236 | "index", 237 | "envdir", 238 | u"envdir Documentation", 239 | u"Jannis Leidel and contributors", 240 | "envdir", 241 | "One line description of project.", 242 | "Miscellaneous", 243 | ) 244 | ] 245 | 246 | # Documents to append as an appendix to all manuals. 247 | # texinfo_appendices = [] 248 | 249 | # If false, no module index is generated. 250 | # texinfo_domain_indices = True 251 | 252 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 253 | # texinfo_show_urls = 'footnote' 254 | 255 | # If true, do not generate a @detailmenu in the "Top" node's menu. 256 | # texinfo_no_detailmenu = False 257 | 258 | 259 | # -- Options for Epub output --------------------------------------------------- 260 | 261 | # Bibliographic Dublin Core info. 262 | epub_title = u"envdir" 263 | epub_author = u"Jannis Leidel and contributors" 264 | epub_publisher = u"Jannis Leidel and contributors" 265 | epub_copyright = u"2013, Jannis Leidel and contributors" 266 | 267 | # The language of the text. It defaults to the language option 268 | # or en if the language is not set. 269 | # epub_language = '' 270 | 271 | # The scheme of the identifier. Typical schemes are ISBN or URL. 272 | # epub_scheme = '' 273 | 274 | # The unique identifier of the text. This can be a ISBN number 275 | # or the project homepage. 276 | # epub_identifier = '' 277 | 278 | # A unique identification for the text. 279 | # epub_uid = '' 280 | 281 | # A tuple containing the cover image and cover page html template filenames. 282 | # epub_cover = () 283 | 284 | # A sequence of (type, uri, title) tuples for the guide element of content.opf. 285 | # epub_guide = () 286 | 287 | # HTML files that should be inserted before the pages created by sphinx. 288 | # The format is a list of tuples containing the path and title. 289 | # epub_pre_files = [] 290 | 291 | # HTML files shat should be inserted after the pages created by sphinx. 292 | # The format is a list of tuples containing the path and title. 293 | # epub_post_files = [] 294 | 295 | # A list of files that should not be packed into the epub file. 296 | # epub_exclude_files = [] 297 | 298 | # The depth of the table of contents in toc.ncx. 299 | # epub_tocdepth = 3 300 | 301 | # Allow duplicate toc entries. 302 | # epub_tocdup = True 303 | 304 | # Fix unsupported image types using the PIL. 305 | # epub_fix_images = False 306 | 307 | # Scale large images. 308 | # epub_max_image_width = 0 309 | 310 | # If 'no', URL addresses will not be shown. 311 | # epub_show_urls = 'inline' 312 | 313 | # If false, no index is generated. 314 | # epub_use_index = True 315 | 316 | 317 | # Example configuration for intersphinx: refer to the Python standard library. 318 | intersphinx_mapping = {"http://docs.python.org/": None} 319 | -------------------------------------------------------------------------------- /envdir/test_envdir.py: -------------------------------------------------------------------------------- 1 | import functools 2 | import os 3 | import platform 4 | import signal 5 | import subprocess 6 | import sys 7 | import threading 8 | 9 | import py 10 | import pytest 11 | 12 | import envdir 13 | from envdir.runner import Response 14 | 15 | try: 16 | FileNotFoundError 17 | except NameError: # " % tmpenvdir 356 | 357 | 358 | def test_dict_like(tmpenvdir): 359 | tmpenvdir.join("ITER").write("test") 360 | env = envdir.open(str(tmpenvdir)) 361 | assert list(env) == ["ITER"] 362 | assert hasattr(env, "__iter__") 363 | 364 | assert [k for k in env] == ["ITER"] 365 | assert list(env.values()) == ["test"] 366 | assert list(env.items()) == [("ITER", "test")] 367 | assert "ITER" in os.environ 368 | env.clear() 369 | assert list(env.items()) == [] 370 | assert "ITER" not in os.environ 371 | with pytest.raises(KeyError): 372 | env["DOESNOTEXISTS"] 373 | default = object() 374 | assert env.get("DOESNOTEXISTS", default) is default 375 | 376 | with envdir.open(str(tmpenvdir)) as env: 377 | assert list(env.items()) == [("ITER", "test")] 378 | 379 | 380 | def test_context_manager_reset(tmpenvdir): 381 | tmpenvdir.join("CONTEXT_MANAGER_RESET").write("test") 382 | # make the var exist in the enviroment 383 | os.environ["CONTEXT_MANAGER_RESET"] = "moot" 384 | with envdir.open(str(tmpenvdir)) as env: 385 | assert os.environ["CONTEXT_MANAGER_RESET"] == "test" 386 | env.clear() 387 | # because we reset the original value 388 | assert os.environ["CONTEXT_MANAGER_RESET"] == "moot" 389 | assert "CONTEXT_MANAGER_RESET" in os.environ 390 | 391 | 392 | def test_context_manager_write(tmpenvdir): 393 | with envdir.open(str(tmpenvdir)) as env: 394 | assert "CONTEXT_MANAGER_WRITE" not in os.environ 395 | env["CONTEXT_MANAGER_WRITE"] = "test" 396 | assert "CONTEXT_MANAGER_WRITE" in os.environ 397 | assert "CONTEXT_MANAGER_WRITE" not in os.environ 398 | 399 | 400 | def test_context_manager_item(tmpenvdir): 401 | tmpenvdir.join("CONTEXT_MANAGER_ITEM").write("test") 402 | 403 | with envdir.open(str(tmpenvdir)) as env: 404 | assert "CONTEXT_MANAGER_ITEM" in os.environ 405 | # the variable is in the env, but not in the env 406 | assert env["CONTEXT_MANAGER_ITEM"] == "test" 407 | del env["CONTEXT_MANAGER_ITEM"] 408 | assert "CONTEXT_MANAGER_ITEM" not in os.environ 409 | assert "CONTEXT_MANAGER_ITEM" not in env 410 | 411 | env["CONTEXT_MANAGER_ITEM_SET"] = "test" 412 | assert "CONTEXT_MANAGER_ITEM_SET" in os.environ 413 | assert tmpenvdir.join("CONTEXT_MANAGER_ITEM_SET").check() 414 | del env["CONTEXT_MANAGER_ITEM_SET"] 415 | assert "CONTEXT_MANAGER_ITEM_SET" not in os.environ 416 | assert not tmpenvdir.join("CONTEXT_MANAGER_ITEM_SET").check() 417 | assert tmpenvdir.ensure("CONTEXT_MANAGER_ITEM_SET") 418 | assert "CONTEXT_MANAGER_ITEM_SET" not in os.environ 419 | 420 | 421 | @pytest.mark.skipif( 422 | sys.platform == "win32", reason="Symlinks are not supported on windows" 423 | ) 424 | def test_envdir_follows_symlinks(run, tmpenvdir, monkeypatch): 425 | """Check envdir follows symbolic links""" 426 | monkeypatch.setattr(os, "execvpe", functools.partial(mocked_execvpe, monkeypatch)) 427 | tmpenvdir.join("DEFAULT").mksymlinkto("SYMLINK_ENV") 428 | tmpenvdir.join("DEFAULT").write("test") 429 | with py.test.raises(Response) as response: 430 | run("envdir", str(tmpenvdir), "ls") 431 | assert "DEFAULT" in os.environ 432 | assert "SYMLINK_ENV" in os.environ 433 | assert os.environ["SYMLINK_ENV"] == "test" 434 | assert response.value.status == 0 435 | assert response.value.message == "" 436 | 437 | 438 | @pytest.mark.skipif( 439 | sys.platform == "win32", reason="Symlinks are not supported on windows" 440 | ) 441 | def test_envdir_raise_broken_symlinks(run, tmpenvdir, monkeypatch): 442 | """If broken symlink is encountered, raise loudly""" 443 | monkeypatch.setattr(os, "execvpe", functools.partial(mocked_execvpe, monkeypatch)) 444 | tmpenvdir.join("DEFAULT").mksymlinkto("SYMLINK_ENV") 445 | tmpenvdir.join("DEFAULT").write("test") 446 | tmpenvdir.join("SYMLINK_ENV").remove() 447 | with pytest.raises(FileNotFoundError): 448 | run("envdir", str(tmpenvdir), "ls") 449 | --------------------------------------------------------------------------------