-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathloader.py
More file actions
161 lines (127 loc) · 5.18 KB
/
loader.py
File metadata and controls
161 lines (127 loc) · 5.18 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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""Python functions loader."""
import importlib
import importlib.machinery
import importlib.util
import os
import os.path
import pathlib
import sys
from uuid import uuid4
from os import PathLike, fspath
from typing import List, Optional, Dict
from azure.functions import Function, FunctionApp
from . import functions
from .protos import RpcFunctionMetadata, BindingInfo
from .constants import MODULE_NOT_FOUND_TS_URL, SCRIPT_FILE_NAME, \
PYTHON_LANGUAGE_RUNTIME
from .utils.wrappers import attach_message_to_exception
_AZURE_NAMESPACE = '__app__'
_DEFAULT_SCRIPT_FILENAME = '__init__.py'
_DEFAULT_ENTRY_POINT = 'main'
_submodule_dirs = []
def register_function_dir(path: PathLike) -> None:
try:
_submodule_dirs.append(fspath(path))
except TypeError as e:
raise RuntimeError(f'Path ({path}) is incompatible with fspath. '
f'It is of type {type(path)}.', e)
def install() -> None:
if _AZURE_NAMESPACE not in sys.modules:
# Create and register the __app__ namespace package.
ns_spec = importlib.machinery.ModuleSpec(_AZURE_NAMESPACE, None)
ns_spec.submodule_search_locations = _submodule_dirs
ns_pkg = importlib.util.module_from_spec(ns_spec)
sys.modules[_AZURE_NAMESPACE] = ns_pkg
def uninstall() -> None:
pass
def build_binding_protos(indexed_function: List[Function]) -> Dict:
return {
binding.name: BindingInfo(
type=binding.type,
data_type=binding.data_type,
direction=binding.direction)
for binding in indexed_function.get_bindings()
}
def process_indexed_function(functions_registry: functions.Registry,
indexed_functions: List[Function]):
fx_metadata_results = []
for indexed_function in indexed_functions:
function_id = str(uuid4())
function_info = functions_registry.add_indexed_function(
function_id,
function=indexed_function)
binding_protos = build_binding_protos(indexed_function)
function_metadata = RpcFunctionMetadata(
name=function_info.name,
function_id=function_id,
managed_dependency_enabled=False, # only enabled for PowerShell
directory=function_info.directory,
script_file=indexed_function.function_script_file,
entry_point=function_info.name,
is_proxy=False, # not supported in V4
language=PYTHON_LANGUAGE_RUNTIME,
bindings=binding_protos,
raw_bindings=indexed_function.get_raw_bindings())
fx_metadata_results.append(function_metadata)
return fx_metadata_results
@attach_message_to_exception(
expt_type=ImportError,
message=f'Please check the requirements.txt file for the missing module. '
f'For more info, please refer the troubleshooting'
f' guide: {MODULE_NOT_FOUND_TS_URL} '
)
def load_function(name: str, directory: str, script_file: str,
entry_point: Optional[str]):
dir_path = pathlib.Path(directory)
script_path = pathlib.Path(script_file) if script_file else pathlib.Path(
_DEFAULT_SCRIPT_FILENAME)
if not entry_point:
entry_point = _DEFAULT_ENTRY_POINT
register_function_dir(dir_path.parent)
try:
rel_script_path = script_path.relative_to(dir_path.parent)
except ValueError:
raise RuntimeError(
f'script path {script_file} is not relative to the specified '
f'directory {directory}'
)
last_part = rel_script_path.parts[-1]
modname, ext = os.path.splitext(last_part)
if ext != '.py':
raise RuntimeError(
f'cannot load function {name}: '
f'invalid Python filename {script_file}')
modname_parts = [_AZURE_NAMESPACE]
modname_parts.extend(rel_script_path.parts[:-1])
# If the __init__.py contains the code, we should avoid double loading.
if modname.lower() != '__init__':
modname_parts.append(modname)
fullmodname = '.'.join(modname_parts)
mod = importlib.import_module(fullmodname)
func = getattr(mod, entry_point, None)
if func is None or not callable(func):
raise RuntimeError(
f'cannot load function {name}: function {entry_point}() is not '
f'present in {rel_script_path}')
return func
@attach_message_to_exception(
expt_type=ImportError,
message=f'Troubleshooting Guide: {MODULE_NOT_FOUND_TS_URL}'
)
def index_function_app(function_path: str) -> List[Function]:
module_name = pathlib.Path(function_path).stem
imported_module = importlib.import_module(module_name)
app: Optional[FunctionApp] = None
for i in imported_module.__dir__():
if isinstance(getattr(imported_module, i, None), FunctionApp):
if not app:
app = getattr(imported_module, i, None)
else:
raise ValueError(
"Multiple instances of FunctionApp are defined")
if not app:
raise ValueError("Could not find instance of FunctionApp in "
f"{SCRIPT_FILE_NAME}.")
return app.get_functions()