/
local_py_modules.py
277 lines (233 loc) · 10.6 KB
/
local_py_modules.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
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
import os
import re
import sys
import inspect
import logging
import importlib
import modulefinder
from typing import List
from typing import Tuple
from unittest.mock import patch
from ..types import PathType
from .pip_pkg import get_all_pip_installed_modules
from ...exceptions import BentoMLException
logger = logging.getLogger(__name__)
def _get_module_src_file(module):
"""
Return module.__file__, change extension to '.py' if __file__ is ending with '.pyc'
"""
return module.__file__[:-1] if module.__file__.endswith(".pyc") else module.__file__
def _is_valid_py_identifier(s):
"""
Return true if string is in a valid python identifier format:
https://docs.python.org/2/reference/lexical_analysis.html#identifiers
"""
return re.fullmatch(r"[A-Za-z_][A-Za-z_0-9]*", s) is not None
def _get_module_relative_file_path(module_name, module_file):
if not os.path.isabs(module_file):
# For modules within current top level package, module_file here should
# already be a relative path to the src file
relative_path = module_file
elif os.path.split(module_file)[1] == "__init__.py":
# for module a.b.c in 'some_path/a/b/c/__init__.py', copy file to
# 'destination/a/b/c/__init__.py'
relative_path = os.path.join(module_name.replace(".", os.sep), "__init__.py")
else:
# for module a.b.c in 'some_path/a/b/c.py', copy file to 'destination/a/b/c.py'
relative_path = os.path.join(module_name.replace(".", os.sep) + ".py")
return relative_path
def _get_module(target_module):
# When target_module is a string, try import it
if isinstance(target_module, str):
try:
target_module = importlib.import_module(target_module)
except ImportError:
pass
return inspect.getmodule(target_module)
def _import_module_from_file(path):
module_name = path.replace(os.sep, ".")[:-3]
spec = importlib.util.spec_from_file_location(module_name, path)
m = importlib.util.module_from_spec(spec)
return m
# TODO: change this to find_local_py_modules_used(svc: Service)
def find_local_py_modules_used(target_module_file: PathType) -> List[Tuple[str, str]]:
"""Find all local python module dependencies of target_module, and list all the
local module python files to the destination directory while maintaining the module
structure unchanged to ensure all imports in target_module still works when loading
from the destination directory again
Args:
path (`Union[str, bytes, os.PathLike]`):
Path to a python source file
Returns:
list of (source file path, target file path) pairs
"""
target_module = _import_module_from_file(target_module_file)
try:
target_module_name = target_module.__spec__.name
except AttributeError:
target_module_name = target_module.__name__
# Find all non pip installed modules must be packaged for target module to run
exclude_modules = ["bentoml"] + get_all_pip_installed_modules()
finder = modulefinder.ModuleFinder(excludes=exclude_modules)
try:
logger.debug(
"Searching for local dependant modules of %s:%s",
target_module_name,
target_module_file,
)
if sys.version_info[0] == 3 and sys.version_info[1] >= 8:
_find_module = modulefinder._find_module
_PKG_DIRECTORY = modulefinder._PKG_DIRECTORY
def _patch_find_module(name, path=None):
"""ref issue: https://bugs.python.org/issue40350"""
importlib.machinery.PathFinder.invalidate_caches()
spec = importlib.machinery.PathFinder.find_spec(name, path)
if spec is not None and spec.loader is None:
return None, None, ("", "", _PKG_DIRECTORY)
return _find_module(name, path)
with patch.object(modulefinder, "_find_module", _patch_find_module):
finder.run_script(target_module_file)
else:
finder.run_script(target_module_file)
except SyntaxError:
# For package with conditional import that may only work with py2
# or py3, ModuleFinder#run_script will try to compile the source
# with current python version. And that may result in SyntaxError.
pass
if finder.badmodules:
logger.debug(
"Find bad module imports that can not be parsed properly: %s",
finder.badmodules.keys(),
)
# Look for dependencies that are not distributed python package, but users'
# local python code, all other dependencies must be defined with @env
# decorator when creating a new BentoService class
user_packages_and_modules = {}
for name, module in finder.modules.items():
if hasattr(module, "__file__") and module.__file__ is not None:
user_packages_and_modules[name] = module
# Lastly, add target module itself
user_packages_and_modules[target_module_name] = target_module
file_list = []
for module_name, module in user_packages_and_modules.items():
module_file = _get_module_src_file(module)
relative_path = _get_module_relative_file_path(module_name, module_file)
file_list.append((module_file, relative_path))
return file_list
def copy_local_py_modules(target_module, destination):
"""Find all local python module dependencies of target_module, and copy all the
local module python files to the destination directory while maintaining the module
structure unchanged to ensure all imports in target_module still works when loading
from the destination directory again
"""
target_module = _get_module(target_module)
# When target module is defined in interactive session, we can not easily
# get the class definition into a python module file and distribute it
if target_module.__name__ == "__main__" and not hasattr(target_module, "__file__"):
raise BentoMLException(
"Custom BentoModel class can not be defined in Python interactive REPL, try"
" writing the class definition to a file and import it."
)
try:
target_module_name = target_module.__spec__.name
except AttributeError:
target_module_name = target_module.__name__
target_module_file = _get_module_src_file(target_module)
logger.debug(
"copy_local_py_modules target_module_name: %s, target_module_file: %s",
target_module_name,
target_module_file,
)
if target_module_name == "__main__":
# Assuming no relative import in this case
target_module_file_name = os.path.split(target_module_file)[1]
target_module_name = target_module_file_name[:-3] # remove '.py'
logger.debug(
"Updating for __main__ module, target_module_name: %s, "
"target_module_file: %s",
target_module_name,
target_module_file,
)
# Find all non pip installed modules must be packaged for target module to run
# exclude_modules = ['bentoml'] + get_all_pip_installed_modules()
# finder = modulefinder.ModuleFinder(excludes=exclude_modules)
#
# try:
# logger.debug(
# "Searching for local dependant modules of %s:%s",
# target_module_name,
# target_module_file,
# )
# if sys.version_info[0] == 3 and sys.version_info[1] >= 8:
# _find_module = modulefinder._find_module
# _PKG_DIRECTORY = modulefinder._PKG_DIRECTORY
#
# def _patch_find_module(name, path=None):
# """ref issue: https://bugs.python.org/issue40350"""
#
# importlib.machinery.PathFinder.invalidate_caches()
#
# spec = importlib.machinery.PathFinder.find_spec(name, path)
#
# if spec is not None and spec.loader is None:
# return None, None, ("", "", _PKG_DIRECTORY)
#
# return _find_module(name, path)
#
# with patch.object(modulefinder, '_find_module', _patch_find_module):
# finder.run_script(target_module_file)
# else:
# finder.run_script(target_module_file)
# except SyntaxError:
# # For package with conditional import that may only work with py2
# # or py3, ModuleFinder#run_script will try to compile the source
# # with current python version. And that may result in SyntaxError.
# pass
#
# if finder.badmodules:
# logger.debug(
# "Find bad module imports that can not be parsed properly: %s",
# finder.badmodules.keys(),
# )
#
# # Look for dependencies that are not distributed python package, but users'
# # local python code, all other dependencies must be defined with @env
# # decorator when creating a new BentoService class
# user_packages_and_modules = {}
# for name, module in finder.modules.items():
# if hasattr(module, "__file__") and module.__file__ is not None:
# user_packages_and_modules[name] = module
# # Remove "__main__" module, if target module is loaded as __main__, it should
# # be in module_files as (module_name, module_file) in current context
# if "__main__" in user_packages_and_modules:
# del user_packages_and_modules["__main__"]
#
# # Lastly, add target module itself
# user_packages_and_modules[target_module_name] = target_module
# logger.debug(
# "Copying user local python dependencies: %s", user_packages_and_modules
# )
#
# for module_name, module in user_packages_and_modules.items():
# module_file = _get_module_src_file(module)
# relative_path = _get_module_relative_file_path(module_name, module_file)
# target_file = os.path.join(destination, relative_path)
#
# # Create target directory if not exist
# Path(os.path.dirname(target_file)).mkdir(parents=True, exist_ok=True)
#
# # Copy module file to BentoArchive for distribution
# logger.debug("Copying local python module '%s'", module_file)
# copyfile(module_file, target_file)
#
# for root, _, files in os.walk(destination):
# if "__init__.py" not in files:
# logger.debug("Creating empty __init__.py under folder:'%s'", root)
# Path(os.path.join(root, "__init__.py")).touch()
#
# target_module_relative_path = _get_module_relative_file_path(
# target_module_name, target_module_file
# )
# logger.debug("Done copying local python dependant modules")
#
# return target_module_name, target_module_relative_path