-
Notifications
You must be signed in to change notification settings - Fork 0
/
plpy_venv--1.0.sql
299 lines (233 loc) · 9.41 KB
/
plpy_venv--1.0.sql
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
--
-- Create a virtual environment
--
CREATE OR REPLACE FUNCTION @[email protected]_venv(name text DEFAULT 'venv')
RETURNS text
LANGUAGE plpython3u
AS $BODY$
import os
import shutil
import sys
from virtualenv.run import cli_run
# Get the data directory
res = plpy.execute("SELECT setting FROM pg_settings WHERE name = 'data_directory'")
data_dir = res[0]['setting']
venvs_dir = os.path.join(data_dir, 'venvs')
venv_dir = os.path.join(venvs_dir, name)
# Check for traversal attacks
if os.path.commonprefix((os.path.realpath(venv_dir), venvs_dir)) != venvs_dir:
plpy.error('Invalid virtual environment name: {}.'.format(name))
if not os.path.exists(venvs_dir):
os.makedirs(venvs_dir)
if sys.platform in ('win32', 'win64', 'cygwin'):
python_bin = os.path.join(sys.base_prefix, 'Scripts', 'python.exe')
else:
python_bin = os.path.join(sys.base_prefix, 'bin', 'python3')
if not os.path.exists(venv_dir):
cli_run(['-p', python_bin, venv_dir])
else:
plpy.error('Virtual environment directory {} already exists.'.format(venv_dir))
return venv_dir
$BODY$;
REVOKE ALL PRIVILEGES ON FUNCTION @[email protected]_venv(text) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION @[email protected]_venv(text) TO CURRENT_USER;
COMMENT ON FUNCTION @[email protected]_venv(text) IS 'Create a Python virtual environment (default name: venv).';
--
-- Activate a virtual environment
--
CREATE OR REPLACE FUNCTION @[email protected]_venv(name text DEFAULT 'venv')
RETURNS boolean
LANGUAGE plpython3u
AS $BODY$
import os
import sys
# Get the data directory
res = plpy.execute("SELECT setting FROM pg_settings WHERE name = 'data_directory'")
data_dir = res[0]['setting']
venvs_dir = os.path.join(data_dir, 'venvs')
venv_dir = os.path.join(venvs_dir, name)
# Check for traversal attacks
if os.path.commonprefix((os.path.realpath(venv_dir), venvs_dir)) != venvs_dir:
plpy.error('Invalid virtual environment name: {}.'.format(name))
if sys.platform in ('win32', 'win64', 'cygwin'):
activate_this = os.path.join(venv_dir, 'Scripts', 'activate_this.py')
else:
activate_this = os.path.join(venv_dir, 'bin', 'activate_this.py')
try:
exec(open(activate_this).read(), dict(__file__=activate_this))
except FileNotFoundError:
plpy.error('Virtual environment {} does not exist.'.format(name))
return True
$BODY$;
REVOKE ALL PRIVILEGES ON FUNCTION @[email protected]_venv(text) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION @[email protected]_venv(text) TO CURRENT_USER;
COMMENT ON FUNCTION @[email protected]_venv(text) IS 'Activate a Python virtual environment (default name: venv).';
--
-- Delete a virtual environment
--
CREATE OR REPLACE FUNCTION @[email protected]_venv(name text)
RETURNS boolean
LANGUAGE plpython3u
AS $BODY$
import os
import pathlib
import shutil
import sys
# Get the data directory
res = plpy.execute("SELECT setting FROM pg_settings WHERE name = 'data_directory'")
data_dir = res[0]['setting']
venvs_dir = os.path.join(data_dir, 'venvs')
venv_dir = os.path.join(venvs_dir, name)
# Check for traversal attacks
if os.path.commonprefix((os.path.realpath(venv_dir), venvs_dir)) != venvs_dir:
plpy.error('Invalid virtual environment name: {}.'.format(name))
# Is this venv active?
if sys.prefix != sys.base_prefix and sys.prefix == venv_dir:
plpy.error('Virtual environment {} is currently active.'.format(name))
dir = pathlib.Path(venv_dir)
try:
shutil.rmtree(dir)
except FileNotFoundError:
plpy.error('Virtual environment {} does not exist.'.format(name))
return True
$BODY$;
REVOKE ALL PRIVILEGES ON FUNCTION @[email protected]_venv(text) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION @[email protected]_venv(text) TO CURRENT_USER;
COMMENT ON FUNCTION @[email protected]_venv(text) IS 'Delete a Python virtual environment.';
--
-- Return the active virtual environment
--
CREATE OR REPLACE FUNCTION @[email protected]_venv()
RETURNS text
LANGUAGE plpython3u
AS $BODY$
import pathlib
import sys
if sys.prefix == sys.base_prefix:
return None
else:
return pathlib.PurePath(sys.prefix).name
$BODY$;
REVOKE ALL PRIVILEGES ON FUNCTION @[email protected]_venv() FROM PUBLIC;
GRANT EXECUTE ON FUNCTION @[email protected]_venv() TO CURRENT_USER;
COMMENT ON FUNCTION @[email protected]_venv() IS 'Return the path to the active Python virtual environment.';
--
-- Install packages using PIP
--
CREATE OR REPLACE FUNCTION @[email protected]_install(packages text[])
RETURNS boolean
LANGUAGE plpython3u
AS $BODY$
import os
import subprocess
import sys
if sys.prefix == sys.base_prefix:
plpy.error('No virtual environment is currently active.')
if sys.platform in ('win32', 'win64', 'cygwin'):
pip = os.path.join(sys.prefix, 'Scripts', 'pip.exe')
else:
pip = os.path.join(sys.prefix, 'bin', 'pip')
proc = subprocess.Popen([pip, 'install'] + packages, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = proc.communicate()
if out.decode('utf8') != '':
plpy.notice(out.decode('utf8'))
if proc.returncode != 0:
if err is not None:
plpy.error('Installation error {}:\n{}'.format(proc.returncode, err.decode('utf8')))
else:
plpy.error('Installation error {}.'.format(proc.returncode))
return True
$BODY$;
REVOKE ALL PRIVILEGES ON FUNCTION @[email protected]_install(text[]) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION @[email protected]_install(text[]) TO CURRENT_USER;
COMMENT ON FUNCTION @[email protected]_install(text[]) IS 'Install packages into the active Python virtual environment.';
--
-- Upgrade packages using PIP
--
CREATE OR REPLACE FUNCTION @[email protected]_upgrade(packages text[])
RETURNS boolean
LANGUAGE plpython3u
AS $BODY$
import os
import subprocess
import sys
if sys.prefix == sys.base_prefix:
plpy.error('No virtual environment is currently active.')
if sys.platform in ('win32', 'win64', 'cygwin'):
pip = os.path.join(sys.prefix, 'Scripts', 'pip.exe')
else:
pip = os.path.join(sys.prefix, 'bin', 'pip')
proc = subprocess.Popen([pip, 'install', '--upgrade'] + packages, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = proc.communicate()
if out.decode('utf8') != '':
plpy.notice(out.decode('utf8'))
if proc.returncode != 0:
if err is not None:
plpy.error('Upgrade error {}:\n{}'.format(proc.returncode, err.decode('utf8')))
else:
plpy.error('Upgrade error {}.'.format(proc.returncode))
return True
$BODY$;
REVOKE ALL PRIVILEGES ON FUNCTION @[email protected]_upgrade(text[]) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION @[email protected]_upgrade(text[]) TO CURRENT_USER;
COMMENT ON FUNCTION @[email protected]_upgrade(text[]) IS 'Upgrade packages from the active Python virtual environment.';
--
-- Uninstall packages using PIP
--
CREATE OR REPLACE FUNCTION @[email protected]_uninstall(packages text[])
RETURNS boolean
LANGUAGE plpython3u
AS $BODY$
import os
import subprocess
import sys
if sys.prefix == sys.base_prefix:
plpy.error('No virtual environment is currently active.')
if sys.platform in ('win32', 'win64', 'cygwin'):
pip = os.path.join(sys.prefix, 'Scripts', 'pip.exe')
else:
pip = os.path.join(sys.prefix, 'bin', 'pip')
proc = subprocess.Popen([pip, 'uninstall', '-y'] + packages, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = proc.communicate()
if out.decode('utf8') != '':
plpy.notice(out.decode('utf8'))
if proc.returncode != 0:
if err is not None:
plpy.error('Uninstallation error {}:\n{}'.format(proc.returncode, err.decode('utf8')))
else:
plpy.error('Uninstallation error {}.'.format(proc.returncode))
return True
$BODY$;
REVOKE ALL PRIVILEGES ON FUNCTION @[email protected]_uninstall(text[]) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION @[email protected]_uninstall(text[]) TO CURRENT_USER;
COMMENT ON FUNCTION @[email protected]_uninstall(text[]) IS 'Uninstall packages from the active Python virtual environment.';
--
-- List installed packages using PIP
--
CREATE OR REPLACE FUNCTION @[email protected]_freeze()
RETURNS text[]
LANGUAGE plpython3u
AS $BODY$
import os
import subprocess
import sys
if sys.prefix == sys.base_prefix:
plpy.error('No virtual environment is currently active.')
if sys.platform in ('win32', 'win64', 'cygwin'):
pip = os.path.join(sys.prefix, 'Scripts', 'pip.exe')
else:
pip = os.path.join(sys.prefix, 'bin', 'pip')
proc = subprocess.Popen([pip, 'freeze'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = proc.communicate()
if out.decode('utf8') != '':
return out.decode('utf8').strip().split('\n')
if proc.returncode != 0:
if err is not None:
plpy.error('Freeze error {}:\n{}'.format(proc.returncode, err.decode('utf8')))
else:
plpy.error('Freeze error {}.'.format(proc.returncode))
return []
$BODY$;
REVOKE ALL PRIVILEGES ON FUNCTION @[email protected]_freeze() FROM PUBLIC;
GRANT EXECUTE ON FUNCTION @[email protected]_freeze() TO CURRENT_USER;
COMMENT ON FUNCTION @[email protected]_freeze() IS 'List packages installed in the active Python virtual environment in requirements format.';