sphinx-cmd 0.3.1__tar.gz → 0.4.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.
- {sphinx_cmd-0.3.1/sphinx_cmd.egg-info → sphinx_cmd-0.4.0}/PKG-INFO +31 -14
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/README.md +30 -13
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/sphinx_cmd/__init__.py +1 -1
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/sphinx_cmd/cli.py +26 -24
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/sphinx_cmd/commands/mv.py +23 -9
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/sphinx_cmd/commands/rm.py +110 -17
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/sphinx_cmd/config.py +72 -4
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0/sphinx_cmd.egg-info}/PKG-INFO +31 -14
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/tests/test_cli.py +18 -0
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/tests/test_config.py +83 -1
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/tests/test_custom_directives.py +1 -0
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/tests/test_integration.py +2 -2
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/tests/test_mv.py +3 -1
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/tests/test_rm.py +122 -15
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/LICENSE +0 -0
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/MANIFEST.in +0 -0
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/pyproject.toml +0 -0
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/setup.cfg +0 -0
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/sphinx_cmd/commands/__init__.py +0 -0
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/sphinx_cmd/py.typed +0 -0
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/sphinx_cmd.egg-info/SOURCES.txt +0 -0
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/sphinx_cmd.egg-info/dependency_links.txt +0 -0
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/sphinx_cmd.egg-info/entry_points.txt +0 -0
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/sphinx_cmd.egg-info/requires.txt +0 -0
- {sphinx_cmd-0.3.1 → sphinx_cmd-0.4.0}/sphinx_cmd.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sphinx-cmd
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: Command-line tools for Sphinx documentation management
|
|
5
5
|
Author-email: Jared Dillard <jared.dillard@gmail.com>
|
|
6
6
|
Project-URL: Homepage, https://github.com/jdillard/sphinx-cmd
|
|
@@ -32,6 +32,9 @@ Dynamic: license-file
|
|
|
32
32
|
|
|
33
33
|
A collection of command-line tools for managing Sphinx documentation.
|
|
34
34
|
|
|
35
|
+
[](https://pypi.python.org/pypi/sphinx-cmd)
|
|
36
|
+
[](https://pepy.tech/project/sphinx-cmd)
|
|
37
|
+
|
|
35
38
|
## Installation
|
|
36
39
|
|
|
37
40
|
```bash
|
|
@@ -42,6 +45,23 @@ pip install sphinx-cmd
|
|
|
42
45
|
|
|
43
46
|
The `sphinx-cmd` tool provides subcommands for different Sphinx documentation management tasks.
|
|
44
47
|
|
|
48
|
+
### Global Options
|
|
49
|
+
|
|
50
|
+
Options that apply to all commands:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
# Specify a context path (directory containing conf.py)
|
|
54
|
+
sphinx-cmd --context /path/to/docs COMMAND
|
|
55
|
+
|
|
56
|
+
# Dry run to preview changes without executing them
|
|
57
|
+
sphinx-cmd --dry-run COMMAND
|
|
58
|
+
|
|
59
|
+
# Process additional directives beyond defaults
|
|
60
|
+
sphinx-cmd --directives drawio-figure,drawio-image COMMAND
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
By default, `sphinx-cmd` will automatically detect the context of your documentation project by finding the nearest `conf.py` file in the directory tree.
|
|
64
|
+
|
|
45
65
|
### `sphinx-cmd rm`
|
|
46
66
|
|
|
47
67
|
Delete unused .rst files and their unique assets (images, includes, etc) if not used elsewhere.
|
|
@@ -50,16 +70,14 @@ Delete unused .rst files and their unique assets (images, includes, etc) if not
|
|
|
50
70
|
# Remove files and assets
|
|
51
71
|
sphinx-cmd rm path/to/docs
|
|
52
72
|
|
|
53
|
-
#
|
|
54
|
-
sphinx-cmd rm path/to/docs
|
|
55
|
-
|
|
56
|
-
# Process additional directives beyond defaults
|
|
57
|
-
sphinx-cmd rm path/to/docs --directives drawio-figure,drawio-image
|
|
73
|
+
# Using global options
|
|
74
|
+
sphinx-cmd --dry-run --directives drawio-figure,drawio-image rm path/to/docs
|
|
58
75
|
```
|
|
59
76
|
|
|
60
77
|
#### Features
|
|
61
78
|
|
|
62
79
|
- Configure custom directives to be processed
|
|
80
|
+
- Only deletes unused unique assets in the provided context path
|
|
63
81
|
|
|
64
82
|
### `sphinx-cmd mv`
|
|
65
83
|
|
|
@@ -72,14 +90,11 @@ sphinx-cmd mv old-file.rst new-file.rst
|
|
|
72
90
|
# Move to a different directory
|
|
73
91
|
sphinx-cmd mv chapter1.rst topics/chapter1.rst
|
|
74
92
|
|
|
75
|
-
# Preview the move without making changes
|
|
76
|
-
sphinx-cmd mv old-file.rst new-file.rst --dry-run
|
|
77
|
-
|
|
78
93
|
# Move without updating references
|
|
79
94
|
sphinx-cmd mv old-file.rst new-file.rst --no-update-refs
|
|
80
95
|
|
|
81
|
-
#
|
|
82
|
-
sphinx-cmd mv old-file.rst new-file.rst
|
|
96
|
+
# Using global options
|
|
97
|
+
sphinx-cmd --dry-run --directives drawio-figure mv old-file.rst new-file.rst
|
|
83
98
|
```
|
|
84
99
|
|
|
85
100
|
#### Features
|
|
@@ -97,10 +112,10 @@ You can add custom directives to be processed in two ways:
|
|
|
97
112
|
|
|
98
113
|
### 1. Command Line
|
|
99
114
|
|
|
100
|
-
Use the `--directives` option with any command to add custom directives for a single run:
|
|
115
|
+
Use the `--directives` global option with any command to add custom directives for a single run:
|
|
101
116
|
|
|
102
117
|
```bash
|
|
103
|
-
sphinx-cmd
|
|
118
|
+
sphinx-cmd --directives drawio-figure,drawio-image rm path/to/docs
|
|
104
119
|
```
|
|
105
120
|
|
|
106
121
|
### 2. Configuration File
|
|
@@ -132,6 +147,7 @@ pytest
|
|
|
132
147
|
|
|
133
148
|
# Run linters
|
|
134
149
|
black sphinx_cmd tests
|
|
150
|
+
isort sphinx_cmd tests
|
|
135
151
|
flake8 sphinx_cmd tests
|
|
136
152
|
mypy sphinx_cmd
|
|
137
153
|
|
|
@@ -141,7 +157,7 @@ sphinx-cmd rm --help
|
|
|
141
157
|
sphinx-cmd mv --help
|
|
142
158
|
```
|
|
143
159
|
|
|
144
|
-
|
|
160
|
+
### Adding New Commands
|
|
145
161
|
|
|
146
162
|
The architecture is designed to make adding new commands easy:
|
|
147
163
|
|
|
@@ -149,6 +165,7 @@ The architecture is designed to make adding new commands easy:
|
|
|
149
165
|
2. Implement an `execute(args)` function in your new file
|
|
150
166
|
3. Import the command in `sphinx_cmd/cli.py`
|
|
151
167
|
4. Add a new subparser for your command in `create_parser()`
|
|
168
|
+
5. Create new tests (e.g., `tests/test_new_command.py`)
|
|
152
169
|
|
|
153
170
|
## License
|
|
154
171
|
|
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
A collection of command-line tools for managing Sphinx documentation.
|
|
4
4
|
|
|
5
|
+
[](https://pypi.python.org/pypi/sphinx-cmd)
|
|
6
|
+
[](https://pepy.tech/project/sphinx-cmd)
|
|
7
|
+
|
|
5
8
|
## Installation
|
|
6
9
|
|
|
7
10
|
```bash
|
|
@@ -12,6 +15,23 @@ pip install sphinx-cmd
|
|
|
12
15
|
|
|
13
16
|
The `sphinx-cmd` tool provides subcommands for different Sphinx documentation management tasks.
|
|
14
17
|
|
|
18
|
+
### Global Options
|
|
19
|
+
|
|
20
|
+
Options that apply to all commands:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
# Specify a context path (directory containing conf.py)
|
|
24
|
+
sphinx-cmd --context /path/to/docs COMMAND
|
|
25
|
+
|
|
26
|
+
# Dry run to preview changes without executing them
|
|
27
|
+
sphinx-cmd --dry-run COMMAND
|
|
28
|
+
|
|
29
|
+
# Process additional directives beyond defaults
|
|
30
|
+
sphinx-cmd --directives drawio-figure,drawio-image COMMAND
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
By default, `sphinx-cmd` will automatically detect the context of your documentation project by finding the nearest `conf.py` file in the directory tree.
|
|
34
|
+
|
|
15
35
|
### `sphinx-cmd rm`
|
|
16
36
|
|
|
17
37
|
Delete unused .rst files and their unique assets (images, includes, etc) if not used elsewhere.
|
|
@@ -20,16 +40,14 @@ Delete unused .rst files and their unique assets (images, includes, etc) if not
|
|
|
20
40
|
# Remove files and assets
|
|
21
41
|
sphinx-cmd rm path/to/docs
|
|
22
42
|
|
|
23
|
-
#
|
|
24
|
-
sphinx-cmd rm path/to/docs
|
|
25
|
-
|
|
26
|
-
# Process additional directives beyond defaults
|
|
27
|
-
sphinx-cmd rm path/to/docs --directives drawio-figure,drawio-image
|
|
43
|
+
# Using global options
|
|
44
|
+
sphinx-cmd --dry-run --directives drawio-figure,drawio-image rm path/to/docs
|
|
28
45
|
```
|
|
29
46
|
|
|
30
47
|
#### Features
|
|
31
48
|
|
|
32
49
|
- Configure custom directives to be processed
|
|
50
|
+
- Only deletes unused unique assets in the provided context path
|
|
33
51
|
|
|
34
52
|
### `sphinx-cmd mv`
|
|
35
53
|
|
|
@@ -42,14 +60,11 @@ sphinx-cmd mv old-file.rst new-file.rst
|
|
|
42
60
|
# Move to a different directory
|
|
43
61
|
sphinx-cmd mv chapter1.rst topics/chapter1.rst
|
|
44
62
|
|
|
45
|
-
# Preview the move without making changes
|
|
46
|
-
sphinx-cmd mv old-file.rst new-file.rst --dry-run
|
|
47
|
-
|
|
48
63
|
# Move without updating references
|
|
49
64
|
sphinx-cmd mv old-file.rst new-file.rst --no-update-refs
|
|
50
65
|
|
|
51
|
-
#
|
|
52
|
-
sphinx-cmd mv old-file.rst new-file.rst
|
|
66
|
+
# Using global options
|
|
67
|
+
sphinx-cmd --dry-run --directives drawio-figure mv old-file.rst new-file.rst
|
|
53
68
|
```
|
|
54
69
|
|
|
55
70
|
#### Features
|
|
@@ -67,10 +82,10 @@ You can add custom directives to be processed in two ways:
|
|
|
67
82
|
|
|
68
83
|
### 1. Command Line
|
|
69
84
|
|
|
70
|
-
Use the `--directives` option with any command to add custom directives for a single run:
|
|
85
|
+
Use the `--directives` global option with any command to add custom directives for a single run:
|
|
71
86
|
|
|
72
87
|
```bash
|
|
73
|
-
sphinx-cmd
|
|
88
|
+
sphinx-cmd --directives drawio-figure,drawio-image rm path/to/docs
|
|
74
89
|
```
|
|
75
90
|
|
|
76
91
|
### 2. Configuration File
|
|
@@ -102,6 +117,7 @@ pytest
|
|
|
102
117
|
|
|
103
118
|
# Run linters
|
|
104
119
|
black sphinx_cmd tests
|
|
120
|
+
isort sphinx_cmd tests
|
|
105
121
|
flake8 sphinx_cmd tests
|
|
106
122
|
mypy sphinx_cmd
|
|
107
123
|
|
|
@@ -111,7 +127,7 @@ sphinx-cmd rm --help
|
|
|
111
127
|
sphinx-cmd mv --help
|
|
112
128
|
```
|
|
113
129
|
|
|
114
|
-
|
|
130
|
+
### Adding New Commands
|
|
115
131
|
|
|
116
132
|
The architecture is designed to make adding new commands easy:
|
|
117
133
|
|
|
@@ -119,6 +135,7 @@ The architecture is designed to make adding new commands easy:
|
|
|
119
135
|
2. Implement an `execute(args)` function in your new file
|
|
120
136
|
3. Import the command in `sphinx_cmd/cli.py`
|
|
121
137
|
4. Add a new subparser for your command in `create_parser()`
|
|
138
|
+
5. Create new tests (e.g., `tests/test_new_command.py`)
|
|
122
139
|
|
|
123
140
|
## License
|
|
124
141
|
|
|
@@ -4,6 +4,7 @@ Main CLI entry point for sphinx-cmd with subcommands.
|
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
6
|
import argparse
|
|
7
|
+
import os
|
|
7
8
|
import sys
|
|
8
9
|
|
|
9
10
|
from sphinx_cmd import __version__
|
|
@@ -21,6 +22,26 @@ def create_parser():
|
|
|
21
22
|
"--version", action="version", version=f"%(prog)s {__version__}"
|
|
22
23
|
)
|
|
23
24
|
|
|
25
|
+
# Add global options
|
|
26
|
+
parser.add_argument(
|
|
27
|
+
"--context",
|
|
28
|
+
"-c",
|
|
29
|
+
help="Path to Sphinx documentation context"
|
|
30
|
+
"(will auto-detect nearest conf.py by default)",
|
|
31
|
+
)
|
|
32
|
+
parser.add_argument(
|
|
33
|
+
"-n",
|
|
34
|
+
"--dry-run",
|
|
35
|
+
action="store_true",
|
|
36
|
+
help="Preview operations without making any changes",
|
|
37
|
+
)
|
|
38
|
+
parser.add_argument(
|
|
39
|
+
"--directives",
|
|
40
|
+
type=lambda s: [x.strip() for x in s.split(",")],
|
|
41
|
+
help="Additional directives to process as comma-separated list"
|
|
42
|
+
" (e.g. 'drawio-figure,drawio-image')",
|
|
43
|
+
)
|
|
44
|
+
|
|
24
45
|
# Create subparsers for subcommands
|
|
25
46
|
subparsers = parser.add_subparsers(
|
|
26
47
|
dest="command", help="Available commands", metavar="COMMAND"
|
|
@@ -33,18 +54,6 @@ def create_parser():
|
|
|
33
54
|
rm_parser.add_argument(
|
|
34
55
|
"path", help="Path to a single .rst file or a directory of .rst files"
|
|
35
56
|
)
|
|
36
|
-
rm_parser.add_argument(
|
|
37
|
-
"-n",
|
|
38
|
-
"--dry-run",
|
|
39
|
-
action="store_true",
|
|
40
|
-
help="Preview deletions without removing files",
|
|
41
|
-
)
|
|
42
|
-
rm_parser.add_argument(
|
|
43
|
-
"--directives",
|
|
44
|
-
type=lambda s: [x.strip() for x in s.split(",")],
|
|
45
|
-
help="Additional directives to process as comma-separated list"
|
|
46
|
-
" (e.g. 'drawio-figure,drawio-image')",
|
|
47
|
-
)
|
|
48
57
|
rm_parser.set_defaults(command_name="rm")
|
|
49
58
|
|
|
50
59
|
# Add 'mv' subcommand
|
|
@@ -53,23 +62,11 @@ def create_parser():
|
|
|
53
62
|
)
|
|
54
63
|
mv_parser.add_argument("source", help="Source file to move")
|
|
55
64
|
mv_parser.add_argument("destination", help="Destination file or directory")
|
|
56
|
-
mv_parser.add_argument(
|
|
57
|
-
"-n",
|
|
58
|
-
"--dry-run",
|
|
59
|
-
action="store_true",
|
|
60
|
-
help="Preview move without actually moving files",
|
|
61
|
-
)
|
|
62
65
|
mv_parser.add_argument(
|
|
63
66
|
"--no-update-refs",
|
|
64
67
|
action="store_true",
|
|
65
68
|
help="Do not update references to the moved file",
|
|
66
69
|
)
|
|
67
|
-
mv_parser.add_argument(
|
|
68
|
-
"--directives",
|
|
69
|
-
type=lambda s: [x.strip() for x in s.split(",")],
|
|
70
|
-
help="Additional directives to process as comma-separated list"
|
|
71
|
-
" (e.g. 'drawio-figure,drawio-image')",
|
|
72
|
-
)
|
|
73
70
|
mv_parser.set_defaults(command_name="mv")
|
|
74
71
|
|
|
75
72
|
return parser
|
|
@@ -85,6 +82,11 @@ def main():
|
|
|
85
82
|
parser.print_help()
|
|
86
83
|
sys.exit(1)
|
|
87
84
|
|
|
85
|
+
# If context path is provided, check that it exists
|
|
86
|
+
if args.context and not os.path.exists(args.context):
|
|
87
|
+
print(f"Error: Context path does not exist: {args.context}", file=sys.stderr)
|
|
88
|
+
sys.exit(1)
|
|
89
|
+
|
|
88
90
|
# Import and execute the appropriate command
|
|
89
91
|
try:
|
|
90
92
|
if args.command_name == "rm":
|
|
@@ -7,7 +7,7 @@ import re
|
|
|
7
7
|
import shutil
|
|
8
8
|
from collections import defaultdict
|
|
9
9
|
from pathlib import Path
|
|
10
|
-
from typing import Dict, List, Tuple
|
|
10
|
+
from typing import Dict, List, Optional, Tuple
|
|
11
11
|
|
|
12
12
|
# Regex patterns for different types of references in reStructuredText
|
|
13
13
|
REFERENCE_PATTERNS = {
|
|
@@ -41,7 +41,9 @@ def find_all_rst_files(root_path: str) -> List[str]:
|
|
|
41
41
|
return rst_files
|
|
42
42
|
|
|
43
43
|
|
|
44
|
-
def extract_references(
|
|
44
|
+
def extract_references(
|
|
45
|
+
file_path: str, context_path: Optional[str] = None
|
|
46
|
+
) -> Dict[str, List[str]]:
|
|
45
47
|
"""Extract all file references from an .rst file."""
|
|
46
48
|
references = defaultdict(list)
|
|
47
49
|
|
|
@@ -78,7 +80,7 @@ def extract_references(file_path: str) -> Dict[str, List[str]]:
|
|
|
78
80
|
|
|
79
81
|
|
|
80
82
|
def find_files_referencing(
|
|
81
|
-
target_file: str, all_files: List[str]
|
|
83
|
+
target_file: str, all_files: List[str], context_path: Optional[str] = None
|
|
82
84
|
) -> List[Tuple[str, str]]:
|
|
83
85
|
"""Find all files that reference the target file."""
|
|
84
86
|
referencing_files = []
|
|
@@ -90,7 +92,8 @@ def find_files_referencing(
|
|
|
90
92
|
continue
|
|
91
93
|
|
|
92
94
|
try:
|
|
93
|
-
references
|
|
95
|
+
# Extract references using context from conf.py if provided
|
|
96
|
+
references = extract_references(file_path, context_path=context_path)
|
|
94
97
|
|
|
95
98
|
for ref_type, ref_list in references.items():
|
|
96
99
|
for ref in ref_list:
|
|
@@ -166,7 +169,11 @@ def update_references_in_file(file_path: str, old_ref: str, new_ref: str) -> boo
|
|
|
166
169
|
|
|
167
170
|
|
|
168
171
|
def move_rst_file(
|
|
169
|
-
source: str,
|
|
172
|
+
source: str,
|
|
173
|
+
destination: str,
|
|
174
|
+
update_references: bool = True,
|
|
175
|
+
dry_run: bool = False,
|
|
176
|
+
context_path: Optional[str] = None,
|
|
170
177
|
) -> None:
|
|
171
178
|
"""Move an RST file and optionally update all references to it."""
|
|
172
179
|
source_path = Path(source).resolve()
|
|
@@ -208,8 +215,10 @@ def move_rst_file(
|
|
|
208
215
|
root_dir = Path.cwd() # Or use a smarter root detection
|
|
209
216
|
all_files = find_all_rst_files(str(root_dir))
|
|
210
217
|
|
|
211
|
-
# Find files that reference the moved file
|
|
212
|
-
referencing_files = find_files_referencing(
|
|
218
|
+
# Find files that reference the moved file, using context if provided
|
|
219
|
+
referencing_files = find_files_referencing(
|
|
220
|
+
str(source_rel), all_files, context_path=context_path
|
|
221
|
+
)
|
|
213
222
|
|
|
214
223
|
if referencing_files:
|
|
215
224
|
print(f"\nUpdating references in {len(referencing_files)} file(s):")
|
|
@@ -241,15 +250,20 @@ def execute(args):
|
|
|
241
250
|
update_refs = getattr(args, "no_update_refs", False)
|
|
242
251
|
update_references = not update_refs
|
|
243
252
|
|
|
253
|
+
# Get global options
|
|
254
|
+
context_path = getattr(args, "context", None)
|
|
255
|
+
dry_run = getattr(args, "dry_run", False)
|
|
256
|
+
|
|
244
257
|
# Perform the move
|
|
245
258
|
move_rst_file(
|
|
246
259
|
args.source,
|
|
247
260
|
args.destination,
|
|
248
261
|
update_references=update_references,
|
|
249
|
-
dry_run=
|
|
262
|
+
dry_run=dry_run,
|
|
263
|
+
context_path=context_path,
|
|
250
264
|
)
|
|
251
265
|
|
|
252
|
-
if not
|
|
266
|
+
if not dry_run:
|
|
253
267
|
print("\n✓ Move completed successfully!")
|
|
254
268
|
else:
|
|
255
269
|
print("\n[Dry run complete - no files were actually moved]")
|
|
@@ -21,7 +21,7 @@ def find_rst_files(path):
|
|
|
21
21
|
return rst_files
|
|
22
22
|
|
|
23
23
|
|
|
24
|
-
def extract_assets(file_path, visited=None, cli_directives=None):
|
|
24
|
+
def extract_assets(file_path, visited=None, cli_directives=None, context_path=None):
|
|
25
25
|
"""Extract asset references from an .rst file, recursively parsing includes."""
|
|
26
26
|
if visited is None:
|
|
27
27
|
visited = set()
|
|
@@ -33,7 +33,7 @@ def extract_assets(file_path, visited=None, cli_directives=None):
|
|
|
33
33
|
visited.add(abs_path)
|
|
34
34
|
|
|
35
35
|
asset_directives = {}
|
|
36
|
-
directive_patterns = get_directive_patterns(cli_directives)
|
|
36
|
+
directive_patterns = get_directive_patterns(cli_directives, context_path)
|
|
37
37
|
|
|
38
38
|
# If file doesn't exist, skip it
|
|
39
39
|
if not os.path.exists(file_path):
|
|
@@ -52,7 +52,10 @@ def extract_assets(file_path, visited=None, cli_directives=None):
|
|
|
52
52
|
if directive == "include":
|
|
53
53
|
# Recursively extract assets from included files
|
|
54
54
|
included_assets = extract_assets(
|
|
55
|
-
asset_full_path,
|
|
55
|
+
asset_full_path,
|
|
56
|
+
visited.copy(),
|
|
57
|
+
cli_directives,
|
|
58
|
+
context_path,
|
|
56
59
|
)
|
|
57
60
|
asset_directives.update(included_assets)
|
|
58
61
|
|
|
@@ -63,14 +66,16 @@ def extract_assets(file_path, visited=None, cli_directives=None):
|
|
|
63
66
|
return asset_directives
|
|
64
67
|
|
|
65
68
|
|
|
66
|
-
def build_asset_index(rst_files, cli_directives=None):
|
|
69
|
+
def build_asset_index(rst_files, cli_directives=None, context_path=None):
|
|
67
70
|
"""Build an index of assets and which files reference them."""
|
|
68
71
|
asset_to_files = defaultdict(set)
|
|
69
72
|
file_to_assets = {}
|
|
70
73
|
asset_directive_map = {}
|
|
71
74
|
|
|
72
75
|
for rst in rst_files:
|
|
73
|
-
asset_directives = extract_assets(
|
|
76
|
+
asset_directives = extract_assets(
|
|
77
|
+
rst, cli_directives=cli_directives, context_path=context_path
|
|
78
|
+
)
|
|
74
79
|
file_to_assets[rst] = set(asset_directives.keys())
|
|
75
80
|
for asset, directive in asset_directives.items():
|
|
76
81
|
asset_to_files[asset].add(rst)
|
|
@@ -78,7 +83,9 @@ def build_asset_index(rst_files, cli_directives=None):
|
|
|
78
83
|
return asset_to_files, file_to_assets, asset_directive_map
|
|
79
84
|
|
|
80
85
|
|
|
81
|
-
def get_transitive_includes(
|
|
86
|
+
def get_transitive_includes(
|
|
87
|
+
file_path, visited=None, cli_directives=None, context_path=None
|
|
88
|
+
):
|
|
82
89
|
"""Get all files included transitively from a file."""
|
|
83
90
|
if visited is None:
|
|
84
91
|
visited = set()
|
|
@@ -94,7 +101,7 @@ def get_transitive_includes(file_path, visited=None, cli_directives=None):
|
|
|
94
101
|
if not os.path.exists(file_path):
|
|
95
102
|
return includes
|
|
96
103
|
|
|
97
|
-
directive_patterns = get_directive_patterns(cli_directives)
|
|
104
|
+
directive_patterns = get_directive_patterns(cli_directives, context_path)
|
|
98
105
|
|
|
99
106
|
try:
|
|
100
107
|
with open(file_path, encoding="utf-8") as f:
|
|
@@ -111,7 +118,10 @@ def get_transitive_includes(file_path, visited=None, cli_directives=None):
|
|
|
111
118
|
# Recursively get includes from the included file
|
|
112
119
|
includes.update(
|
|
113
120
|
get_transitive_includes(
|
|
114
|
-
include_full_path,
|
|
121
|
+
include_full_path,
|
|
122
|
+
visited.copy(),
|
|
123
|
+
cli_directives,
|
|
124
|
+
context_path,
|
|
115
125
|
)
|
|
116
126
|
)
|
|
117
127
|
except Exception as e:
|
|
@@ -121,12 +131,22 @@ def get_transitive_includes(file_path, visited=None, cli_directives=None):
|
|
|
121
131
|
|
|
122
132
|
|
|
123
133
|
def delete_unused_assets_and_pages(
|
|
124
|
-
asset_to_files,
|
|
134
|
+
asset_to_files,
|
|
135
|
+
file_to_assets,
|
|
136
|
+
asset_directive_map,
|
|
137
|
+
dry_run=False,
|
|
138
|
+
context_path=None,
|
|
125
139
|
):
|
|
126
|
-
"""
|
|
140
|
+
"""
|
|
141
|
+
Delete files and their unique assets if not used elsewhere.
|
|
142
|
+
|
|
143
|
+
If context_path is provided, only files within that context will be removed
|
|
144
|
+
for safety reasons.
|
|
145
|
+
"""
|
|
127
146
|
deleted_pages = []
|
|
128
147
|
deleted_assets = []
|
|
129
148
|
affected_dirs = set()
|
|
149
|
+
would_delete_something = False # Flag to track if anything would be deleted
|
|
130
150
|
|
|
131
151
|
# Track which files have been processed to avoid duplicates
|
|
132
152
|
processed_files = set()
|
|
@@ -159,6 +179,22 @@ def delete_unused_assets_and_pages(
|
|
|
159
179
|
for asset in file_unused_assets:
|
|
160
180
|
directive = asset_directive_map.get(asset, "asset")
|
|
161
181
|
if os.path.exists(asset) and asset not in deleted_assets:
|
|
182
|
+
# Check if asset is within context path if context is provided
|
|
183
|
+
is_in_context = True
|
|
184
|
+
if context_path:
|
|
185
|
+
asset_abs_path = os.path.abspath(asset)
|
|
186
|
+
context_abs_path = os.path.abspath(context_path)
|
|
187
|
+
# Check if the asset path starts with the context path
|
|
188
|
+
is_in_context = asset_abs_path.startswith(context_abs_path)
|
|
189
|
+
|
|
190
|
+
if not is_in_context:
|
|
191
|
+
if dry_run:
|
|
192
|
+
print(
|
|
193
|
+
f"[dry-run] Skipping {directive} (outside context):"
|
|
194
|
+
f" {asset}"
|
|
195
|
+
)
|
|
196
|
+
continue
|
|
197
|
+
|
|
162
198
|
if dry_run:
|
|
163
199
|
origin = (
|
|
164
200
|
" (from include)" if file_to_process != rst_file else ""
|
|
@@ -166,6 +202,7 @@ def delete_unused_assets_and_pages(
|
|
|
166
202
|
print(
|
|
167
203
|
f"[dry-run] Would delete {directive}: {asset}{origin}"
|
|
168
204
|
)
|
|
205
|
+
would_delete_something = True
|
|
169
206
|
else:
|
|
170
207
|
affected_dirs.add(os.path.dirname(asset))
|
|
171
208
|
os.remove(asset)
|
|
@@ -173,10 +210,27 @@ def delete_unused_assets_and_pages(
|
|
|
173
210
|
|
|
174
211
|
# Delete the file if it exists and isn't the main rst file being checked
|
|
175
212
|
if file_to_process != rst_file and os.path.exists(file_to_process):
|
|
213
|
+
# Check if file is within context path if context is provided
|
|
214
|
+
is_in_context = True
|
|
215
|
+
if context_path:
|
|
216
|
+
file_abs_path = os.path.abspath(file_to_process)
|
|
217
|
+
context_abs_path = os.path.abspath(context_path)
|
|
218
|
+
# Check if the file path starts with the context path
|
|
219
|
+
is_in_context = file_abs_path.startswith(context_abs_path)
|
|
220
|
+
|
|
221
|
+
if not is_in_context:
|
|
222
|
+
if dry_run:
|
|
223
|
+
print(
|
|
224
|
+
f"[dry-run] Skipping included file (outside context):"
|
|
225
|
+
f" {file_to_process}"
|
|
226
|
+
)
|
|
227
|
+
continue
|
|
228
|
+
|
|
176
229
|
if dry_run:
|
|
177
230
|
print(
|
|
178
231
|
f"[dry-run] Would delete included file: {file_to_process}"
|
|
179
232
|
)
|
|
233
|
+
would_delete_something = True
|
|
180
234
|
else:
|
|
181
235
|
affected_dirs.add(os.path.dirname(file_to_process))
|
|
182
236
|
os.remove(file_to_process)
|
|
@@ -184,14 +238,28 @@ def delete_unused_assets_and_pages(
|
|
|
184
238
|
|
|
185
239
|
# Finally, delete the main rst file
|
|
186
240
|
if os.path.exists(rst_file):
|
|
241
|
+
# Check if file is within context path if context is provided
|
|
242
|
+
is_in_context = True
|
|
243
|
+
if context_path:
|
|
244
|
+
file_abs_path = os.path.abspath(rst_file)
|
|
245
|
+
context_abs_path = os.path.abspath(context_path)
|
|
246
|
+
# Check if the file path starts with the context path
|
|
247
|
+
is_in_context = file_abs_path.startswith(context_abs_path)
|
|
248
|
+
|
|
249
|
+
if not is_in_context:
|
|
250
|
+
if dry_run:
|
|
251
|
+
print(f"[dry-run] Skipping page (outside context): {rst_file}")
|
|
252
|
+
continue
|
|
253
|
+
|
|
187
254
|
if dry_run:
|
|
188
255
|
print(f"[dry-run] Would delete page: {rst_file}")
|
|
256
|
+
would_delete_something = True
|
|
189
257
|
else:
|
|
190
258
|
affected_dirs.add(os.path.dirname(rst_file))
|
|
191
259
|
os.remove(rst_file)
|
|
192
260
|
deleted_pages.append(rst_file)
|
|
193
261
|
|
|
194
|
-
return deleted_pages, deleted_assets, affected_dirs
|
|
262
|
+
return deleted_pages, deleted_assets, affected_dirs, would_delete_something
|
|
195
263
|
|
|
196
264
|
|
|
197
265
|
def remove_empty_dirs(dirs, original_path, dry_run=False):
|
|
@@ -239,18 +307,43 @@ def execute(args):
|
|
|
239
307
|
"""Execute the rm command."""
|
|
240
308
|
original_path = os.path.abspath(args.path)
|
|
241
309
|
rst_files = find_rst_files(args.path)
|
|
310
|
+
|
|
311
|
+
# Get global options
|
|
312
|
+
context_path = getattr(args, "context", None)
|
|
313
|
+
dry_run = getattr(args, "dry_run", False)
|
|
314
|
+
directives = getattr(args, "directives", None)
|
|
315
|
+
|
|
316
|
+
# Display context information
|
|
317
|
+
if context_path:
|
|
318
|
+
context_abs_path = os.path.abspath(context_path)
|
|
319
|
+
print(f"Context path: {context_abs_path}")
|
|
320
|
+
print("Safety mode: Only files within the context path will be removed")
|
|
321
|
+
else:
|
|
322
|
+
print("Context path not set - all unused files will be removed")
|
|
323
|
+
|
|
242
324
|
asset_to_files, file_to_assets, asset_directive_map = build_asset_index(
|
|
243
|
-
rst_files, cli_directives=
|
|
325
|
+
rst_files, cli_directives=directives, context_path=context_path
|
|
244
326
|
)
|
|
245
|
-
deleted_pages, deleted_assets, affected_dirs =
|
|
246
|
-
|
|
327
|
+
deleted_pages, deleted_assets, affected_dirs, would_delete_something = (
|
|
328
|
+
delete_unused_assets_and_pages(
|
|
329
|
+
asset_to_files,
|
|
330
|
+
file_to_assets,
|
|
331
|
+
asset_directive_map,
|
|
332
|
+
dry_run,
|
|
333
|
+
context_path,
|
|
334
|
+
)
|
|
247
335
|
)
|
|
248
336
|
|
|
249
337
|
deleted_dirs = []
|
|
250
338
|
if affected_dirs:
|
|
251
|
-
deleted_dirs = remove_empty_dirs(affected_dirs, original_path,
|
|
252
|
-
|
|
253
|
-
if
|
|
339
|
+
deleted_dirs = remove_empty_dirs(affected_dirs, original_path, dry_run)
|
|
340
|
+
|
|
341
|
+
if dry_run:
|
|
342
|
+
# In dry-run mode, show a summary of what would be deleted
|
|
343
|
+
if not would_delete_something:
|
|
344
|
+
print("\n[dry-run] No unused files found, nothing would be deleted.")
|
|
345
|
+
else:
|
|
346
|
+
# Not in dry-run mode, report what was actually deleted
|
|
254
347
|
print(f"\nDeleted {len(deleted_assets)} unused asset(s):")
|
|
255
348
|
for a in deleted_assets:
|
|
256
349
|
directive = asset_directive_map.get(a, "asset")
|
|
@@ -6,7 +6,7 @@ Configuration handling for sphinx-cmd.
|
|
|
6
6
|
import re
|
|
7
7
|
import sys
|
|
8
8
|
from pathlib import Path
|
|
9
|
-
from typing import Dict, Optional
|
|
9
|
+
from typing import Any, Dict, Optional, Union
|
|
10
10
|
|
|
11
11
|
# Use the standard library tomllib if Python 3.11+, otherwise use tomli package
|
|
12
12
|
if sys.version_info >= (3, 11):
|
|
@@ -28,7 +28,40 @@ def get_config_path() -> Optional[Path]:
|
|
|
28
28
|
return None
|
|
29
29
|
|
|
30
30
|
|
|
31
|
-
def
|
|
31
|
+
def find_sphinx_conf(start_path: Optional[Union[str, Path]] = None) -> Optional[Path]:
|
|
32
|
+
"""
|
|
33
|
+
Find the nearest conf.py file by traversing up from the starting path.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
start_path: The path to start searching from. Defaults to current directory.
|
|
37
|
+
|
|
38
|
+
Returns:
|
|
39
|
+
Path to conf.py if found, None otherwise.
|
|
40
|
+
"""
|
|
41
|
+
if start_path is None:
|
|
42
|
+
start_path = Path.cwd()
|
|
43
|
+
else:
|
|
44
|
+
start_path = Path(start_path)
|
|
45
|
+
|
|
46
|
+
# Make sure we have an absolute path
|
|
47
|
+
start_path = start_path.resolve()
|
|
48
|
+
|
|
49
|
+
# If start_path is a file, use its parent directory
|
|
50
|
+
if start_path.is_file():
|
|
51
|
+
start_path = start_path.parent
|
|
52
|
+
|
|
53
|
+
# Traverse up the directory tree
|
|
54
|
+
current_dir = start_path
|
|
55
|
+
while current_dir != current_dir.parent: # Stop at root
|
|
56
|
+
conf_path = current_dir / "conf.py"
|
|
57
|
+
if conf_path.exists():
|
|
58
|
+
return conf_path
|
|
59
|
+
current_dir = current_dir.parent
|
|
60
|
+
|
|
61
|
+
return None
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def load_config(cli_directives=None, context_path=None) -> Dict[str, Any]:
|
|
32
65
|
"""
|
|
33
66
|
Load configuration from a TOML file and merge with CLI directives.
|
|
34
67
|
|
|
@@ -37,12 +70,27 @@ def load_config(cli_directives=None) -> Dict:
|
|
|
37
70
|
|
|
38
71
|
Args:
|
|
39
72
|
cli_directives: Optional list of directive names passed from the command line
|
|
73
|
+
context_path: Optional path to use as context for finding conf.py
|
|
40
74
|
|
|
41
75
|
Returns:
|
|
42
76
|
Dict: The merged configuration (defaults + user config + CLI directives)
|
|
43
77
|
"""
|
|
44
78
|
config = DEFAULT_CONFIG.copy()
|
|
45
79
|
|
|
80
|
+
# Add docs context information
|
|
81
|
+
config["sphinx_context"] = []
|
|
82
|
+
|
|
83
|
+
# Try to find Sphinx conf.py if context_path is provided or automatically
|
|
84
|
+
if context_path:
|
|
85
|
+
sphinx_conf = find_sphinx_conf(context_path)
|
|
86
|
+
if sphinx_conf:
|
|
87
|
+
config["sphinx_context"] = [str(sphinx_conf.parent)]
|
|
88
|
+
else:
|
|
89
|
+
# Auto-detect from current directory
|
|
90
|
+
sphinx_conf = find_sphinx_conf()
|
|
91
|
+
if sphinx_conf:
|
|
92
|
+
config["sphinx_context"] = [str(sphinx_conf.parent)]
|
|
93
|
+
|
|
46
94
|
config_path = get_config_path()
|
|
47
95
|
if config_path:
|
|
48
96
|
try:
|
|
@@ -72,17 +120,20 @@ def load_config(cli_directives=None) -> Dict:
|
|
|
72
120
|
return config
|
|
73
121
|
|
|
74
122
|
|
|
75
|
-
def get_directive_patterns(
|
|
123
|
+
def get_directive_patterns(
|
|
124
|
+
cli_directives=None, context_path=None
|
|
125
|
+
) -> Dict[str, re.Pattern]:
|
|
76
126
|
"""
|
|
77
127
|
Get compiled regex patterns for all directives.
|
|
78
128
|
|
|
79
129
|
Args:
|
|
80
130
|
cli_directives: Optional list of directive names passed from the command line
|
|
131
|
+
context_path: Optional path to use as context for finding conf.py
|
|
81
132
|
|
|
82
133
|
Returns:
|
|
83
134
|
Dict[str, re.Pattern]: Dictionary of directive names to compiled regex patterns
|
|
84
135
|
"""
|
|
85
|
-
config = load_config(cli_directives)
|
|
136
|
+
config = load_config(cli_directives, context_path)
|
|
86
137
|
patterns = {}
|
|
87
138
|
|
|
88
139
|
for name in config["directives"]:
|
|
@@ -91,3 +142,20 @@ def get_directive_patterns(cli_directives=None) -> Dict[str, re.Pattern]:
|
|
|
91
142
|
patterns[name] = re.compile(pattern, re.MULTILINE)
|
|
92
143
|
|
|
93
144
|
return patterns
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def get_sphinx_context(cli_context: Optional[str] = None) -> Optional[Path]:
|
|
148
|
+
"""
|
|
149
|
+
Get the Sphinx documentation context directory.
|
|
150
|
+
|
|
151
|
+
Args:
|
|
152
|
+
cli_context: Optional path provided via command line
|
|
153
|
+
|
|
154
|
+
Returns:
|
|
155
|
+
Path to the Sphinx docs directory (containing conf.py) if found, None otherwise
|
|
156
|
+
"""
|
|
157
|
+
config = load_config(context_path=cli_context)
|
|
158
|
+
sphinx_context = config.get("sphinx_context")
|
|
159
|
+
if sphinx_context and len(sphinx_context) > 0:
|
|
160
|
+
return Path(sphinx_context[0])
|
|
161
|
+
return None
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sphinx-cmd
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: Command-line tools for Sphinx documentation management
|
|
5
5
|
Author-email: Jared Dillard <jared.dillard@gmail.com>
|
|
6
6
|
Project-URL: Homepage, https://github.com/jdillard/sphinx-cmd
|
|
@@ -32,6 +32,9 @@ Dynamic: license-file
|
|
|
32
32
|
|
|
33
33
|
A collection of command-line tools for managing Sphinx documentation.
|
|
34
34
|
|
|
35
|
+
[](https://pypi.python.org/pypi/sphinx-cmd)
|
|
36
|
+
[](https://pepy.tech/project/sphinx-cmd)
|
|
37
|
+
|
|
35
38
|
## Installation
|
|
36
39
|
|
|
37
40
|
```bash
|
|
@@ -42,6 +45,23 @@ pip install sphinx-cmd
|
|
|
42
45
|
|
|
43
46
|
The `sphinx-cmd` tool provides subcommands for different Sphinx documentation management tasks.
|
|
44
47
|
|
|
48
|
+
### Global Options
|
|
49
|
+
|
|
50
|
+
Options that apply to all commands:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
# Specify a context path (directory containing conf.py)
|
|
54
|
+
sphinx-cmd --context /path/to/docs COMMAND
|
|
55
|
+
|
|
56
|
+
# Dry run to preview changes without executing them
|
|
57
|
+
sphinx-cmd --dry-run COMMAND
|
|
58
|
+
|
|
59
|
+
# Process additional directives beyond defaults
|
|
60
|
+
sphinx-cmd --directives drawio-figure,drawio-image COMMAND
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
By default, `sphinx-cmd` will automatically detect the context of your documentation project by finding the nearest `conf.py` file in the directory tree.
|
|
64
|
+
|
|
45
65
|
### `sphinx-cmd rm`
|
|
46
66
|
|
|
47
67
|
Delete unused .rst files and their unique assets (images, includes, etc) if not used elsewhere.
|
|
@@ -50,16 +70,14 @@ Delete unused .rst files and their unique assets (images, includes, etc) if not
|
|
|
50
70
|
# Remove files and assets
|
|
51
71
|
sphinx-cmd rm path/to/docs
|
|
52
72
|
|
|
53
|
-
#
|
|
54
|
-
sphinx-cmd rm path/to/docs
|
|
55
|
-
|
|
56
|
-
# Process additional directives beyond defaults
|
|
57
|
-
sphinx-cmd rm path/to/docs --directives drawio-figure,drawio-image
|
|
73
|
+
# Using global options
|
|
74
|
+
sphinx-cmd --dry-run --directives drawio-figure,drawio-image rm path/to/docs
|
|
58
75
|
```
|
|
59
76
|
|
|
60
77
|
#### Features
|
|
61
78
|
|
|
62
79
|
- Configure custom directives to be processed
|
|
80
|
+
- Only deletes unused unique assets in the provided context path
|
|
63
81
|
|
|
64
82
|
### `sphinx-cmd mv`
|
|
65
83
|
|
|
@@ -72,14 +90,11 @@ sphinx-cmd mv old-file.rst new-file.rst
|
|
|
72
90
|
# Move to a different directory
|
|
73
91
|
sphinx-cmd mv chapter1.rst topics/chapter1.rst
|
|
74
92
|
|
|
75
|
-
# Preview the move without making changes
|
|
76
|
-
sphinx-cmd mv old-file.rst new-file.rst --dry-run
|
|
77
|
-
|
|
78
93
|
# Move without updating references
|
|
79
94
|
sphinx-cmd mv old-file.rst new-file.rst --no-update-refs
|
|
80
95
|
|
|
81
|
-
#
|
|
82
|
-
sphinx-cmd mv old-file.rst new-file.rst
|
|
96
|
+
# Using global options
|
|
97
|
+
sphinx-cmd --dry-run --directives drawio-figure mv old-file.rst new-file.rst
|
|
83
98
|
```
|
|
84
99
|
|
|
85
100
|
#### Features
|
|
@@ -97,10 +112,10 @@ You can add custom directives to be processed in two ways:
|
|
|
97
112
|
|
|
98
113
|
### 1. Command Line
|
|
99
114
|
|
|
100
|
-
Use the `--directives` option with any command to add custom directives for a single run:
|
|
115
|
+
Use the `--directives` global option with any command to add custom directives for a single run:
|
|
101
116
|
|
|
102
117
|
```bash
|
|
103
|
-
sphinx-cmd
|
|
118
|
+
sphinx-cmd --directives drawio-figure,drawio-image rm path/to/docs
|
|
104
119
|
```
|
|
105
120
|
|
|
106
121
|
### 2. Configuration File
|
|
@@ -132,6 +147,7 @@ pytest
|
|
|
132
147
|
|
|
133
148
|
# Run linters
|
|
134
149
|
black sphinx_cmd tests
|
|
150
|
+
isort sphinx_cmd tests
|
|
135
151
|
flake8 sphinx_cmd tests
|
|
136
152
|
mypy sphinx_cmd
|
|
137
153
|
|
|
@@ -141,7 +157,7 @@ sphinx-cmd rm --help
|
|
|
141
157
|
sphinx-cmd mv --help
|
|
142
158
|
```
|
|
143
159
|
|
|
144
|
-
|
|
160
|
+
### Adding New Commands
|
|
145
161
|
|
|
146
162
|
The architecture is designed to make adding new commands easy:
|
|
147
163
|
|
|
@@ -149,6 +165,7 @@ The architecture is designed to make adding new commands easy:
|
|
|
149
165
|
2. Implement an `execute(args)` function in your new file
|
|
150
166
|
3. Import the command in `sphinx_cmd/cli.py`
|
|
151
167
|
4. Add a new subparser for your command in `create_parser()`
|
|
168
|
+
5. Create new tests (e.g., `tests/test_new_command.py`)
|
|
152
169
|
|
|
153
170
|
## License
|
|
154
171
|
|
|
@@ -14,6 +14,12 @@ def test_cli_help():
|
|
|
14
14
|
assert "sphinx-cmd" in parser.format_help()
|
|
15
15
|
assert "rm" in parser.format_help()
|
|
16
16
|
assert "mv" in parser.format_help()
|
|
17
|
+
# Check that global arguments are present
|
|
18
|
+
assert "--context" in parser.format_help()
|
|
19
|
+
assert "-c" in parser.format_help()
|
|
20
|
+
assert "--dry-run" in parser.format_help()
|
|
21
|
+
assert "-n" in parser.format_help()
|
|
22
|
+
assert "--directives" in parser.format_help()
|
|
17
23
|
|
|
18
24
|
|
|
19
25
|
def test_cli_no_command():
|
|
@@ -41,3 +47,15 @@ def test_cli_mv_help():
|
|
|
41
47
|
main()
|
|
42
48
|
# Help should exit with code 0
|
|
43
49
|
assert excinfo.value.code == 0
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def test_cli_invalid_context():
|
|
53
|
+
"""Test CLI behavior when an invalid context path is provided."""
|
|
54
|
+
with patch.object(
|
|
55
|
+
sys,
|
|
56
|
+
"argv",
|
|
57
|
+
["sphinx-cmd", "--context", "/nonexistent/path", "rm", "some_file.rst"],
|
|
58
|
+
):
|
|
59
|
+
with patch.object(sys, "exit") as mock_exit:
|
|
60
|
+
main()
|
|
61
|
+
mock_exit.assert_called_once_with(1)
|
|
@@ -2,7 +2,7 @@ import tempfile
|
|
|
2
2
|
from pathlib import Path
|
|
3
3
|
from unittest.mock import patch
|
|
4
4
|
|
|
5
|
-
from sphinx_cmd.config import get_directive_patterns, load_config
|
|
5
|
+
from sphinx_cmd.config import find_sphinx_conf, get_directive_patterns, load_config
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
def test_default_config():
|
|
@@ -107,3 +107,85 @@ directives = ["drawio-figure"]
|
|
|
107
107
|
match = patterns["custom-directive"].findall(test_string)
|
|
108
108
|
assert len(match) == 1
|
|
109
109
|
assert match[0] == "path/to/custom.file"
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def test_find_sphinx_conf():
|
|
113
|
+
"""Test finding the nearest conf.py file."""
|
|
114
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
115
|
+
tmp_path = Path(tmpdir)
|
|
116
|
+
|
|
117
|
+
# Create a deep directory structure with conf.py at different levels
|
|
118
|
+
docs_dir = tmp_path / "docs"
|
|
119
|
+
docs_dir.mkdir()
|
|
120
|
+
|
|
121
|
+
# Create a conf.py file in the docs directory
|
|
122
|
+
docs_conf = docs_dir / "conf.py"
|
|
123
|
+
docs_conf.touch()
|
|
124
|
+
|
|
125
|
+
# Create subdirectories
|
|
126
|
+
subdir = docs_dir / "subdir"
|
|
127
|
+
subdir.mkdir()
|
|
128
|
+
subsubdir = subdir / "subsubdir"
|
|
129
|
+
subsubdir.mkdir()
|
|
130
|
+
|
|
131
|
+
# Test finding conf.py from docs directory
|
|
132
|
+
found_conf = find_sphinx_conf(docs_dir)
|
|
133
|
+
assert found_conf == docs_conf
|
|
134
|
+
|
|
135
|
+
# Test finding conf.py from subdirectory
|
|
136
|
+
found_conf = find_sphinx_conf(subdir)
|
|
137
|
+
assert found_conf == docs_conf
|
|
138
|
+
|
|
139
|
+
# Test finding conf.py from sub-subdirectory
|
|
140
|
+
found_conf = find_sphinx_conf(subsubdir)
|
|
141
|
+
assert found_conf == docs_conf
|
|
142
|
+
|
|
143
|
+
# Test finding conf.py from a file in the directory
|
|
144
|
+
test_file = subsubdir / "test.rst"
|
|
145
|
+
test_file.touch()
|
|
146
|
+
found_conf = find_sphinx_conf(test_file)
|
|
147
|
+
assert found_conf == docs_conf
|
|
148
|
+
|
|
149
|
+
# Test when no conf.py is found
|
|
150
|
+
outside_dir = tmp_path / "outside"
|
|
151
|
+
outside_dir.mkdir()
|
|
152
|
+
found_conf = find_sphinx_conf(outside_dir)
|
|
153
|
+
assert found_conf is None
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def test_context_in_config():
|
|
157
|
+
"""Test that sphinx_context is properly added to config."""
|
|
158
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
159
|
+
tmp_path = Path(tmpdir)
|
|
160
|
+
|
|
161
|
+
# Create a docs directory with conf.py
|
|
162
|
+
docs_dir = tmp_path / "docs"
|
|
163
|
+
docs_dir.mkdir()
|
|
164
|
+
conf_path = docs_dir / "conf.py"
|
|
165
|
+
conf_path.touch()
|
|
166
|
+
|
|
167
|
+
# Test that context is added when path is provided
|
|
168
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=None):
|
|
169
|
+
config = load_config(context_path=str(docs_dir))
|
|
170
|
+
assert "sphinx_context" in config
|
|
171
|
+
assert config["sphinx_context"] == [str(docs_dir)]
|
|
172
|
+
|
|
173
|
+
# Test auto-detection when no context_path is provided
|
|
174
|
+
subdir = docs_dir / "subdir"
|
|
175
|
+
subdir.mkdir()
|
|
176
|
+
|
|
177
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=None):
|
|
178
|
+
with patch("sphinx_cmd.config.Path.cwd", return_value=subdir):
|
|
179
|
+
config = load_config()
|
|
180
|
+
assert "sphinx_context" in config
|
|
181
|
+
assert config["sphinx_context"] == [str(docs_dir)]
|
|
182
|
+
|
|
183
|
+
# Test when no conf.py is found
|
|
184
|
+
outside_dir = tmp_path / "outside"
|
|
185
|
+
outside_dir.mkdir()
|
|
186
|
+
|
|
187
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=None):
|
|
188
|
+
with patch("sphinx_cmd.config.Path.cwd", return_value=outside_dir):
|
|
189
|
+
config = load_config()
|
|
190
|
+
assert "sphinx_context" in config
|
|
191
|
+
assert config["sphinx_context"] == []
|
|
@@ -19,9 +19,9 @@ Test Page
|
|
|
19
19
|
with open(os.path.join(tmpdir, "test.png"), "w") as f:
|
|
20
20
|
f.write("fake image")
|
|
21
21
|
|
|
22
|
-
# Run the command with dry-run
|
|
22
|
+
# Run the command with dry-run (now using global --dry-run option)
|
|
23
23
|
result = subprocess.run(
|
|
24
|
-
["sphinx-cmd", "rm", tmpdir
|
|
24
|
+
["sphinx-cmd", "--dry-run", "rm", tmpdir], capture_output=True, text=True
|
|
25
25
|
)
|
|
26
26
|
|
|
27
27
|
# Check that it executed successfully
|
|
@@ -145,8 +145,10 @@ def test_move_command_dry_run():
|
|
|
145
145
|
args = Mock()
|
|
146
146
|
args.source = "source.rst"
|
|
147
147
|
args.destination = "destination.rst"
|
|
148
|
-
args.dry_run = True
|
|
148
|
+
args.dry_run = True # Global option, but still accessed in command
|
|
149
149
|
args.no_update_refs = False
|
|
150
|
+
args.directives = None # Mock the global directives option
|
|
151
|
+
args.context = None # Mock the global context option
|
|
150
152
|
|
|
151
153
|
# Run the move command
|
|
152
154
|
from sphinx_cmd.commands.mv import execute
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import os
|
|
2
2
|
import tempfile
|
|
3
|
-
from
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from unittest.mock import Mock, patch
|
|
4
5
|
|
|
5
6
|
from sphinx_cmd.commands.rm import (
|
|
6
7
|
build_asset_index,
|
|
@@ -30,8 +31,12 @@ def test_nested_includes_extraction():
|
|
|
30
31
|
with open(main_file, "w") as f:
|
|
31
32
|
f.write("Main file\n.. include:: include1.rst\n")
|
|
32
33
|
|
|
33
|
-
#
|
|
34
|
-
|
|
34
|
+
# Mock os.getcwd and Path.cwd to return the temp directory
|
|
35
|
+
with patch("os.getcwd", return_value=tmpdir):
|
|
36
|
+
with patch("pathlib.Path.cwd", return_value=Path(tmpdir)):
|
|
37
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=None):
|
|
38
|
+
# Test the transitive includes function
|
|
39
|
+
includes = get_transitive_includes(main_file)
|
|
35
40
|
|
|
36
41
|
# Should have found both include files
|
|
37
42
|
assert len(includes) == 2
|
|
@@ -70,22 +75,33 @@ Test Page
|
|
|
70
75
|
assert "test.rst" in rst_files[0]
|
|
71
76
|
|
|
72
77
|
# Test extracting assets
|
|
73
|
-
|
|
78
|
+
with patch("os.getcwd", return_value=tmpdir):
|
|
79
|
+
with patch("pathlib.Path.cwd", return_value=Path(tmpdir)):
|
|
80
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=None):
|
|
81
|
+
assets = extract_assets(rst_files[0])
|
|
74
82
|
assert len(assets) == 2
|
|
75
83
|
|
|
76
84
|
# Test building asset index
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
85
|
+
with patch("os.getcwd", return_value=tmpdir):
|
|
86
|
+
with patch("pathlib.Path.cwd", return_value=Path(tmpdir)):
|
|
87
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=None):
|
|
88
|
+
asset_to_files, file_to_assets, asset_directive_map = (
|
|
89
|
+
build_asset_index(rst_files)
|
|
90
|
+
)
|
|
80
91
|
assert len(asset_to_files) == 2
|
|
81
92
|
assert len(file_to_assets) == 1
|
|
82
93
|
|
|
83
94
|
# Test dry run with mock args
|
|
84
95
|
args = Mock()
|
|
85
96
|
args.path = test_dir
|
|
86
|
-
args.dry_run = True
|
|
87
|
-
args.directives = None
|
|
88
|
-
|
|
97
|
+
args.dry_run = True # Global option, but still accessed in command
|
|
98
|
+
args.directives = None # Global option, but still accessed in command
|
|
99
|
+
args.context = None # Global option, accessed the same way
|
|
100
|
+
|
|
101
|
+
with patch("os.getcwd", return_value=tmpdir):
|
|
102
|
+
with patch("pathlib.Path.cwd", return_value=Path(tmpdir)):
|
|
103
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=None):
|
|
104
|
+
execute(args)
|
|
89
105
|
|
|
90
106
|
# Verify files still exist after dry run
|
|
91
107
|
assert os.path.exists(os.path.join(test_dir, "test.rst"))
|
|
@@ -129,9 +145,13 @@ def test_empty_directory_removal():
|
|
|
129
145
|
args.path = docs_dir
|
|
130
146
|
args.dry_run = False
|
|
131
147
|
args.directives = None
|
|
148
|
+
args.context = None
|
|
132
149
|
|
|
133
150
|
# Execute the command to remove files
|
|
134
|
-
|
|
151
|
+
with patch("os.getcwd", return_value=tmpdir):
|
|
152
|
+
with patch("pathlib.Path.cwd", return_value=Path(tmpdir)):
|
|
153
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=None):
|
|
154
|
+
execute(args)
|
|
135
155
|
|
|
136
156
|
# Verify that the files were removed
|
|
137
157
|
assert not os.path.exists(rst_path)
|
|
@@ -172,9 +192,13 @@ def test_non_empty_directory_retained():
|
|
|
172
192
|
args.path = docs_dir
|
|
173
193
|
args.dry_run = False
|
|
174
194
|
args.directives = None
|
|
195
|
+
args.context = None
|
|
175
196
|
|
|
176
197
|
# Execute the command to remove files
|
|
177
|
-
|
|
198
|
+
with patch("os.getcwd", return_value=tmpdir):
|
|
199
|
+
with patch("pathlib.Path.cwd", return_value=Path(tmpdir)):
|
|
200
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=None):
|
|
201
|
+
execute(args)
|
|
178
202
|
|
|
179
203
|
# Verify that subdir was removed but nested_dir was retained
|
|
180
204
|
assert not os.path.exists(nested_subdir)
|
|
@@ -202,7 +226,10 @@ Test Document
|
|
|
202
226
|
)
|
|
203
227
|
|
|
204
228
|
# Extract assets
|
|
205
|
-
|
|
229
|
+
with patch("os.getcwd", return_value=tmpdir):
|
|
230
|
+
with patch("pathlib.Path.cwd", return_value=Path(tmpdir)):
|
|
231
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=None):
|
|
232
|
+
assets = extract_assets(test_file)
|
|
206
233
|
|
|
207
234
|
# Check we have the right number of assets
|
|
208
235
|
assert len(assets) == 3
|
|
@@ -236,7 +263,10 @@ def test_get_transitive_includes():
|
|
|
236
263
|
f.write(".. include:: first.rst\n")
|
|
237
264
|
|
|
238
265
|
# Get all transitive includes from main file
|
|
239
|
-
|
|
266
|
+
with patch("os.getcwd", return_value=tmpdir):
|
|
267
|
+
with patch("pathlib.Path.cwd", return_value=Path(tmpdir)):
|
|
268
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=None):
|
|
269
|
+
includes = get_transitive_includes(main_file)
|
|
240
270
|
|
|
241
271
|
# Should find both included files
|
|
242
272
|
assert len(includes) == 2
|
|
@@ -258,7 +288,10 @@ def test_circular_includes_detection():
|
|
|
258
288
|
f.write("File 2\n\n.. include:: file1.rst\n")
|
|
259
289
|
|
|
260
290
|
# Should find both includes but avoid infinite recursion
|
|
261
|
-
|
|
291
|
+
with patch("os.getcwd", return_value=tmpdir):
|
|
292
|
+
with patch("pathlib.Path.cwd", return_value=Path(tmpdir)):
|
|
293
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=None):
|
|
294
|
+
includes = get_transitive_includes(file1)
|
|
262
295
|
assert len(includes) == 2 # Changed from 1 to 2
|
|
263
296
|
assert file2 in includes
|
|
264
297
|
assert file1 in includes # file1 is included by file2
|
|
@@ -317,3 +350,77 @@ def test_remove_empty_dirs_function():
|
|
|
317
350
|
assert len(deleted_dirs) == 2
|
|
318
351
|
assert other_dir in deleted_dirs
|
|
319
352
|
assert parent_dir in deleted_dirs
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
def test_context_path_protection():
|
|
356
|
+
"""Test that files outside the context path are identified in dry-run mode."""
|
|
357
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
358
|
+
# Create a docs directory that will serve as our context
|
|
359
|
+
docs_dir = os.path.join(tmpdir, "docs")
|
|
360
|
+
os.makedirs(docs_dir)
|
|
361
|
+
|
|
362
|
+
# Create another directory outside the context
|
|
363
|
+
outside_dir = os.path.join(tmpdir, "outside")
|
|
364
|
+
os.makedirs(outside_dir)
|
|
365
|
+
|
|
366
|
+
# Create a conf.py file in the docs directory to mark it as a Sphinx context
|
|
367
|
+
with open(os.path.join(docs_dir, "conf.py"), "w") as f:
|
|
368
|
+
f.write("# Sphinx configuration file")
|
|
369
|
+
|
|
370
|
+
# Create an RST file in the docs directory that references a file outside
|
|
371
|
+
# the context
|
|
372
|
+
rst_content = """
|
|
373
|
+
Test Page
|
|
374
|
+
=========
|
|
375
|
+
|
|
376
|
+
.. image:: ../outside/image.png
|
|
377
|
+
"""
|
|
378
|
+
rst_file = os.path.join(docs_dir, "test.rst")
|
|
379
|
+
with open(rst_file, "w") as f:
|
|
380
|
+
f.write(rst_content)
|
|
381
|
+
|
|
382
|
+
# Create the referenced asset in the outside directory
|
|
383
|
+
outside_img = os.path.join(outside_dir, "image.png")
|
|
384
|
+
with open(outside_img, "w") as f:
|
|
385
|
+
f.write("fake image")
|
|
386
|
+
|
|
387
|
+
# Execute the rm command with context_path set to docs_dir (DRY RUN ONLY)
|
|
388
|
+
args = Mock()
|
|
389
|
+
args.path = docs_dir
|
|
390
|
+
args.dry_run = True # Only use dry run mode
|
|
391
|
+
args.directives = None
|
|
392
|
+
args.context = docs_dir
|
|
393
|
+
|
|
394
|
+
# Capture stdout to verify the messages
|
|
395
|
+
import sys
|
|
396
|
+
from io import StringIO
|
|
397
|
+
|
|
398
|
+
captured_output = StringIO()
|
|
399
|
+
original_stdout = sys.stdout
|
|
400
|
+
sys.stdout = captured_output
|
|
401
|
+
|
|
402
|
+
try:
|
|
403
|
+
# Execute in dry run mode
|
|
404
|
+
with patch("os.getcwd", return_value=tmpdir):
|
|
405
|
+
with patch("pathlib.Path.cwd", return_value=Path(tmpdir)):
|
|
406
|
+
with patch("sphinx_cmd.config.get_config_path", return_value=None):
|
|
407
|
+
execute(args)
|
|
408
|
+
|
|
409
|
+
# Get the captured output
|
|
410
|
+
output = captured_output.getvalue()
|
|
411
|
+
|
|
412
|
+
# Verify that it shows the outside file would be skipped
|
|
413
|
+
assert "Skipping" in output
|
|
414
|
+
assert "outside context" in output
|
|
415
|
+
assert outside_img in output
|
|
416
|
+
|
|
417
|
+
# Verify the RST file would be deleted (not skipped)
|
|
418
|
+
assert "Would delete page:" in output
|
|
419
|
+
assert rst_file in output
|
|
420
|
+
|
|
421
|
+
# Verify both files still exist after dry run
|
|
422
|
+
assert os.path.exists(rst_file)
|
|
423
|
+
assert os.path.exists(outside_img)
|
|
424
|
+
|
|
425
|
+
finally:
|
|
426
|
+
sys.stdout = original_stdout
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|