-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
regression_utils.py
59 lines (47 loc) · 1.95 KB
/
regression_utils.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
import os
import sys
from gcloud import datastore
from gcloud import storage
# Defaults from shell environ. May be None.
PROJECT_ID = os.getenv('GCLOUD_TESTS_PROJECT_ID')
DATASET_ID = os.getenv('GCLOUD_TESTS_DATASET_ID')
CLIENT_EMAIL = os.getenv('GCLOUD_TESTS_CLIENT_EMAIL')
KEY_FILENAME = os.getenv('GCLOUD_TESTS_KEY_FILE')
CACHED_RETURN_VALS = {}
ENVIRON_ERROR_MSG = """\
To run the regression tests, you need to set some environment variables.
Please check the Contributing guide for instructions.
"""
def get_environ(require_datastore=False, require_storage=False):
if require_datastore:
if DATASET_ID is None or CLIENT_EMAIL is None or KEY_FILENAME is None:
print >> sys.stderr, ENVIRON_ERROR_MSG
sys.exit(1)
if require_storage:
if PROJECT_ID is None or CLIENT_EMAIL is None or KEY_FILENAME is None:
print >> sys.stderr, ENVIRON_ERROR_MSG
sys.exit(1)
return {
'project_id': PROJECT_ID,
'dataset_id': DATASET_ID,
'client_email': CLIENT_EMAIL,
'key_filename': KEY_FILENAME,
}
def get_dataset():
environ = get_environ(require_datastore=True)
get_dataset_args = (environ['dataset_id'], environ['client_email'],
environ['key_filename'])
key = ('get_dataset', get_dataset_args)
if key not in CACHED_RETURN_VALS:
# Cache return value for the environment.
CACHED_RETURN_VALS[key] = datastore.get_dataset(*get_dataset_args)
return CACHED_RETURN_VALS[key]
def get_storage_connection():
environ = get_environ(require_storage=True)
get_connection_args = (environ['project_id'], environ['client_email'],
environ['key_filename'])
key = ('get_storage_connection', get_connection_args)
if key not in CACHED_RETURN_VALS:
# Cache return value for the environment.
CACHED_RETURN_VALS[key] = storage.get_connection(*get_connection_args)
return CACHED_RETURN_VALS[key]