Skip to content

Instantly share code, notes, and snippets.

@kelvinabrokwa
Last active March 28, 2019 20:09
Show Gist options
  • Save kelvinabrokwa/29d6fab8ddda9bb9c3e042f5aae6846f to your computer and use it in GitHub Desktop.
Save kelvinabrokwa/29d6fab8ddda9bb9c3e042f5aae6846f to your computer and use it in GitHub Desktop.
stages:
- duration: 1.9788742065429688e-05
end: 1553803633.735767
name: build android
start: 1553803633.735747
sub_stages:
- duration: 3.814697265625e-06
end: 1553803633.735756
name: build
start: 1553803633.735752
- duration: 9.5367431640625e-07
end: 1553803633.735765
name: sign
start: 1553803633.735764
- duration: 4.0531158447265625e-06
end: 1553803633.735774
error: Firmware build error
name: build firmware
start: 1553803633.73577
import yaml
from time import time
from contextlib import contextmanager
class Report(object):
def __init__(self):
self.stages = []
def add_stage(self, stage):
self.stages.append(stage)
def dump(self):
return {
"stages": [s.dict for s in self.stages]
}
class Stage(object):
def __init__(self, name):
self.name = name
self.sub_stages = []
self.exception = None
self.start_time = None
self.end_time = None
def add_stage(self, stage):
self.sub_stages.append(stage)
def start(self):
self.start_time = time()
def end(self):
self.end_time = time()
def error(self, exception):
self.exception = exception
@property
def duration(self):
if self.start_time is None or self.end_time is None:
return None
return self.end_time - self.start_time
@property
def dict(self):
d = {
"name": self.name,
"start": self.start_time,
"end": self.end_time,
"duration": self.duration
}
if self.exception is not None:
d["error"] = str(self.exception)
if len(self.sub_stages) > 0:
d["sub_stages"] = [s.dict for s in self.sub_stages]
return d
@contextmanager
def stage(name, parent):
s = Stage(name)
parent.add_stage(s)
s.start()
try:
yield s
except Exception as e:
s.error(e)
finally:
s.end()
#
# The build script
#
def build():
report = Report()
with stage("build android", report) as build_android_stage:
with stage("build", build_android_stage):
pass
with stage("sign", build_android_stage):
pass
try:
with stage("build firmware", report):
raise Exception("Firmware build error")
except Exception:
pass
with open("picard.log.yaml", "w") as f:
f.write(yaml.dump(report.dump(), default_flow_style=False))
if __name__ == "__main__":
build()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment