-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
102 lines (75 loc) · 2.75 KB
/
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
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
"""Common useful utils."""
# pylint: disable=protected-access
import os
from shutil import copy
from itertools import count
from datetime import datetime
RUNTIME_ORDER = '-start_time'
DATE_TIME_FORMAT = '%y.%m.%d_%H_%M_%S'
def safe_copy(src_file, dst_file):
"""Copy src_file to temp file and then rename it to dst_file.
Args:
src_file (str): src file path
dst_file (str): dst file path
"""
temp_dst_file = '%s.temp' % dst_file
copy(src_file, temp_dst_file)
os.rename(temp_dst_file, dst_file)
def get_test_index(test):
"""Get the index of the test under its parent or None if it's the top.
The returned index starts count at 1.
Returns:
number. test's index under its parent.
"""
if test.parent is None:
return None
return test.parent._tests.index(test) + 1
def get_work_dir(base_dir, test_name, test_item):
"""Get the working directory for the given test.
Creates a work directory for by joining the given base directory,
the test name and the current date time string. If the work directory
already exists the new one will get the copy number extension.
Args:
base_dir (str): base directory path.
test_name (str): test name.
test_item (object): test instance.
Returns:
str. path of the working directory.
"""
if test_item is None:
basic_work_dir = test_name
else:
test_index = get_test_index(test_item)
if test_index is None:
basic_work_dir = datetime.strftime(datetime.now(),
DATE_TIME_FORMAT)
else:
basic_work_dir = "%d_%s" % (test_index, test_name)
basic_work_dir = os.path.join(base_dir, basic_work_dir)
work_dir = basic_work_dir
copy_count = count()
while os.path.exists(work_dir):
work_dir = basic_work_dir + '(%s)' % copy_count.next()
os.makedirs(work_dir)
return work_dir
def get_class_fields(cls, field_type):
"""Get all fields of the class that inherit from the given type.
* This method searches also in the parent classes.
* Fields of sons override fields of parents.
* Ignores fields starting with '_'.
Args:
cls (type): class to search.
field_type (type): field type to find.
Yields:
tuple. all found (field name, field value).
"""
if cls is object:
return
for parent_class in cls.__bases__:
for (field_name, field) in get_class_fields(parent_class, field_type):
yield (field_name, field)
for field_name in cls.__dict__:
if not field_name.startswith("_"):
field = getattr(cls, field_name)
if isinstance(field, field_type):
yield (field_name, field)