sphinx-cmd 0.1.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.
- sphinx_cmd/__init__.py +2 -0
- sphinx_cmd/cli.py +64 -0
- sphinx_cmd/commands/__init__.py +1 -0
- sphinx_cmd/commands/rm.py +104 -0
- sphinx_cmd/py.typed +2 -0
- sphinx_cmd-0.1.0.dist-info/METADATA +89 -0
- sphinx_cmd-0.1.0.dist-info/RECORD +11 -0
- sphinx_cmd-0.1.0.dist-info/WHEEL +5 -0
- sphinx_cmd-0.1.0.dist-info/entry_points.txt +2 -0
- sphinx_cmd-0.1.0.dist-info/licenses/LICENSE +21 -0
- sphinx_cmd-0.1.0.dist-info/top_level.txt +1 -0
sphinx_cmd/__init__.py
ADDED
sphinx_cmd/cli.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Main CLI entry point for sphinx-cmd with subcommands.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
import sys
|
|
8
|
+
|
|
9
|
+
from sphinx_cmd.commands import rm
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def create_parser():
|
|
13
|
+
"""Create the main argument parser with subcommands."""
|
|
14
|
+
parser = argparse.ArgumentParser(
|
|
15
|
+
prog="sphinx-cmd",
|
|
16
|
+
description="Command-line tools for Sphinx documentation management",
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
# Add version option
|
|
20
|
+
parser.add_argument("--version", action="version", version="%(prog)s 0.1.0")
|
|
21
|
+
|
|
22
|
+
# Create subparsers for subcommands
|
|
23
|
+
subparsers = parser.add_subparsers(
|
|
24
|
+
dest="command", help="Available commands", metavar="COMMAND"
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
# Add 'rm' subcommand
|
|
28
|
+
rm_parser = subparsers.add_parser(
|
|
29
|
+
"rm", help="Delete unused .rst files and their unique assets"
|
|
30
|
+
)
|
|
31
|
+
rm_parser.add_argument(
|
|
32
|
+
"path", help="Path to a single .rst file or a directory of .rst files"
|
|
33
|
+
)
|
|
34
|
+
rm_parser.add_argument(
|
|
35
|
+
"-n",
|
|
36
|
+
"--dry-run",
|
|
37
|
+
action="store_true",
|
|
38
|
+
help="Preview deletions without removing files",
|
|
39
|
+
)
|
|
40
|
+
rm_parser.set_defaults(func=rm.execute)
|
|
41
|
+
|
|
42
|
+
return parser
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def main():
|
|
46
|
+
"""Main entry point for the CLI."""
|
|
47
|
+
parser = create_parser()
|
|
48
|
+
args = parser.parse_args()
|
|
49
|
+
|
|
50
|
+
# If no command is provided, show help
|
|
51
|
+
if not hasattr(args, "func"):
|
|
52
|
+
parser.print_help()
|
|
53
|
+
sys.exit(1)
|
|
54
|
+
|
|
55
|
+
# Execute the appropriate command
|
|
56
|
+
try:
|
|
57
|
+
args.func(args)
|
|
58
|
+
except Exception as e:
|
|
59
|
+
print(f"Error: {e}", file=sys.stderr)
|
|
60
|
+
sys.exit(1)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
if __name__ == "__main__":
|
|
64
|
+
main()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# This file makes commands a Python package
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Command to delete unused .rst files and their unique assets.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import os
|
|
7
|
+
import re
|
|
8
|
+
from collections import defaultdict
|
|
9
|
+
|
|
10
|
+
# Regex patterns for reStructuredText directives
|
|
11
|
+
DIRECTIVE_PATTERNS = {
|
|
12
|
+
"image": re.compile(r"^\s*\.\.\s+image::\s+(.+)$", re.MULTILINE),
|
|
13
|
+
"figure": re.compile(r"^\s*\.\.\s+figure::\s+(.+)$", re.MULTILINE),
|
|
14
|
+
"include": re.compile(r"^\s*\.\.\s+include::\s+(.+)$", re.MULTILINE),
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def find_rst_files(path):
|
|
19
|
+
"""Find all .rst files in the given path."""
|
|
20
|
+
if os.path.isfile(path) and path.endswith(".rst"):
|
|
21
|
+
return [path]
|
|
22
|
+
rst_files = []
|
|
23
|
+
for root, _, files in os.walk(path):
|
|
24
|
+
for file in files:
|
|
25
|
+
if file.endswith(".rst"):
|
|
26
|
+
rst_files.append(os.path.join(root, file))
|
|
27
|
+
return rst_files
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def extract_assets(file_path):
|
|
31
|
+
"""Extract asset references from an .rst file."""
|
|
32
|
+
asset_directives = {}
|
|
33
|
+
with open(file_path, encoding="utf-8") as f:
|
|
34
|
+
content = f.read()
|
|
35
|
+
for directive, pattern in DIRECTIVE_PATTERNS.items():
|
|
36
|
+
for match in pattern.findall(content):
|
|
37
|
+
asset_path = match.strip()
|
|
38
|
+
asset_full_path = os.path.normpath(
|
|
39
|
+
os.path.join(os.path.dirname(file_path), asset_path)
|
|
40
|
+
)
|
|
41
|
+
asset_directives[asset_full_path] = directive
|
|
42
|
+
return asset_directives
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def build_asset_index(rst_files):
|
|
46
|
+
"""Build an index of assets and which files reference them."""
|
|
47
|
+
asset_to_files = defaultdict(set)
|
|
48
|
+
file_to_assets = {}
|
|
49
|
+
asset_directive_map = {}
|
|
50
|
+
|
|
51
|
+
for rst in rst_files:
|
|
52
|
+
asset_directives = extract_assets(rst)
|
|
53
|
+
file_to_assets[rst] = set(asset_directives.keys())
|
|
54
|
+
for asset, directive in asset_directives.items():
|
|
55
|
+
asset_to_files[asset].add(rst)
|
|
56
|
+
asset_directive_map[asset] = directive
|
|
57
|
+
return asset_to_files, file_to_assets, asset_directive_map
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def delete_unused_assets_and_pages(
|
|
61
|
+
asset_to_files, file_to_assets, asset_directive_map, dry_run=False
|
|
62
|
+
):
|
|
63
|
+
"""Delete files and their unique assets if not used elsewhere."""
|
|
64
|
+
deleted_pages = []
|
|
65
|
+
deleted_assets = []
|
|
66
|
+
|
|
67
|
+
for rst_file, assets in file_to_assets.items():
|
|
68
|
+
unused_assets = [a for a in assets if len(asset_to_files[a]) == 1]
|
|
69
|
+
if len(unused_assets) == len(assets): # All assets are unique to this file
|
|
70
|
+
for asset in unused_assets:
|
|
71
|
+
directive = asset_directive_map.get(asset, "asset")
|
|
72
|
+
if os.path.exists(asset):
|
|
73
|
+
if dry_run:
|
|
74
|
+
print(f"[dry-run] Would delete {directive}: {asset}")
|
|
75
|
+
else:
|
|
76
|
+
os.remove(asset)
|
|
77
|
+
deleted_assets.append(asset)
|
|
78
|
+
if os.path.exists(rst_file):
|
|
79
|
+
if dry_run:
|
|
80
|
+
print(f"[dry-run] Would delete page: {rst_file}")
|
|
81
|
+
else:
|
|
82
|
+
os.remove(rst_file)
|
|
83
|
+
deleted_pages.append(rst_file)
|
|
84
|
+
|
|
85
|
+
return deleted_pages, deleted_assets
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def execute(args):
|
|
89
|
+
"""Execute the rm command."""
|
|
90
|
+
rst_files = find_rst_files(args.path)
|
|
91
|
+
asset_to_files, file_to_assets, asset_directive_map = build_asset_index(rst_files)
|
|
92
|
+
deleted_pages, deleted_assets = delete_unused_assets_and_pages(
|
|
93
|
+
asset_to_files, file_to_assets, asset_directive_map, args.dry_run
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
if not args.dry_run:
|
|
97
|
+
print(f"\nDeleted {len(deleted_assets)} unused asset(s):")
|
|
98
|
+
for a in deleted_assets:
|
|
99
|
+
directive = asset_directive_map.get(a, "asset")
|
|
100
|
+
print(f" - ({directive}) {a}")
|
|
101
|
+
|
|
102
|
+
print(f"\nDeleted {len(deleted_pages)} RST page(s):")
|
|
103
|
+
for p in deleted_pages:
|
|
104
|
+
print(f" - {p}")
|
sphinx_cmd/py.typed
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sphinx-cmd
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Command-line tools for Sphinx documentation management
|
|
5
|
+
Author-email: Jared Dillard <jared.dillard@gmail.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/jdillard/sphinx-cmd
|
|
7
|
+
Project-URL: Bug Reports, https://github.com/jdillard/sphinx-cmd/issues
|
|
8
|
+
Project-URL: Source, https://github.com/jdillard/sphinx-cmd
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Requires-Python: >=3.9
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Provides-Extra: dev
|
|
20
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
21
|
+
Requires-Dist: black; extra == "dev"
|
|
22
|
+
Requires-Dist: flake8; extra == "dev"
|
|
23
|
+
Requires-Dist: mypy; extra == "dev"
|
|
24
|
+
Requires-Dist: isort; extra == "dev"
|
|
25
|
+
Provides-Extra: test
|
|
26
|
+
Requires-Dist: pytest>=7.0.0; extra == "test"
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# Sphinx-CMD
|
|
30
|
+
|
|
31
|
+
A collection of command-line tools for managing Sphinx documentation.
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install sphinx-cmd
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Commands
|
|
40
|
+
|
|
41
|
+
The `sphinx-cmd` tool provides subcommands for different Sphinx documentation management tasks.
|
|
42
|
+
|
|
43
|
+
### `sphinx-cmd rm`
|
|
44
|
+
|
|
45
|
+
Delete unused .rst files and their unique assets if not used elsewhere.
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
# Remove files and assets
|
|
49
|
+
sphinx-cmd rm path/to/docs
|
|
50
|
+
|
|
51
|
+
# Dry run to preview deletions
|
|
52
|
+
sphinx-cmd rm path/to/docs --dry-run
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Development
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
# Clone the repository
|
|
59
|
+
git clone https://github.com/yourusername/sphinx-cmd.git
|
|
60
|
+
cd sphinx-cmd
|
|
61
|
+
|
|
62
|
+
# Install in development mode with dev dependencies
|
|
63
|
+
pip install -e ".[dev]"
|
|
64
|
+
|
|
65
|
+
# Run tests
|
|
66
|
+
pytest
|
|
67
|
+
|
|
68
|
+
# Run linters
|
|
69
|
+
black sphinx_cmd tests
|
|
70
|
+
flake8 sphinx_cmd tests
|
|
71
|
+
mypy sphinx_cmd
|
|
72
|
+
|
|
73
|
+
# Test the command
|
|
74
|
+
sphinx-cmd --help
|
|
75
|
+
sphinx-cmd rm --help
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Adding New Commands
|
|
79
|
+
|
|
80
|
+
The architecture is designed to make adding new commands easy:
|
|
81
|
+
|
|
82
|
+
1. Create a new file in `sphinx_cmd/commands/` (e.g., `new_command.py`)
|
|
83
|
+
2. Implement an `execute(args)` function in your new file
|
|
84
|
+
3. Import the command in `sphinx_cmd/cli.py`
|
|
85
|
+
4. Add a new subparser for your command in `create_parser()`
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
MIT License - see LICENSE file for details.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
sphinx_cmd/__init__.py,sha256=6CRj7wEXp-niiWbr3VB8QDB7Vu2VxrGV84kUEccXvsE,74
|
|
2
|
+
sphinx_cmd/cli.py,sha256=U-1ZbRVpxehtGy3dMlX1gfSieeipTULLQ7XFHTMDpsg,1562
|
|
3
|
+
sphinx_cmd/py.typed,sha256=Z1NT10vEwXGlu11pohpVrJs-oJCePmP_tPRaOTBFrdE,87
|
|
4
|
+
sphinx_cmd/commands/__init__.py,sha256=cEagAkhLadXxPhpyrqoCg12B6Ic7cdvs7D1C4RFmWFM,44
|
|
5
|
+
sphinx_cmd/commands/rm.py,sha256=VJFG2y5Z7koF_3B6paYqRwwY6VPpwa2VjDent20k3Uw,3721
|
|
6
|
+
sphinx_cmd-0.1.0.dist-info/licenses/LICENSE,sha256=eSTM6yTvO8WDoSP6D3JQniBg3S0p_Tes_bIApYUR36I,1070
|
|
7
|
+
sphinx_cmd-0.1.0.dist-info/METADATA,sha256=W5b4TPFtMrOXEqD5jKdNqgDsV2TMFAHbG7yaCU95SMU,2318
|
|
8
|
+
sphinx_cmd-0.1.0.dist-info/WHEEL,sha256=DnLRTWE75wApRYVsjgc6wsVswC54sMSJhAEd4xhDpBk,91
|
|
9
|
+
sphinx_cmd-0.1.0.dist-info/entry_points.txt,sha256=S9t73xfMqsZesy60xMfdsiXmtTAJF04ypCgu20Q9pqM,51
|
|
10
|
+
sphinx_cmd-0.1.0.dist-info/top_level.txt,sha256=2Lre69zMwzGKaKa9YqzRAgEajeWuv7eTNvPjeXWnsNc,11
|
|
11
|
+
sphinx_cmd-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jared Dillard
|
|
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.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
sphinx_cmd
|