setup.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. """A setuptools based setup module.
  2. """
  3. from __future__ import print_function
  4. import os
  5. import fnmatch
  6. import re
  7. import yaml
  8. # Always prefer setuptools over distutils
  9. from setuptools import setup, Command
  10. from setuptools_lint.setuptools_command import PylintCommand
  11. from six import string_types
  12. from yamllint.config import YamlLintConfig
  13. from yamllint.cli import Format
  14. from yamllint import linter
  15. def find_files(base_dir, exclude_dirs, include_dirs, file_regex):
  16. ''' find files matching file_regex '''
  17. found = []
  18. exclude_regex = ''
  19. include_regex = ''
  20. if exclude_dirs is not None:
  21. exclude_regex = r'|'.join([fnmatch.translate(x) for x in exclude_dirs]) or r'$.'
  22. if include_dirs is not None:
  23. include_regex = r'|'.join([fnmatch.translate(x) for x in include_dirs]) or r'$.'
  24. for root, dirs, files in os.walk(base_dir):
  25. if exclude_dirs is not None:
  26. # filter out excludes for dirs
  27. dirs[:] = [d for d in dirs if not re.match(exclude_regex, d)]
  28. if include_dirs is not None:
  29. # filter for includes for dirs
  30. dirs[:] = [d for d in dirs if re.match(include_regex, d)]
  31. matches = [os.path.join(root, f) for f in files if re.search(file_regex, f) is not None]
  32. found.extend(matches)
  33. return found
  34. class OpenShiftAnsibleYamlLint(Command):
  35. ''' Command to run yamllint '''
  36. description = "Run yamllint tests"
  37. user_options = [
  38. ('excludes=', 'e', 'directories to exclude'),
  39. ('config-file=', 'c', 'config file to use'),
  40. ('format=', 'f', 'format to use (standard, parsable)'),
  41. ]
  42. def initialize_options(self):
  43. ''' initialize_options '''
  44. # Reason: Defining these attributes as a part of initialize_options is
  45. # consistent with upstream usage
  46. # Status: permanently disabled
  47. # pylint: disable=attribute-defined-outside-init
  48. self.excludes = None
  49. self.config_file = None
  50. self.format = None
  51. def finalize_options(self):
  52. ''' finalize_options '''
  53. # Reason: These attributes are defined in initialize_options and this
  54. # usage is consistant with upstream usage
  55. # Status: permanently disabled
  56. # pylint: disable=attribute-defined-outside-init
  57. if isinstance(self.excludes, string_types):
  58. self.excludes = self.excludes.split(',')
  59. if self.format is None:
  60. self.format = 'standard'
  61. assert (self.format in ['standard', 'parsable']), (
  62. 'unknown format {0}.'.format(self.format))
  63. if self.config_file is None:
  64. self.config_file = '.yamllint'
  65. assert os.path.isfile(self.config_file), (
  66. 'yamllint config file {0} does not exist.'.format(self.config_file))
  67. def run(self):
  68. ''' run command '''
  69. if self.excludes is not None:
  70. print("Excludes:\n{0}".format(yaml.dump(self.excludes, default_flow_style=False)))
  71. config = YamlLintConfig(file=self.config_file)
  72. has_errors = False
  73. has_warnings = False
  74. if self.format == 'parsable':
  75. format_method = Format.parsable
  76. else:
  77. format_method = Format.standard_color
  78. for yaml_file in find_files(os.getcwd(), self.excludes, None, r'\.ya?ml$'):
  79. first = True
  80. with open(yaml_file, 'r') as contents:
  81. for problem in linter.run(contents, config):
  82. if first and self.format != 'parsable':
  83. print('\n{0}:'.format(os.path.relpath(yaml_file)))
  84. first = False
  85. print(format_method(problem, yaml_file))
  86. if problem.level == linter.PROBLEM_LEVELS['error']:
  87. has_errors = True
  88. elif problem.level == linter.PROBLEM_LEVELS['warning']:
  89. has_warnings = True
  90. assert not has_errors, 'yamllint errors found'
  91. assert not has_warnings, 'yamllint warnings found'
  92. class OpenShiftAnsiblePylint(PylintCommand):
  93. ''' Class to override the default behavior of PylintCommand '''
  94. # Reason: This method needs to be an instance method to conform to the
  95. # overridden method's signature
  96. # Status: permanently disabled
  97. # pylint: disable=no-self-use
  98. def find_all_modules(self):
  99. ''' find all python files to test '''
  100. exclude_dirs = ['.tox', 'utils', 'test', 'tests', 'git']
  101. modules = []
  102. for match in find_files(os.getcwd(), exclude_dirs, None, r'\.py$'):
  103. package = os.path.basename(match).replace('.py', '')
  104. modules.append(('openshift_ansible', package, match))
  105. return modules
  106. def get_finalized_command(self, cmd):
  107. ''' override get_finalized_command to ensure we use our
  108. find_all_modules method '''
  109. if cmd == 'build_py':
  110. return self
  111. # Reason: This method needs to be an instance method to conform to the
  112. # overridden method's signature
  113. # Status: permanently disabled
  114. # pylint: disable=no-self-use
  115. def with_project_on_sys_path(self, func, func_args, func_kwargs):
  116. ''' override behavior, since we don't need to build '''
  117. return func(*func_args, **func_kwargs)
  118. class UnsupportedCommand(Command):
  119. ''' Basic Command to override unsupported commands '''
  120. user_options = []
  121. # Reason: This method needs to be an instance method to conform to the
  122. # overridden method's signature
  123. # Status: permanently disabled
  124. # pylint: disable=no-self-use
  125. def initialize_options(self):
  126. ''' initialize_options '''
  127. pass
  128. # Reason: This method needs to be an instance method to conform to the
  129. # overridden method's signature
  130. # Status: permanently disabled
  131. # pylint: disable=no-self-use
  132. def finalize_options(self):
  133. ''' initialize_options '''
  134. pass
  135. # Reason: This method needs to be an instance method to conform to the
  136. # overridden method's signature
  137. # Status: permanently disabled
  138. # pylint: disable=no-self-use
  139. def run(self):
  140. ''' run command '''
  141. print("Unsupported command for openshift-ansible")
  142. setup(
  143. name='openshift-ansible',
  144. license="Apache 2.0",
  145. cmdclass={
  146. 'install': UnsupportedCommand,
  147. 'develop': UnsupportedCommand,
  148. 'build': UnsupportedCommand,
  149. 'build_py': UnsupportedCommand,
  150. 'build_ext': UnsupportedCommand,
  151. 'egg_info': UnsupportedCommand,
  152. 'sdist': UnsupportedCommand,
  153. 'lint': OpenShiftAnsiblePylint,
  154. 'yamllint': OpenShiftAnsibleYamlLint,
  155. },
  156. packages=[],
  157. )