From 0c8a74765fee01541c2ceb54cc0ec5a07ccf7886 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Dec 02 2019 09:43:48 +0000 Subject: Run black on the dist_git_auth plugin and pagure_distgit Signed-off-by: Pierre-Yves Chibon --- diff --git a/dist_git_auth.py b/dist_git_auth.py index c30b4dd..9625f8f 100644 --- a/dist_git_auth.py +++ b/dist_git_auth.py @@ -30,6 +30,7 @@ if "PAGURE_CONFIG" not in os.environ and os.path.exists( from pagure.config import config as pagure_config from pagure.lib import model + try: from pagure.lib import get_user except ImportError: @@ -44,6 +45,7 @@ _log = logging.getLogger(__name__) class DistGitAuth(GitAuthHelper): """ A Fedora and CentOS dist-Git authentication module. """ + is_dynamic = True def __init__(self): @@ -51,25 +53,27 @@ class DistGitAuth(GitAuthHelper): self.is_debug = pagure_config.get("ACL_DEBUG", False) self.global_pr_only = pagure_config.get("PR_ONLY", False) - self.block_unspecified = pagure_config.get("ACL_BLOCK_UNSPECIFIED", - True) - self.blacklists = [re.compile(refre) - for refre - in pagure_config["BLACKLIST_RES"]] + self.block_unspecified = pagure_config.get( + "ACL_BLOCK_UNSPECIFIED", True + ) + self.blacklists = [ + re.compile(refre) for refre in pagure_config["BLACKLIST_RES"] + ] self.unspecified_blacklist = [ re.compile(refre) - for refre - in pagure_config.get("UNSPECIFIED_BLACKLIST_RES", [])] - self.rcm_branches = [re.compile(refre) - for refre - in pagure_config["RCM_BRANCHES"]] + for refre in pagure_config.get("UNSPECIFIED_BLACKLIST_RES", []) + ] + self.rcm_branches = [ + re.compile(refre) for refre in pagure_config["RCM_BRANCHES"] + ] self.rcm_group = pagure_config.get("RCM_GROUP") self.supported_sigs = set(pagure_config.get("SUPPORTED_SIGS", [])) self.sig_prefixes = pagure_config.get("SIG_PREFIXES", []) self.protected_namespaces = pagure_config.get( - "ACL_PROTECTED_NAMESPACES", ["rpms"]) + "ACL_PROTECTED_NAMESPACES", ["rpms"] + ) - pdc_url = pagure_config.get('PDC_URL') + pdc_url = pagure_config.get("PDC_URL") if pdc_url and PDCClient: self.pdcclient = PDCClient(pdc_url, develop=True) else: @@ -84,23 +88,25 @@ class DistGitAuth(GitAuthHelper): if not self.pdcclient: # No way to confirm this is a supported branch, not supported return None - if not refname.startswith('refs/heads/'): + if not refname.startswith("refs/heads/"): # Anything outside of refs/heads is out-of-scope return None - refname = refname[len('refs/heads/'):] + refname = refname[len("refs/heads/") :] namespace2pdctype = { - 'rpms': 'rpm', - 'modules': 'module', - 'container': 'container', + "rpms": "rpm", + "modules": "module", + "container": "container", } - res = list(self.pdcclient.get_paged( - self.pdcclient['component-branches'], - global_component=project.name, - type=namespace2pdctype[project.namespace], - name=refname, - fields=['active'], - )) + res = list( + self.pdcclient.get_paged( + self.pdcclient["component-branches"], + global_component=project.name, + type=namespace2pdctype[project.namespace], + name=refname, + fields=["active"], + ) + ) if len(res) == 0: # No status return None @@ -108,7 +114,7 @@ class DistGitAuth(GitAuthHelper): # PDC couldn't make up its mind.... # Should never happen, but just in case... raise ValueError("PDC was unable to make up its mind") - return res[0]['active'] + return res[0]["active"] def info(self, msg): """ Function to print information. @@ -148,7 +154,7 @@ class DistGitAuth(GitAuthHelper): return False # Do not allow branch deletions - if not project.is_fork and revto == '0'*40: + if not project.is_fork and revto == "0" * 40: self.info("Branch deletion is not allowed") return False @@ -189,9 +195,9 @@ class DistGitAuth(GitAuthHelper): self.debug("Blacklists: %s" % self.blacklists) self.debug("User: %s" % user) self.debug("User groups: %s" % usergroups) - self.debug('Committer: %s' % is_committer) - self.debug('SIG memberships: %s' % user_sigs) - self.debug('RCM: %s' % is_rcm) + self.debug("Committer: %s" % is_committer) + self.debug("SIG memberships: %s" % user_sigs) + self.debug("RCM: %s" % is_rcm) # We have data, start the actual ACL checking if ( @@ -234,8 +240,9 @@ class DistGitAuth(GitAuthHelper): self.debug("No supported status available") if self.block_unspecified: - self.info("Access to namespace %s is restricted" - % project.namespace) + self.info( + "Access to namespace %s is restricted" % project.namespace + ) return False # Block second level blacklists @@ -254,10 +261,8 @@ class DistGitAuth(GitAuthHelper): # this is not a fork, only allow pushing if this is a PR merge. pr_only = project.settings.get("pull_request_access_only", False) if ( - (pr_only - or (self.global_pr_only and not project.is_fork)) - and pull_request is None - ): + pr_only or (self.global_pr_only and not project.is_fork) + ) and pull_request is None: self.info("A pull request is required for this branch") return False diff --git a/pagure_distgit/plugin.py b/pagure_distgit/plugin.py index f79df44..3809ec8 100644 --- a/pagure_distgit/plugin.py +++ b/pagure_distgit/plugin.py @@ -122,7 +122,7 @@ def _is_active_in_pdc(name, namespace): 500, error_code=APIERROR.ENOCODE, error="This pagure instance has no PDC_URL configured, please inform" - " your pagure administrators" + " your pagure administrators", ) else: pdc_url = "%s/component-branches/" % pdc_url.rstrip("/") @@ -134,8 +134,9 @@ def _is_active_in_pdc(name, namespace): "modules": "module", "container": "container", } - to_pdc_namespace = flask.current_app.config.get("PDC_NAMESPACES") \ - or to_pdc_namespace + to_pdc_namespace = ( + flask.current_app.config.get("PDC_NAMESPACES") or to_pdc_namespace + ) try: pdc_namespace = to_pdc_namespace[namespace] @@ -143,12 +144,15 @@ def _is_active_in_pdc(name, namespace): raise pagure.exceptions.APIError( 500, error_code=APIERROR.ENOCODE, - error="Namespace: %s could not be converted to a PDC namespace" % - namespace + error="Namespace: %s could not be converted to a PDC namespace" + % namespace, ) url = "%s?global_component=%s&name=master&type=%s" % ( - pdc_url, name, pdc_namespace) + pdc_url, + name, + pdc_namespace, + ) _log.info("Querying PDC at: %s", url) try: @@ -157,7 +161,7 @@ def _is_active_in_pdc(name, namespace): raise pagure.exceptions.APIError( 500, error_code=APIERROR.ENOCODE, - error="An error occured while querying pdc: %s" % err + error="An error occured while querying pdc: %s" % err, ) try: @@ -166,10 +170,12 @@ def _is_active_in_pdc(name, namespace): raise pagure.exceptions.APIError( 500, error_code=APIERROR.ENOCODE, - error="The output of %s could not be converted to JSON" % req.url + error="The output of %s could not be converted to JSON" % req.url, ) - _log.info("%s/%s is active: %s", namespace, name, data["results"][0]["active"]) + _log.info( + "%s/%s is active: %s", namespace, name, data["results"][0]["active"] + ) return data["results"][0]["active"] == True @@ -196,7 +202,7 @@ def take_orphan_endpoint(namespace, repo): ) user_grps = set(user_obj.groups) - req_grps = set(['packager']) + req_grps = set(["packager"]) if not user_grps.intersection(req_grps): raise pagure.exceptions.APIError( 403, @@ -210,7 +216,7 @@ def take_orphan_endpoint(namespace, repo): 400, error_code=APIERROR.EINVALIDREQ, errors="This project has been retired and cannot be unorphaned " - "here, please open a releng ticket for it.", + "here, please open a releng ticket for it.", ) try: @@ -225,9 +231,7 @@ def take_orphan_endpoint(namespace, repo): pagure.lib.notify.log( repo, topic="project.adopt", - msg=dict( - project=repo.to_json(public=True), agent=user_obj.username - ), + msg=dict(project=repo.to_json(public=True), agent=user_obj.username), ) output = {"point_of_contact": repo.user.user} @@ -242,9 +246,14 @@ def bodhi_updates_endpoint(namespace, repo): """ if namespace not in ["rpms"]: raise pagure.exceptions.APIError( - 400, error_code=APIERROR.EINVALIDREQ, errors=["Namespace not supported"]) + 400, + error_code=APIERROR.EINVALIDREQ, + errors=["Namespace not supported"], + ) - _log.info("Received request for the bodhi updates of: %s/%s", namespace, repo) + _log.info( + "Received request for the bodhi updates of: %s/%s", namespace, repo + ) repo = _get_repo(repo, namespace=namespace) html = pagure.utils.is_true(flask.request.args.get("html", False)) @@ -262,11 +271,17 @@ def bodhi_updates_endpoint(namespace, repo): req = requests.get(url) if not req.ok: raise pagure.exceptions.APIError( - 400, error_code=APIERROR.EINVALIDREQ, errors=["Could not call bodhi"]) + 400, + error_code=APIERROR.EINVALIDREQ, + errors=["Could not call bodhi"], + ) data = req.json() for release in data.get("releases", []): - if any(word in release["long_name"] for word in ["Flatpaks", "Containers", "Modular"]): + if any( + word in release["long_name"] + for word in ["Flatpaks", "Containers", "Modular"] + ): continue if release["state"] in ["pending", "current"]: releases[release["name"]] = release["long_name"] @@ -278,14 +293,20 @@ def bodhi_updates_endpoint(namespace, repo): page += 1 # Retrieves the updates of that package in bodhi - update_url = "%s/updates?packages=%s" % (bodhi_base_url.rstrip("/"), repo.name) + update_url = "%s/updates?packages=%s" % ( + bodhi_base_url.rstrip("/"), + repo.name, + ) updates = collections.defaultdict(dict) _log.info("Calling bodhi at: %s", update_url) req = requests.get(update_url) if not req.ok: raise pagure.exceptions.APIError( - 400, error_code=APIERROR.EINVALIDREQ, errors=["Could not call bodhi"]) + 400, + error_code=APIERROR.EINVALIDREQ, + errors=["Could not call bodhi"], + ) data = req.json() for update in data["updates"]: @@ -308,30 +329,45 @@ def bodhi_updates_endpoint(namespace, repo): row = "{release}{stable}{testing}" stable = "" if updates.get(release, {}).get("stable"): - stable = "{0}".format( - updates[release].get("stable")) + stable = ( + "{0}".format( + updates[release].get("stable") + ) + ) else: # This makes things a little slower and is only needed up until all releases will have gone # through bodhi all the time (ie: up until about F31 is eol). - mdapi_url = "https://mdapi.fedoraproject.org/%s/srcpkg/%s" % (release.lower(), repo.name) + mdapi_url = "https://mdapi.fedoraproject.org/%s/srcpkg/%s" % ( + release.lower(), + repo.name, + ) _log.info("Calling mdapi at: %s", mdapi_url) req = requests.get(mdapi_url) if req.ok: _log.info("mdapi returned ok") data = req.json() - build = "%s-%s-%s" % (data["basename"], data["version"], data["release"]) - stable = "{0}".format(build) + build = "%s-%s-%s" % ( + data["basename"], + data["version"], + data["release"], + ) + stable = ( + "{0}".format( + build + ) + ) testing = "" if updates.get(release, {}).get("testing"): - testing = "{0}".format( - updates[release].get("testing")) + testing = ( + "{0}".format( + updates[release].get("testing") + ) + ) row = row.format( - release=releases[release], - stable=stable, - testing=testing, + release=releases[release], stable=stable, testing=testing ) html_output += row html_output += ""