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!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.4KB

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