From 06b7a799d7b30e31815ce3d26a7e35290581dba0 Mon Sep 17 00:00:00 2001 From: Chenxiong Qi Date: Sep 29 2016 06:22:50 +0000 Subject: Fix PEP8 errors Signed-off-by: Chenxiong Qi --- diff --git a/src/pyrpkg/__init__.py b/src/pyrpkg/__init__.py index 282a59f..17abde0 100644 --- a/src/pyrpkg/__init__.py +++ b/src/pyrpkg/__init__.py @@ -10,34 +10,27 @@ # the full text of the license. import errno +import fnmatch +import git +import glob +import koji +import logging import os +import posixpath import pwd -import sys -import shutil import re -import pycurl -if sys.version_info[0:2] >= (2, 5): - import subprocess -else: - # We need a subprocess that has check_call - from kitchen.pycompat27 import subprocess -import koji import rpm -import logging -import git -import tempfile -import fnmatch -import posixpath +import shutil import six -import glob +import sys +import tempfile + from ConfigParser import ConfigParser + +from osbs.api import OSBS +from osbs.conf import Configuration from six.moves import configparser from six.moves import urllib -# Try to import krb, it's OK if it fails -try: - import krbV -except ImportError: - pass from pyrpkg.errors import HashtypeMixingError, rpkgError, rpkgAuthError, \ UnknownTargetError @@ -46,13 +39,21 @@ from pyrpkg.lookaside import CGILookasideCache from pyrpkg.sources import SourcesFile from pyrpkg.utils import cached_property, log_result -from osbs.api import OSBS -from osbs.conf import Configuration +if sys.version_info[0:2] >= (2, 5): + import subprocess +else: + # We need a subprocess that has check_call + from kitchen.pycompat27 import subprocess + +# Try to import krb, it's OK if it fails +try: + import krbV +except ImportError: + pass -# Setup our logger -# Null logger to avoid spurious messages, add a handler in app code class NullHandler(logging.Handler): + """Null logger to avoid spurious messages, add a handler in app code""" def emit(self, record): pass @@ -851,7 +852,7 @@ class Commands(object): try: ctx = krbV.default_context() ccache = ctx.default_ccache() - princ = ccache.principal() + princ = ccache.principal() # noqa return True except krbV.Krb5Error: return False @@ -1019,7 +1020,7 @@ class Commands(object): # get the name cmd = ['rpm', '-qp', '--nosignature', '--qf', '%{NAME}', srpm] # Run the command - self.log.debug('Running: %s' % ' '.join(cmd)) + self.log.debug('Running: %s', ' '.join(cmd)) try: proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -1860,8 +1861,7 @@ class Commands(object): self.log.info('Note: You can skip NVR construction & NVR' ' check with --skip-nvr-check. See help for' ' more info.') - raise rpkgError('Cannot continue without properly' - ' constructed NVR.') + raise rpkgError('Cannot continue without properly constructed NVR.') else: self.log.info('NVR checking will be skipped so I do not' ' care that I am not able to construct NVR.' @@ -1893,13 +1893,12 @@ class Commands(object): chain.append([url]) # This next list comp is ugly, but it's how we properly get a : # put in between each build set - cmd.extend(' : '.join([' '.join(sets) for sets in chain]).split()) + cmd.extend(' : '.join([' '.join(build_sets) for build_sets in chain]).split()) self.log.info('Chain building %s + %s for %s', build_reference, chain[:-1], self.target) self.log.debug('Building chain %s for %s with options %s and a priority of %s', chain, self.target, opts, priority) self.log.debug(' '.join(cmd)) - task_id = self.kojisession.chainBuild(chain, self.target, opts, - priority=priority) + task_id = self.kojisession.chainBuild(chain, self.target, opts, priority=priority) # Now handle the normal build else: cmd.append(url) @@ -1907,8 +1906,7 @@ class Commands(object): self.log.debug('Building %s for %s with options %s and a priority of %s', url, self.target, opts, priority) self.log.debug(' '.join(cmd)) - task_id = self.kojisession.build(url, self.target, opts, - priority=priority) + task_id = self.kojisession.build(url, self.target, opts, priority=priority) self.log.info('Created task: %s', task_id) self.log.info('Task info: %s/taskinfo?taskID=%s', self.kojiweburl, task_id) return task_id @@ -2491,7 +2489,8 @@ class Commands(object): for image in image_names: print(" docker pull %s" % image) else: - raise RuntimeError("Build '%s' wasn't processed correctly. Please, report this." % build_id) + raise RuntimeError( + "Build '%s' wasn't processed correctly. Please, report this." % build_id) else: raise RuntimeError("Build has failed.") diff --git a/src/pyrpkg/cli.py b/src/pyrpkg/cli.py index a38b79d..a48d7c9 100755 --- a/src/pyrpkg/cli.py +++ b/src/pyrpkg/cli.py @@ -27,6 +27,7 @@ import utils OSBS_DEFAULT_CONF_FILE = "/etc/osbs/osbs.conf" + class cliClient(object): """This is a client class for rpkg clients.""" @@ -450,12 +451,12 @@ defined, packages will be built sequentially.""" % {'name': self.name}) 'By default the prep and configure stages will be ' 'done as well, unless the short-circuit option is ' 'used.') - compile_parser.add_argument( - '--short-circuit', action='store_true', - help='short-circuit compile') - compile_parser.add_argument( - '--nocheck', action = 'store_true', - help = 'nocheck compile') + compile_parser.add_argument('--short-circuit', + action='store_true', + help='short-circuit compile') + compile_parser.add_argument('--nocheck', + action='store_true', + help='nocheck compile') compile_parser.set_defaults(command=self.compile) def register_diff(self): @@ -529,13 +530,15 @@ defined, packages will be built sequentially.""" % {'name': self.name}) 'All leading sections will be processed as well, ' 'unless the short-circuit option is used.') install_parser.add_argument( - '--short-circuit', action='store_true', default=False, + '--short-circuit', + action='store_true', + default=False, help='short-circuit install') install_parser.add_argument( - '--nocheck', action = 'store_true', - help = 'nocheck install') - install_parser.set_defaults(command=self.install, - default = False) + '--nocheck', + action='store_true', + help='nocheck install') + install_parser.set_defaults(command=self.install, default=False) def register_lint(self): """Register the lint target""" @@ -688,8 +691,7 @@ defined, packages will be built sequentially.""" % {'name': self.name}) push_parser = self.subparsers.add_parser( 'push', help='Push changes to remote repository') - push_parser.add_argument('--force', '-f', help='Force push', - action='store_true') + push_parser.add_argument('--force', '-f', help='Force push', action='store_true') push_parser.set_defaults(command=self.push) def register_scratch_build(self): @@ -863,20 +865,21 @@ see API KEY section of copr-cli(1) man page. help='Scratch build', action="store_true") - self.container_build_parser.add_argument('--target', - help='Override the default target', - default=None) - self.container_build_parser.add_argument('--build-with', - help='Build container with ' - 'specified builder type. Default ' - 'is koji', - dest="build_with", - choices=("koji", "osbs"), - default="koji") - self.container_build_parser.add_argument('--nowait', - action='store_true', - default=False, - help="Don't wait on build") + self.container_build_parser.add_argument( + '--target', + help='Override the default target', + default=None) + self.container_build_parser.add_argument( + '--build-with', + help='Build container with specified builder type. Default is koji', + dest="build_with", + choices=("koji", "osbs"), + default="koji") + self.container_build_parser.add_argument( + '--nowait', + action='store_true', + default=False, + help="Don't wait on build") self.container_build_parser.set_defaults(command=self.container_build) @@ -954,17 +957,16 @@ see API KEY section of copr-cli(1) man page. return # Pass info off to our koji task watcher - return self._watch_koji_tasks(self.cmd.kojisession, - [task_id]) + return self._watch_koji_tasks(self.cmd.kojisession, [task_id]) def chainbuild(self): if self.cmd.module_name in self.args.package: - raise Exception('%s must not be in the chain' % - self.cmd.module_name) + raise Exception('%s must not be in the chain' % self.cmd.module_name) + # make sure we didn't get an empty chain if self.args.package == [':']: - raise Exception('Must provide at least one dependency ' - 'build') + raise Exception('Must provide at least one dependency build') + # Break the chain up into sections sets = False urls = [] @@ -975,25 +977,23 @@ see API KEY section of copr-cli(1) man page. # We've hit the end of a set, add the set as a unit to the # url list and reset the build_set. urls.append(build_set) - self.log.debug('Created a build set: %s' % ' '.join(build_set)) + self.log.debug('Created a build set: %s', ' '.join(build_set)) build_set = [] sets = True else: # Figure out the scm url to build from package name - hash = self.cmd.get_latest_commit(component, - self.cmd.branch_merge) - url = self.cmd.anongiturl % {'module': - component} + '#%s' % hash + hash = self.cmd.get_latest_commit(component, self.cmd.branch_merge) + url = self.cmd.anongiturl % {'module': component} + '#%s' % hash # If there are no ':' in the chain list, treat each object as # an individual chain if ':' in self.args.package: build_set.append(url) else: urls.append([url]) - self.log.debug('Created a build set: %s' % url) + self.log.debug('Created a build set: %s', url) # Take care of the last build set if we have one if build_set: - self.log.debug('Created a build set: %s' % ' '.join(build_set)) + self.log.debug('Created a build set: %s', ' '.join(build_set)) urls.append(build_set) # See if we ended in a : making our last build it's own group if self.args.package[-1] == ':': @@ -1093,18 +1093,18 @@ see API KEY section of copr-cli(1) man page. err_args = {"plugin.section": section_name, "root.section": self.name} if self.config.has_option(section_name, "kojiconfig"): - kojiconfig=self.config.get(section_name, "kojiconfig") + kojiconfig = self.config.get(section_name, "kojiconfig") else: err_args["option"] = "kojiconfig" self.log.debug(err_msg % err_args) - kojiconfig=self.config.get(self.name, "kojiconfig") + kojiconfig = self.config.get(self.name, "kojiconfig") if self.config.has_option(section_name, "build_client"): - build_client=self.config.get(section_name, "build_client") + build_client = self.config.get(section_name, "build_client") else: err_args["option"] = "kojiconfig" self.log.debug(err_msg % err_args) - build_client=self.config.get(self.name, "build_client") + build_client = self.config.get(self.name, "build_client") self.cmd.container_build_koji(target_override, opts=opts, kojiconfig=kojiconfig, @@ -1474,14 +1474,17 @@ Tasks still running. You can continue to watch with the '%s watch-task' command. else: self.user = pwd.getpwuid(os.getuid())[0] + # Add a class stolen from /usr/bin/koji to watch tasks # this was cut/pasted from koji, and then modified for local use. # The formatting is koji style, not the stile of this file. Do not use these # functions as a style guide. # This is fragile and hopefully will be replaced by a real kojiclient lib. + + class TaskWatcher(object): - def __init__(self,task_id,session,log,level=0,quiet=False): + def __init__(self, task_id, session, log, level=0, quiet=False): self.id = task_id self.session = session self.info = None @@ -1489,7 +1492,7 @@ class TaskWatcher(object): self.quiet = quiet self.log = log - #XXX - a bunch of this stuff needs to adapt to different tasks + # XXX - a bunch of this stuff needs to adapt to different tasks def str(self): if self.info: @@ -1507,7 +1510,7 @@ class TaskWatcher(object): return '' error = None try: - result = self.session.getTaskResult(self.id) + self.session.getTaskResult(self.id) except (xmlrpc_client.Fault, koji.GenericError) as e: error = e if error is None: @@ -1529,24 +1532,23 @@ class TaskWatcher(object): raise Exception("No such task id: %i" % self.id) state = self.info['state'] if last: - #compare and note status changes + # compare and note status changes laststate = last['state'] if laststate != state: - self.log.info("%s: %s -> %s" % (self.str(), - self.display_state(last), - self.display_state(self.info))) + self.log.info("%s: %s -> %s", + self.str(), self.display_state(last), self.display_state(self.info)) return True return False else: # First time we're seeing this task, so just show the current state - self.log.info("%s: %s" % (self.str(), self.display_state(self.info))) + self.log.info("%s: %s", self.str(), self.display_state(self.info)) return False def is_done(self): if self.info is None: return False state = koji.TASK_STATES[self.info['state']] - return (state in ['CLOSED','CANCELED','FAILED']) + return (state in ['CLOSED', 'CANCELED', 'FAILED']) def is_success(self): if self.info is None: diff --git a/src/pyrpkg/errors.py b/src/pyrpkg/errors.py index 917acaf..5ea5f08 100644 --- a/src/pyrpkg/errors.py +++ b/src/pyrpkg/errors.py @@ -19,6 +19,7 @@ class rpkgAuthError(rpkgError): """Raised in case of authentication errors""" faultCode = 1002 + class UnknownTargetError(Exception): faultCode = 1004 diff --git a/src/pyrpkg/utils.py b/src/pyrpkg/utils.py index ba297a2..03606a4 100644 --- a/src/pyrpkg/utils.py +++ b/src/pyrpkg/utils.py @@ -14,18 +14,23 @@ This module contains a bunch of utilities used elsewhere in pyrpkg. import warnings -warnings.simplefilter('always', DeprecationWarning) import os import six if six.PY3: - u = lambda s: s + def u(s): + return s + getcwd = os.getcwd else: - u = lambda s: s.decode('utf-8') + def u(s): + return s.decode('utf-8') + getcwd = os.getcwdu +warnings.simplefilter('always', DeprecationWarning) + class cached_property(property): """A property caching its return value diff --git a/test/commands/__init__.py b/test/commands/__init__.py index 0e670c2..380dc78 100644 --- a/test/commands/__init__.py +++ b/test/commands/__init__.py @@ -85,7 +85,9 @@ class CommandTestCase(unittest.TestCase): def get_tags(self, gitdir): result = [] - tags = subprocess.Popen(['git', 'tag', '-n1'], cwd=gitdir, stdout=subprocess.PIPE, universal_newlines=True).communicate()[0] + tags = subprocess.Popen(['git', 'tag', '-n1'], cwd=gitdir, + stdout=subprocess.PIPE, + universal_newlines=True).communicate()[0] for line in tags.split('\n'): if not line: diff --git a/test/commands/test_add_tag.py b/test/commands/test_add_tag.py index af1a397..6e13404 100644 --- a/test/commands/test_add_tag.py +++ b/test/commands/test_add_tag.py @@ -35,8 +35,8 @@ class CommandAddTagTestCase(CommandTestCase): # `git tag` will call $EDITOR to ask the user to write a message os.environ['GIT_EDITOR'] = ('/usr/bin/python -c "import sys; ' - 'open(sys.argv[1], \'w\').write(\'%s\')"' - % message) + 'open(sys.argv[1], \'w\').write(\'%s\')"' + % message) cmd.add_tag(tag) diff --git a/test/commands/test_clone.py b/test/commands/test_clone.py index b6cf492..5541a9b 100644 --- a/test/commands/test_clone.py +++ b/test/commands/test_clone.py @@ -12,6 +12,7 @@ CLONE_CONFIG = ''' sendemail.to %(module)s-owner@fedoraproject.org ''' + class CommandCloneTestCase(CommandTestCase): def test_clone_anonymous(self): self.make_new_git(self.module) diff --git a/test/commands/test_patch.py b/test/commands/test_patch.py index 6c5d146..878871a 100644 --- a/test/commands/test_patch.py +++ b/test/commands/test_patch.py @@ -26,7 +26,7 @@ class CommandPatchTestCase(CommandTestCase): self.build_client, self.user, self.dist, self.target, self.quiet) line, offset = cmd._byte_offset_to_line_number(self.text_ascii, 10) - #10 byte offset mean line 1 and character 11 + # 10 byte offset mean line 1 and character 11 self.assertEqual(line, 1) self.assertEqual(offset, 11) @@ -52,6 +52,6 @@ class CommandPatchTestCase(CommandTestCase): self.target, self.quiet) text = self.text_utf8.decode('UTF-8', 'ignore') line, offset = cmd._byte_offset_to_line_number(text, 9) - #9 byte offset mean line 3 and second character + # 9 byte offset mean line 3 and second character self.assertEqual(line, 3) self.assertEqual(offset, 2) diff --git a/test/test_commands.py b/test/test_commands.py index 03bf675..7267c89 100644 --- a/test/test_commands.py +++ b/test/test_commands.py @@ -290,8 +290,7 @@ class LoadBranchMergeTest(CommandTestCase): try: self.cmd.load_branch_merge() except rpkgError as e: - self.assertEqual('Unable to find remote branch. Use --dist', - str(e)) + self.assertEqual('Unable to find remote branch. Use --dist', str(e)) else: self.fail("It's expected to raise rpkgError, but not.") diff --git a/test/test_lookaside.py b/test/test_lookaside.py index c509290..7509e61 100644 --- a/test/test_lookaside.py +++ b/test/test_lookaside.py @@ -10,19 +10,14 @@ import hashlib import os import shutil -import sys import tempfile import unittest import mock import pycurl -old_path = list(sys.path) -src_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../src') -sys.path.insert(0, src_path) from pyrpkg.lookaside import CGILookasideCache from pyrpkg.errors import DownloadError, InvalidHashType, UploadError -sys.path = old_path class CGILookasideCacheTestCase(unittest.TestCase): diff --git a/test/test_sources.py b/test/test_sources.py index 11561ad..fafbaf9 100644 --- a/test/test_sources.py +++ b/test/test_sources.py @@ -1,14 +1,9 @@ import os import shutil -import sys import tempfile import unittest -old_path = list(sys.path) -src_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../src') -sys.path.insert(0, src_path) from pyrpkg import sources -sys.path = old_path class SourceFileEntryTestCase(unittest.TestCase): @@ -232,7 +227,7 @@ class SourcesFileTestCase(unittest.TestCase): s.write() with open(self.sourcesfile) as f: - lines = f.readlines() + lines = f.readlines() self.assertEqual(len(lines), 2) self.assertEqual(lines[0], 'MD5 (afile) = ahash\n') @@ -250,7 +245,7 @@ class SourcesFileTestCase(unittest.TestCase): s.write() with open(self.sourcesfile) as f: - lines = f.readlines() + lines = f.readlines() self.assertEqual(len(lines), 3) self.assertEqual(lines[0], 'MD5 (afile) = ahash\n') @@ -269,11 +264,7 @@ class SourcesFileTestCase(unittest.TestCase): s.write() with open(self.sourcesfile) as f: - lines = f.readlines() + lines = f.readlines() self.assertEqual(len(lines), 1) self.assertEqual(lines[0], 'MD5 (thirdfile) = thirdhash\n') - - -if __name__ == '__main__': - unittest.main() diff --git a/test/test_utils.py b/test/test_utils.py index 9f98b69..96f6bdf 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1,15 +1,9 @@ -import os -import sys import unittest import warnings import mock -old_path = list(sys.path) -src_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../src') -sys.path.insert(0, src_path) from pyrpkg.utils import cached_property, warn_deprecated, log_result -sys.path = old_path class CachedPropertyTestCase(unittest.TestCase): @@ -162,8 +156,10 @@ class DeprecationUtilsTestCase(unittest.TestCase): class LogResultTestCase(unittest.TestCase): def setUp(self): self.logs = [] + def info(msg): self.logs.append(msg) + self.log_func = info def test_dict_result(self):