Development of an internal social media platform with personalised dashboards for students
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. from setuptools.extern.six.moves 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.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. )