install_data.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. # This module should be kept compatible with Python 2.1.
  6. __revision__ = "$Id: install_data.py 37828 2004-11-10 22:23:15Z loewis $"
  7. import os
  8. from types import StringType
  9. from distutils.core import Command
  10. from distutils.util import change_root, convert_path
  11. class install_data (Command):
  12. description = "install data files"
  13. user_options = [
  14. ('install-dir=', 'd',
  15. "base directory for installing data files "
  16. "(default: installation base dir)"),
  17. ('root=', None,
  18. "install everything relative to this alternate root directory"),
  19. ('force', 'f', "force installation (overwrite existing files)"),
  20. ]
  21. boolean_options = ['force']
  22. def initialize_options (self):
  23. self.install_dir = None
  24. self.outfiles = []
  25. self.root = None
  26. self.force = 0
  27. self.data_files = self.distribution.data_files
  28. self.warn_dir = 1
  29. def finalize_options (self):
  30. self.set_undefined_options('install',
  31. ('install_data', 'install_dir'),
  32. ('root', 'root'),
  33. ('force', 'force'),
  34. )
  35. def run (self):
  36. self.mkpath(self.install_dir)
  37. for f in self.data_files:
  38. if type(f) is StringType:
  39. # it's a simple file, so copy it
  40. f = convert_path(f)
  41. if self.warn_dir:
  42. self.warn("setup script did not provide a directory for "
  43. "'%s' -- installing right in '%s'" %
  44. (f, self.install_dir))
  45. (out, _) = self.copy_file(f, self.install_dir)
  46. self.outfiles.append(out)
  47. else:
  48. # it's a tuple with path to install to and a list of files
  49. dir = convert_path(f[0])
  50. if not os.path.isabs(dir):
  51. dir = os.path.join(self.install_dir, dir)
  52. elif self.root:
  53. dir = change_root(self.root, dir)
  54. self.mkpath(dir)
  55. if f[1] == []:
  56. # If there are no files listed, the user must be
  57. # trying to create an empty directory, so add the
  58. # directory to the list of output files.
  59. self.outfiles.append(dir)
  60. else:
  61. # Copy files, adding them to the list of output files.
  62. for data in f[1]:
  63. data = convert_path(data)
  64. (out, _) = self.copy_file(data, dir)
  65. self.outfiles.append(out)
  66. def get_inputs (self):
  67. return self.data_files or []
  68. def get_outputs (self):
  69. return self.outfiles