@@ -31,8 +31,9 @@ def log_test_failure_data(test, test_logpath, driver, browser):
3131 log_file = codecs .open (basic_file_path , "w+" , "utf-8" )
3232 last_page = get_last_page (driver )
3333 data_to_save = []
34- data_to_save .append ("Last_Page: %s" % last_page )
35- data_to_save .append ("Browser: %s " % browser )
34+ data_to_save .append ("Last Page: %s" % last_page )
35+ data_to_save .append (" Browser: %s" % browser )
36+ data_to_save .append ("Timestamp: %s" % int (time .time ()))
3637 if sys .version_info [0 ] >= 3 and hasattr (test , '_outcome' ):
3738 if test ._outcome .errors :
3839 try :
@@ -104,6 +105,47 @@ def get_html_source_with_base_href(driver, page_source):
104105 return ''
105106
106107
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+
107149def log_folder_setup (log_path , archive_logs = False ):
108150 """ Handle Logging """
109151 if log_path .endswith ("/" ):
@@ -115,30 +157,23 @@ def log_folder_setup(log_path, archive_logs=False):
115157 pass # Should only be reachable during multi-threaded runs
116158 else :
117159 archived_folder = "%s/../archived_logs/" % log_path
160+ archived_folder = os .path .realpath (archived_folder ) + '/'
118161 if not os .path .exists (archived_folder ):
119162 try :
120163 os .makedirs (archived_folder )
121164 except Exception :
122165 pass # Should only be reachable during multi-threaded runs
123- if not "" .join (sys .argv ) == "-c" :
124- # Only move log files if the test run is not multi-threaded.
125- # (Running tests with "-n NUM" will create threads that only
126- # have "-c" in the sys.argv list. Easy to catch.)
127- archived_logs = "%slogs_%s" % (
128- archived_folder , int (time .time ()))
129- if "_logs" not in log_path :
130- # Don't move files in a custom-named log folder (in case
131- # the user specifed a folder with important files in it)
132- # unless the folder name contains "_logs".
133- # The default name for the log folder is "latest_logs".
134- return
166+ archived_logs = "%slogs_%s" % (
167+ archived_folder , int (time .time ()))
168+
169+ if len (os .listdir (log_path )) > 0 :
135170 shutil .move (log_path , archived_logs )
136171 os .makedirs (log_path )
137172 if not settings .ARCHIVE_EXISTING_LOGS and not archive_logs :
138173 shutil .rmtree (archived_logs )
139- elif len (os .listdir (archived_logs )) == 0 :
140- # Don't archive an empty directory
141- shutil .rmtree (archived_logs )
142174 else :
143- # Logs are saved/archived
144- 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