Skip to content

Commit e29d29d

Browse files
author
Konstantinacc
committed
Added resume_log and create_log functions.
Function resume_log checks if there is a previous log file that needs to be continued. Covers 3 cases: Case 1: Test runs for 1st time and there is no previous log file. Case 2: Test runs for 2nd time and there is a previous log file which contains some of the urls that should be compiled and log file should be completed with the rest. Case 3: Test runs for 2nd time and there is a previous log file which contains all the urls that should be compiled and test should be run again for all urls. Function create_log dumps the test results to `log_file`.
1 parent ebb36fe commit e29d29d

File tree

1 file changed

+113
-35
lines changed

1 file changed

+113
-35
lines changed

codebender_testing/utils.py

Lines changed: 113 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -528,62 +528,136 @@ def compile_all_sketches(self, url, selector, **kwargs):
528528
assert len(sketches) > 0
529529
self.compile_sketches(sketches, **kwargs)
530530

531-
def compile_sketches(self, sketches, iframe=False, project_view=False, logfile=None, compile_type='sketch', create_report=False, comment=False):
532-
"""Compiles the sketches with URLs given by the `sketches` list.
533-
`logfile` specifies a path to a file to which test results will be
534-
logged. If it is not `None`, compile errors will not cause the test
535-
to halt, but rather be logged to the given file. `logfile` may be a time
536-
format string, which will be formatted appropriately.
537-
`iframe` specifies whether the urls pointed to by `selector` are contained
538-
within an iframe.
539-
If the `--full` argument is provided (and hence
540-
`self.run_full_compile_tests` is `True`, we do not log, and limit the
541-
number of sketches compiled to 1.
531+
def resume_log (self, logfile, compile_type, sketches):
532+
"""Resume previous log, if any. Coves 3 cases:
533+
Case 1: Test runs for 1st time and there is no previous log file.
534+
Case 2: Test runs for 2nd time and there is a previous log file which contains
535+
some of the urls that should be compiled and log file should be completed with
536+
the rest.
537+
Case 3: Test runs for 2nd time and there is a previous log file which contains
538+
all the urls that should be compiled and test should be run again for all urls.
542539
"""
543-
544-
# Log filename
540+
"""Creates a variable in which current date and time are stored."""
545541
log_time = gmtime()
546-
# Keeps the logs of each compile
542+
543+
"""Creates an empty dictionary each time that a test runs."""
547544
log_entry = {}
548545

546+
"""Creates an empty dictionary each time that a test runs."""
549547
urls_visited = {}
548+
549+
"""Calls `read_last_log` function and checks if there is a previous log file
550+
of the same compile_type (e.g. sketch). If there is, a dictionary containing
551+
`timestamp` and `log` keys with their corresponding values is returned.
552+
Otherwise, a dictionary where `timestamp` and `log` values are `None` is returned.
553+
No previous log:
554+
{'timestamp': None, 'log': None}
555+
Previous log exists:
556+
{'timestamp': '2016-02-14_10-42-17',
557+
'log': {
558+
'https://staging.codebender.cc/sketch:30360': {'success': ['Arduino Uno']},
559+
'https://staging.codebender.cc/sketch:30352': {'success': ['Arduino Uno']}
560+
}
561+
}
562+
"""
550563
last_log = read_last_log(compile_type)
551-
if compile_type != 'target_library' and last_log['log']:
552-
# resume previous compile
553-
log_time = strptime(last_log['timestamp'], '%Y-%m-%d_%H-%M-%S')
554-
log_entry = last_log['log']
555-
for url in last_log['log']:
556-
urls_visited[url] = True
557564

565+
if compile_type != 'target_library' and last_log['log']:
566+
"""Checks if `last_log[log]` has a value (is not `None`).
567+
If it has, this means that there is a log file created previously and dictionaries
568+
`log_entry` and `urls_visited` should be updated.
569+
{'timestamp': '2016-02-14_12-44-16',
570+
'log': {
571+
'https://staging.codebender.cc/sketch:30360': {'success': ['Arduino Uno']},
572+
'https://staging.codebender.cc/sketch:30352': {'success': ['Arduino Uno']}
573+
}
574+
}
575+
"""
576+
if last_log['log']:
577+
log_time = strptime(last_log['timestamp'], '%Y-%m-%d_%H-%M-%S')
578+
579+
""" Test has stopped its execution for some reason, (e.g.to avoid saucelabs timeout)
580+
and `log_entry` dictionary will be filled with the entries of `last_log[log]` values.
581+
log_entry = {'https://staging.codebender.cc/sketch:30360': {'success': ['Arduino Uno']},
582+
'https://staging.codebender.cc/sketch:30352': {'success': ['Arduino Uno']}}
583+
"""
584+
log_entry = last_log['log']
585+
586+
""" Test has stopped its execution for some reason,(e.g.to avoid saucelabs timeout)
587+
and `urls_visited` dictionary will be filled with the urls already visited when the test stopped.
588+
urls_visited = {'https://staging.codebender.cc/sketch:30360': True,
589+
'https://staging.codebender.cc/sketch:30352': True}
590+
"""
591+
for url in last_log['log']:
592+
urls_visited[url] = True
593+
594+
"""Creates an empty dictionary each time that a test runs."""
558595
urls_to_visit = []
596+
597+
"""If a test has stopped its execution for some reason,
598+
(e.g.to avoid saucelabs timeout) `urls_to_visit` dictionary will
599+
be filled with the urls that remain to be visited.
600+
urls_to_visit = {'https://staging.codebender.cc/sketch:30358',
601+
'https://staging.codebender.cc/sketch:30355'}
602+
"""
559603
for url in sketches:
560604
if url not in urls_visited:
561605
urls_to_visit.append(url)
562606

607+
"""If the urls_to_visit is empty, this means that the test was completed
608+
and should start again. `urls_to_visit` equals to all `sketches` and `log_entry`
609+
is an empty dictionary.
610+
"""
563611
if len(urls_to_visit) == 0:
564612
urls_to_visit = sketches
565613
log_entry = {}
566614
log_time = gmtime()
567615

568616
current_date = strftime('%Y-%m-%d', log_time)
569-
# Initialize DisqusWrapper
570-
disqus_wrapper = DisqusWrapper(log_time)
617+
618+
""" If `logfile` has a value and is not `None` we create `log_file`."""
571619
if logfile:
572620
log_file = strftime(logfile, log_time)
573621

622+
return (urls_to_visit, log_entry, log_file, disqus_wrapper, current_date)
623+
624+
def create_log (self, log_file, log_entry,compile_type):
625+
# Dump the test results to `log_file`.
626+
with open(log_file, 'w') as f:
627+
f.write(jsondump(log_entry))
628+
629+
630+
def compile_sketches(self, sketches, iframe=False, project_view=False, logfile=None, compile_type='sketch', create_report=False, comment=False):
631+
"""Compiles the sketches with URLs given by the `sketches` list.
632+
`logfile` specifies a path to a file to which test results will be
633+
logged. If it is not `None`, compile errors will not cause the test
634+
to halt, but rather be logged to the given file. `logfile` may be a time
635+
format string, which will be formatted appropriately.
636+
`iframe` specifies whether the urls pointed to by `selector` are contained
637+
within an iframe.
638+
If the `--full` argument is provided (and hence
639+
`self.run_full_compile_tests` is `True`, we do not log, and limit the
640+
number of sketches compiled to 1.
641+
"""
642+
643+
urls_to_visit, log_entry, log_file, log_time, current_date = self.resume_log(logfile, compile_type, sketches)
644+
645+
# Initialize DisqusWrapper.
646+
disqus_wrapper = DisqusWrapper(log_time)
647+
574648
print '\nCompiling:', len(urls_to_visit), 'sketches'
575649
total_sketches = len(urls_to_visit)
576650
tic = time.time()
577651

578652
for sketch in urls_to_visit:
579-
# Read the boards map in case current sketch/example requires a special board configuration
653+
# Read the boards map in case current sketch/example requires a special board configuration.
580654
boards = BOARDS_DB['default_boards']
581655
url_fragments = urlparse(sketch)
582656
if url_fragments.path in BOARDS_DB['special_boards']:
583657
boards = BOARDS_DB['special_boards'][url_fragments.path]
584658

585659
if len(boards) > 0:
586-
# Run Verify
660+
# Run Verify.
587661
results = self.compile_sketch(sketch, boards, iframe=iframe, project_view=project_view)
588662
else:
589663
results = [
@@ -592,22 +666,28 @@ def compile_sketches(self, sketches, iframe=False, project_view=False, logfile=N
592666
}
593667
]
594668

595-
# Used when not funning in Full mode
669+
"""If test is not running in full mode (-F option) or logfile is None
670+
no logs are produced inside /logs directory and we continue with sketches
671+
compilation.
672+
"""
596673
if logfile is None or not self.run_full_compile_tests:
597674
toc = time.time()
598675
continue
599676

600-
# Register current URL into log
677+
# Register current URL into log.
601678
if sketch not in log_entry:
602679
log_entry[sketch] = {}
680+
print "\n"
681+
print "sketch"
682+
print "log_entry[" + sketch +"]", log_entry[sketch]
603683

604684
test_status = '.'
605-
# Log the compilation results
685+
686+
# Log the compilation results.
606687
openFailFlag = False
607688
for result in results:
608689
if result['status'] in ['success', 'fail', 'error'] and result['status'] not in log_entry[sketch]:
609690
log_entry[sketch][result['status']] = []
610-
611691
if result['status'] == 'success':
612692
log_entry[sketch]['success'].append(result['board'])
613693
elif result['status'] == 'fail':
@@ -627,13 +707,11 @@ def compile_sketches(self, sketches, iframe=False, project_view=False, logfile=N
627707
log_entry[sketch]['unsupported'] = True
628708
test_status = 'U'
629709

630-
# Update Disqus comments
631-
if compile_type in ['library', 'target_library'] and comment:
632-
log_entry = disqus_wrapper.update_comment(sketch, results, current_date, log_entry, openFailFlag, total_sketches)
710+
self.create_log(log_file,log_entry, compile_type)
633711

634-
# Dump the test results to `logfile`.
635-
with open(log_file, 'w') as f:
636-
f.write(jsondump(log_entry))
712+
# Update Disqus comments.
713+
if comment and compile_type in ['library', 'target_library']:
714+
log_entry = disqus_wrapper.update_comment(sketch, results, current_date, log_entry, openFailFlag, total_sketches)
637715

638716
# Display progress
639717
sys.stdout.write(test_status)
@@ -645,7 +723,7 @@ def compile_sketches(self, sketches, iframe=False, project_view=False, logfile=N
645723
print 'Test duration:', int(toc - tic), 'sec'
646724
return
647725

648-
# Generate a report if requested
726+
# Generate a report if requested.
649727
if compile_type != 'target_library' and create_report:
650728
report_creator(compile_type, log_entry, log_file)
651729
print '\nTest duration:', int(toc - tic), 'sec'

0 commit comments

Comments
 (0)