util.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. """Old stuff
  2. Prefer io.py now.
  3. """
  4. import logging
  5. import os
  6. import time
  7. from .io import (cd, touch, mkdirs, syscall as system)
  8. LOG = logging.getLogger()
  9. def run(script_fn):
  10. cwd, basename = os.path.split(script_fn)
  11. with cd(cwd):
  12. system('/bin/bash {}'.format(basename))
  13. def rmdirs(path):
  14. if os.path.isdir(path):
  15. if len(path) < 20 and 'home' in path:
  16. LOG.error('Refusing to rm {!r} since it might be your homedir.'.format(path))
  17. return
  18. cmd = 'rm -rf {}'.format(path)
  19. system(cmd)
  20. def logger_record(func):
  21. def wrapper(*args, **kwargs):
  22. LOG.info("====>Begin executing function: {}".format(func.__name__))
  23. if args:
  24. LOG.info("====>args={}".format(args))
  25. if kwargs:
  26. LOG.info("====>kwargs={}".format(kwargs))
  27. # function execution
  28. t_start = time.time()
  29. func(*args, **kwargs)
  30. t_end = time.time()
  31. LOG.info("====>End executing function: {}, time cost: {} second."
  32. .format(func.__name__, (t_end-t_start)))
  33. return wrapper