Skip to content

Commit 0c8566f

Browse files
authored
Make some stuff in mypy.build take Options instead of Manager (#7876)
This is a bit cleaner and I want to call some of these functions from mypyc in a place where we don't have a BuildManager.
1 parent bbed891 commit 0c8566f

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

mypy/build.py

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,21 @@ def default_data_dir() -> str:
255255
return os.path.dirname(__file__)
256256

257257

258+
def normpath(path: str, options: Options) -> str:
259+
"""Convert path to absolute; but to relative in bazel mode.
260+
261+
(Bazel's distributed cache doesn't like filesystem metadata to
262+
end up in output files.)
263+
"""
264+
# TODO: Could we always use relpath? (A worry in non-bazel
265+
# mode would be that a moved file may change its full module
266+
# name without changing its size, mtime or hash.)
267+
if options.bazel:
268+
return os.path.relpath(path)
269+
else:
270+
return os.path.abspath(path)
271+
272+
258273
CacheMeta = NamedTuple('CacheMeta',
259274
[('id', str),
260275
('path', str),
@@ -589,9 +604,10 @@ def __init__(self, data_dir: str,
589604
self.fscache = fscache
590605
self.find_module_cache = FindModuleCache(self.search_paths, self.fscache, self.options)
591606
if options.sqlite_cache:
592-
self.metastore = SqliteMetadataStore(_cache_dir_prefix(self)) # type: MetadataStore
607+
self.metastore = SqliteMetadataStore(
608+
_cache_dir_prefix(self.options)) # type: MetadataStore
593609
else:
594-
self.metastore = FilesystemMetadataStore(_cache_dir_prefix(self))
610+
self.metastore = FilesystemMetadataStore(_cache_dir_prefix(self.options))
595611

596612
# a mapping from source files to their corresponding shadow files
597613
# for efficient lookup
@@ -623,7 +639,7 @@ def maybe_swap_for_shadow_path(self, path: str) -> str:
623639
if not self.shadow_map:
624640
return path
625641

626-
path = self.normpath(path)
642+
path = normpath(path, self.options)
627643

628644
previously_checked = path in self.shadow_equivalence_map
629645
if not previously_checked:
@@ -651,20 +667,6 @@ def getmtime(self, path: str) -> int:
651667
else:
652668
return int(self.metastore.getmtime(path))
653669

654-
def normpath(self, path: str) -> str:
655-
"""Convert path to absolute; but to relative in bazel mode.
656-
657-
(Bazel's distributed cache doesn't like filesystem metadata to
658-
end up in output files.)
659-
"""
660-
# TODO: Could we always use relpath? (A worry in non-bazel
661-
# mode would be that a moved file may change its full module
662-
# name without changing its size, mtime or hash.)
663-
if self.options.bazel:
664-
return os.path.relpath(path)
665-
else:
666-
return os.path.abspath(path)
667-
668670
def all_imported_modules_in_file(self,
669671
file: MypyFile) -> List[Tuple[int, str, int]]:
670672
"""Find all reachable import statements in a file.
@@ -866,7 +868,7 @@ def write_deps_cache(rdeps: Dict[str, Dict[str, Set[str]]],
866868

867869
for id in rdeps:
868870
if id != FAKE_ROOT_MODULE:
869-
_, _, deps_json = get_cache_names(id, graph[id].xpath, manager)
871+
_, _, deps_json = get_cache_names(id, graph[id].xpath, manager.options)
870872
else:
871873
deps_json = DEPS_ROOT_FILE
872874
assert deps_json
@@ -896,7 +898,7 @@ def write_deps_cache(rdeps: Dict[str, Dict[str, Set[str]]],
896898
error = True
897899

898900
if error:
899-
manager.errors.set_file(_cache_dir_prefix(manager), None)
901+
manager.errors.set_file(_cache_dir_prefix(manager.options), None)
900902
manager.errors.report(0, 0, "Error writing fine-grained dependencies cache",
901903
blocker=True)
902904

@@ -963,7 +965,7 @@ def generate_deps_for_cache(manager: BuildManager,
963965
def write_plugins_snapshot(manager: BuildManager) -> None:
964966
"""Write snapshot of versions and hashes of currently active plugins."""
965967
if not manager.metastore.write(PLUGIN_SNAPSHOT_FILE, json.dumps(manager.plugins_snapshot)):
966-
manager.errors.set_file(_cache_dir_prefix(manager), None)
968+
manager.errors.set_file(_cache_dir_prefix(manager.options), None)
967969
manager.errors.report(0, 0, "Error writing plugins snapshot",
968970
blocker=True)
969971

@@ -1073,18 +1075,18 @@ def _load_json_file(file: str, manager: BuildManager,
10731075
return result
10741076

10751077

1076-
def _cache_dir_prefix(manager: BuildManager) -> str:
1078+
def _cache_dir_prefix(options: Options) -> str:
10771079
"""Get current cache directory (or file if id is given)."""
1078-
if manager.options.bazel:
1080+
if options.bazel:
10791081
# This is needed so the cache map works.
10801082
return os.curdir
1081-
cache_dir = manager.options.cache_dir
1082-
pyversion = manager.options.python_version
1083+
cache_dir = options.cache_dir
1084+
pyversion = options.python_version
10831085
base = os.path.join(cache_dir, '%d.%d' % pyversion)
10841086
return base
10851087

10861088

1087-
def get_cache_names(id: str, path: str, manager: BuildManager) -> Tuple[str, str, Optional[str]]:
1089+
def get_cache_names(id: str, path: str, options: Options) -> Tuple[str, str, Optional[str]]:
10881090
"""Return the file names for the cache files.
10891091
10901092
Args:
@@ -1097,8 +1099,8 @@ def get_cache_names(id: str, path: str, manager: BuildManager) -> Tuple[str, str
10971099
A tuple with the file names to be used for the meta JSON, the
10981100
data JSON, and the fine-grained deps JSON, respectively.
10991101
"""
1100-
if manager.options.cache_map:
1101-
pair = manager.options.cache_map.get(manager.normpath(path))
1102+
if options.cache_map:
1103+
pair = options.cache_map.get(normpath(path, options))
11021104
else:
11031105
pair = None
11041106
if pair is not None:
@@ -1107,15 +1109,15 @@ def get_cache_names(id: str, path: str, manager: BuildManager) -> Tuple[str, str
11071109
# prefix directory.
11081110
# Solve this by rewriting the paths as relative to the root dir.
11091111
# This only makes sense when using the filesystem backed cache.
1110-
root = _cache_dir_prefix(manager)
1112+
root = _cache_dir_prefix(options)
11111113
return (os.path.relpath(pair[0], root), os.path.relpath(pair[1], root), None)
11121114
prefix = os.path.join(*id.split('.'))
11131115
is_package = os.path.basename(path).startswith('__init__.py')
11141116
if is_package:
11151117
prefix = os.path.join(prefix, '__init__')
11161118

11171119
deps_json = None
1118-
if manager.options.cache_fine_grained:
1120+
if options.cache_fine_grained:
11191121
deps_json = prefix + '.deps.json'
11201122
return (prefix + '.meta.json', prefix + '.data.json', deps_json)
11211123

@@ -1133,7 +1135,7 @@ def find_cache_meta(id: str, path: str, manager: BuildManager) -> Optional[Cache
11331135
valid; otherwise None.
11341136
"""
11351137
# TODO: May need to take more build options into account
1136-
meta_json, data_json, _ = get_cache_names(id, path, manager)
1138+
meta_json, data_json, _ = get_cache_names(id, path, manager.options)
11371139
manager.trace('Looking for {} at {}'.format(id, meta_json))
11381140
t0 = time.time()
11391141
meta = _load_json_file(meta_json, manager,
@@ -1237,7 +1239,7 @@ def validate_meta(meta: Optional[CacheMeta], id: str, path: Optional[str],
12371239

12381240
if bazel:
12391241
# Normalize path under bazel to make sure it isn't absolute
1240-
path = manager.normpath(path)
1242+
path = normpath(path, manager.options)
12411243
try:
12421244
st = manager.get_stat(path)
12431245
except OSError:
@@ -1325,7 +1327,7 @@ def validate_meta(meta: Optional[CacheMeta], id: str, path: Optional[str],
13251327
meta_str = json.dumps(meta_dict, indent=2, sort_keys=True)
13261328
else:
13271329
meta_str = json.dumps(meta_dict)
1328-
meta_json, _, _ = get_cache_names(id, path, manager)
1330+
meta_json, _, _ = get_cache_names(id, path, manager.options)
13291331
manager.log('Updating mtime for {}: file {}, meta {}, mtime {}'
13301332
.format(id, path, meta_json, meta.mtime))
13311333
t1 = time.time()
@@ -1388,7 +1390,7 @@ def write_cache(id: str, path: str, tree: MypyFile,
13881390
bazel = manager.options.bazel
13891391

13901392
# Obtain file paths.
1391-
meta_json, data_json, _ = get_cache_names(id, path, manager)
1393+
meta_json, data_json, _ = get_cache_names(id, path, manager.options)
13921394
manager.log('Writing {} {} {} {}'.format(
13931395
id, path, meta_json, data_json))
13941396

@@ -1491,7 +1493,7 @@ def delete_cache(id: str, path: str, manager: BuildManager) -> None:
14911493
# We don't delete .deps files on errors, since the dependencies
14921494
# are mostly generated from other files and the metadata is
14931495
# tracked separately.
1494-
meta_path, data_path, _ = get_cache_names(id, path, manager)
1496+
meta_path, data_path, _ = get_cache_names(id, path, manager.options)
14951497
cache_paths = [meta_path, data_path]
14961498
manager.log('Deleting {} {} {}'.format(id, path, " ".join(x for x in cache_paths if x)))
14971499

0 commit comments

Comments
 (0)