cp.py 1.0 KB

123456789101112131415161718192021222324252627282930
  1. import os
  2. from _common import sort_by_keywords
  3. def oncopy(unit, *args):
  4. keywords = {'RESULT': 1, 'KEEP_DIR_STRUCT': 0, 'DESTINATION': 1, 'FROM': 1}
  5. flat_args, spec_args = sort_by_keywords(keywords, args)
  6. dest_dir = spec_args['DESTINATION'][0] if 'DESTINATION' in spec_args else ''
  7. from_dir = spec_args['FROM'][0] if 'FROM' in spec_args else ''
  8. keep_struct = 'KEEP_DIR_STRUCT' in spec_args
  9. save_in_var = 'RESULT' in spec_args
  10. targets = []
  11. for source in flat_args:
  12. rel_path = ''
  13. path_list = source.split(os.sep)
  14. filename = path_list[-1]
  15. if keep_struct:
  16. if path_list[:-1]:
  17. rel_path = os.path.join(*path_list[:-1])
  18. source_path = os.path.join(from_dir, rel_path, filename)
  19. target_path = os.path.join(dest_dir, rel_path, filename)
  20. if save_in_var:
  21. targets.append(target_path)
  22. unit.oncopy_file([source_path, target_path])
  23. if save_in_var:
  24. unit.set([spec_args["RESULT"][0], " ".join(targets)])