2323:::
2424"""
2525import collections .abc
26- from collections import defaultdict
2726import inspect
2827import os
2928import posixpath
3029import sys
30+ from collections import defaultdict
3131from typing import Dict , Optional , Tuple , Union
3232
3333
3434class _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
0 commit comments