Skip to content

Commit 2913033

Browse files
LeoQuotecooperlees
andauthored
make simple root page update optional (#1145)
* make simple root page update optional * add changelog * add skip index tests * Apply suggestions from code review Co-authored-by: Cooper Lees <me@cooperlees.com> * Update CHANGES.md * Update main.py Co-authored-by: Cooper Lees <me@cooperlees.com>
1 parent 25a7c2d commit 2913033

4 files changed

Lines changed: 46 additions & 8 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Add PEP 629 header to Simple API HTML `PR #1122`
66
- Added support to exclude specific Python minor versions `PR #1110` - Thanks **davidkim83**
77
- Add S3 Docker Image building `PR #1092`
8+
- Add `--skip-simple-root` option for `bandersnatch sync` command `PR #1145`
89

910
## Other
1011

src/bandersnatch/main.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ def _sync_parser(subparsers: argparse._SubParsersAction) -> None:
113113
help="The name of package to sync",
114114
)
115115
m.set_defaults(op="sync")
116+
m.add_argument(
117+
"--skip-simple-root",
118+
action="store_true",
119+
default=False,
120+
help="Skip updating simple index root page",
121+
)
116122

117123

118124
def _make_parser() -> argparse.ArgumentParser:
@@ -156,7 +162,9 @@ async def async_main(args: argparse.Namespace, config: ConfigParser) -> int:
156162
elif args.op.lower() == "verify":
157163
return await bandersnatch.verify.metadata_verify(config, args)
158164
elif args.op.lower() == "sync":
159-
return await bandersnatch.mirror.mirror(config, args.packages)
165+
return await bandersnatch.mirror.mirror(
166+
config, args.packages, not args.skip_simple_root
167+
)
160168

161169
if args.force_check:
162170
storage_plugin = next(iter(storage_backend_plugins()))

src/bandersnatch/mirror.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ def __init__(self, master: Master, workers: int = 3):
5454
self.altered_packages: Dict[str, Set[str]] = {}
5555

5656
async def synchronize(
57-
self, specific_packages: Optional[List[str]] = None
57+
self,
58+
specific_packages: Optional[List[str]] = None,
59+
sync_simple_index: bool = True,
5860
) -> Dict[str, Set[str]]:
5961
logger.info(f"Syncing with {self.master.url}.")
6062
self.now = datetime.datetime.utcnow()
@@ -84,7 +86,7 @@ async def synchronize(
8486
)
8587

8688
await self.sync_packages()
87-
self.finalize_sync()
89+
self.finalize_sync(sync_index_page=sync_simple_index)
8890
return self.altered_packages
8991

9092
def _filter_packages(self) -> None:
@@ -164,7 +166,7 @@ async def sync_packages(self) -> None:
164166
# TODO Remove this check by following packages_to_sync's typing
165167
self.on_error(e)
166168

167-
def finalize_sync(self) -> None:
169+
def finalize_sync(self, sync_index_page: bool = True) -> None:
168170
raise NotImplementedError()
169171

170172
def on_error(self, exception: BaseException, **kwargs: Dict) -> None:
@@ -341,8 +343,9 @@ async def process_package(self, package: Package) -> None:
341343
# Cleanup old legacy non PEP 503 Directories created for the Simple API
342344
await self.cleanup_non_pep_503_paths(package)
343345

344-
def finalize_sync(self) -> None:
345-
self.sync_index_page()
346+
def finalize_sync(self, sync_index_page: bool = True) -> None:
347+
if sync_index_page:
348+
self.sync_index_page()
346349
if self.need_wrapup:
347350
self.wrapup_successful_sync()
348351
return None
@@ -979,7 +982,9 @@ async def download_file(
979982

980983

981984
async def mirror(
982-
config: configparser.ConfigParser, specific_packages: Optional[List[str]] = None
985+
config: configparser.ConfigParser,
986+
specific_packages: Optional[List[str]] = None,
987+
sync_simple_index: bool = True,
983988
) -> int:
984989
config_values = validate_config_values(config)
985990

@@ -1046,7 +1051,9 @@ async def mirror(
10461051
download_mirror=config_values.download_mirror,
10471052
download_mirror_no_fallback=config_values.download_mirror_no_fallback,
10481053
)
1049-
changed_packages = await mirror.synchronize(specific_packages)
1054+
changed_packages = await mirror.synchronize(
1055+
specific_packages, sync_simple_index=sync_simple_index
1056+
)
10501057

10511058
logger.info(f"{len(changed_packages)} packages had changes")
10521059
for package_name, changes in changed_packages.items():

src/bandersnatch/tests/test_mirror.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,28 @@ async def test_mirror_empty_resume_from_todo_list(mirror: BandersnatchMirror) ->
297297
assert open("status").read() == "20"
298298

299299

300+
@pytest.mark.asyncio
301+
async def test_mirror_sync_package_skip_index(mirror: BandersnatchMirror) -> None:
302+
mirror.master.all_packages = mock.AsyncMock(return_value={"foo": 1}) # type: ignore
303+
mirror.json_save = True
304+
# Recall bootstrap so we have the json dirs
305+
mirror._bootstrap()
306+
await mirror.synchronize(sync_simple_index=False)
307+
308+
assert """\
309+
json{0}foo
310+
last-modified
311+
packages{0}2.7{0}f{0}foo{0}foo.whl
312+
packages{0}any{0}f{0}foo{0}foo.zip
313+
pypi{0}foo{0}json
314+
simple{0}foo{0}index.html""".format(
315+
sep
316+
) == utils.find(
317+
mirror.webdir, dirs=False
318+
)
319+
assert open("status", "rb").read() == b"1"
320+
321+
300322
@pytest.mark.asyncio
301323
async def test_mirror_sync_package(mirror: BandersnatchMirror) -> None:
302324
mirror.master.all_packages = mock.AsyncMock(return_value={"foo": 1}) # type: ignore

0 commit comments

Comments
 (0)