cp.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import os
  2. from _common import sort_by_keywords
  3. def oncopy(unit, *args):
  4. keywords = {
  5. 'RESULT': 1,
  6. 'KEEP_DIR_STRUCT': 0,
  7. 'DESTINATION': 1,
  8. 'FROM': 1,
  9. 'OUTPUT_INCLUDES': -1,
  10. 'AUTO': 0,
  11. 'WITH_CONTEXT': 0,
  12. }
  13. flat_args, spec_args = sort_by_keywords(keywords, args)
  14. dest_dir = spec_args['DESTINATION'][0] if 'DESTINATION' in spec_args else ''
  15. from_dir = spec_args['FROM'][0] if 'FROM' in spec_args else ''
  16. output_includes = spec_args['OUTPUT_INCLUDES'] if 'OUTPUT_INCLUDES' in spec_args else None
  17. keep_struct = 'KEEP_DIR_STRUCT' in spec_args
  18. save_in_var = 'RESULT' in spec_args
  19. auto = 'AUTO' in spec_args
  20. with_context = 'WITH_CONTEXT' in spec_args
  21. targets = []
  22. for source in flat_args:
  23. rel_path = ''
  24. path_list = source.split(os.sep)
  25. filename = path_list[-1]
  26. if keep_struct:
  27. if path_list[:-1]:
  28. rel_path = os.path.join(*path_list[:-1])
  29. source_path = os.path.join(from_dir, rel_path, filename)
  30. target_path = os.path.join(dest_dir, rel_path, filename)
  31. if save_in_var:
  32. targets.append(target_path)
  33. unit.oncopy_file(
  34. [source_path, target_path]
  35. + (['OUTPUT_INCLUDES'] + output_includes if output_includes else [])
  36. + (['OUTPUT_INCLUDES', source_path] if with_context else [])
  37. + (['AUTO'] if auto else [])
  38. )
  39. if save_in_var:
  40. unit.set([spec_args["RESULT"][0], " ".join(targets)])