forked from facebook/pyre-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_directories.py
94 lines (70 loc) · 2.81 KB
/
find_directories.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Copyright (c) 2016-present, Facebook, Inc.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import logging
import os
from typing import Optional
from .filesystem import find_root
CONFIGURATION_FILE: str = ".pyre_configuration"
LOCAL_CONFIGURATION_FILE: str = ".pyre_configuration.local"
BINARY_NAME: str = "pyre.bin"
CLIENT_NAME: str = "pyre-client"
LOG: logging.Logger = logging.getLogger(__name__)
def find_project_root(original_directory: str) -> str:
"""Pyre always runs from the directory containing the nearest .pyre_configuration,
if one exists."""
global_root = find_root(original_directory, CONFIGURATION_FILE)
return global_root or original_directory
def find_local_root(
original_directory: str, local_root: Optional[str] = None
) -> Optional[str]:
if local_root:
return local_root
global_root = find_root(original_directory, CONFIGURATION_FILE)
local_root = find_root(original_directory, LOCAL_CONFIGURATION_FILE)
# If the global configuration root is deeper than local configuration, ignore local.
if global_root and local_root and global_root.startswith(local_root):
local_root = None
if local_root:
return local_root
def _find_directory_upwards(base: str, target: str) -> Optional[str]:
"""
Walk directories upwards from base, until the root directory is
reached. At each step, check if the target directory exist, and return
it if found. Return None if the search is unsuccessful.
"""
while True:
step = os.path.join(base, target)
LOG.debug("Trying with: `%s`", step)
if os.path.isdir(step):
return step
parent_directory = os.path.dirname(base)
if parent_directory == base:
# We have reached the root.
break
base = parent_directory
return None
def find_typeshed() -> Optional[str]:
override = os.getenv("PYRE_TYPESHED")
if override:
return override
current_directory = os.path.dirname(os.path.realpath(__file__))
# Prefer the typeshed we bundled ourselves (if any) to the one
# from the environment.
bundled_typeshed = _find_directory_upwards(
current_directory, "pyre_check/typeshed/"
)
if bundled_typeshed:
return bundled_typeshed
try:
import typeshed # pyre-fixme: Can't find module import typeshed
return typeshed.typeshed
except ImportError:
LOG.debug("`import typeshed` failed, attempting a manual lookup")
# This is a terrible, terrible hack.
return _find_directory_upwards(current_directory, "typeshed/")
def find_taint_models_directory() -> Optional[str]:
return _find_directory_upwards(
os.path.dirname(os.path.realpath(__file__)), "pyre_check/taint/"
)