env-dir-bootstrap 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ """
2
+ env_dir_bootstrap package.
3
+
4
+ Provides a simple way to bootstrap a directory environment variable
5
+ with content from the distribution package.
6
+
7
+ :author: Ronaldo Webb
8
+ :since: 1.0.0
9
+ """
10
+
11
+ from .bootstrap import EnvDirBootstrap
12
+
13
+ __all__ = ["EnvDirBootstrap"]
14
+ __version__ = "1.0.0"
@@ -0,0 +1,179 @@
1
+ """
2
+ bootstrap module.
3
+
4
+ Provides the EnvDirBootstrap class for bootstrapping a directory from
5
+ distribution package resources driven by an environment variable.
6
+
7
+ :author: Ronaldo Webb
8
+ :since: 1.0.0
9
+ """
10
+
11
+ import importlib.resources
12
+ import importlib.resources.abc
13
+ import os
14
+ from collections.abc import Sequence
15
+ from pathlib import Path
16
+ from logenrich import setup_logger
17
+
18
+
19
+ class EnvDirBootstrap:
20
+ """
21
+ Bootstraps a directory from distribution package resources.
22
+
23
+ The directory path is read from an environment variable. On the first
24
+ call to :meth:`setup` the directory is created and the provided resources
25
+ are copied from the specified package. Subsequent calls to :meth:`setup`
26
+ are no-ops when the directory already exists.
27
+
28
+ If the environment variable is not set, :meth:`resolve` falls back to
29
+ locating the resource directly within the package.
30
+
31
+ :author: Ronaldo Webb
32
+ :since: 1.0.0
33
+ """
34
+
35
+ def __init__(self, env_var: str, resources: Sequence[str], package: str) -> None:
36
+ """
37
+ Initialise the bootstrapper.
38
+
39
+ :param env_var: Name of the environment variable that holds the
40
+ target directory path.
41
+ :param resources: Names of files or directories within *package*
42
+ to copy into the target directory on first bootstrap.
43
+ :param package: Fully-qualified name of the package that contains
44
+ the resources (e.g. ``"my_app.data"``).
45
+ :author: Ronaldo Webb
46
+ :since: 1.0.0
47
+ """
48
+ self._env_var = env_var
49
+ self._resources = tuple(resources)
50
+ self._package = package
51
+ self._logger = setup_logger(__name__)
52
+
53
+ def _get_env_dir(self) -> str | None:
54
+ """
55
+ Return the value of the environment variable, or *None* if unset.
56
+
57
+ :return: Directory path string or ``None``.
58
+ :author: Ronaldo Webb
59
+ :since: 1.0.0
60
+ """
61
+ return os.environ.get(self._env_var)
62
+
63
+ def _copy_traversable(
64
+ self,
65
+ source: importlib.resources.abc.Traversable,
66
+ target: Path,
67
+ ) -> None:
68
+ """
69
+ Recursively copy a *Traversable* directory tree into *target*.
70
+
71
+ :param source: Source traversable directory.
72
+ :param target: Destination directory path.
73
+ :author: Ronaldo Webb
74
+ :since: 1.0.0
75
+ """
76
+ for item in source.iterdir():
77
+ dest = target / item.name
78
+ if item.is_dir():
79
+ dest.mkdir(parents=True, exist_ok=True)
80
+ self._copy_traversable(item, dest)
81
+ else:
82
+ dest.write_bytes(item.read_bytes())
83
+
84
+ def _copy_resource(self, resource: str, target_dir: Path) -> None:
85
+ """
86
+ Copy a single package resource into *target_dir*.
87
+
88
+ If *resource* is a file it is copied directly. If it is a directory
89
+ it is copied recursively.
90
+
91
+ :param resource: Relative resource name within the package.
92
+ :param target_dir: Destination directory for the copied resource.
93
+ :author: Ronaldo Webb
94
+ :since: 1.0.0
95
+ """
96
+ ref = importlib.resources.files(self._package).joinpath(resource)
97
+ dest = target_dir / resource
98
+ if ref.is_file():
99
+ dest.parent.mkdir(parents=True, exist_ok=True)
100
+ dest.write_bytes(ref.read_bytes())
101
+ elif ref.is_dir():
102
+ dest.mkdir(parents=True, exist_ok=True)
103
+ self._copy_traversable(ref, dest)
104
+
105
+ def setup(self) -> None:
106
+ """
107
+ Bootstrap the environment directory.
108
+
109
+ Creates the directory specified by the environment variable and
110
+ copies any resources that are not already present in it. If the
111
+ environment variable is not set this method returns immediately
112
+ without making any changes. Resources that already exist in the
113
+ target directory are left untouched.
114
+
115
+ :author: Ronaldo Webb
116
+ :since: 1.0.0
117
+ """
118
+ env_dir = self._get_env_dir()
119
+ if env_dir is None:
120
+ self._logger.debug(
121
+ "Environment variable '%s' is not set; skipping setup.",
122
+ self._env_var,
123
+ )
124
+ return
125
+ target = Path(env_dir)
126
+ target.mkdir(parents=True, exist_ok=True)
127
+ copied = 0
128
+ for resource in self._resources:
129
+ if (target / resource).exists():
130
+ self._logger.debug(
131
+ "Resource '%s' already exists in '%s'; skipping.", resource, target
132
+ )
133
+ continue
134
+ self._copy_resource(resource, target)
135
+ copied += 1
136
+ if copied > 0:
137
+ self._logger.info(
138
+ "Directory '%s' bootstrapped with %d resource(s).",
139
+ target,
140
+ copied,
141
+ )
142
+
143
+ def resolve(self, name: str) -> Path:
144
+ """
145
+ Return the full path to *name* under the bootstrap directory.
146
+
147
+ If the environment variable is set, the path is constructed as
148
+ ``<env_dir>/<name>`` and returned only when it exists on the
149
+ file system. If the environment variable is not set, the resource
150
+ is looked up directly within the package and its path is returned.
151
+
152
+ :param name: Relative file or directory name to resolve.
153
+ :return: Absolute :class:`~pathlib.Path` to the resolved resource.
154
+ :raises FileNotFoundError: When the path does not exist under the
155
+ environment directory, or when the environment variable is unset
156
+ and the resource cannot be found in the package.
157
+ :author: Ronaldo Webb
158
+ :since: 1.0.0
159
+
160
+ .. note::
161
+ When the environment variable is unset, the path is derived
162
+ directly from the installed package resource tree. The package
163
+ must therefore be installed as an **unpacked directory** (the
164
+ default for ``pip install`` and ``poetry install``). Zip-archived
165
+ wheels or zip-import environments are not supported by this
166
+ fallback path.
167
+ """
168
+ env_dir = self._get_env_dir()
169
+ if env_dir is not None:
170
+ path = Path(env_dir).resolve() / name
171
+ if path.exists():
172
+ return path
173
+ raise FileNotFoundError(f"Path does not exist under '{env_dir}': {name}")
174
+ ref = importlib.resources.files(self._package).joinpath(name)
175
+ if ref.is_file() or ref.is_dir():
176
+ return Path(str(ref))
177
+ raise FileNotFoundError(
178
+ f"Resource '{name}' not found in package '{self._package}'."
179
+ )
@@ -0,0 +1,146 @@
1
+ Metadata-Version: 2.4
2
+ Name: env-dir-bootstrap
3
+ Version: 1.0.0
4
+ Summary: A Python package that provides a simple way to bootstrap a directory environment variable with the content coming from the distribution package.
5
+ License: MIT License
6
+
7
+ Copyright (c) 2026 Ronaldo Webb
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ SOFTWARE.
26
+ License-File: LICENSE
27
+ Author: Ronaldo Webb
28
+ Author-email: ron@ronella.xyz
29
+ Requires-Python: >=3.14
30
+ Classifier: License :: Other/Proprietary License
31
+ Classifier: Programming Language :: Python :: 3
32
+ Classifier: Programming Language :: Python :: 3.14
33
+ Requires-Dist: logenrich (>=1.0.0,<2.0.0)
34
+ Description-Content-Type: text/markdown
35
+
36
+ # env-dir-bootstrap 1.0.0
37
+
38
+ > A Python library that bootstraps a directory from a distribution package into a path driven by an environment variable.
39
+
40
+ On first run the target directory is created and the specified package resources are copied into it. Subsequent runs are no-ops when the directory already exists.
41
+
42
+ ## Prerequisites
43
+
44
+ - Python `^3.14`
45
+ - [Poetry](https://python-poetry.org/) `2.x`
46
+
47
+ ## Installation
48
+
49
+ ```bash
50
+ poetry add env-dir-bootstrap
51
+ ```
52
+
53
+ ## Usage
54
+
55
+ ```python
56
+ from env_dir_bootstrap import EnvDirBootstrap
57
+
58
+ # Point MY_APP_DIR at a writable directory path via environment variable.
59
+ bootstrapper = EnvDirBootstrap(
60
+ env_var="MY_APP_DIR",
61
+ resources=["config.yaml", "templates/"],
62
+ package="my_app.data",
63
+ )
64
+
65
+ # Create the directory and copy resources once (no-op if it already exists).
66
+ bootstrapper.setup()
67
+
68
+ # Resolve a resource path under the bootstrapped directory.
69
+ config_path = bootstrapper.resolve("config.yaml")
70
+ ```
71
+
72
+ ## Configuration
73
+
74
+ `EnvDirBootstrap` is configured through its constructor:
75
+
76
+ | Parameter | Type | Description |
77
+ |-------------|-------------------|-------------------------------------------------------------------------------|
78
+ | `env_var` | `str` | Environment variable whose value is the target directory path. |
79
+ | `resources` | `Sequence[str]` | File or sub-directory names within `package` to copy on first bootstrap. |
80
+ | `package` | `str` | Fully-qualified name of the package that contains the resources. |
81
+
82
+ Logging is configured via `logging.ini` at the project root. The log file is written to `env_dir_bootstrap.log`.
83
+
84
+ ## Development
85
+
86
+ ### Architecture
87
+
88
+ ```mermaid
89
+ graph TD
90
+ A[Application] -->|instantiates| B[EnvDirBootstrap]
91
+ B -->|reads| C[ENV_VAR]
92
+ C -->|resolves to| D[Target Directory]
93
+ B -->|first run: copies from| E[Distribution Package Resources]
94
+ E -->|into| D
95
+ B -->|resolve| F[Resource Path]
96
+ D -->|contains| F
97
+ ```
98
+
99
+ ### Install dependencies
100
+
101
+ ```bash
102
+ poetry install
103
+ ```
104
+
105
+ ### Run tests
106
+
107
+ ```bash
108
+ poetry run pytest --cov=env_dir_bootstrap tests --cov-report html
109
+ ```
110
+
111
+ ### Format and lint
112
+
113
+ ```bash
114
+ poetry run black env_dir_bootstrap
115
+ poetry run pylint env_dir_bootstrap
116
+ ```
117
+
118
+ ## Publishing to PyPI
119
+
120
+ ### Prerequisites
121
+
122
+ - A [PyPI](https://pypi.org/) account with an API token.
123
+
124
+ ### Configure the token
125
+
126
+ ```bash
127
+ poetry config pypi-token.pypi <your-token>
128
+ ```
129
+
130
+ ### Build and publish
131
+
132
+ ```bash
133
+ poetry publish --build
134
+ ```
135
+
136
+ This builds the source distribution and wheel, then uploads them to PyPI in one step.
137
+
138
+ > **Note:** PyPI releases are immutable. Once a version is published, it cannot be overwritten.
139
+ > To fix a mistake, yank the release via the PyPI web UI and publish a new version.
140
+
141
+ ## [Changelog](CHANGELOG.md)
142
+
143
+ ## Author(s)
144
+
145
+ **Ronaldo Webb** — ron@ronella.xyz
146
+
@@ -0,0 +1,6 @@
1
+ env_dir_bootstrap/__init__.py,sha256=tZxIggOSbGfC-hcRNAMbwUEyMBioTjo-uYWb7s3mWak,292
2
+ env_dir_bootstrap/bootstrap.py,sha256=46ppfgPagD5hxMbb8u1BrVagQNB2IBIIK8mQqtieCD4,6732
3
+ env_dir_bootstrap-1.0.0.dist-info/licenses/LICENSE,sha256=oF52tb_UOAk1k2zlljzGTkTCURKAS6ARSLDsoCptyUA,1090
4
+ env_dir_bootstrap-1.0.0.dist-info/METADATA,sha256=G96Nd4iyYVmyIh3qTZcOoTKTMVAnM4JV0Zt8BjO1hUY,4689
5
+ env_dir_bootstrap-1.0.0.dist-info/WHEEL,sha256=Vz2fHgx6HFtSwhs8KvkHLqH5Ea4w1_rner5uNVGCeIE,88
6
+ env_dir_bootstrap-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 2.3.2
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ronaldo Webb
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.