core.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. # implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # Copyright (C) 2013 Association of Universities for Research in Astronomy
  17. # (AURA)
  18. #
  19. # Redistribution and use in source and binary forms, with or without
  20. # modification, are permitted provided that the following conditions are met:
  21. #
  22. # 1. Redistributions of source code must retain the above copyright
  23. # notice, this list of conditions and the following disclaimer.
  24. #
  25. # 2. Redistributions in binary form must reproduce the above
  26. # copyright notice, this list of conditions and the following
  27. # disclaimer in the documentation and/or other materials provided
  28. # with the distribution.
  29. #
  30. # 3. The name of AURA and its representatives may not be used to
  31. # endorse or promote products derived from this software without
  32. # specific prior written permission.
  33. #
  34. # THIS SOFTWARE IS PROVIDED BY AURA ``AS IS'' AND ANY EXPRESS OR IMPLIED
  35. # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  36. # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  37. # DISCLAIMED. IN NO EVENT SHALL AURA BE LIABLE FOR ANY DIRECT, INDIRECT,
  38. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  39. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  41. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  42. # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  43. # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  44. # DAMAGE.
  45. from distutils import core
  46. from distutils import errors
  47. import logging
  48. import os
  49. import sys
  50. import warnings
  51. from pbr import util
  52. if sys.version_info[0] == 3:
  53. string_type = str
  54. integer_types = (int,)
  55. else:
  56. string_type = basestring # flake8: noqa
  57. integer_types = (int, long) # flake8: noqa
  58. def pbr(dist, attr, value):
  59. """Implements the actual pbr setup() keyword.
  60. When used, this should be the only keyword in your setup() aside from
  61. `setup_requires`.
  62. If given as a string, the value of pbr is assumed to be the relative path
  63. to the setup.cfg file to use. Otherwise, if it evaluates to true, it
  64. simply assumes that pbr should be used, and the default 'setup.cfg' is
  65. used.
  66. This works by reading the setup.cfg file, parsing out the supported
  67. metadata and command options, and using them to rebuild the
  68. `DistributionMetadata` object and set the newly added command options.
  69. The reason for doing things this way is that a custom `Distribution` class
  70. will not play nicely with setup_requires; however, this implementation may
  71. not work well with distributions that do use a `Distribution` subclass.
  72. """
  73. if not value:
  74. return
  75. if isinstance(value, string_type):
  76. path = os.path.abspath(value)
  77. else:
  78. path = os.path.abspath('setup.cfg')
  79. if not os.path.exists(path):
  80. raise errors.DistutilsFileError(
  81. 'The setup.cfg file %s does not exist.' % path)
  82. # Converts the setup.cfg file to setup() arguments
  83. try:
  84. attrs = util.cfg_to_args(path, dist.script_args)
  85. except Exception:
  86. e = sys.exc_info()[1]
  87. # NB: This will output to the console if no explicit logging has
  88. # been setup - but thats fine, this is a fatal distutils error, so
  89. # being pretty isn't the #1 goal.. being diagnosable is.
  90. logging.exception('Error parsing')
  91. raise errors.DistutilsSetupError(
  92. 'Error parsing %s: %s: %s' % (path, e.__class__.__name__, e))
  93. # Repeat some of the Distribution initialization code with the newly
  94. # provided attrs
  95. if attrs:
  96. # Skips 'options' and 'licence' support which are rarely used; may
  97. # add back in later if demanded
  98. for key, val in attrs.items():
  99. if hasattr(dist.metadata, 'set_' + key):
  100. getattr(dist.metadata, 'set_' + key)(val)
  101. elif hasattr(dist.metadata, key):
  102. setattr(dist.metadata, key, val)
  103. elif hasattr(dist, key):
  104. setattr(dist, key, val)
  105. else:
  106. msg = 'Unknown distribution option: %s' % repr(key)
  107. warnings.warn(msg)
  108. # Re-finalize the underlying Distribution
  109. try:
  110. super(dist.__class__, dist).finalize_options()
  111. except TypeError:
  112. # If dist is not declared as a new-style class (with object as
  113. # a subclass) then super() will not work on it. This is the case
  114. # for Python 2. In that case, fall back to doing this the ugly way
  115. dist.__class__.__bases__[-1].finalize_options(dist)
  116. # This bit comes out of distribute/setuptools
  117. if isinstance(dist.metadata.version, integer_types + (float,)):
  118. # Some people apparently take "version number" too literally :)
  119. dist.metadata.version = str(dist.metadata.version)