Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

setopt.py 5.0KB

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