Skip to content

Commit f0558a1

Browse files
committed
Run precommit
1 parent ca06786 commit f0558a1

File tree

2 files changed

+53
-32
lines changed

2 files changed

+53
-32
lines changed

python/runfiles/runfiles.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,47 +23,52 @@
2323
:::
2424
"""
2525
import collections.abc
26-
from collections import defaultdict
2726
import inspect
2827
import os
2928
import posixpath
3029
import sys
30+
from collections import defaultdict
3131
from typing import Dict, Optional, Tuple, Union
3232

3333

3434
class _RepositoryMapping:
3535
"""Repository mapping for resolving apparent repository names to canonical ones.
36-
37-
Handles both exact mappings and prefix-based mappings introduced by the
36+
37+
Handles both exact mappings and prefix-based mappings introduced by the
3838
--incompatible_compact_repo_mapping_manifest flag.
3939
"""
4040

4141
def __init__(
42-
self,
43-
exact_mappings: Dict[Tuple[str, str], str],
44-
prefixed_mappings: Dict[Tuple[str, str], str]
42+
self,
43+
exact_mappings: Dict[Tuple[str, str], str],
44+
prefixed_mappings: Dict[Tuple[str, str], str],
4545
) -> None:
4646
"""Initialize repository mapping with exact and prefixed mappings.
47-
47+
4848
Args:
4949
exact_mappings: Dict mapping (source_canonical, target_apparent) -> target_canonical
5050
prefixed_mappings: Dict mapping (source_prefix, target_apparent) -> target_canonical
5151
"""
5252
self._exact_mappings = exact_mappings
5353
self._prefixed_mappings = prefixed_mappings
54-
54+
5555
# Group prefixed mappings by target_apparent for faster lookups
5656
self._grouped_prefixed_mappings = defaultdict(list)
57-
for (prefix_source, target_app), target_canonical in self._prefixed_mappings.items():
58-
self._grouped_prefixed_mappings[target_app].append((prefix_source, target_canonical))
57+
for (
58+
prefix_source,
59+
target_app,
60+
), target_canonical in self._prefixed_mappings.items():
61+
self._grouped_prefixed_mappings[target_app].append(
62+
(prefix_source, target_canonical)
63+
)
5964

6065
@staticmethod
6166
def create_from_file(repo_mapping_path: Optional[str]) -> "_RepositoryMapping":
6267
"""Create RepositoryMapping from a repository mapping manifest file.
63-
68+
6469
Args:
6570
repo_mapping_path: Path to the repository mapping file, or None if not available
66-
71+
6772
Returns:
6873
RepositoryMapping instance with parsed mappings
6974
"""
@@ -72,7 +77,7 @@ def create_from_file(repo_mapping_path: Optional[str]) -> "_RepositoryMapping":
7277
# In this case, just apply empty repo mappings.
7378
if not repo_mapping_path:
7479
return _RepositoryMapping({}, {})
75-
80+
7681
try:
7782
with open(repo_mapping_path, "r", encoding="utf-8", newline="\n") as f:
7883
content = f.read()
@@ -95,27 +100,29 @@ def create_from_file(repo_mapping_path: Optional[str]) -> "_RepositoryMapping":
95100

96101
def lookup(self, source_repo: str, target_apparent: str) -> Optional[str]:
97102
"""Look up repository mapping for the given source and target.
98-
99-
This handles both exact mappings and prefix-based mappings introduced by the
103+
104+
This handles both exact mappings and prefix-based mappings introduced by the
100105
--incompatible_compact_repo_mapping_manifest flag. Exact mappings are tried
101106
first, followed by prefix-based mappings where order matters.
102-
107+
103108
Args:
104109
source_repo: Source canonical repository name
105110
target_apparent: Target apparent repository name
106-
111+
107112
Returns:
108113
target_canonical repository name, or None if no mapping exists
109114
"""
110115
key = (source_repo, target_apparent)
111-
116+
112117
# Try exact mapping first
113118
if key in self._exact_mappings:
114119
return self._exact_mappings[key]
115120

116121
# Try prefixed mapping if no exact match found
117122
if target_apparent in self._grouped_prefixed_mappings:
118-
for prefix_source, target_canonical in self._grouped_prefixed_mappings[target_apparent]:
123+
for prefix_source, target_canonical in self._grouped_prefixed_mappings[
124+
target_apparent
125+
]:
119126
if source_repo.startswith(prefix_source):
120127
return target_canonical
121128

@@ -297,7 +304,9 @@ def Rlocation(self, path: str, source_repo: Optional[str] = None) -> Optional[st
297304
if source_repo is not None:
298305
target_canonical = self._repo_mapping.lookup(source_repo, target_repo)
299306
if target_canonical is not None:
300-
return self._strategy.RlocationChecked(target_canonical + "/" + remainder)
307+
return self._strategy.RlocationChecked(
308+
target_canonical + "/" + remainder
309+
)
301310

302311
# No mapping found - assume target_repo is already canonical or
303312
# we're not using Bzlmod

tests/runfiles/runfiles_test.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,10 @@ def testDirectoryBasedRlocationWithCompactRepoMappingOrderMatters(self) -> None:
638638

639639
def testRepositoryMappingLookup(self) -> None:
640640
"""Test _RepositoryMapping.lookup() method for both exact and prefix-based mappings."""
641-
from python.runfiles.runfiles import _RepositoryMapping # buildifier: disable=bzl-visibility
642-
641+
from python.runfiles.runfiles import (
642+
_RepositoryMapping, # buildifier: disable=bzl-visibility
643+
)
644+
643645
exact_mappings = {
644646
("", "my_workspace"): "_main",
645647
("", "config_lib"): "config_lib~1.0.0",
@@ -649,25 +651,35 @@ def testRepositoryMappingLookup(self) -> None:
649651
("deps+", "external_dep"): "external_dep~prefix",
650652
("test_deps+", "test_lib"): "test_lib~2.1.0",
651653
}
652-
654+
653655
repo_mapping = _RepositoryMapping(exact_mappings, prefixed_mappings)
654-
656+
655657
# Test exact lookups
656658
self.assertEqual(repo_mapping.lookup("", "my_workspace"), "_main")
657659
self.assertEqual(repo_mapping.lookup("", "config_lib"), "config_lib~1.0.0")
658-
self.assertEqual(repo_mapping.lookup("deps+specific_repo", "external_dep"), "external_dep~exact")
659-
660+
self.assertEqual(
661+
repo_mapping.lookup("deps+specific_repo", "external_dep"),
662+
"external_dep~exact",
663+
)
664+
660665
# Test prefix-based lookups
661-
self.assertEqual(repo_mapping.lookup("deps+some_repo", "external_dep"), "external_dep~prefix")
662-
self.assertEqual(repo_mapping.lookup("test_deps+another_repo", "test_lib"), "test_lib~2.1.0")
663-
666+
self.assertEqual(
667+
repo_mapping.lookup("deps+some_repo", "external_dep"), "external_dep~prefix"
668+
)
669+
self.assertEqual(
670+
repo_mapping.lookup("test_deps+another_repo", "test_lib"), "test_lib~2.1.0"
671+
)
672+
664673
# Test that exact takes precedence over prefix
665-
self.assertEqual(repo_mapping.lookup("deps+specific_repo", "external_dep"), "external_dep~exact")
666-
674+
self.assertEqual(
675+
repo_mapping.lookup("deps+specific_repo", "external_dep"),
676+
"external_dep~exact",
677+
)
678+
667679
# Test non-existent mapping
668680
self.assertIsNone(repo_mapping.lookup("nonexistent", "repo"))
669681
self.assertIsNone(repo_mapping.lookup("unknown+repo", "missing"))
670-
682+
671683
# Test empty mapping
672684
empty_mapping = _RepositoryMapping({}, {})
673685
self.assertIsNone(empty_mapping.lookup("any", "repo"))

0 commit comments

Comments
 (0)