forked from qiniu/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsandbox_common.py
More file actions
60 lines (49 loc) · 1.33 KB
/
Copy pathsandbox_common.py
File metadata and controls
60 lines (49 loc) · 1.33 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
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
from qiniu.services.sandbox import Sandbox
from qiniu.services.sandbox.config import (
env,
load_dotenv_if_present,
required_env,
sandbox_client,
sandbox_template,
)
__all__ = [
'cleanup_sandbox',
'create_sandbox',
'env',
'load_example_env',
'required_env',
'run_example',
'sandbox_client',
'sandbox_template',
]
def load_example_env():
root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
load_dotenv_if_present(
os.path.join(root, '.env'),
os.path.join(os.getcwd(), '.env'),
)
def create_sandbox(**options):
client = options.pop('client', None) or sandbox_client()
template = options.pop('template', sandbox_template())
options.setdefault('timeout', 300)
sandbox = Sandbox.create(template, client=client, **options)
print('Sandbox created:', sandbox.sandbox_id)
return sandbox
def cleanup_sandbox(sandbox):
if sandbox is None:
return
try:
sandbox.kill()
print('Sandbox killed:', sandbox.sandbox_id)
except Exception as err:
print('Failed to kill sandbox:', sandbox.sandbox_id, err)
def run_example(fn):
load_example_env()
try:
fn()
except Exception as err:
print(err)
raise