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.
 
 
 
 

151 lines
5.0 KiB

  1. from distutils.util import convert_path
  2. from distutils import log
  3. from distutils.errors import DistutilsOptionError
  4. import distutils
  5. import os
  6. from setuptools import Command
  7. __all__ = ['config_file', 'edit_config', 'option_base', 'setopt']
  8. def config_file(kind="local"):
  9. """Get the filename of the distutils, local, global, or per-user config
  10. `kind` must be one of "local", "global", or "user"
  11. """
  12. if kind == 'local':
  13. return 'setup.cfg'
  14. if kind == 'global':
  15. return os.path.join(
  16. os.path.dirname(distutils.__file__), 'distutils.cfg'
  17. )
  18. if kind == 'user':
  19. dot = os.name == 'posix' and '.' or ''
  20. return os.path.expanduser(convert_path("~/%spydistutils.cfg" % dot))
  21. raise ValueError(
  22. "config_file() type must be 'local', 'global', or 'user'", kind
  23. )
  24. def edit_config(filename, settings, dry_run=False):
  25. """Edit a configuration file to include `settings`
  26. `settings` is a dictionary of dictionaries or ``None`` values, keyed by
  27. command/section name. A ``None`` value means to delete the entire section,
  28. while a dictionary lists settings to be changed or deleted in that section.
  29. A setting of ``None`` means to delete that setting.
  30. """
  31. from setuptools.compat import ConfigParser
  32. log.debug("Reading configuration from %s", filename)
  33. opts = ConfigParser.RawConfigParser()
  34. opts.read([filename])
  35. for section, options in settings.items():
  36. if options is None:
  37. log.info("Deleting section [%s] from %s", section, filename)
  38. opts.remove_section(section)
  39. else:
  40. if not opts.has_section(section):
  41. log.debug("Adding new section [%s] to %s", section, filename)
  42. opts.add_section(section)
  43. for option, value in options.items():
  44. if value is None:
  45. log.debug(
  46. "Deleting %s.%s from %s",
  47. section, option, filename
  48. )
  49. opts.remove_option(section, option)
  50. if not opts.options(section):
  51. log.info("Deleting empty [%s] section from %s",
  52. section, filename)
  53. opts.remove_section(section)
  54. else:
  55. log.debug(
  56. "Setting %s.%s to %r in %s",
  57. section, option, value, filename
  58. )
  59. opts.set(section, option, value)
  60. log.info("Writing %s", filename)
  61. if not dry_run:
  62. with open(filename, 'w') as f:
  63. opts.write(f)
  64. class option_base(Command):
  65. """Abstract base class for commands that mess with config files"""
  66. user_options = [
  67. ('global-config', 'g',
  68. "save options to the site-wide distutils.cfg file"),
  69. ('user-config', 'u',
  70. "save options to the current user's pydistutils.cfg file"),
  71. ('filename=', 'f',
  72. "configuration file to use (default=setup.cfg)"),
  73. ]
  74. boolean_options = [
  75. 'global-config', 'user-config',
  76. ]
  77. def initialize_options(self):
  78. self.global_config = None
  79. self.user_config = None
  80. self.filename = None
  81. def finalize_options(self):
  82. filenames = []
  83. if self.global_config:
  84. filenames.append(config_file('global'))
  85. if self.user_config:
  86. filenames.append(config_file('user'))
  87. if self.filename is not None:
  88. filenames.append(self.filename)
  89. if not filenames:
  90. filenames.append(config_file('local'))
  91. if len(filenames) > 1:
  92. raise DistutilsOptionError(
  93. "Must specify only one configuration file option",
  94. filenames
  95. )
  96. self.filename, = filenames
  97. class setopt(option_base):
  98. """Save command-line options to a file"""
  99. description = "set an option in setup.cfg or another config file"
  100. user_options = [
  101. ('command=', 'c', 'command to set an option for'),
  102. ('option=', 'o', 'option to set'),
  103. ('set-value=', 's', 'value of the option'),
  104. ('remove', 'r', 'remove (unset) the value'),
  105. ] + option_base.user_options
  106. boolean_options = option_base.boolean_options + ['remove']
  107. def initialize_options(self):
  108. option_base.initialize_options(self)
  109. self.command = None
  110. self.option = None
  111. self.set_value = None
  112. self.remove = None
  113. def finalize_options(self):
  114. option_base.finalize_options(self)
  115. if self.command is None or self.option is None:
  116. raise DistutilsOptionError("Must specify --command *and* --option")
  117. if self.set_value is None and not self.remove:
  118. raise DistutilsOptionError("Must specify --set-value or --remove")
  119. def run(self):
  120. edit_config(
  121. self.filename, {
  122. self.command: {self.option.replace('-', '_'): self.set_value}
  123. },
  124. self.dry_run
  125. )