This repository was archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathfile.py
189 lines (165 loc) · 7.3 KB
/
file.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
import base64
import inspect
import io
import pathlib
import sys
import tarfile
import tempfile
import uuid
from typing import Any, get_type_hints
import psycopg2
import psycopg2.extensions
import greenplumpython as gp
from greenplumpython.func import NormalFunction
_CHUNK_SIZE = 256 * 1024 * 1024 # Must be much < 1 GB
@gp.create_function
def _dump_file_chunk(tmp_dir_handle: str, chunk_base64: str) -> str:
try:
_gd = globals()["GD"] # type: ignore reportUnknownVariableType
except KeyError:
_gd = sys.modules["plpy"]._GD
if tmp_dir_handle not in _gd:
server_tmp_dir = tempfile.TemporaryDirectory(prefix="pygp.srv.")
_gd[tmp_dir_handle] = server_tmp_dir # Pin to GD for later UDFs
else:
server_tmp_dir = _gd[tmp_dir_handle] # type: ignore reportUnknownVariableType
server_tmp_dir_path: pathlib.Path = pathlib.Path(server_tmp_dir.name) # type: ignore reportUnknownVariableType
server_tmp_dir_path.mkdir(parents=True, exist_ok=True)
tmp_archive_path = server_tmp_dir_path / f"{tmp_dir_handle}.tar.gz"
with open(tmp_archive_path, "ab") as tmp_archive:
tmp_archive.write(base64.b64decode(chunk_base64))
return server_tmp_dir.name
@gp.create_function
def _extract_files(server_tmp_dir: str, tmp_dir_handle: str, returning: str) -> list[str]:
server_tmp_dir_path: pathlib.Path = pathlib.Path(server_tmp_dir)
tmp_archive_path = server_tmp_dir_path / f"{tmp_dir_handle}.tar.gz"
extracted_root = server_tmp_dir_path / "extracted"
if not extracted_root.exists():
with tarfile.open(tmp_archive_path, "r:gz") as tmp_archive:
extracted_root.mkdir()
tmp_archive.extractall(str(extracted_root))
tmp_archive_path.unlink()
if returning == "root":
yield str(extracted_root.resolve())
else:
assert returning == "files"
for path in extracted_root.rglob("*"):
if path.is_file() and not path.is_symlink():
yield str(path.resolve())
def _remove_tmp_dir(conn: psycopg2.extensions.connection, db: gp.Database, tmp_dir_handle: str):
@gp.create_function
def udf(tmp_dir_handle: str) -> None:
try:
_gd = globals()["GD"] # type: ignore reportUnknownVariableType
except KeyError:
_gd = sys.modules["plpy"]._GD
_gd[tmp_dir_handle].cleanup()
with conn.cursor() as cursor:
cursor.execute(udf._serialize(db))
cursor.execute(f"SELECT {udf._qualified_name_str}('{tmp_dir_handle}');")
def _archive_and_upload(
util_conn: psycopg2.extensions.connection,
tmp_dir_handle: str,
files: list[str],
db: gp.Database,
) -> str:
with tempfile.TemporaryDirectory(prefix="pygp.cln.") as local_tmp_dir:
local_tmp_dir_path: pathlib.Path = pathlib.Path(local_tmp_dir)
tmp_archive_path = local_tmp_dir_path / f"{tmp_dir_handle}.tar.gz"
with tarfile.open(tmp_archive_path, "w:gz") as tmp_archive:
for file_path in files:
tmp_archive.add(pathlib.Path(file_path))
with util_conn.cursor() as cursor:
cursor.execute(f"CREATE TEMP TABLE {tmp_dir_handle} (id serial, text_base64 text);")
with open(tmp_archive_path, "rb") as tmp_archive:
while True:
chunk = tmp_archive.read(_CHUNK_SIZE)
if len(chunk) == 0:
break
chunk_base64 = base64.b64encode(chunk)
cursor.copy_expert(
f"COPY {tmp_dir_handle} (text_base64) FROM STDIN",
io.BytesIO(chunk_base64),
)
util_conn.commit()
cursor.execute(_dump_file_chunk._serialize(db))
cursor.execute(
f"""
SELECT {_dump_file_chunk._qualified_name_str}('{tmp_dir_handle}', text_base64)
FROM "{tmp_dir_handle}"
ORDER BY id;
"""
)
return cursor.fetchall()[0][0]
@classmethod
def _from_files(_, files: list[str], parser: NormalFunction, db: gp.Database) -> gp.DataFrame:
tmp_dir_handle = f"__pygp_tar_{uuid.uuid4().hex}"
server_options = "-c gp_session_role=utility" if db._is_variant("greenplum") else None
with psycopg2.connect(db._dsn, options=server_options) as util_conn: # type: ignore reportUnknownVariableType
server_tmp_dir = _archive_and_upload(util_conn, tmp_dir_handle, files, db) # type: ignore reportUnknownArgumentType
func_sig = inspect.signature(parser.unwrap())
result_members = get_type_hints(func_sig.return_annotation)
df = db.apply(
lambda: parser(_extract_files(server_tmp_dir, tmp_dir_handle, "files")),
expand=len(result_members) == 0,
)
# _remove_tmp_dir(util_conn, db, tmp_dir_handle) # Cannot remove now since the returned DataFrame depends on it.
return df
setattr(gp.DataFrame, "from_files", _from_files)
import subprocess
@gp.create_function
def _install_on_server(server_tmp_dir: str, local_tmp_dir: str, requirements: str) -> str:
assert sys.executable, "Python executable is required to install packages."
server_tmp_dir_path: pathlib.Path = pathlib.Path(server_tmp_dir)
local_tmp_dir_path = pathlib.Path(local_tmp_dir)
cmd = [
sys.executable,
"-m",
"pip",
"install",
"--no-index",
"--requirement",
"/dev/stdin",
"--find-links",
str(
server_tmp_dir_path
/ "extracted"
/ local_tmp_dir_path.relative_to(local_tmp_dir_path.root)
),
]
try:
output = subprocess.check_output(
cmd, text=True, stderr=subprocess.STDOUT, input=requirements
)
return output
except subprocess.CalledProcessError as e:
raise Exception(e.stdout)
def _install_packages(db: gp.Database, requirements: str):
tmp_dir_handle = f"__pygp_tar_{uuid.uuid4().hex}"
with tempfile.TemporaryDirectory(prefix="pygp.cln.") as local_pkg_dir:
local_tmp_dir_path = pathlib.Path(local_pkg_dir)
cmd = [
sys.executable,
"-m",
"pip",
"download",
"--requirement",
"/dev/stdin",
"--dest",
str(local_tmp_dir_path),
]
try:
subprocess.check_output(cmd, text=True, stderr=subprocess.STDOUT, input=requirements)
except subprocess.CalledProcessError as e:
raise e from Exception(e.stdout)
server_options = "-c gp_session_role=utility" if db._is_variant("greenplum") else None
with psycopg2.connect(db._dsn, options=server_options) as util_conn: # type: ignore reportUnknownVariableType
server_tmp_dir = _archive_and_upload(util_conn, tmp_dir_handle, [local_pkg_dir], db) # type: ignore reportUnknownArgumentType
extracted = db.apply(lambda: _extract_files(server_tmp_dir, tmp_dir_handle, "root"))
assert len(list(extracted)) == 1
installed = extracted.apply(
lambda _: _install_on_server(server_tmp_dir, local_pkg_dir, requirements)
)
assert len(list(installed)) == 1
_remove_tmp_dir(util_conn, db, tmp_dir_handle) # type: ignore reportUnknownArgumentType
setattr(gp.Database, "install_packages", _install_packages)