install_data.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """distutils.command.install_data
  2. Implements the Distutils 'install_data' command, for installing
  3. platform-independent data files."""
  4. # contributed by Bastian Kleineidam
  5. import os
  6. from distutils.core import Command
  7. from distutils.util import change_root, convert_path
  8. class install_data(Command):
  9. description = "install data files"
  10. user_options = [
  11. ('install-dir=', 'd',
  12. "base directory for installing data files "
  13. "(default: installation base dir)"),
  14. ('root=', None,
  15. "install everything relative to this alternate root directory"),
  16. ('force', 'f', "force installation (overwrite existing files)"),
  17. ]
  18. boolean_options = ['force']
  19. def initialize_options(self):
  20. self.install_dir = None
  21. self.outfiles = []
  22. self.root = None
  23. self.force = 0
  24. self.data_files = self.distribution.data_files
  25. self.warn_dir = 1
  26. def finalize_options(self):
  27. self.set_undefined_options('install',
  28. ('install_data', 'install_dir'),
  29. ('root', 'root'),
  30. ('force', 'force'),
  31. )
  32. def run(self):
  33. self.mkpath(self.install_dir)
  34. for f in self.data_files:
  35. if isinstance(f, str):
  36. # it's a simple file, so copy it
  37. f = convert_path(f)
  38. if self.warn_dir:
  39. self.warn("setup script did not provide a directory for "
  40. "'%s' -- installing right in '%s'" %
  41. (f, self.install_dir))
  42. (out, _) = self.copy_file(f, self.install_dir)
  43. self.outfiles.append(out)
  44. else:
  45. # it's a tuple with path to install to and a list of files
  46. dir = convert_path(f[0])
  47. if not os.path.isabs(dir):
  48. dir = os.path.join(self.install_dir, dir)
  49. elif self.root:
  50. dir = change_root(self.root, dir)
  51. self.mkpath(dir)
  52. if f[1] == []:
  53. # If there are no files listed, the user must be
  54. # trying to create an empty directory, so add the
  55. # directory to the list of output files.
  56. self.outfiles.append(dir)
  57. else:
  58. # Copy files, adding them to the list of output files.
  59. for data in f[1]:
  60. data = convert_path(data)
  61. (out, _) = self.copy_file(data, dir)
  62. self.outfiles.append(out)
  63. def get_inputs(self):
  64. return self.data_files or []
  65. def get_outputs(self):
  66. return self.outfiles