fsg 0.1.0__tar.gz
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.
- fsg-0.1.0/.gitignore +9 -0
- fsg-0.1.0/LICENSE +21 -0
- fsg-0.1.0/PKG-INFO +161 -0
- fsg-0.1.0/README.md +134 -0
- fsg-0.1.0/pyproject.toml +56 -0
- fsg-0.1.0/src/fsg/__init__.py +3 -0
- fsg-0.1.0/src/fsg/__main__.py +4 -0
- fsg-0.1.0/src/fsg/cli.py +209 -0
- fsg-0.1.0/src/fsg/config.py +142 -0
- fsg-0.1.0/src/fsg/constants.py +13 -0
- fsg-0.1.0/src/fsg/hooks/__init__.py +5 -0
- fsg-0.1.0/src/fsg/hooks/git_hook.py +69 -0
- fsg-0.1.0/src/fsg/hooks/hook.py +70 -0
- fsg-0.1.0/src/fsg/hooks/pre_commit_hook.py +74 -0
- fsg-0.1.0/src/fsg/readme_section.py +89 -0
- fsg-0.1.0/src/fsg/trees/__init__.py +5 -0
- fsg-0.1.0/src/fsg/trees/git_project_tree.py +44 -0
- fsg-0.1.0/src/fsg/trees/plain_project_tree.py +34 -0
- fsg-0.1.0/src/fsg/trees/project_tree.py +112 -0
- fsg-0.1.0/tests/__init__.py +0 -0
- fsg-0.1.0/tests/conftest.py +60 -0
- fsg-0.1.0/tests/hooks/__init__.py +0 -0
- fsg-0.1.0/tests/hooks/test_git_hook.py +99 -0
- fsg-0.1.0/tests/hooks/test_pre_commit_hook.py +112 -0
- fsg-0.1.0/tests/test_cli.py +446 -0
- fsg-0.1.0/tests/test_config.py +174 -0
- fsg-0.1.0/tests/test_readme_section.py +172 -0
- fsg-0.1.0/tests/trees/__init__.py +0 -0
- fsg-0.1.0/tests/trees/test_git_project_tree.py +85 -0
- fsg-0.1.0/tests/trees/test_plain_project_tree.py +65 -0
- fsg-0.1.0/tests/trees/test_project_tree.py +233 -0
fsg-0.1.0/.gitignore
ADDED
fsg-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Patrick Hähnel
|
|
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.
|
fsg-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: fsg
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Generates clean directory trees that respect .gitignore and keep your docs up to date.
|
|
5
|
+
Project-URL: Homepage, https://github.com/KarnesTH/fsg
|
|
6
|
+
Project-URL: Issues, https://github.com/KarnesTH/fsg/issues
|
|
7
|
+
Project-URL: Changelog, https://github.com/KarnesTH/fsg/blob/main/CHANGELOG.md
|
|
8
|
+
Author: Patrick Hähnel
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: cli,directory,documentation,gitignore,pre-commit,project-structure,readme,tree
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Environment :: Console
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Classifier: Topic :: Documentation
|
|
23
|
+
Classifier: Topic :: Software Development :: Documentation
|
|
24
|
+
Classifier: Topic :: Utilities
|
|
25
|
+
Requires-Python: >=3.11
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
<p align="center">
|
|
29
|
+
<img src="assets/logo.svg" alt="fsg logo" width="128" />
|
|
30
|
+
</p>
|
|
31
|
+
|
|
32
|
+
<h1 align="center">fsg</h1>
|
|
33
|
+
|
|
34
|
+
<p align="center">
|
|
35
|
+
<img src="https://img.shields.io/badge/version-0.1.0-blue" alt="Version 0.1.0" />
|
|
36
|
+
<img src="https://img.shields.io/badge/python-3.11%2B-3776AB?logo=python&logoColor=white" alt="Python 3.11+" />
|
|
37
|
+
<img src="https://img.shields.io/badge/dependencies-none-brightgreen" alt="No dependencies" />
|
|
38
|
+
<img src="https://img.shields.io/badge/license-MIT-green" alt="MIT License" />
|
|
39
|
+
</p>
|
|
40
|
+
|
|
41
|
+
<p align="center">Generates clean directory trees that respect .gitignore and keep your docs up to date.</p>
|
|
42
|
+
|
|
43
|
+
## Features
|
|
44
|
+
|
|
45
|
+
- Inside a git repository, only files git tracks or would track are listed (`.gitignore` is respected)
|
|
46
|
+
- Outside a repository, common generated and tool directories are skipped (`.venv`, `__pycache__`, `node_modules`, `target`, `.idea`, …)
|
|
47
|
+
- Directories first, entries sorted case-insensitively, empty directories omitted
|
|
48
|
+
- Keeps a marked section of your README up to date, with a check mode for CI
|
|
49
|
+
- Sets up the commit hook for you: extends an existing `.pre-commit-config.yaml` or the native git hook, never overwrites
|
|
50
|
+
- Limit the depth, render directories only or exclude entries by glob pattern
|
|
51
|
+
- Options can live in `.fsg.toml` or `[tool.fsg]` in `pyproject.toml`
|
|
52
|
+
- Output is wrapped in a `plaintext` code fence (optional) and always written as UTF-8
|
|
53
|
+
- Optional ASCII branches (`|--`, `` `-- ``) for environments without Unicode support
|
|
54
|
+
|
|
55
|
+
## Installation
|
|
56
|
+
|
|
57
|
+
Requires Python 3.11+ and git (optional, for `.gitignore` support).
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
uv tool install fsg # or: pipx install fsg
|
|
61
|
+
uvx fsg # run without installing
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Usage
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
fsg [options] [path]
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
| Argument | Description |
|
|
71
|
+
|----------------------------------|-----------------------------------------------------------------------------|
|
|
72
|
+
| `path` | Directory to render (default: current directory) |
|
|
73
|
+
| `-d N`, `--depth N` | Render only the first N levels below the root |
|
|
74
|
+
| `--dirs-only`, `--no-dirs-only` | Render directories only |
|
|
75
|
+
| `-e`, `--exclude PATTERN` | Leave out entries whose name or relative path matches the glob (repeatable) |
|
|
76
|
+
| `--fence`, `--no-fence` | Wrap the tree in a Markdown code fence (default: on) |
|
|
77
|
+
| `--ascii`, `--no-ascii` | Draw the tree with ASCII instead of box-drawing characters |
|
|
78
|
+
| `-u`, `--update FILE` | Write the tree between the fsg markers of FILE |
|
|
79
|
+
| `-c`, `--check FILE` | Exit with status 1 when the tree in FILE is out of date |
|
|
80
|
+
| `--setup-hook FILE` | Run `fsg --update FILE` on every commit |
|
|
81
|
+
| `-h`, `--help` | Show the help text and exit |
|
|
82
|
+
| `-V`, `--version` | Show the version and exit |
|
|
83
|
+
|
|
84
|
+
Example:
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
fsg src --depth 2
|
|
88
|
+
```
|
|
89
|
+
<!-- fsg:start -->
|
|
90
|
+
```plaintext
|
|
91
|
+
src/
|
|
92
|
+
└── fsg/
|
|
93
|
+
├── hooks/
|
|
94
|
+
├── trees/
|
|
95
|
+
├── __init__.py
|
|
96
|
+
├── __main__.py
|
|
97
|
+
├── cli.py
|
|
98
|
+
├── config.py
|
|
99
|
+
├── constants.py
|
|
100
|
+
└── readme_section.py
|
|
101
|
+
```
|
|
102
|
+
<!-- fsg:end -->
|
|
103
|
+
|
|
104
|
+
## Configuration
|
|
105
|
+
|
|
106
|
+
With `--update` and `--check`, the path and all options not given on the command line are read from `.fsg.toml`
|
|
107
|
+
or the `[tool.fsg]` table in `pyproject.toml`, searched from the directory of the target file upwards.
|
|
108
|
+
`path` is relative to the config file. Command line options take precedence, exclude patterns are combined.
|
|
109
|
+
Plain `fsg` ignores the config.
|
|
110
|
+
|
|
111
|
+
```toml
|
|
112
|
+
[tool.fsg]
|
|
113
|
+
path = "src"
|
|
114
|
+
depth = 2
|
|
115
|
+
dirs-only = false
|
|
116
|
+
exclude = ["*.log", "docs/api"]
|
|
117
|
+
fence = true
|
|
118
|
+
ascii = false
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Keep your README up to date
|
|
122
|
+
|
|
123
|
+
Add the markers where the tree should appear:
|
|
124
|
+
|
|
125
|
+
```markdown
|
|
126
|
+
<!-- fsg:start -->
|
|
127
|
+
<!-- fsg:end -->
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
fsg --update README.md # writes the tree between the markers
|
|
132
|
+
fsg --check README.md # exit 1 if outdated, 2 if the file or markers are missing
|
|
133
|
+
fsg --setup-hook README.md # updates the tree on every commit
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
`--setup-hook` adds fsg to an existing `.pre-commit-config.yaml`. Without one, it adds a step to the git
|
|
137
|
+
`pre-commit` hook that updates the tree and stops the commit so you can stage the change.
|
|
138
|
+
|
|
139
|
+
### pre-commit
|
|
140
|
+
|
|
141
|
+
To add the hook by hand:
|
|
142
|
+
|
|
143
|
+
```yaml
|
|
144
|
+
repos:
|
|
145
|
+
- repo: https://github.com/KarnesTH/fsg
|
|
146
|
+
rev: v0.1.0
|
|
147
|
+
hooks:
|
|
148
|
+
- id: fsg # updates README.md
|
|
149
|
+
# - id: fsg-check # only checks, e.g. in CI
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Use `args` for another file, e.g. `args: [--update, docs/structure.md]`.
|
|
153
|
+
|
|
154
|
+
## Development
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
git clone https://github.com/KarnesTH/fsg.git
|
|
158
|
+
cd fsg
|
|
159
|
+
uv sync
|
|
160
|
+
uv run pytest
|
|
161
|
+
```
|
fsg-0.1.0/README.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="assets/logo.svg" alt="fsg logo" width="128" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">fsg</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<img src="https://img.shields.io/badge/version-0.1.0-blue" alt="Version 0.1.0" />
|
|
9
|
+
<img src="https://img.shields.io/badge/python-3.11%2B-3776AB?logo=python&logoColor=white" alt="Python 3.11+" />
|
|
10
|
+
<img src="https://img.shields.io/badge/dependencies-none-brightgreen" alt="No dependencies" />
|
|
11
|
+
<img src="https://img.shields.io/badge/license-MIT-green" alt="MIT License" />
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
<p align="center">Generates clean directory trees that respect .gitignore and keep your docs up to date.</p>
|
|
15
|
+
|
|
16
|
+
## Features
|
|
17
|
+
|
|
18
|
+
- Inside a git repository, only files git tracks or would track are listed (`.gitignore` is respected)
|
|
19
|
+
- Outside a repository, common generated and tool directories are skipped (`.venv`, `__pycache__`, `node_modules`, `target`, `.idea`, …)
|
|
20
|
+
- Directories first, entries sorted case-insensitively, empty directories omitted
|
|
21
|
+
- Keeps a marked section of your README up to date, with a check mode for CI
|
|
22
|
+
- Sets up the commit hook for you: extends an existing `.pre-commit-config.yaml` or the native git hook, never overwrites
|
|
23
|
+
- Limit the depth, render directories only or exclude entries by glob pattern
|
|
24
|
+
- Options can live in `.fsg.toml` or `[tool.fsg]` in `pyproject.toml`
|
|
25
|
+
- Output is wrapped in a `plaintext` code fence (optional) and always written as UTF-8
|
|
26
|
+
- Optional ASCII branches (`|--`, `` `-- ``) for environments without Unicode support
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
Requires Python 3.11+ and git (optional, for `.gitignore` support).
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
uv tool install fsg # or: pipx install fsg
|
|
34
|
+
uvx fsg # run without installing
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
fsg [options] [path]
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
| Argument | Description |
|
|
44
|
+
|----------------------------------|-----------------------------------------------------------------------------|
|
|
45
|
+
| `path` | Directory to render (default: current directory) |
|
|
46
|
+
| `-d N`, `--depth N` | Render only the first N levels below the root |
|
|
47
|
+
| `--dirs-only`, `--no-dirs-only` | Render directories only |
|
|
48
|
+
| `-e`, `--exclude PATTERN` | Leave out entries whose name or relative path matches the glob (repeatable) |
|
|
49
|
+
| `--fence`, `--no-fence` | Wrap the tree in a Markdown code fence (default: on) |
|
|
50
|
+
| `--ascii`, `--no-ascii` | Draw the tree with ASCII instead of box-drawing characters |
|
|
51
|
+
| `-u`, `--update FILE` | Write the tree between the fsg markers of FILE |
|
|
52
|
+
| `-c`, `--check FILE` | Exit with status 1 when the tree in FILE is out of date |
|
|
53
|
+
| `--setup-hook FILE` | Run `fsg --update FILE` on every commit |
|
|
54
|
+
| `-h`, `--help` | Show the help text and exit |
|
|
55
|
+
| `-V`, `--version` | Show the version and exit |
|
|
56
|
+
|
|
57
|
+
Example:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
fsg src --depth 2
|
|
61
|
+
```
|
|
62
|
+
<!-- fsg:start -->
|
|
63
|
+
```plaintext
|
|
64
|
+
src/
|
|
65
|
+
└── fsg/
|
|
66
|
+
├── hooks/
|
|
67
|
+
├── trees/
|
|
68
|
+
├── __init__.py
|
|
69
|
+
├── __main__.py
|
|
70
|
+
├── cli.py
|
|
71
|
+
├── config.py
|
|
72
|
+
├── constants.py
|
|
73
|
+
└── readme_section.py
|
|
74
|
+
```
|
|
75
|
+
<!-- fsg:end -->
|
|
76
|
+
|
|
77
|
+
## Configuration
|
|
78
|
+
|
|
79
|
+
With `--update` and `--check`, the path and all options not given on the command line are read from `.fsg.toml`
|
|
80
|
+
or the `[tool.fsg]` table in `pyproject.toml`, searched from the directory of the target file upwards.
|
|
81
|
+
`path` is relative to the config file. Command line options take precedence, exclude patterns are combined.
|
|
82
|
+
Plain `fsg` ignores the config.
|
|
83
|
+
|
|
84
|
+
```toml
|
|
85
|
+
[tool.fsg]
|
|
86
|
+
path = "src"
|
|
87
|
+
depth = 2
|
|
88
|
+
dirs-only = false
|
|
89
|
+
exclude = ["*.log", "docs/api"]
|
|
90
|
+
fence = true
|
|
91
|
+
ascii = false
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Keep your README up to date
|
|
95
|
+
|
|
96
|
+
Add the markers where the tree should appear:
|
|
97
|
+
|
|
98
|
+
```markdown
|
|
99
|
+
<!-- fsg:start -->
|
|
100
|
+
<!-- fsg:end -->
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
fsg --update README.md # writes the tree between the markers
|
|
105
|
+
fsg --check README.md # exit 1 if outdated, 2 if the file or markers are missing
|
|
106
|
+
fsg --setup-hook README.md # updates the tree on every commit
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
`--setup-hook` adds fsg to an existing `.pre-commit-config.yaml`. Without one, it adds a step to the git
|
|
110
|
+
`pre-commit` hook that updates the tree and stops the commit so you can stage the change.
|
|
111
|
+
|
|
112
|
+
### pre-commit
|
|
113
|
+
|
|
114
|
+
To add the hook by hand:
|
|
115
|
+
|
|
116
|
+
```yaml
|
|
117
|
+
repos:
|
|
118
|
+
- repo: https://github.com/KarnesTH/fsg
|
|
119
|
+
rev: v0.1.0
|
|
120
|
+
hooks:
|
|
121
|
+
- id: fsg # updates README.md
|
|
122
|
+
# - id: fsg-check # only checks, e.g. in CI
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Use `args` for another file, e.g. `args: [--update, docs/structure.md]`.
|
|
126
|
+
|
|
127
|
+
## Development
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
git clone https://github.com/KarnesTH/fsg.git
|
|
131
|
+
cd fsg
|
|
132
|
+
uv sync
|
|
133
|
+
uv run pytest
|
|
134
|
+
```
|
fsg-0.1.0/pyproject.toml
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "fsg"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Generates clean directory trees that respect .gitignore and keep your docs up to date."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
authors = [{ name = "Patrick Hähnel" }]
|
|
9
|
+
requires-python = ">=3.11"
|
|
10
|
+
dependencies = []
|
|
11
|
+
keywords = ["tree", "directory", "project-structure", "readme", "documentation", "gitignore", "pre-commit", "cli"]
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Development Status :: 4 - Beta",
|
|
14
|
+
"Environment :: Console",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
19
|
+
"Programming Language :: Python :: 3.11",
|
|
20
|
+
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"Programming Language :: Python :: 3.13",
|
|
22
|
+
"Programming Language :: Python :: 3.14",
|
|
23
|
+
"Topic :: Documentation",
|
|
24
|
+
"Topic :: Software Development :: Documentation",
|
|
25
|
+
"Topic :: Utilities",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://github.com/KarnesTH/fsg"
|
|
30
|
+
Issues = "https://github.com/KarnesTH/fsg/issues"
|
|
31
|
+
Changelog = "https://github.com/KarnesTH/fsg/blob/main/CHANGELOG.md"
|
|
32
|
+
|
|
33
|
+
[project.scripts]
|
|
34
|
+
fsg = "fsg.cli:main"
|
|
35
|
+
|
|
36
|
+
[dependency-groups]
|
|
37
|
+
dev = [
|
|
38
|
+
"pytest>=9.1.1",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[build-system]
|
|
42
|
+
requires = ["hatchling>=1.27"]
|
|
43
|
+
build-backend = "hatchling.build"
|
|
44
|
+
|
|
45
|
+
[tool.hatch.build.targets.wheel]
|
|
46
|
+
packages = ["src/fsg"]
|
|
47
|
+
|
|
48
|
+
[tool.hatch.build.targets.sdist]
|
|
49
|
+
include = ["src", "tests", "CHANGELOG.md"]
|
|
50
|
+
|
|
51
|
+
[tool.pytest.ini_options]
|
|
52
|
+
testpaths = ["tests"]
|
|
53
|
+
|
|
54
|
+
[tool.fsg]
|
|
55
|
+
path = "src"
|
|
56
|
+
depth = 2
|
fsg-0.1.0/src/fsg/cli.py
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import io
|
|
3
|
+
import subprocess
|
|
4
|
+
import sys
|
|
5
|
+
from importlib.metadata import version
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from fsg.config import Config
|
|
9
|
+
from fsg.hooks import GitHook, Hook
|
|
10
|
+
from fsg.readme_section import ReadmeSection
|
|
11
|
+
from fsg.trees import ProjectTree
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
|
|
15
|
+
"""Parses the command line arguments and returns the resulting namespace.
|
|
16
|
+
|
|
17
|
+
Boolean options are None when not given.
|
|
18
|
+
Exits with status 2 when --depth is smaller than 1.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
argv: Argument list to parse instead of the command line arguments.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
parser = argparse.ArgumentParser(
|
|
25
|
+
prog="fsg",
|
|
26
|
+
description="Generates clean directory trees that respect .gitignore and keep your docs up to date.",
|
|
27
|
+
epilog=(
|
|
28
|
+
"With --update or --check, the path and options not given on the command line are read from "
|
|
29
|
+
".fsg.toml or [tool.fsg] in pyproject.toml, searched from the directory of FILE upwards."
|
|
30
|
+
),
|
|
31
|
+
)
|
|
32
|
+
parser.add_argument(
|
|
33
|
+
"path",
|
|
34
|
+
nargs="?",
|
|
35
|
+
type=Path,
|
|
36
|
+
help="project directory to render (default: the configured path or the current directory)",
|
|
37
|
+
)
|
|
38
|
+
parser.add_argument(
|
|
39
|
+
"-d",
|
|
40
|
+
"--depth",
|
|
41
|
+
type=int,
|
|
42
|
+
metavar="N",
|
|
43
|
+
help="render only the first N levels below the root",
|
|
44
|
+
)
|
|
45
|
+
parser.add_argument(
|
|
46
|
+
"--dirs-only",
|
|
47
|
+
action=argparse.BooleanOptionalAction,
|
|
48
|
+
help="render directories only",
|
|
49
|
+
)
|
|
50
|
+
parser.add_argument(
|
|
51
|
+
"-e",
|
|
52
|
+
"--exclude",
|
|
53
|
+
action="append",
|
|
54
|
+
default=[],
|
|
55
|
+
metavar="PATTERN",
|
|
56
|
+
help="leave out entries whose name or relative path matches the glob pattern (repeatable)",
|
|
57
|
+
)
|
|
58
|
+
parser.add_argument(
|
|
59
|
+
"--fence",
|
|
60
|
+
action=argparse.BooleanOptionalAction,
|
|
61
|
+
help="wrap the tree in a Markdown code fence (default: on)",
|
|
62
|
+
)
|
|
63
|
+
parser.add_argument(
|
|
64
|
+
"--ascii",
|
|
65
|
+
action=argparse.BooleanOptionalAction,
|
|
66
|
+
dest="use_ascii",
|
|
67
|
+
help="draw the tree with ASCII characters instead of box-drawing characters",
|
|
68
|
+
)
|
|
69
|
+
mode = parser.add_mutually_exclusive_group()
|
|
70
|
+
mode.add_argument(
|
|
71
|
+
"-u",
|
|
72
|
+
"--update",
|
|
73
|
+
type=Path,
|
|
74
|
+
metavar="FILE",
|
|
75
|
+
help="write the tree between the <!-- fsg:start --> and <!-- fsg:end --> markers of FILE",
|
|
76
|
+
)
|
|
77
|
+
mode.add_argument(
|
|
78
|
+
"-c",
|
|
79
|
+
"--check",
|
|
80
|
+
type=Path,
|
|
81
|
+
metavar="FILE",
|
|
82
|
+
help="exit with status 1 when the tree between the markers of FILE is out of date",
|
|
83
|
+
)
|
|
84
|
+
mode.add_argument(
|
|
85
|
+
"--setup-hook",
|
|
86
|
+
type=Path,
|
|
87
|
+
metavar="FILE",
|
|
88
|
+
help="add 'fsg --update FILE' to .pre-commit-config.yaml if present, otherwise to the git pre-commit hook",
|
|
89
|
+
)
|
|
90
|
+
parser.add_argument(
|
|
91
|
+
"-V",
|
|
92
|
+
"--version",
|
|
93
|
+
action="version",
|
|
94
|
+
version=f"%(prog)s {version('fsg')}",
|
|
95
|
+
)
|
|
96
|
+
args = parser.parse_args(argv)
|
|
97
|
+
if args.depth is not None and args.depth < 1:
|
|
98
|
+
parser.error("--depth must be at least 1")
|
|
99
|
+
return args
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def main(argv: list[str] | None = None) -> int:
|
|
103
|
+
"""Runs the mode selected by the arguments and returns the exit status.
|
|
104
|
+
|
|
105
|
+
Without a mode the tree is rendered from the arguments alone and written UTF-8 encoded to stdout.
|
|
106
|
+
With --update or --check the config found from the directory of the target file fills in the path
|
|
107
|
+
and every option not given. Returns 0 on success, 1 when the path is not a directory or --check finds
|
|
108
|
+
an outdated tree, and 2 when a file cannot be read, lacks the markers, the config is invalid or the
|
|
109
|
+
hook cannot be set up.
|
|
110
|
+
|
|
111
|
+
Args:
|
|
112
|
+
argv: Argument list to use instead of the command line arguments.
|
|
113
|
+
"""
|
|
114
|
+
|
|
115
|
+
args = parse_args(argv)
|
|
116
|
+
for stream in (sys.stdout, sys.stderr):
|
|
117
|
+
if isinstance(stream, io.TextIOWrapper):
|
|
118
|
+
stream.reconfigure(encoding="utf-8")
|
|
119
|
+
if args.setup_hook:
|
|
120
|
+
return _setup_hook(args.setup_hook)
|
|
121
|
+
target = args.update or args.check
|
|
122
|
+
config = Config()
|
|
123
|
+
if target is not None:
|
|
124
|
+
try:
|
|
125
|
+
config = Config.find(target.resolve().parent)
|
|
126
|
+
except (OSError, ValueError) as error:
|
|
127
|
+
return _fail(str(error), 2)
|
|
128
|
+
root = args.path or config.path or Path(".")
|
|
129
|
+
if not root.is_dir():
|
|
130
|
+
return _fail(f"{root}: not a directory", 1)
|
|
131
|
+
output = ProjectTree.for_path(root, **config.merge(args)).render()
|
|
132
|
+
if target is None:
|
|
133
|
+
print(output)
|
|
134
|
+
return 0
|
|
135
|
+
return _sync(target, output, check=bool(args.check))
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def _sync(target: Path, output: str, check: bool) -> int:
|
|
139
|
+
"""Compares or writes the tree in the marked section of the target and prints one status line.
|
|
140
|
+
|
|
141
|
+
Args:
|
|
142
|
+
target: File holding the markers.
|
|
143
|
+
output: Rendered tree.
|
|
144
|
+
check: Only compares when True, otherwise writes the tree.
|
|
145
|
+
"""
|
|
146
|
+
|
|
147
|
+
try:
|
|
148
|
+
section = ReadmeSection(target)
|
|
149
|
+
except FileNotFoundError:
|
|
150
|
+
return _fail(f"{target}: file not found", 2)
|
|
151
|
+
except UnicodeDecodeError:
|
|
152
|
+
return _fail(f"{target}: not a UTF-8 text file", 2)
|
|
153
|
+
except (OSError, ValueError) as error:
|
|
154
|
+
return _fail(f"{target}: {getattr(error, 'strerror', None) or error}", 2)
|
|
155
|
+
if check:
|
|
156
|
+
if section.is_current(output):
|
|
157
|
+
print(f"{target}: tree is up to date")
|
|
158
|
+
return 0
|
|
159
|
+
print(f"{target}: tree is out of date, run fsg --update {target}", file=sys.stderr)
|
|
160
|
+
return 1
|
|
161
|
+
if section.update(output):
|
|
162
|
+
print(f"{target}: tree updated")
|
|
163
|
+
else:
|
|
164
|
+
print(f"{target}: tree already up to date")
|
|
165
|
+
return 0
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _setup_hook(target: Path) -> int:
|
|
169
|
+
"""Adds the update step for the target to the hooks of the repository around the current directory and prints the result.
|
|
170
|
+
|
|
171
|
+
Args:
|
|
172
|
+
target: File holding the markers, relative to the current directory.
|
|
173
|
+
"""
|
|
174
|
+
|
|
175
|
+
repository = Hook.repository_root(Path.cwd())
|
|
176
|
+
if repository is None:
|
|
177
|
+
return _fail("not inside a git repository", 2)
|
|
178
|
+
try:
|
|
179
|
+
relative = target.resolve().relative_to(repository.resolve()).as_posix()
|
|
180
|
+
except ValueError:
|
|
181
|
+
return _fail(f"{target}: not inside the repository {repository}", 2)
|
|
182
|
+
hook = Hook.for_repository(repository, relative)
|
|
183
|
+
try:
|
|
184
|
+
location = hook.location()
|
|
185
|
+
added = hook.install()
|
|
186
|
+
except (OSError, ValueError, subprocess.CalledProcessError) as error:
|
|
187
|
+
return _fail(str(error), 2)
|
|
188
|
+
root = repository.resolve()
|
|
189
|
+
location = location.resolve()
|
|
190
|
+
shown = (location.relative_to(root) if location.is_relative_to(root) else location).as_posix()
|
|
191
|
+
if not added:
|
|
192
|
+
print(f"{shown}: fsg hook already present")
|
|
193
|
+
elif isinstance(hook, GitHook):
|
|
194
|
+
print(f"{shown}: fsg hook added")
|
|
195
|
+
else:
|
|
196
|
+
print(f"{shown}: fsg hook added, run 'pre-commit install' if not done yet")
|
|
197
|
+
return 0
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _fail(message: str, status: int) -> int:
|
|
201
|
+
"""Prints the message prefixed with fsg: to stderr and returns the status.
|
|
202
|
+
|
|
203
|
+
Args:
|
|
204
|
+
message: Error description.
|
|
205
|
+
status: Exit status to return.
|
|
206
|
+
"""
|
|
207
|
+
|
|
208
|
+
print(f"fsg: {message}", file=sys.stderr)
|
|
209
|
+
return status
|