sphinx-cmd 0.2.0__tar.gz → 0.2.2__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.2.0/sphinx_cmd.egg-info → sphinx_cmd-0.2.2}/PKG-INFO +2 -2
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2}/README.md +1 -1
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2}/pyproject.toml +4 -1
- sphinx_cmd-0.2.2/sphinx_cmd/__init__.py +4 -0
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2}/sphinx_cmd/cli.py +5 -1
- sphinx_cmd-0.2.2/sphinx_cmd/commands/rm.py +263 -0
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2/sphinx_cmd.egg-info}/PKG-INFO +2 -2
- sphinx_cmd-0.2.2/tests/test_rm.py +316 -0
- sphinx_cmd-0.2.0/sphinx_cmd/__init__.py +0 -2
- sphinx_cmd-0.2.0/sphinx_cmd/commands/rm.py +0 -104
- sphinx_cmd-0.2.0/tests/test_rm.py +0 -63
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2}/LICENSE +0 -0
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2}/MANIFEST.in +0 -0
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2}/setup.cfg +0 -0
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2}/sphinx_cmd/commands/__init__.py +0 -0
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2}/sphinx_cmd/commands/mv.py +0 -0
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2}/sphinx_cmd/py.typed +0 -0
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2}/sphinx_cmd.egg-info/SOURCES.txt +0 -0
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2}/sphinx_cmd.egg-info/dependency_links.txt +0 -0
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2}/sphinx_cmd.egg-info/entry_points.txt +0 -0
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2}/sphinx_cmd.egg-info/requires.txt +0 -0
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2}/sphinx_cmd.egg-info/top_level.txt +0 -0
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2}/tests/test_cli.py +0 -0
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2}/tests/test_integration.py +0 -0
- {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.2}/tests/test_mv.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sphinx-cmd
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
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
|
|
@@ -42,7 +42,7 @@ The `sphinx-cmd` tool provides subcommands for different Sphinx documentation ma
|
|
|
42
42
|
|
|
43
43
|
### `sphinx-cmd rm`
|
|
44
44
|
|
|
45
|
-
Delete unused .rst files and their unique assets if not used elsewhere.
|
|
45
|
+
Delete unused .rst files and their unique assets (images, includes, etc) if not used elsewhere.
|
|
46
46
|
|
|
47
47
|
```bash
|
|
48
48
|
# Remove files and assets
|
|
@@ -14,7 +14,7 @@ The `sphinx-cmd` tool provides subcommands for different Sphinx documentation ma
|
|
|
14
14
|
|
|
15
15
|
### `sphinx-cmd rm`
|
|
16
16
|
|
|
17
|
-
Delete unused .rst files and their unique assets if not used elsewhere.
|
|
17
|
+
Delete unused .rst files and their unique assets (images, includes, etc) if not used elsewhere.
|
|
18
18
|
|
|
19
19
|
```bash
|
|
20
20
|
# Remove files and assets
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "sphinx-cmd"
|
|
7
|
-
|
|
7
|
+
dynamic = ["version"]
|
|
8
8
|
authors = [
|
|
9
9
|
{name = "Jared Dillard", email = "jared.dillard@gmail.com"},
|
|
10
10
|
]
|
|
@@ -48,6 +48,9 @@ packages = ["sphinx_cmd", "sphinx_cmd.commands"]
|
|
|
48
48
|
[tool.setuptools.package-data]
|
|
49
49
|
sphinx_cmd = ["py.typed"]
|
|
50
50
|
|
|
51
|
+
[tool.setuptools.dynamic]
|
|
52
|
+
version = {attr = "sphinx_cmd.__version__"}
|
|
53
|
+
|
|
51
54
|
[tool.black]
|
|
52
55
|
line-length = 88
|
|
53
56
|
target-version = ['py39', 'py310', 'py311', 'py312']
|
|
@@ -6,6 +6,8 @@ Main CLI entry point for sphinx-cmd with subcommands.
|
|
|
6
6
|
import argparse
|
|
7
7
|
import sys
|
|
8
8
|
|
|
9
|
+
from sphinx_cmd import __version__
|
|
10
|
+
|
|
9
11
|
|
|
10
12
|
def create_parser():
|
|
11
13
|
"""Create the main argument parser with subcommands."""
|
|
@@ -15,7 +17,9 @@ def create_parser():
|
|
|
15
17
|
)
|
|
16
18
|
|
|
17
19
|
# Add version option
|
|
18
|
-
parser.add_argument(
|
|
20
|
+
parser.add_argument(
|
|
21
|
+
"--version", action="version", version=f"%(prog)s {__version__}"
|
|
22
|
+
)
|
|
19
23
|
|
|
20
24
|
# Create subparsers for subcommands
|
|
21
25
|
subparsers = parser.add_subparsers(
|
|
@@ -0,0 +1,263 @@
|
|
|
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, visited=None):
|
|
31
|
+
"""Extract asset references from an .rst file, recursively parsing includes."""
|
|
32
|
+
if visited is None:
|
|
33
|
+
visited = set()
|
|
34
|
+
|
|
35
|
+
# Avoid circular includes
|
|
36
|
+
abs_path = os.path.abspath(file_path)
|
|
37
|
+
if abs_path in visited:
|
|
38
|
+
return {}
|
|
39
|
+
visited.add(abs_path)
|
|
40
|
+
|
|
41
|
+
asset_directives = {}
|
|
42
|
+
|
|
43
|
+
# If file doesn't exist, skip it
|
|
44
|
+
if not os.path.exists(file_path):
|
|
45
|
+
return asset_directives
|
|
46
|
+
|
|
47
|
+
try:
|
|
48
|
+
with open(file_path, encoding="utf-8") as f:
|
|
49
|
+
content = f.read()
|
|
50
|
+
for directive, pattern in DIRECTIVE_PATTERNS.items():
|
|
51
|
+
for match in pattern.findall(content):
|
|
52
|
+
asset_path = match.strip()
|
|
53
|
+
asset_full_path = os.path.normpath(
|
|
54
|
+
os.path.join(os.path.dirname(file_path), asset_path)
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
if directive == "include":
|
|
58
|
+
# Recursively extract assets from included files
|
|
59
|
+
included_assets = extract_assets(
|
|
60
|
+
asset_full_path, visited.copy()
|
|
61
|
+
)
|
|
62
|
+
asset_directives.update(included_assets)
|
|
63
|
+
|
|
64
|
+
asset_directives[asset_full_path] = directive
|
|
65
|
+
except Exception as e:
|
|
66
|
+
print(f"Warning: Could not read {file_path}: {e}")
|
|
67
|
+
|
|
68
|
+
return asset_directives
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def build_asset_index(rst_files):
|
|
72
|
+
"""Build an index of assets and which files reference them."""
|
|
73
|
+
asset_to_files = defaultdict(set)
|
|
74
|
+
file_to_assets = {}
|
|
75
|
+
asset_directive_map = {}
|
|
76
|
+
|
|
77
|
+
for rst in rst_files:
|
|
78
|
+
asset_directives = extract_assets(rst)
|
|
79
|
+
file_to_assets[rst] = set(asset_directives.keys())
|
|
80
|
+
for asset, directive in asset_directives.items():
|
|
81
|
+
asset_to_files[asset].add(rst)
|
|
82
|
+
asset_directive_map[asset] = directive
|
|
83
|
+
return asset_to_files, file_to_assets, asset_directive_map
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def get_transitive_includes(file_path, visited=None):
|
|
87
|
+
"""Get all files included transitively from a file."""
|
|
88
|
+
if visited is None:
|
|
89
|
+
visited = set()
|
|
90
|
+
|
|
91
|
+
# Avoid circular includes
|
|
92
|
+
abs_path = os.path.abspath(file_path)
|
|
93
|
+
if abs_path in visited:
|
|
94
|
+
return set()
|
|
95
|
+
visited.add(abs_path)
|
|
96
|
+
|
|
97
|
+
includes = set()
|
|
98
|
+
|
|
99
|
+
if not os.path.exists(file_path):
|
|
100
|
+
return includes
|
|
101
|
+
|
|
102
|
+
try:
|
|
103
|
+
with open(file_path, encoding="utf-8") as f:
|
|
104
|
+
content = f.read()
|
|
105
|
+
pattern = DIRECTIVE_PATTERNS["include"]
|
|
106
|
+
for match in pattern.findall(content):
|
|
107
|
+
include_path = match.strip()
|
|
108
|
+
include_full_path = os.path.normpath(
|
|
109
|
+
os.path.join(os.path.dirname(file_path), include_path)
|
|
110
|
+
)
|
|
111
|
+
includes.add(include_full_path)
|
|
112
|
+
# Recursively get includes from the included file
|
|
113
|
+
includes.update(
|
|
114
|
+
get_transitive_includes(include_full_path, visited.copy())
|
|
115
|
+
)
|
|
116
|
+
except Exception as e:
|
|
117
|
+
print(f"Warning: Could not read {file_path}: {e}")
|
|
118
|
+
|
|
119
|
+
return includes
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def delete_unused_assets_and_pages(
|
|
123
|
+
asset_to_files, file_to_assets, asset_directive_map, dry_run=False
|
|
124
|
+
):
|
|
125
|
+
"""Delete files and their unique assets if not used elsewhere."""
|
|
126
|
+
deleted_pages = []
|
|
127
|
+
deleted_assets = []
|
|
128
|
+
affected_dirs = set()
|
|
129
|
+
|
|
130
|
+
# Track which files have been processed to avoid duplicates
|
|
131
|
+
processed_files = set()
|
|
132
|
+
|
|
133
|
+
for rst_file, assets in file_to_assets.items():
|
|
134
|
+
# Skip if already processed (can happen with transitive includes)
|
|
135
|
+
if rst_file in processed_files:
|
|
136
|
+
continue
|
|
137
|
+
|
|
138
|
+
unused_assets = [a for a in assets if len(asset_to_files[a]) == 1]
|
|
139
|
+
if len(unused_assets) == len(assets): # All assets are unique to this file
|
|
140
|
+
# Get all files transitively included by this file
|
|
141
|
+
included_files = get_transitive_includes(rst_file)
|
|
142
|
+
|
|
143
|
+
# Process the main file and all its includes
|
|
144
|
+
for file_to_process in [rst_file] + list(included_files):
|
|
145
|
+
if (
|
|
146
|
+
file_to_process in processed_files
|
|
147
|
+
or file_to_process not in file_to_assets
|
|
148
|
+
):
|
|
149
|
+
continue
|
|
150
|
+
|
|
151
|
+
processed_files.add(file_to_process)
|
|
152
|
+
file_assets = file_to_assets.get(file_to_process, set())
|
|
153
|
+
file_unused_assets = [
|
|
154
|
+
a for a in file_assets if len(asset_to_files[a]) == 1
|
|
155
|
+
]
|
|
156
|
+
|
|
157
|
+
# Delete unused assets for this file
|
|
158
|
+
for asset in file_unused_assets:
|
|
159
|
+
directive = asset_directive_map.get(asset, "asset")
|
|
160
|
+
if os.path.exists(asset) and asset not in deleted_assets:
|
|
161
|
+
if dry_run:
|
|
162
|
+
origin = (
|
|
163
|
+
" (from include)" if file_to_process != rst_file else ""
|
|
164
|
+
)
|
|
165
|
+
print(
|
|
166
|
+
f"[dry-run] Would delete {directive}: {asset}{origin}"
|
|
167
|
+
)
|
|
168
|
+
else:
|
|
169
|
+
affected_dirs.add(os.path.dirname(asset))
|
|
170
|
+
os.remove(asset)
|
|
171
|
+
deleted_assets.append(asset)
|
|
172
|
+
|
|
173
|
+
# Delete the file if it exists and isn't the main rst file being checked
|
|
174
|
+
if file_to_process != rst_file and os.path.exists(file_to_process):
|
|
175
|
+
if dry_run:
|
|
176
|
+
print(
|
|
177
|
+
f"[dry-run] Would delete included file: {file_to_process}"
|
|
178
|
+
)
|
|
179
|
+
else:
|
|
180
|
+
affected_dirs.add(os.path.dirname(file_to_process))
|
|
181
|
+
os.remove(file_to_process)
|
|
182
|
+
deleted_pages.append(file_to_process)
|
|
183
|
+
|
|
184
|
+
# Finally, delete the main rst file
|
|
185
|
+
if os.path.exists(rst_file):
|
|
186
|
+
if dry_run:
|
|
187
|
+
print(f"[dry-run] Would delete page: {rst_file}")
|
|
188
|
+
else:
|
|
189
|
+
affected_dirs.add(os.path.dirname(rst_file))
|
|
190
|
+
os.remove(rst_file)
|
|
191
|
+
deleted_pages.append(rst_file)
|
|
192
|
+
|
|
193
|
+
return deleted_pages, deleted_assets, affected_dirs
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def remove_empty_dirs(dirs, original_path, dry_run=False):
|
|
197
|
+
"""Remove empty directories, bottom-up."""
|
|
198
|
+
deleted_dirs = []
|
|
199
|
+
|
|
200
|
+
# Add parent directories to the affected dirs set
|
|
201
|
+
all_dirs = set(dirs)
|
|
202
|
+
for dir_path in dirs:
|
|
203
|
+
# Add all parent directories up to but not including the original path
|
|
204
|
+
parent = os.path.dirname(dir_path)
|
|
205
|
+
while parent and os.path.exists(parent) and parent != original_path:
|
|
206
|
+
all_dirs.add(parent)
|
|
207
|
+
parent = os.path.dirname(parent)
|
|
208
|
+
|
|
209
|
+
# Sort by path depth (deepest first)
|
|
210
|
+
sorted_dirs = sorted(all_dirs, key=lambda d: d.count(os.sep), reverse=True)
|
|
211
|
+
|
|
212
|
+
# Process directories from deepest to shallowest
|
|
213
|
+
for dir_path in sorted_dirs:
|
|
214
|
+
if not os.path.exists(dir_path) or not os.path.isdir(dir_path):
|
|
215
|
+
continue
|
|
216
|
+
|
|
217
|
+
# Check if directory is empty
|
|
218
|
+
if not os.listdir(dir_path):
|
|
219
|
+
if dry_run:
|
|
220
|
+
print(f"[dry-run] Would delete empty directory: {dir_path}")
|
|
221
|
+
else:
|
|
222
|
+
os.rmdir(dir_path)
|
|
223
|
+
deleted_dirs.append(dir_path)
|
|
224
|
+
|
|
225
|
+
# Check if the original path (if it's a directory) is now empty and should
|
|
226
|
+
# be removed
|
|
227
|
+
if os.path.isdir(original_path) and not os.listdir(original_path):
|
|
228
|
+
if dry_run:
|
|
229
|
+
print(f"[dry-run] Would delete empty directory: {original_path}")
|
|
230
|
+
else:
|
|
231
|
+
os.rmdir(original_path)
|
|
232
|
+
deleted_dirs.append(original_path)
|
|
233
|
+
|
|
234
|
+
return deleted_dirs
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def execute(args):
|
|
238
|
+
"""Execute the rm command."""
|
|
239
|
+
original_path = os.path.abspath(args.path)
|
|
240
|
+
rst_files = find_rst_files(args.path)
|
|
241
|
+
asset_to_files, file_to_assets, asset_directive_map = build_asset_index(rst_files)
|
|
242
|
+
deleted_pages, deleted_assets, affected_dirs = delete_unused_assets_and_pages(
|
|
243
|
+
asset_to_files, file_to_assets, asset_directive_map, args.dry_run
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
deleted_dirs = []
|
|
247
|
+
if affected_dirs:
|
|
248
|
+
deleted_dirs = remove_empty_dirs(affected_dirs, original_path, args.dry_run)
|
|
249
|
+
|
|
250
|
+
if not args.dry_run:
|
|
251
|
+
print(f"\nDeleted {len(deleted_assets)} unused asset(s):")
|
|
252
|
+
for a in deleted_assets:
|
|
253
|
+
directive = asset_directive_map.get(a, "asset")
|
|
254
|
+
print(f" - ({directive}) {a}")
|
|
255
|
+
|
|
256
|
+
print(f"\nDeleted {len(deleted_pages)} RST page(s):")
|
|
257
|
+
for p in deleted_pages:
|
|
258
|
+
print(f" - {p}")
|
|
259
|
+
|
|
260
|
+
if deleted_dirs:
|
|
261
|
+
print(f"\nDeleted {len(deleted_dirs)} empty directory/directories:")
|
|
262
|
+
for d in deleted_dirs:
|
|
263
|
+
print(f" - {d}")
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sphinx-cmd
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
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
|
|
@@ -42,7 +42,7 @@ The `sphinx-cmd` tool provides subcommands for different Sphinx documentation ma
|
|
|
42
42
|
|
|
43
43
|
### `sphinx-cmd rm`
|
|
44
44
|
|
|
45
|
-
Delete unused .rst files and their unique assets if not used elsewhere.
|
|
45
|
+
Delete unused .rst files and their unique assets (images, includes, etc) if not used elsewhere.
|
|
46
46
|
|
|
47
47
|
```bash
|
|
48
48
|
# Remove files and assets
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import tempfile
|
|
3
|
+
from unittest.mock import Mock
|
|
4
|
+
|
|
5
|
+
from sphinx_cmd.commands.rm import (
|
|
6
|
+
build_asset_index,
|
|
7
|
+
execute,
|
|
8
|
+
extract_assets,
|
|
9
|
+
find_rst_files,
|
|
10
|
+
get_transitive_includes,
|
|
11
|
+
remove_empty_dirs,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_nested_includes_extraction():
|
|
16
|
+
"""Test that nested includes are properly extracted."""
|
|
17
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
18
|
+
# Create a more complex nested include structure
|
|
19
|
+
main_file = os.path.join(tmpdir, "main.rst")
|
|
20
|
+
include1 = os.path.join(tmpdir, "include1.rst")
|
|
21
|
+
include2 = os.path.join(tmpdir, "include2.rst")
|
|
22
|
+
|
|
23
|
+
# Create the files with content
|
|
24
|
+
with open(include2, "w") as f:
|
|
25
|
+
f.write("Level 2 include\n")
|
|
26
|
+
|
|
27
|
+
with open(include1, "w") as f:
|
|
28
|
+
f.write("Level 1 include\n.. include:: include2.rst\n")
|
|
29
|
+
|
|
30
|
+
with open(main_file, "w") as f:
|
|
31
|
+
f.write("Main file\n.. include:: include1.rst\n")
|
|
32
|
+
|
|
33
|
+
# Test the transitive includes function
|
|
34
|
+
includes = get_transitive_includes(main_file)
|
|
35
|
+
|
|
36
|
+
# Should have found both include files
|
|
37
|
+
assert len(includes) == 2
|
|
38
|
+
assert include1 in includes
|
|
39
|
+
assert include2 in includes
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_rm_command_functionality():
|
|
43
|
+
"""Test the rm command functionality."""
|
|
44
|
+
# Create a temporary directory structure
|
|
45
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
46
|
+
# Create test files
|
|
47
|
+
test_dir = os.path.join(tmpdir, "docs")
|
|
48
|
+
os.makedirs(test_dir)
|
|
49
|
+
|
|
50
|
+
# Create an RST file
|
|
51
|
+
rst_content = """
|
|
52
|
+
Test Page
|
|
53
|
+
=========
|
|
54
|
+
|
|
55
|
+
.. image:: image.png
|
|
56
|
+
.. figure:: figure.jpg
|
|
57
|
+
"""
|
|
58
|
+
with open(os.path.join(test_dir, "test.rst"), "w") as f:
|
|
59
|
+
f.write(rst_content)
|
|
60
|
+
|
|
61
|
+
# Create referenced asset files
|
|
62
|
+
with open(os.path.join(test_dir, "image.png"), "w") as f:
|
|
63
|
+
f.write("fake image")
|
|
64
|
+
with open(os.path.join(test_dir, "figure.jpg"), "w") as f:
|
|
65
|
+
f.write("fake figure")
|
|
66
|
+
|
|
67
|
+
# Test finding RST files
|
|
68
|
+
rst_files = find_rst_files(test_dir)
|
|
69
|
+
assert len(rst_files) == 1
|
|
70
|
+
assert "test.rst" in rst_files[0]
|
|
71
|
+
|
|
72
|
+
# Test extracting assets
|
|
73
|
+
assets = extract_assets(rst_files[0])
|
|
74
|
+
assert len(assets) == 2
|
|
75
|
+
|
|
76
|
+
# Test building asset index
|
|
77
|
+
asset_to_files, file_to_assets, asset_directive_map = build_asset_index(
|
|
78
|
+
rst_files
|
|
79
|
+
)
|
|
80
|
+
assert len(asset_to_files) == 2
|
|
81
|
+
assert len(file_to_assets) == 1
|
|
82
|
+
|
|
83
|
+
# Test dry run with mock args
|
|
84
|
+
args = Mock()
|
|
85
|
+
args.path = test_dir
|
|
86
|
+
args.dry_run = True
|
|
87
|
+
execute(args)
|
|
88
|
+
|
|
89
|
+
# Verify files still exist after dry run
|
|
90
|
+
assert os.path.exists(os.path.join(test_dir, "test.rst"))
|
|
91
|
+
assert os.path.exists(os.path.join(test_dir, "image.png"))
|
|
92
|
+
assert os.path.exists(os.path.join(test_dir, "figure.jpg"))
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def test_empty_directory_removal():
|
|
96
|
+
"""Test that empty directories are properly removed."""
|
|
97
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
98
|
+
# Create a nested directory structure
|
|
99
|
+
docs_dir = os.path.join(tmpdir, "docs")
|
|
100
|
+
nested_dir = os.path.join(docs_dir, "nested")
|
|
101
|
+
nested_subdir = os.path.join(nested_dir, "subdir")
|
|
102
|
+
os.makedirs(nested_subdir)
|
|
103
|
+
|
|
104
|
+
# Create an RST file in the nested subdirectory
|
|
105
|
+
rst_path = os.path.join(nested_subdir, "test.rst")
|
|
106
|
+
rst_content = """Test page
|
|
107
|
+
=========
|
|
108
|
+
|
|
109
|
+
.. image:: image.png
|
|
110
|
+
"""
|
|
111
|
+
with open(rst_path, "w") as f:
|
|
112
|
+
f.write(rst_content)
|
|
113
|
+
|
|
114
|
+
# Create an image in the same directory
|
|
115
|
+
img_path = os.path.join(nested_subdir, "image.png")
|
|
116
|
+
with open(img_path, "w") as f:
|
|
117
|
+
f.write("fake image")
|
|
118
|
+
|
|
119
|
+
# Verify that directories exist before the test
|
|
120
|
+
assert os.path.exists(docs_dir)
|
|
121
|
+
assert os.path.exists(nested_dir)
|
|
122
|
+
assert os.path.exists(nested_subdir)
|
|
123
|
+
assert os.path.exists(rst_path)
|
|
124
|
+
assert os.path.exists(img_path)
|
|
125
|
+
|
|
126
|
+
# Execute the rm command
|
|
127
|
+
args = Mock()
|
|
128
|
+
args.path = docs_dir
|
|
129
|
+
args.dry_run = False
|
|
130
|
+
|
|
131
|
+
# Execute the command to remove files
|
|
132
|
+
execute(args)
|
|
133
|
+
|
|
134
|
+
# Verify that the files were removed
|
|
135
|
+
assert not os.path.exists(rst_path)
|
|
136
|
+
assert not os.path.exists(img_path)
|
|
137
|
+
|
|
138
|
+
# Verify that all directories were removed
|
|
139
|
+
assert not os.path.exists(nested_subdir)
|
|
140
|
+
assert not os.path.exists(nested_dir)
|
|
141
|
+
assert not os.path.exists(docs_dir)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def test_non_empty_directory_retained():
|
|
145
|
+
"""Test that non-empty directories are not removed."""
|
|
146
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
147
|
+
# Create a nested directory structure
|
|
148
|
+
docs_dir = os.path.join(tmpdir, "docs")
|
|
149
|
+
nested_dir = os.path.join(docs_dir, "nested")
|
|
150
|
+
nested_subdir = os.path.join(nested_dir, "subdir")
|
|
151
|
+
os.makedirs(nested_subdir)
|
|
152
|
+
|
|
153
|
+
# Create an RST file in the nested subdirectory
|
|
154
|
+
rst_path = os.path.join(nested_subdir, "test.rst")
|
|
155
|
+
with open(rst_path, "w") as f:
|
|
156
|
+
f.write("Test page\n=========\n\n.. image:: image.png")
|
|
157
|
+
|
|
158
|
+
# Create an image in the same directory
|
|
159
|
+
img_path = os.path.join(nested_subdir, "image.png")
|
|
160
|
+
with open(img_path, "w") as f:
|
|
161
|
+
f.write("fake image")
|
|
162
|
+
|
|
163
|
+
# Create another file in the parent directory that should be retained
|
|
164
|
+
other_file = os.path.join(nested_dir, "other.txt")
|
|
165
|
+
with open(other_file, "w") as f:
|
|
166
|
+
f.write("This file should be retained")
|
|
167
|
+
|
|
168
|
+
# Execute the rm command
|
|
169
|
+
args = Mock()
|
|
170
|
+
args.path = docs_dir
|
|
171
|
+
args.dry_run = False
|
|
172
|
+
|
|
173
|
+
# Execute the command to remove files
|
|
174
|
+
execute(args)
|
|
175
|
+
|
|
176
|
+
# Verify that subdir was removed but nested_dir was retained
|
|
177
|
+
assert not os.path.exists(nested_subdir)
|
|
178
|
+
assert os.path.exists(nested_dir)
|
|
179
|
+
assert os.path.exists(docs_dir)
|
|
180
|
+
assert os.path.exists(other_file)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def test_extract_assets_basic():
|
|
184
|
+
"""Test that extract_assets finds assets in a simple file."""
|
|
185
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
186
|
+
# Create a test file with multiple directives
|
|
187
|
+
test_file = os.path.join(tmpdir, "test.rst")
|
|
188
|
+
|
|
189
|
+
with open(test_file, "w") as f:
|
|
190
|
+
f.write(
|
|
191
|
+
"""
|
|
192
|
+
Test Document
|
|
193
|
+
============
|
|
194
|
+
|
|
195
|
+
.. image:: img.png
|
|
196
|
+
.. figure:: fig.jpg
|
|
197
|
+
.. include:: inc.rst
|
|
198
|
+
"""
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
# Extract assets
|
|
202
|
+
assets = extract_assets(test_file)
|
|
203
|
+
|
|
204
|
+
# Check we have the right number of assets
|
|
205
|
+
assert len(assets) == 3
|
|
206
|
+
|
|
207
|
+
# Check all asset paths are correct
|
|
208
|
+
img_path = os.path.join(tmpdir, "img.png")
|
|
209
|
+
fig_path = os.path.join(tmpdir, "fig.jpg")
|
|
210
|
+
inc_path = os.path.join(tmpdir, "inc.rst")
|
|
211
|
+
|
|
212
|
+
assert img_path in assets
|
|
213
|
+
assert fig_path in assets
|
|
214
|
+
assert inc_path in assets
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def test_get_transitive_includes():
|
|
218
|
+
"""Test that get_transitive_includes correctly finds all included files."""
|
|
219
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
220
|
+
# Create a multi-level include structure
|
|
221
|
+
main_file = os.path.join(tmpdir, "main.rst")
|
|
222
|
+
first_include = os.path.join(tmpdir, "first.rst")
|
|
223
|
+
second_include = os.path.join(tmpdir, "second.rst")
|
|
224
|
+
|
|
225
|
+
# Create the files
|
|
226
|
+
with open(second_include, "w") as f:
|
|
227
|
+
f.write("Content in second include\n")
|
|
228
|
+
|
|
229
|
+
with open(first_include, "w") as f:
|
|
230
|
+
f.write(".. include:: second.rst\n")
|
|
231
|
+
|
|
232
|
+
with open(main_file, "w") as f:
|
|
233
|
+
f.write(".. include:: first.rst\n")
|
|
234
|
+
|
|
235
|
+
# Get all transitive includes from main file
|
|
236
|
+
includes = get_transitive_includes(main_file)
|
|
237
|
+
|
|
238
|
+
# Should find both included files
|
|
239
|
+
assert len(includes) == 2
|
|
240
|
+
assert first_include in includes
|
|
241
|
+
assert second_include in includes
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def test_circular_includes_detection():
|
|
245
|
+
"""Test that circular includes are detected and don't cause infinite recursion."""
|
|
246
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
247
|
+
# Create two files that include each other
|
|
248
|
+
file1 = os.path.join(tmpdir, "file1.rst")
|
|
249
|
+
file2 = os.path.join(tmpdir, "file2.rst")
|
|
250
|
+
|
|
251
|
+
with open(file1, "w") as f:
|
|
252
|
+
f.write("File 1\n\n.. include:: file2.rst\n")
|
|
253
|
+
|
|
254
|
+
with open(file2, "w") as f:
|
|
255
|
+
f.write("File 2\n\n.. include:: file1.rst\n")
|
|
256
|
+
|
|
257
|
+
# Should find both includes but avoid infinite recursion
|
|
258
|
+
includes = get_transitive_includes(file1)
|
|
259
|
+
assert len(includes) == 2 # Changed from 1 to 2
|
|
260
|
+
assert file2 in includes
|
|
261
|
+
assert file1 in includes # file1 is included by file2
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def test_remove_empty_dirs_function():
|
|
265
|
+
"""Test the remove_empty_dirs function directly."""
|
|
266
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
267
|
+
# Create a nested directory structure
|
|
268
|
+
parent_dir = os.path.join(tmpdir, "parent")
|
|
269
|
+
child_dir = os.path.join(parent_dir, "child")
|
|
270
|
+
grandchild_dir = os.path.join(child_dir, "grandchild")
|
|
271
|
+
os.makedirs(grandchild_dir)
|
|
272
|
+
|
|
273
|
+
# Create a separate directory that should not be removed
|
|
274
|
+
other_dir = os.path.join(parent_dir, "other")
|
|
275
|
+
os.makedirs(other_dir)
|
|
276
|
+
with open(os.path.join(other_dir, "file.txt"), "w") as f:
|
|
277
|
+
f.write("Keep this directory")
|
|
278
|
+
|
|
279
|
+
# Test dry run
|
|
280
|
+
affected_dirs = {child_dir, grandchild_dir}
|
|
281
|
+
deleted_dirs = remove_empty_dirs(affected_dirs, parent_dir, dry_run=True)
|
|
282
|
+
|
|
283
|
+
# Verify nothing was deleted in dry run
|
|
284
|
+
assert os.path.exists(grandchild_dir)
|
|
285
|
+
assert os.path.exists(child_dir)
|
|
286
|
+
assert os.path.exists(parent_dir)
|
|
287
|
+
assert len(deleted_dirs) == 0
|
|
288
|
+
|
|
289
|
+
# Test actual removal
|
|
290
|
+
deleted_dirs = remove_empty_dirs(affected_dirs, parent_dir, dry_run=False)
|
|
291
|
+
|
|
292
|
+
# Verify directories were removed
|
|
293
|
+
assert not os.path.exists(grandchild_dir)
|
|
294
|
+
assert not os.path.exists(child_dir)
|
|
295
|
+
assert os.path.exists(parent_dir) # Parent has 'other' dir so is not empty
|
|
296
|
+
assert os.path.exists(other_dir)
|
|
297
|
+
|
|
298
|
+
# Should have removed exactly 2 dirs
|
|
299
|
+
assert len(deleted_dirs) == 2
|
|
300
|
+
assert grandchild_dir in deleted_dirs
|
|
301
|
+
assert child_dir in deleted_dirs
|
|
302
|
+
|
|
303
|
+
# Now test removal of the original path when empty
|
|
304
|
+
# First remove the file in other_dir
|
|
305
|
+
os.remove(os.path.join(other_dir, "file.txt"))
|
|
306
|
+
|
|
307
|
+
# Then remove the now-empty other_dir
|
|
308
|
+
affected_dirs = {other_dir}
|
|
309
|
+
deleted_dirs = remove_empty_dirs(affected_dirs, parent_dir, dry_run=False)
|
|
310
|
+
|
|
311
|
+
# Now parent_dir should also be removed since it's empty
|
|
312
|
+
assert not os.path.exists(other_dir)
|
|
313
|
+
assert not os.path.exists(parent_dir)
|
|
314
|
+
assert len(deleted_dirs) == 2
|
|
315
|
+
assert other_dir in deleted_dirs
|
|
316
|
+
assert parent_dir in deleted_dirs
|
|
@@ -1,104 +0,0 @@
|
|
|
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}")
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import os
|
|
2
|
-
import tempfile
|
|
3
|
-
from unittest.mock import Mock
|
|
4
|
-
|
|
5
|
-
from sphinx_cmd.commands.rm import (
|
|
6
|
-
build_asset_index,
|
|
7
|
-
execute,
|
|
8
|
-
extract_assets,
|
|
9
|
-
find_rst_files,
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def test_rm_command_functionality():
|
|
14
|
-
"""Test the rm command functionality."""
|
|
15
|
-
# Create a temporary directory structure
|
|
16
|
-
with tempfile.TemporaryDirectory() as tmpdir:
|
|
17
|
-
# Create test files
|
|
18
|
-
test_dir = os.path.join(tmpdir, "docs")
|
|
19
|
-
os.makedirs(test_dir)
|
|
20
|
-
|
|
21
|
-
# Create an RST file
|
|
22
|
-
rst_content = """
|
|
23
|
-
Test Page
|
|
24
|
-
=========
|
|
25
|
-
|
|
26
|
-
.. image:: image.png
|
|
27
|
-
.. figure:: figure.jpg
|
|
28
|
-
"""
|
|
29
|
-
with open(os.path.join(test_dir, "test.rst"), "w") as f:
|
|
30
|
-
f.write(rst_content)
|
|
31
|
-
|
|
32
|
-
# Create referenced asset files
|
|
33
|
-
with open(os.path.join(test_dir, "image.png"), "w") as f:
|
|
34
|
-
f.write("fake image")
|
|
35
|
-
with open(os.path.join(test_dir, "figure.jpg"), "w") as f:
|
|
36
|
-
f.write("fake figure")
|
|
37
|
-
|
|
38
|
-
# Test finding RST files
|
|
39
|
-
rst_files = find_rst_files(test_dir)
|
|
40
|
-
assert len(rst_files) == 1
|
|
41
|
-
assert "test.rst" in rst_files[0]
|
|
42
|
-
|
|
43
|
-
# Test extracting assets
|
|
44
|
-
assets = extract_assets(rst_files[0])
|
|
45
|
-
assert len(assets) == 2
|
|
46
|
-
|
|
47
|
-
# Test building asset index
|
|
48
|
-
asset_to_files, file_to_assets, asset_directive_map = build_asset_index(
|
|
49
|
-
rst_files
|
|
50
|
-
)
|
|
51
|
-
assert len(asset_to_files) == 2
|
|
52
|
-
assert len(file_to_assets) == 1
|
|
53
|
-
|
|
54
|
-
# Test dry run with mock args
|
|
55
|
-
args = Mock()
|
|
56
|
-
args.path = test_dir
|
|
57
|
-
args.dry_run = True
|
|
58
|
-
execute(args)
|
|
59
|
-
|
|
60
|
-
# Verify files still exist after dry run
|
|
61
|
-
assert os.path.exists(os.path.join(test_dir, "test.rst"))
|
|
62
|
-
assert os.path.exists(os.path.join(test_dir, "image.png"))
|
|
63
|
-
assert os.path.exists(os.path.join(test_dir, "figure.jpg"))
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|