Created
March 7, 2012 02:35
-
-
Save lost-theory/1990526 to your computer and use it in GitHub Desktop.
decorate fabric "lcd" with build step, build #, and time elapsed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from fabric.api import local, lcd | |
_step = 1 | |
def format_step(msg1, step, desc, msg2, fillchar=">", cols=80): | |
out = "%s %s %d (%s) %s" % ( | |
fillchar*3, | |
msg1, | |
step, | |
desc, | |
msg2, | |
) | |
return out + " " + fillchar*(cols-len(out)) | |
class step(object): | |
def __init__(self, path, desc): | |
self.desc = desc | |
self.start = 0 | |
self.path = path | |
self.ctxmgr = lcd(path) | |
def __enter__(self, *args, **kwargs): | |
global _step | |
print format_step("START STEP", _step, self.desc, "at dir %r" % self.path) | |
self.start = time.time() | |
return self.ctxmgr.__enter__(*args, **kwargs) | |
def __exit__(self, *args, **kwargs): | |
global _step | |
ret = self.ctxmgr.__exit__(*args, **kwargs) | |
print format_step("FINISH STEP", _step, self.desc, "took %.2fs" % (time.time()-self.start), fillchar="<") + "\n\n" | |
_step += 1 | |
return ret | |
if __name__ == "__main__": | |
with step("ws", "clean workspace & clone"): | |
local("pwd && ls") | |
time.sleep(0.9) | |
local("date") | |
with step("ws/something/tests", "run unit tests"): | |
local("pwd && ls") | |
time.sleep(0.9) | |
local("date") | |
with step("ws/something", "run pylint"): | |
local("pwd && ls") | |
time.sleep(0.9) | |
local("date") | |
#output: | |
""" | |
$ python ctx_deco.py | |
>>> START STEP 1 (clean workspace & clone) at dir 'ws' >>>>>>>>>>>>>>>>>>>>>>>>>> | |
[localhost] local: pwd && ls | |
/Users/steven/ws | |
something | |
[localhost] local: date | |
Tue Mar 6 18:38:04 PST 2012 | |
<<< FINISH STEP 1 (clean workspace & clone) took 0.91s <<<<<<<<<<<<<<<<<<<<<<<<<< | |
>>> START STEP 2 (run unit tests) at dir 'ws/something/tests' >>>>>>>>>>>>>>>>>>> | |
[localhost] local: pwd && ls | |
/Users/steven/ws/something/tests | |
[localhost] local: date | |
Tue Mar 6 18:38:05 PST 2012 | |
<<< FINISH STEP 2 (run unit tests) took 0.91s <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | |
>>> START STEP 3 (run pylint) at dir 'ws/something' >>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
[localhost] local: pwd && ls | |
/Users/steven/ws/something | |
tests | |
[localhost] local: date | |
Tue Mar 6 18:38:06 PST 2012 | |
<<< FINISH STEP 3 (run pylint) took 0.91s <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment