sphinx-cmd 0.2.1__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.
Files changed (22) hide show
  1. {sphinx_cmd-0.2.1/sphinx_cmd.egg-info → sphinx_cmd-0.2.2}/PKG-INFO +2 -2
  2. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/README.md +1 -1
  3. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/sphinx_cmd/__init__.py +1 -1
  4. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/sphinx_cmd/commands/rm.py +123 -18
  5. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2/sphinx_cmd.egg-info}/PKG-INFO +2 -2
  6. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/tests/test_rm.py +109 -0
  7. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/LICENSE +0 -0
  8. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/MANIFEST.in +0 -0
  9. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/pyproject.toml +0 -0
  10. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/setup.cfg +0 -0
  11. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/sphinx_cmd/cli.py +0 -0
  12. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/sphinx_cmd/commands/__init__.py +0 -0
  13. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/sphinx_cmd/commands/mv.py +0 -0
  14. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/sphinx_cmd/py.typed +0 -0
  15. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/sphinx_cmd.egg-info/SOURCES.txt +0 -0
  16. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/sphinx_cmd.egg-info/dependency_links.txt +0 -0
  17. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/sphinx_cmd.egg-info/entry_points.txt +0 -0
  18. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/sphinx_cmd.egg-info/requires.txt +0 -0
  19. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/sphinx_cmd.egg-info/top_level.txt +0 -0
  20. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/tests/test_cli.py +0 -0
  21. {sphinx_cmd-0.2.1 → sphinx_cmd-0.2.2}/tests/test_integration.py +0 -0
  22. {sphinx_cmd-0.2.1 → 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.1
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
@@ -1,4 +1,4 @@
1
1
  # This file makes sphinx_cmd a Python package
2
2
  """Sphinx command-line utilities."""
3
3
 
4
- __version__ = "0.2.1"
4
+ __version__ = "0.2.2"
@@ -27,18 +27,44 @@ def find_rst_files(path):
27
27
  return rst_files
28
28
 
29
29
 
30
- def extract_assets(file_path):
31
- """Extract asset references from an .rst file."""
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
+
32
41
  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
+
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
+
42
68
  return asset_directives
43
69
 
44
70
 
@@ -57,6 +83,42 @@ def build_asset_index(rst_files):
57
83
  return asset_to_files, file_to_assets, asset_directive_map
58
84
 
59
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
+
60
122
  def delete_unused_assets_and_pages(
61
123
  asset_to_files, file_to_assets, asset_directive_map, dry_run=False
62
124
  ):
@@ -65,18 +127,61 @@ def delete_unused_assets_and_pages(
65
127
  deleted_assets = []
66
128
  affected_dirs = set()
67
129
 
130
+ # Track which files have been processed to avoid duplicates
131
+ processed_files = set()
132
+
68
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
+
69
138
  unused_assets = [a for a in assets if len(asset_to_files[a]) == 1]
70
139
  if len(unused_assets) == len(assets): # All assets are unique to this file
71
- for asset in unused_assets:
72
- directive = asset_directive_map.get(asset, "asset")
73
- if os.path.exists(asset):
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):
74
175
  if dry_run:
75
- print(f"[dry-run] Would delete {directive}: {asset}")
176
+ print(
177
+ f"[dry-run] Would delete included file: {file_to_process}"
178
+ )
76
179
  else:
77
- affected_dirs.add(os.path.dirname(asset))
78
- os.remove(asset)
79
- deleted_assets.append(asset)
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
80
185
  if os.path.exists(rst_file):
81
186
  if dry_run:
82
187
  print(f"[dry-run] Would delete page: {rst_file}")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sphinx-cmd
3
- Version: 0.2.1
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
@@ -7,10 +7,38 @@ from sphinx_cmd.commands.rm import (
7
7
  execute,
8
8
  extract_assets,
9
9
  find_rst_files,
10
+ get_transitive_includes,
10
11
  remove_empty_dirs,
11
12
  )
12
13
 
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
+
14
42
  def test_rm_command_functionality():
15
43
  """Test the rm command functionality."""
16
44
  # Create a temporary directory structure
@@ -152,6 +180,87 @@ def test_non_empty_directory_retained():
152
180
  assert os.path.exists(other_file)
153
181
 
154
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
+
155
264
  def test_remove_empty_dirs_function():
156
265
  """Test the remove_empty_dirs function directly."""
157
266
  with tempfile.TemporaryDirectory() as tmpdir:
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes