-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcontext.py
92 lines (83 loc) · 2.71 KB
/
context.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
# -*- coding: UTF-8 -*-
"""Preparing the tests to be executed."""
import sys
import os
import struct
import tempfile
import pkgutil
import tempfile
import zipfile
# adding the project folder to support running test files individually and
# from the IDE
sys.path.insert(0, os.path.abspath(
os.path.join(os.path.dirname(__file__), '..')))
try:
from registrant import registrant
except BaseException:
import registrant # noqa: F401
temp_dir = tempfile.gettempdir()
for gdb_zip in [r'data\Adv_ogr_gdb.zip', r'data\Basic_ogr_gdb.zip']:
zip_ref = zipfile.ZipFile(gdb_zip, 'r')
zip_ref.extractall(temp_dir)
zip_ref.close()
NO_OGR_ENV_MESSAGE = """
Running tests with Python installation with no ogr available"""
NO_ARCPY_ENV_MESSAGE = """
Running tests with Python installation with no arcpy available"""
TEST_CONFIG = {
'Basic': {
'name': 'Basic',
'json_results': r'data\Basic.json',
'xml_schema': r'data\XmlBasicSchema.xml',
},
'Advanced': {
'name': 'Adv',
'json_results': r'data\Adv.json',
'xml_schema': r'data\XmlAdvSchema.xml',
},
'Basic_ogr': {
'name': 'Basic_ogr',
'json_results': r'data\Basic_ogr.json',
'ogr_geodatabase': os.path.join(temp_dir, 'Basic_ogr.gdb'),
},
'Advanced_ogr': {
'name': 'Adv_ogr',
'json_results': r'data\Adv_ogr.json',
'ogr_geodatabase': os.path.join(temp_dir, 'Adv_ogr.gdb'),
},
'Complete': {
'name': 'Complete',
'json_results': r'data\Compl.json',
'xml_schema': r'data\XmlComplSchema.xml',
},
}
PYTHON_VERSION = '_{version}_{struct_size}bit'.format(
version=sys.version.split(' ')[0],
struct_size=str(struct.calcsize('P') * 8),
)
# ----------------------------------------------------------------------
def prepare_test(test_type):
"""Prepare the geodatabase data for running tests on."""
cfg = TEST_CONFIG[test_type]
test_type = cfg['name']
out_report_folder = os.path.join(tempfile.gettempdir(), test_type)
if not os.path.exists(out_report_folder):
os.mkdir(out_report_folder)
arcpy_loader = pkgutil.find_loader('arcpy')
if arcpy_loader:
import arcpy
arcpy.env.overwriteOutput = True
xml_schema = cfg['xml_schema']
in_gdb = arcpy.CreateFileGDB_management(
out_folder_path=out_report_folder,
out_name=test_type,
).getOutput(0)
arcpy.ImportXMLWorkspaceDocument_management(
target_geodatabase=in_gdb,
in_file=xml_schema,
import_type='SCHEMA_ONLY',
)
else:
in_gdb = cfg['ogr_geodatabase']
json_results = cfg['json_results']
return (in_gdb, out_report_folder, json_results)