sphinx-cmd 0.2.0__tar.gz → 0.2.1__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 (24) hide show
  1. {sphinx_cmd-0.2.0/sphinx_cmd.egg-info → sphinx_cmd-0.2.1}/PKG-INFO +1 -1
  2. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/pyproject.toml +4 -1
  3. sphinx_cmd-0.2.1/sphinx_cmd/__init__.py +4 -0
  4. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/sphinx_cmd/cli.py +5 -1
  5. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/sphinx_cmd/commands/rm.py +56 -2
  6. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1/sphinx_cmd.egg-info}/PKG-INFO +1 -1
  7. sphinx_cmd-0.2.1/tests/test_rm.py +207 -0
  8. sphinx_cmd-0.2.0/sphinx_cmd/__init__.py +0 -2
  9. sphinx_cmd-0.2.0/tests/test_rm.py +0 -63
  10. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/LICENSE +0 -0
  11. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/MANIFEST.in +0 -0
  12. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/README.md +0 -0
  13. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/setup.cfg +0 -0
  14. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/sphinx_cmd/commands/__init__.py +0 -0
  15. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/sphinx_cmd/commands/mv.py +0 -0
  16. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/sphinx_cmd/py.typed +0 -0
  17. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/sphinx_cmd.egg-info/SOURCES.txt +0 -0
  18. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/sphinx_cmd.egg-info/dependency_links.txt +0 -0
  19. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/sphinx_cmd.egg-info/entry_points.txt +0 -0
  20. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/sphinx_cmd.egg-info/requires.txt +0 -0
  21. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/sphinx_cmd.egg-info/top_level.txt +0 -0
  22. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/tests/test_cli.py +0 -0
  23. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/tests/test_integration.py +0 -0
  24. {sphinx_cmd-0.2.0 → sphinx_cmd-0.2.1}/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.0
3
+ Version: 0.2.1
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
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "sphinx-cmd"
7
- version = "0.2.0"
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']
@@ -0,0 +1,4 @@
1
+ # This file makes sphinx_cmd a Python package
2
+ """Sphinx command-line utilities."""
3
+
4
+ __version__ = "0.2.1"
@@ -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("--version", action="version", version="%(prog)s 0.1.0")
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(
@@ -63,6 +63,7 @@ def delete_unused_assets_and_pages(
63
63
  """Delete files and their unique assets if not used elsewhere."""
64
64
  deleted_pages = []
65
65
  deleted_assets = []
66
+ affected_dirs = set()
66
67
 
67
68
  for rst_file, assets in file_to_assets.items():
68
69
  unused_assets = [a for a in assets if len(asset_to_files[a]) == 1]
@@ -73,26 +74,74 @@ def delete_unused_assets_and_pages(
73
74
  if dry_run:
74
75
  print(f"[dry-run] Would delete {directive}: {asset}")
75
76
  else:
77
+ affected_dirs.add(os.path.dirname(asset))
76
78
  os.remove(asset)
77
79
  deleted_assets.append(asset)
78
80
  if os.path.exists(rst_file):
79
81
  if dry_run:
80
82
  print(f"[dry-run] Would delete page: {rst_file}")
81
83
  else:
84
+ affected_dirs.add(os.path.dirname(rst_file))
82
85
  os.remove(rst_file)
83
86
  deleted_pages.append(rst_file)
84
87
 
85
- return deleted_pages, deleted_assets
88
+ return deleted_pages, deleted_assets, affected_dirs
89
+
90
+
91
+ def remove_empty_dirs(dirs, original_path, dry_run=False):
92
+ """Remove empty directories, bottom-up."""
93
+ deleted_dirs = []
94
+
95
+ # Add parent directories to the affected dirs set
96
+ all_dirs = set(dirs)
97
+ for dir_path in dirs:
98
+ # Add all parent directories up to but not including the original path
99
+ parent = os.path.dirname(dir_path)
100
+ while parent and os.path.exists(parent) and parent != original_path:
101
+ all_dirs.add(parent)
102
+ parent = os.path.dirname(parent)
103
+
104
+ # Sort by path depth (deepest first)
105
+ sorted_dirs = sorted(all_dirs, key=lambda d: d.count(os.sep), reverse=True)
106
+
107
+ # Process directories from deepest to shallowest
108
+ for dir_path in sorted_dirs:
109
+ if not os.path.exists(dir_path) or not os.path.isdir(dir_path):
110
+ continue
111
+
112
+ # Check if directory is empty
113
+ if not os.listdir(dir_path):
114
+ if dry_run:
115
+ print(f"[dry-run] Would delete empty directory: {dir_path}")
116
+ else:
117
+ os.rmdir(dir_path)
118
+ deleted_dirs.append(dir_path)
119
+
120
+ # Check if the original path (if it's a directory) is now empty and should
121
+ # be removed
122
+ if os.path.isdir(original_path) and not os.listdir(original_path):
123
+ if dry_run:
124
+ print(f"[dry-run] Would delete empty directory: {original_path}")
125
+ else:
126
+ os.rmdir(original_path)
127
+ deleted_dirs.append(original_path)
128
+
129
+ return deleted_dirs
86
130
 
87
131
 
88
132
  def execute(args):
89
133
  """Execute the rm command."""
134
+ original_path = os.path.abspath(args.path)
90
135
  rst_files = find_rst_files(args.path)
91
136
  asset_to_files, file_to_assets, asset_directive_map = build_asset_index(rst_files)
92
- deleted_pages, deleted_assets = delete_unused_assets_and_pages(
137
+ deleted_pages, deleted_assets, affected_dirs = delete_unused_assets_and_pages(
93
138
  asset_to_files, file_to_assets, asset_directive_map, args.dry_run
94
139
  )
95
140
 
141
+ deleted_dirs = []
142
+ if affected_dirs:
143
+ deleted_dirs = remove_empty_dirs(affected_dirs, original_path, args.dry_run)
144
+
96
145
  if not args.dry_run:
97
146
  print(f"\nDeleted {len(deleted_assets)} unused asset(s):")
98
147
  for a in deleted_assets:
@@ -102,3 +151,8 @@ def execute(args):
102
151
  print(f"\nDeleted {len(deleted_pages)} RST page(s):")
103
152
  for p in deleted_pages:
104
153
  print(f" - {p}")
154
+
155
+ if deleted_dirs:
156
+ print(f"\nDeleted {len(deleted_dirs)} empty directory/directories:")
157
+ for d in deleted_dirs:
158
+ print(f" - {d}")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sphinx-cmd
3
- Version: 0.2.0
3
+ Version: 0.2.1
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
@@ -0,0 +1,207 @@
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
+ remove_empty_dirs,
11
+ )
12
+
13
+
14
+ def test_rm_command_functionality():
15
+ """Test the rm command functionality."""
16
+ # Create a temporary directory structure
17
+ with tempfile.TemporaryDirectory() as tmpdir:
18
+ # Create test files
19
+ test_dir = os.path.join(tmpdir, "docs")
20
+ os.makedirs(test_dir)
21
+
22
+ # Create an RST file
23
+ rst_content = """
24
+ Test Page
25
+ =========
26
+
27
+ .. image:: image.png
28
+ .. figure:: figure.jpg
29
+ """
30
+ with open(os.path.join(test_dir, "test.rst"), "w") as f:
31
+ f.write(rst_content)
32
+
33
+ # Create referenced asset files
34
+ with open(os.path.join(test_dir, "image.png"), "w") as f:
35
+ f.write("fake image")
36
+ with open(os.path.join(test_dir, "figure.jpg"), "w") as f:
37
+ f.write("fake figure")
38
+
39
+ # Test finding RST files
40
+ rst_files = find_rst_files(test_dir)
41
+ assert len(rst_files) == 1
42
+ assert "test.rst" in rst_files[0]
43
+
44
+ # Test extracting assets
45
+ assets = extract_assets(rst_files[0])
46
+ assert len(assets) == 2
47
+
48
+ # Test building asset index
49
+ asset_to_files, file_to_assets, asset_directive_map = build_asset_index(
50
+ rst_files
51
+ )
52
+ assert len(asset_to_files) == 2
53
+ assert len(file_to_assets) == 1
54
+
55
+ # Test dry run with mock args
56
+ args = Mock()
57
+ args.path = test_dir
58
+ args.dry_run = True
59
+ execute(args)
60
+
61
+ # Verify files still exist after dry run
62
+ assert os.path.exists(os.path.join(test_dir, "test.rst"))
63
+ assert os.path.exists(os.path.join(test_dir, "image.png"))
64
+ assert os.path.exists(os.path.join(test_dir, "figure.jpg"))
65
+
66
+
67
+ def test_empty_directory_removal():
68
+ """Test that empty directories are properly removed."""
69
+ with tempfile.TemporaryDirectory() as tmpdir:
70
+ # Create a nested directory structure
71
+ docs_dir = os.path.join(tmpdir, "docs")
72
+ nested_dir = os.path.join(docs_dir, "nested")
73
+ nested_subdir = os.path.join(nested_dir, "subdir")
74
+ os.makedirs(nested_subdir)
75
+
76
+ # Create an RST file in the nested subdirectory
77
+ rst_path = os.path.join(nested_subdir, "test.rst")
78
+ rst_content = """Test page
79
+ =========
80
+
81
+ .. image:: image.png
82
+ """
83
+ with open(rst_path, "w") as f:
84
+ f.write(rst_content)
85
+
86
+ # Create an image in the same directory
87
+ img_path = os.path.join(nested_subdir, "image.png")
88
+ with open(img_path, "w") as f:
89
+ f.write("fake image")
90
+
91
+ # Verify that directories exist before the test
92
+ assert os.path.exists(docs_dir)
93
+ assert os.path.exists(nested_dir)
94
+ assert os.path.exists(nested_subdir)
95
+ assert os.path.exists(rst_path)
96
+ assert os.path.exists(img_path)
97
+
98
+ # Execute the rm command
99
+ args = Mock()
100
+ args.path = docs_dir
101
+ args.dry_run = False
102
+
103
+ # Execute the command to remove files
104
+ execute(args)
105
+
106
+ # Verify that the files were removed
107
+ assert not os.path.exists(rst_path)
108
+ assert not os.path.exists(img_path)
109
+
110
+ # Verify that all directories were removed
111
+ assert not os.path.exists(nested_subdir)
112
+ assert not os.path.exists(nested_dir)
113
+ assert not os.path.exists(docs_dir)
114
+
115
+
116
+ def test_non_empty_directory_retained():
117
+ """Test that non-empty directories are not removed."""
118
+ with tempfile.TemporaryDirectory() as tmpdir:
119
+ # Create a nested directory structure
120
+ docs_dir = os.path.join(tmpdir, "docs")
121
+ nested_dir = os.path.join(docs_dir, "nested")
122
+ nested_subdir = os.path.join(nested_dir, "subdir")
123
+ os.makedirs(nested_subdir)
124
+
125
+ # Create an RST file in the nested subdirectory
126
+ rst_path = os.path.join(nested_subdir, "test.rst")
127
+ with open(rst_path, "w") as f:
128
+ f.write("Test page\n=========\n\n.. image:: image.png")
129
+
130
+ # Create an image in the same directory
131
+ img_path = os.path.join(nested_subdir, "image.png")
132
+ with open(img_path, "w") as f:
133
+ f.write("fake image")
134
+
135
+ # Create another file in the parent directory that should be retained
136
+ other_file = os.path.join(nested_dir, "other.txt")
137
+ with open(other_file, "w") as f:
138
+ f.write("This file should be retained")
139
+
140
+ # Execute the rm command
141
+ args = Mock()
142
+ args.path = docs_dir
143
+ args.dry_run = False
144
+
145
+ # Execute the command to remove files
146
+ execute(args)
147
+
148
+ # Verify that subdir was removed but nested_dir was retained
149
+ assert not os.path.exists(nested_subdir)
150
+ assert os.path.exists(nested_dir)
151
+ assert os.path.exists(docs_dir)
152
+ assert os.path.exists(other_file)
153
+
154
+
155
+ def test_remove_empty_dirs_function():
156
+ """Test the remove_empty_dirs function directly."""
157
+ with tempfile.TemporaryDirectory() as tmpdir:
158
+ # Create a nested directory structure
159
+ parent_dir = os.path.join(tmpdir, "parent")
160
+ child_dir = os.path.join(parent_dir, "child")
161
+ grandchild_dir = os.path.join(child_dir, "grandchild")
162
+ os.makedirs(grandchild_dir)
163
+
164
+ # Create a separate directory that should not be removed
165
+ other_dir = os.path.join(parent_dir, "other")
166
+ os.makedirs(other_dir)
167
+ with open(os.path.join(other_dir, "file.txt"), "w") as f:
168
+ f.write("Keep this directory")
169
+
170
+ # Test dry run
171
+ affected_dirs = {child_dir, grandchild_dir}
172
+ deleted_dirs = remove_empty_dirs(affected_dirs, parent_dir, dry_run=True)
173
+
174
+ # Verify nothing was deleted in dry run
175
+ assert os.path.exists(grandchild_dir)
176
+ assert os.path.exists(child_dir)
177
+ assert os.path.exists(parent_dir)
178
+ assert len(deleted_dirs) == 0
179
+
180
+ # Test actual removal
181
+ deleted_dirs = remove_empty_dirs(affected_dirs, parent_dir, dry_run=False)
182
+
183
+ # Verify directories were removed
184
+ assert not os.path.exists(grandchild_dir)
185
+ assert not os.path.exists(child_dir)
186
+ assert os.path.exists(parent_dir) # Parent has 'other' dir so is not empty
187
+ assert os.path.exists(other_dir)
188
+
189
+ # Should have removed exactly 2 dirs
190
+ assert len(deleted_dirs) == 2
191
+ assert grandchild_dir in deleted_dirs
192
+ assert child_dir in deleted_dirs
193
+
194
+ # Now test removal of the original path when empty
195
+ # First remove the file in other_dir
196
+ os.remove(os.path.join(other_dir, "file.txt"))
197
+
198
+ # Then remove the now-empty other_dir
199
+ affected_dirs = {other_dir}
200
+ deleted_dirs = remove_empty_dirs(affected_dirs, parent_dir, dry_run=False)
201
+
202
+ # Now parent_dir should also be removed since it's empty
203
+ assert not os.path.exists(other_dir)
204
+ assert not os.path.exists(parent_dir)
205
+ assert len(deleted_dirs) == 2
206
+ assert other_dir in deleted_dirs
207
+ assert parent_dir in deleted_dirs
@@ -1,2 +0,0 @@
1
- # This file is intentionally empty
2
- # It makes sphinx_cmd a Python package
@@ -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