Skip to content

Commit 4cd875c

Browse files
authored
add install command (#108)
* add install command * improve usage without import docs * fix linting, tweak __main__.py
1 parent 352dc9e commit 4cd875c

File tree

6 files changed

+105
-26
lines changed

6 files changed

+105
-26
lines changed

README.md

+4-10
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,8 @@ outputs:
6161

6262
## Usage without Import
6363

64-
modify `/usr/lib/python3.8/sitecustomize.py` making `debug` available in any python 3.8 code
64+
devtools can be used without `from devtools import debug` if you add `debug` into `__builtins__`
65+
in `sitecustomize.py`.
6566

66-
```py
67-
# add devtools debug to builtins
68-
try:
69-
from devtools import debug
70-
except ImportError:
71-
pass
72-
else:
73-
__builtins__['debug'] = debug
74-
```
67+
For instructions on adding `debug` to `__builtins__`,
68+
see the [installation docs](https://python-devtools.helpmanual.io/usage/#usage-without-import).

devtools/__main__.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import os
2+
import sys
3+
from pathlib import Path
4+
5+
from .version import VERSION
6+
7+
# language=python
8+
install_code = """
9+
# add devtools `debug` function to builtins
10+
try:
11+
from devtools import debug
12+
except ImportError:
13+
pass
14+
else:
15+
__builtins__['debug'] = debug
16+
"""
17+
18+
19+
def print_code() -> int:
20+
print(install_code)
21+
return 0
22+
23+
24+
def install() -> int:
25+
print('[WARNING: this command is experimental, report issues at github.com/samuelcolvin/python-devtools]\n')
26+
27+
if 'debug' in __builtins__.__dict__:
28+
print('Looks like devtools is already installed.')
29+
return 0
30+
31+
try:
32+
import sitecustomize # type: ignore
33+
except ImportError:
34+
paths = [Path(p) for p in sys.path]
35+
try:
36+
path = next(p for p in paths if p.is_dir() and p.name == 'site-packages')
37+
except StopIteration:
38+
# what else makes sense to try?
39+
print(f'unable to file a suitable path to save `sitecustomize.py` to from sys.path: {paths}')
40+
return 1
41+
else:
42+
install_path = path / 'sitecustomize.py'
43+
else:
44+
install_path = Path(sitecustomize.__file__)
45+
46+
print(f'Found path "{install_path}" to install devtools into __builtins__')
47+
print('To install devtools, run the following command:\n')
48+
if os.access(install_path, os.W_OK):
49+
print(f' python -m devtools print-code >> {install_path}\n')
50+
else:
51+
print(f' python -m devtools print-code | sudo tee -a {install_path} > /dev/null\n')
52+
print('Note: "sudo" is required because the path is not writable by the current user.')
53+
54+
return 0
55+
56+
57+
if __name__ == '__main__':
58+
if 'install' in sys.argv:
59+
sys.exit(install())
60+
elif 'print-code' in sys.argv:
61+
sys.exit(print_code())
62+
else:
63+
print(f'python-devtools v{VERSION}, CLI usage: python -m devtools [install|print-code]')
64+
sys.exit(1)

docs/examples/sitecustomize.py

-8
This file was deleted.

docs/plugins.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def build_history():
3939
history = re.sub(r'#(\d+)', r'[#\1](https://github.com/samuelcolvin/python-devtools/issues/\1)', history)
4040
history = re.sub(r'( +)@([\w\-]+)', r'\1[@\2](https://github.com/\2)', history, flags=re.I)
4141
history = re.sub('@@', '@', history)
42-
(THIS_DIR / '.history.md').write_text(history)
42+
output_file = THIS_DIR / '.history.md'
43+
if not output_file.exists() or history != output_file.read_text():
44+
(THIS_DIR / '.history.md').write_text(history)
4345

4446

4547
def gen_example_html(markdown: str):
@@ -71,10 +73,12 @@ def set_version(markdown: str, page: Page) -> str:
7173
def remove_files(files: Files) -> Files:
7274
to_remove = []
7375
for file in files:
74-
if file.src_path in {'plugins.py', 'requirements.txt'}:
76+
if file.src_path == 'requirements.txt':
7577
to_remove.append(file)
7678
elif file.src_path.startswith('__pycache__/'):
7779
to_remove.append(file)
80+
elif file.src_path.endswith('.py'):
81+
to_remove.append(file)
7882

7983
logger.debug('removing files: %s', [f.src_path for f in to_remove])
8084
for f in to_remove:

docs/usage.md

+30-6
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,43 @@ For more details on prettier printing, see
8383
For more details on ansi colours, see
8484
[ansi.py](https://github.com/samuelcolvin/python-devtools/blob/main/devtools/ansi.py).
8585

86-
## Usage without import
86+
## Usage without Import
8787

8888
We all know the annoyance of running code only to discover a missing import, this can be particularly
8989
frustrating when the function you're using isn't used except during development.
9090

91-
You can setup your environment to make `debug` available at all times by editing `sitecustomize.py`,
92-
with ubuntu and python3.8 this file can be found at `/usr/lib/python3.8/sitecustomize.py` but you might
93-
need to look elsewhere depending on your OS/python version.
91+
devtool's `debug` function can be used without import if you add `debug` to `__builtins__`
92+
in `sitecustomize.py`.
9493

95-
Add the following to `sitecustomize.py`
94+
Two ways to do this:
95+
96+
### Automatic install
97+
98+
!!! warning
99+
This is experimental, please [create an issue](https://github.com/samuelcolvin/python-devtools/issues)
100+
if you encounter any problems.
101+
102+
To install `debug` into `__builtins__` automatically, run:
103+
104+
```bash
105+
python -m devtools install
106+
```
107+
108+
This command won't write to any files, but it should print a command for you to run to add/edit `sitecustomize.py`.
109+
110+
### Manual install
111+
112+
To manually add `debug` to `__builtins__`, add the following to `sitecustomize.py` or any code
113+
which is always imported.
96114

97115
```py
98-
{!examples/sitecustomize.py!}
116+
# add devtools `debug` function to builtins
117+
try:
118+
from devtools import debug
119+
except ImportError:
120+
pass
121+
else:
122+
__builtins__['debug'] = debug
99123
```
100124

101125
The `ImportError` exception is important since you'll want python to run fine even if *devtools* isn't installed.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ filterwarnings = 'error'
5454
[tool.coverage.run]
5555
source = ['devtools']
5656
branch = true
57+
omit = ['devtools/__main__.py']
5758

5859
[tool.coverage.report]
5960
precision = 2

0 commit comments

Comments
 (0)