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.

79 lines
2.5 KiB

  1. """Install platform-independent data files."""
  2. # Contributed by Bastian Kleineidam
  3. import os
  4. from shutil import Error
  5. from sysconfig import get_paths, format_value
  6. from packaging import logger
  7. from packaging.util import convert_path
  8. from packaging.command.cmd import Command
  9. class install_data(Command):
  10. description = "install platform-independent data files"
  11. user_options = [
  12. ('install-dir=', 'd',
  13. "base directory for installing data files "
  14. "(default: installation base dir)"),
  15. ('root=', None,
  16. "install everything relative to this alternate root directory"),
  17. ('force', 'f', "force installation (overwrite existing files)"),
  18. ]
  19. boolean_options = ['force']
  20. def initialize_options(self):
  21. self.install_dir = None
  22. self.outfiles = []
  23. self.data_files_out = []
  24. self.root = None
  25. self.force = False
  26. self.data_files = self.distribution.data_files
  27. self.warn_dir = True
  28. def finalize_options(self):
  29. self.set_undefined_options('install_dist',
  30. ('install_data', 'install_dir'),
  31. 'root', 'force')
  32. def run(self):
  33. self.mkpath(self.install_dir)
  34. for _file in self.data_files.items():
  35. destination = convert_path(self.expand_categories(_file[1]))
  36. dir_dest = os.path.abspath(os.path.dirname(destination))
  37. self.mkpath(dir_dest)
  38. try:
  39. out = self.copy_file(_file[0], dir_dest)[0]
  40. except Error as e:
  41. logger.warning('%s: %s', self.get_command_name(), e)
  42. out = destination
  43. self.outfiles.append(out)
  44. self.data_files_out.append((_file[0], destination))
  45. def expand_categories(self, path_with_categories):
  46. local_vars = get_paths()
  47. local_vars['distribution.name'] = self.distribution.metadata['Name']
  48. expanded_path = format_value(path_with_categories, local_vars)
  49. expanded_path = format_value(expanded_path, local_vars)
  50. if '{' in expanded_path and '}' in expanded_path:
  51. logger.warning(
  52. '%s: unable to expand %s, some categories may be missing',
  53. self.get_command_name(), path_with_categories)
  54. return expanded_path
  55. def get_source_files(self):
  56. return list(self.data_files)
  57. def get_inputs(self):
  58. return list(self.data_files)
  59. def get_outputs(self):
  60. return self.outfiles
  61. def get_resources_out(self):
  62. return self.data_files_out