repoquery.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # pylint: skip-file
  2. # flake8: noqa
  3. '''
  4. class that wraps the repoquery commands in a subprocess
  5. '''
  6. # pylint: disable=too-many-lines,wrong-import-position,wrong-import-order
  7. from collections import defaultdict # noqa: E402
  8. # pylint: disable=no-name-in-module,import-error
  9. # Reason: pylint errors with "No name 'version' in module 'distutils'".
  10. # This is a bug: https://github.com/PyCQA/pylint/issues/73
  11. from distutils.version import LooseVersion # noqa: E402
  12. import subprocess # noqa: E402
  13. class RepoqueryCLIError(Exception):
  14. '''Exception class for repoquerycli'''
  15. pass
  16. def _run(cmds):
  17. ''' Actually executes the command. This makes mocking easier. '''
  18. proc = subprocess.Popen(cmds,
  19. stdin=subprocess.PIPE,
  20. stdout=subprocess.PIPE,
  21. stderr=subprocess.PIPE)
  22. stdout, stderr = proc.communicate()
  23. return proc.returncode, stdout, stderr
  24. # pylint: disable=too-few-public-methods
  25. class RepoqueryCLI(object):
  26. ''' Class to wrap the command line tools '''
  27. def __init__(self,
  28. verbose=False):
  29. ''' Constructor for RepoqueryCLI '''
  30. self.verbose = verbose
  31. self.verbose = True
  32. def _repoquery_cmd(self, cmd, output=False, output_type='json'):
  33. '''Base command for repoquery '''
  34. cmds = ['/usr/bin/repoquery', '--plugins', '--quiet']
  35. cmds.extend(cmd)
  36. rval = {}
  37. results = ''
  38. err = None
  39. if self.verbose:
  40. print(' '.join(cmds))
  41. returncode, stdout, stderr = _run(cmds)
  42. rval = {
  43. "returncode": returncode,
  44. "results": results,
  45. "cmd": ' '.join(cmds),
  46. }
  47. if returncode == 0:
  48. if output:
  49. if output_type == 'raw':
  50. rval['results'] = stdout
  51. if self.verbose:
  52. print(stdout)
  53. print(stderr)
  54. if err:
  55. rval.update({
  56. "err": err,
  57. "stderr": stderr,
  58. "stdout": stdout,
  59. "cmd": cmds
  60. })
  61. else:
  62. rval.update({
  63. "stderr": stderr,
  64. "stdout": stdout,
  65. "results": {},
  66. })
  67. return rval