IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

steps.py 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import hashlib
  2. import json
  3. from jail import jail_run
  4. import shutil
  5. def filehash(fname):
  6. h = hashlib.sha256()
  7. with open(fname, 'rb') as f:
  8. while True:
  9. data = f.read(1024*1024*4)
  10. if not data:
  11. break
  12. h.update(data)
  13. res = h.hexdigest()
  14. return res
  15. class RunStep(object):
  16. def __init__(self, spec):
  17. if not isinstance(spec, list) and \
  18. not isinstance(spec, str):
  19. raise ValueError('Run spec must be a list or a string')
  20. self.spec = spec
  21. def hash(self, base):
  22. res = hashlib.sha256(
  23. json.dumps(( base, self.spec ))
  24. .encode('utf-8')).hexdigest()
  25. return res
  26. def execute(self, path):
  27. spec = self.spec
  28. if isinstance(spec, list)
  29. spec = ' && ' .join(self.spec)
  30. jail_run(path, spec)
  31. class CopyStep(object):
  32. def __init__(self, spec):
  33. if not isinstance(spec, list):
  34. raise ValueError('CopyStep spec should be a list')
  35. self.spec = spec
  36. def hash(self, base):
  37. if len(self.spec) == 0:
  38. fh = []
  39. elif isinstance(self.spec[0], list):
  40. fh = list(map(lambda a: filehash(a[0]), self.spec))
  41. else:
  42. fh = [ filehash(self.spec[0]) ]
  43. res = hashlib.sha256(
  44. json.dumps(( base, fh, self.spec ))
  45. .encode('utf-8')).hexdigest()
  46. return res
  47. def execute(self, path):
  48. lst = [ self.spec ] \
  49. if not isinstance(self.spec[0], list) \
  50. else self.spec
  51. for a in lst:
  52. shutil.copyfile(a[0], os.path.join(path, a[1]))
  53. def create_step(spec):
  54. if not isinstance(spec, dict):
  55. raise ValueError('Step specification must be a dictionary')
  56. if 'copy' in spec:
  57. return CopyStep(spec['copy'])
  58. elif 'run' in spec:
  59. return RunStep(spec['run'])
  60. raise ValueError('Unrecognized step spec: ' + json.dumps(spec))