@@ -105,6 +105,47 @@ def get_html_source_with_base_href(driver, page_source):
105
105
return ''
106
106
107
107
108
+ def copytree (src , dst , symlinks = False , ignore = None ):
109
+ if not os .path .exists (dst ):
110
+ os .makedirs (dst )
111
+ for item in os .listdir (src ):
112
+ s = os .path .join (src , item )
113
+ d = os .path .join (dst , item )
114
+ if os .path .isdir (s ):
115
+ copytree (s , d , symlinks , ignore )
116
+ else :
117
+ if not os .path .exists (d ) or (
118
+ os .stat (s ).st_mtime - os .stat (d ).st_mtime > 1 ):
119
+ shutil .copy2 (s , d )
120
+
121
+
122
+ def archive_logs_if_set (log_path , archive_logs = False ):
123
+ """ Handle Logging """
124
+ if "-n" in sys .argv or "" .join (sys .argv ) == "-c" :
125
+ return # Skip if multithreaded
126
+ if log_path .endswith ("/" ):
127
+ log_path = log_path [:- 1 ]
128
+ if not os .path .exists (log_path ):
129
+ try :
130
+ os .makedirs (log_path )
131
+ except Exception :
132
+ pass # Only reachable during multi-threaded runs
133
+ else :
134
+ if settings .ARCHIVE_EXISTING_LOGS or archive_logs :
135
+ if len (os .listdir (log_path )) > 0 :
136
+ archived_folder = "%s/../archived_logs/" % log_path
137
+ archived_folder = os .path .realpath (archived_folder ) + '/'
138
+ log_path = os .path .realpath (log_path ) + '/'
139
+ if not os .path .exists (archived_folder ):
140
+ try :
141
+ os .makedirs (archived_folder )
142
+ except Exception :
143
+ pass # Only reachable during multi-threaded runs
144
+ time_id = str (int (time .time ()))
145
+ archived_logs = "%slogs_%s" % (archived_folder , time_id )
146
+ copytree (log_path , archived_logs )
147
+
148
+
108
149
def log_folder_setup (log_path , archive_logs = False ):
109
150
""" Handle Logging """
110
151
if log_path .endswith ("/" ):
@@ -116,30 +157,23 @@ def log_folder_setup(log_path, archive_logs=False):
116
157
pass # Should only be reachable during multi-threaded runs
117
158
else :
118
159
archived_folder = "%s/../archived_logs/" % log_path
160
+ archived_folder = os .path .realpath (archived_folder ) + '/'
119
161
if not os .path .exists (archived_folder ):
120
162
try :
121
163
os .makedirs (archived_folder )
122
164
except Exception :
123
165
pass # Should only be reachable during multi-threaded runs
124
- if not "" .join (sys .argv ) == "-c" :
125
- # Only move log files if the test run is not multi-threaded.
126
- # (Running tests with "-n NUM" will create threads that only
127
- # have "-c" in the sys.argv list. Easy to catch.)
128
- archived_logs = "%slogs_%s" % (
129
- archived_folder , int (time .time ()))
130
- if "_logs" not in log_path :
131
- # Don't move files in a custom-named log folder (in case
132
- # the user specifed a folder with important files in it)
133
- # unless the folder name contains "_logs".
134
- # The default name for the log folder is "latest_logs".
135
- return
166
+ archived_logs = "%slogs_%s" % (
167
+ archived_folder , int (time .time ()))
168
+
169
+ if len (os .listdir (log_path )) > 0 :
136
170
shutil .move (log_path , archived_logs )
137
171
os .makedirs (log_path )
138
172
if not settings .ARCHIVE_EXISTING_LOGS and not archive_logs :
139
173
shutil .rmtree (archived_logs )
140
- elif len (os .listdir (archived_logs )) == 0 :
141
- # Don't archive an empty directory
142
- shutil .rmtree (archived_logs )
143
174
else :
144
- # Logs are saved/archived
145
- pass
175
+ if ("-n" in sys .argv or "" .join (sys .argv ) == "-c" ):
176
+ # Logs are saved/archived now if tests are multithreaded
177
+ pass
178
+ else :
179
+ shutil .rmtree (archived_logs ) # (Archive test run later)
0 commit comments