This repository was archived by the owner on Mar 27, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathworkspace.py
More file actions
386 lines (339 loc) · 17.2 KB
/
Copy pathworkspace.py
File metadata and controls
386 lines (339 loc) · 17.2 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
from .config import GlobalConfig
from .fs import FileSystem, LocalFileSystem
from .imports import get_imports
from .fetch import fetch_dependency
from .package_configuation import ConfigParser
from typing import Dict, Set, List
import json
import logging
import sys
import os
import os.path
import shutil
import threading
import opentracing
import jedi._compatibility
log = logging.getLogger(__name__)
class DummyFile:
def __init__(self, contents):
self.contents = contents
def read(self):
return self.contents
def close(self):
pass
class Module:
def __init__(self,
name: str,
qualified_name: str,
path: str,
is_package: bool=False,
is_external: bool=False,
is_stdlib: bool=False,
is_native: bool=False,
is_namespace_package: bool=False):
self.name = name
self.qualified_name = qualified_name
self.path = path
self.is_package = is_package
self.is_external = is_external
self.is_stdlib = is_stdlib
self.is_native = is_native
self.is_namespace_package = is_namespace_package
def __repr__(self):
return "PythonModule({}, {})".format(self.name, self.path)
class Workspace:
def __init__(self, fs: FileSystem, project_root: str, original_root_path: str= ""):
self.project_packages = set()
self.PROJECT_ROOT = project_root
self.repo = None
self.hash = None
if original_root_path.startswith("git://") and "?" in original_root_path:
repo_and_hash = original_root_path.split("?")
self.repo = repo_and_hash[0]
original_root_path = self.repo
self.hash = repo_and_hash[1]
self.is_stdlib = (self.repo == GlobalConfig.STDLIB_REPO_URL)
if original_root_path.startswith("git://"):
original_root_path = original_root_path[6:]
# turn the original root path into something that can be used as a file/path name or cache key
self.key = original_root_path.replace("/", ".").replace("\\", ".")
if self.hash:
self.key = ".".join((self.key, self.hash))
self.PYTHON_PATH = GlobalConfig.PYTHON_PATH # TODO: allow different Python versions per project/workspace
self.PACKAGES_PATH = os.path.join(GlobalConfig.PACKAGES_PARENT, self.key)
log.debug("Setting Python path to %s", self.PYTHON_PATH)
log.debug("Setting package path to %s", self.PACKAGES_PATH)
self.fs = fs
self.local_fs = LocalFileSystem()
self.source_paths = {path for path in self.fs.walk(self.PROJECT_ROOT) if path.endswith(".py")}
self.project = {}
self.stdlib = {}
self.dependencies = {}
self.module_paths = {}
# keep track of which package folders have been indexed, since we fetch and index new folders on-demand
self.indexed_folders = set()
self.indexing_lock = threading.Lock()
# keep track of which packages we've tried to fetch, so we don't keep trying if they were unfetchable
self.fetched = set()
self.config = {}
self.populate_config()
self.index_project()
for n in sys.builtin_module_names:
self.stdlib[n] = "native" # TODO: figure out how to provide code intelligence for compiled-in modules
if "nt" not in self.stdlib:
self.stdlib["nt"] = "native" # this is missing on non-Windows systems; add it so we don't try to fetch it
if os.path.exists(self.PYTHON_PATH):
log.debug("Indexing standard library at %s", self.PYTHON_PATH)
self.index_dependencies(self.stdlib, self.PYTHON_PATH, is_stdlib=True)
else:
log.warning("Standard library not found at %s", self.PYTHON_PATH)
# if the dependencies are already cached from a previous session, go ahead and index them, otherwise just
# create the folder and let them be fetched on-demand
if os.path.exists(self.PACKAGES_PATH):
self.index_external_modules()
else:
os.makedirs(self.PACKAGES_PATH)
def cleanup(self):
log.info("Removing package cache %s", self.PACKAGES_PATH)
shutil.rmtree(self.PACKAGES_PATH, True)
def index_dependencies(self,
index: Dict[str, Module],
library_path: str,
is_stdlib: bool=False,
breadcrumb: str=None):
"""
Given a root library path (e.g., the Python root path or the dist-packages root path), this method traverses
it recursively and indexes all the packages and modules contained therein. It constructs a mapping from the
fully qualified module name to a Module object containing the metadata that Jedi needs.
:param index: the dictionary that should be used to store this index (will be modified)
:param library_path: the root path containing the modules and packages to be indexed
:param is_stdlib: flag indicating whether this invocation is indexing the standard library
:param breadcrumb: should be omitted by the caller; this method uses it to keep track of the fully qualified
module name
"""
parent, this = os.path.split(library_path)
basename, extension = os.path.splitext(this)
if Workspace.is_package(library_path) or extension == ".py" and this != "__init__.py":
qualified_name = ".".join((breadcrumb, basename)) if breadcrumb else basename
elif extension == ".so":
basename = basename.split(".")[0]
qualified_name = ".".join((breadcrumb, basename)) if breadcrumb else basename
else:
qualified_name = breadcrumb
if os.path.isdir(library_path):
# recursively index this folder
for child in os.listdir(library_path):
self.index_dependencies(index, os.path.join(library_path, child), is_stdlib, qualified_name)
elif this == "__init__.py":
# we're already inside a package
module_name = os.path.basename(parent)
the_module = Module(module_name,
qualified_name,
library_path,
True,
True,
is_stdlib)
index[qualified_name] = the_module
self.module_paths[os.path.abspath(the_module.path)] = the_module
self.fetched.add(module_name)
elif extension == ".py":
# just a regular non-package module
the_module = Module(basename,
qualified_name,
library_path,
False,
True,
is_stdlib)
index[qualified_name] = the_module
self.module_paths[os.path.abspath(the_module.path)] = the_module
self.fetched.add(basename)
elif extension == ".so":
# native module -- mark it as such and report a warning or something
the_module = Module(basename,
qualified_name,
"",
False,
True,
is_stdlib,
True)
index[qualified_name] = the_module
self.module_paths[os.path.abspath(the_module.path)] = the_module
self.fetched.add(basename)
def populate_config(self):
all_paths = list(self.fs.walk(self.PROJECT_ROOT))
if '/Pipfile' in all_paths:
json_data = self.fs.open('/Pipfile')
parsed_json = ConfigParser(json_data)
self.config = parsed_json
def index_project(self):
"""
This method traverses all the project files (starting with self.PROJECT_ROOT) and indexes all the packages and
modules contained therein. It constructs a mapping from the fully qualified module name to a Module object
containing the metadata that Jedi needs. Because it only has a flat list of paths/uris to work with (as
opposed to being able to walk the file tree top-down), it does some extra work to figure out the qualified
names of each module.
"""
all_paths = list(self.fs.walk(self.PROJECT_ROOT))
# TODO: maybe try to exec setup.py with a sandboxed global env and builtins dict or something
# pre-compute the set of all packages in this project -- this will be useful when trying to figure out each
# module's qualified name, as well as the packages that are exported by this project
package_paths = {}
top_level_modules = set()
for path in all_paths:
folder, filename = os.path.split(path)
if filename == "__init__.py":
package_paths[folder] = True
if folder == "/"\
and filename.endswith(".py")\
and filename not in {"__init__.py", "setup.py", "tests.py", "test.py"}:
basename, extension = os.path.splitext(filename)
top_level_modules.add(basename)
# figure out this project's exports (for xpackages)
for path in package_paths:
if path.startswith("/"):
path = path[1:]
else:
continue
path_components = path.split("/")
if len(path_components) > 1:
continue
else:
self.project_packages.add(path_components[0])
if not self.project_packages: # if this is the case, then the exports must be in top-level Python files
for m in top_level_modules:
self.project_packages.add(m)
# now index all modules and packages, taking care to compute their qualified names correctly (can be tricky
# depending on how the folders are nested, and whether they have '__init__.py's or not
for path in all_paths:
folder, filename = os.path.split(path)
basename, ext = os.path.splitext(filename)
if filename == "__init__.py":
parent, this = os.path.split(folder)
elif filename.endswith(".py"):
parent = folder
this = basename
else:
continue
qualified_name_components = [this]
# A module's qualified name should only contain the names of its enclosing folders that are packages (i.e.,
# that contain an '__init__.py'), not the names of *all* its enclosing folders. Hence, the following loop
# only accumulates qualified name components until it encounters a folder that isn't in the pre-computed
# set of packages.
while parent and parent != "/" and parent in package_paths:
parent, this = os.path.split(parent)
qualified_name_components.append(this)
qualified_name_components.reverse()
qualified_name = ".".join(qualified_name_components)
if filename == "__init__.py":
module_name = os.path.basename(folder)
the_module = Module(module_name, qualified_name, path, True)
self.project[qualified_name] = the_module
self.module_paths[the_module.path] = the_module
elif ext == ".py" and not basename.startswith("__") and not basename.endswith("__"):
the_module = Module(basename, qualified_name, path)
self.project[qualified_name] = the_module
self.module_paths[the_module.path] = the_module
def find_stdlib_module(self, qualified_name: str) -> Module:
return self.stdlib.get(qualified_name, None)
def find_project_module(self, qualified_name: str) -> Module:
return self.project.get(qualified_name, None)
def find_external_module(self, qualified_name: str) -> Module:
package_name = qualified_name.split(".")[0]
if package_name not in self.fetched:
self.indexing_lock.acquire()
self.fetched.add(package_name)
fetch_dependency(package_name, self.PACKAGES_PATH, self.config)
self.index_external_modules()
self.indexing_lock.release()
the_module = self.dependencies.get(qualified_name, None)
if the_module and the_module.is_native:
raise NotImplementedError("Unable to analyze native modules")
else:
return the_module
def index_external_modules(self):
for path in os.listdir(self.PACKAGES_PATH):
if path not in self.indexed_folders:
self.index_dependencies(self.dependencies, os.path.join(self.PACKAGES_PATH, path))
self.indexed_folders.add(path)
def open_module_file(self, the_module: Module, parent_span: opentracing.Span):
if the_module.path not in self.source_paths:
return None
elif the_module.is_external:
return DummyFile(self.local_fs.open(the_module.path, parent_span))
else:
return DummyFile(self.fs.open(the_module.path, parent_span))
def get_module_by_path(self, path: str) -> Module:
return self.module_paths.get(path, None)
def get_modules(self, qualified_name: str) -> List[Module]:
project_module = self.project.get(qualified_name, None)
external_module = self.find_external_module(qualified_name)
stdlib_module = self.stdlib.get(qualified_name, None)
return list(filter(None, [project_module, external_module, stdlib_module]))
def get_dependencies(self, parent_span: opentracing.Span) -> list:
top_level_stdlib = {p.split(".")[0] for p in self.stdlib}
top_level_imports = get_imports(self.fs, self.PROJECT_ROOT, parent_span)
stdlib_imports = top_level_imports & top_level_stdlib
external_imports = top_level_imports - top_level_stdlib - self.project_packages
dependencies = [{"attributes": {"name": n}} for n in external_imports]
if stdlib_imports:
dependencies.append({
"attributes": {
"name": "cpython",
"repoURL": "git://github.com/python/cpython"
}
})
return dependencies
def get_package_information(self, parent_span: opentracing.Span) -> list:
if self.is_stdlib:
return [{
"package": {"name": "cpython"},
"dependencies": []
}]
else:
return [
{
"package": {"name": p},
# multiple packages in the project share the same dependencies
"dependencies": self.get_dependencies(parent_span)
} for p in self.project_packages
]
# finds a project module using the newer, more dynamic import rules detailed in PEP 420
# (see https://www.python.org/dev/peps/pep-0420/)
def find_internal_module(self, name: str, qualified_name: str, dirs: List[str]):
module_paths = []
for parent in dirs:
if os.path.join(parent, name, "__init__.py") in self.source_paths:
# there's a folder at this level that implements a package with the name we're looking for
module_path = os.path.join(parent, name, "__init__.py")
module_file = DummyFile(self.fs.open(module_path))
return module_file, module_path, True
elif os.path.basename(parent) == name and os.path.join(parent, "__init__.py") in self.source_paths:
# we're already in a package with the name we're looking for
module_path = os.path.join(parent, "__init__.py")
module_file = DummyFile(self.fs.open(module_path))
return module_file, module_path, True
elif os.path.join(parent, name + ".py") in self.source_paths:
# there's a file at this level that implements a module with the name we're looking for
module_path = os.path.join(parent, name + ".py")
module_file = DummyFile(self.fs.open(module_path))
return module_file, module_path, False
elif self.folder_exists(os.path.join(parent, name)):
# there's a folder at this level that implements a namespace package with the name we're looking for
module_paths.append(os.path.join(parent, name))
elif os.path.basename(parent) == name:
# we're already in a namespace package with the name we're looking for
module_paths.append(parent)
if not module_paths:
return None, None, None
return None, jedi._compatibility.ImplicitNSInfo(qualified_name, module_paths), False
def folder_exists(self, name):
for path in self.source_paths:
if os.path.commonpath((name, path)) == name:
return True
return False
@staticmethod
def is_package(path: str) -> bool:
return os.path.isdir(path) and "__init__.py" in os.listdir(path)
@staticmethod
def get_top_level_package_names(index: Dict[str, Module]) -> Set[str]:
return {name.split(".")[0] for name in index}