mirror of
https://github.com/certbot/certbot.git
synced 2026-08-02 03:02:15 +02:00
Merge pull request #6932 from adferrand/pylint-squash
Update Pylint to 1.9.4 (squashed PR)
This commit is contained in:
@@ -166,7 +166,6 @@ class NginxConfigurator(common.Installer):
|
||||
# Entry point in main.py for installing cert
|
||||
def deploy_cert(self, domain, cert_path, key_path,
|
||||
chain_path=None, fullchain_path=None):
|
||||
# pylint: disable=unused-argument
|
||||
"""Deploys certificate to specified virtual host.
|
||||
|
||||
.. note:: Aborts if the vhost is missing ssl_certificate or
|
||||
@@ -187,8 +186,7 @@ class NginxConfigurator(common.Installer):
|
||||
for vhost in vhosts:
|
||||
self._deploy_cert(vhost, cert_path, key_path, chain_path, fullchain_path)
|
||||
|
||||
def _deploy_cert(self, vhost, cert_path, key_path, chain_path, fullchain_path):
|
||||
# pylint: disable=unused-argument
|
||||
def _deploy_cert(self, vhost, cert_path, key_path, chain_path, fullchain_path): # pylint: disable=unused-argument
|
||||
"""
|
||||
Helper function for deploy_cert() that handles the actual deployment
|
||||
this exists because we might want to do multiple deployments per
|
||||
@@ -411,9 +409,8 @@ class NginxConfigurator(common.Installer):
|
||||
rank = matches[0]['rank']
|
||||
wildcards = [x for x in matches if x['rank'] == rank]
|
||||
return max(wildcards, key=lambda x: len(x['name']))['vhost']
|
||||
else:
|
||||
# Exact or regex match
|
||||
return matches[0]['vhost']
|
||||
# Exact or regex match
|
||||
return matches[0]['vhost']
|
||||
|
||||
def _rank_matches_by_name(self, vhost_list, target_name):
|
||||
"""Returns a ranked list of vhosts from vhost_list that match target_name.
|
||||
@@ -507,25 +504,23 @@ class NginxConfigurator(common.Installer):
|
||||
if matching_port == "" or matching_port is None:
|
||||
# if no port is specified, Nginx defaults to listening on port 80.
|
||||
return test_port == self.DEFAULT_LISTEN_PORT
|
||||
else:
|
||||
return test_port == matching_port
|
||||
return test_port == matching_port
|
||||
|
||||
def _vhost_listening_on_port_no_ssl(self, vhost, port):
|
||||
found_matching_port = False
|
||||
if len(vhost.addrs) == 0:
|
||||
if not vhost.addrs:
|
||||
# if there are no listen directives at all, Nginx defaults to
|
||||
# listening on port 80.
|
||||
found_matching_port = (port == self.DEFAULT_LISTEN_PORT)
|
||||
else:
|
||||
for addr in vhost.addrs:
|
||||
if self._port_matches(port, addr.get_port()) and addr.ssl == False:
|
||||
if self._port_matches(port, addr.get_port()) and not addr.ssl:
|
||||
found_matching_port = True
|
||||
|
||||
if found_matching_port:
|
||||
# make sure we don't have an 'ssl on' directive
|
||||
return not self.parser.has_ssl_on_directive(vhost)
|
||||
else:
|
||||
return False
|
||||
return False
|
||||
|
||||
def _get_redirect_ranked_matches(self, target_name, port):
|
||||
"""Gets a ranked list of plaintextish port-listening vhosts matching target_name
|
||||
@@ -614,7 +609,7 @@ class NginxConfigurator(common.Installer):
|
||||
|
||||
# If the vhost was implicitly listening on the default Nginx port,
|
||||
# have it continue to do so.
|
||||
if len(vhost.addrs) == 0:
|
||||
if not vhost.addrs:
|
||||
listen_block = [['\n ', 'listen', ' ', self.DEFAULT_LISTEN_PORT]]
|
||||
self.parser.add_server_directives(vhost, listen_block)
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
"""nginx plugin constants."""
|
||||
import pkg_resources
|
||||
import platform
|
||||
|
||||
import pkg_resources
|
||||
|
||||
FREEBSD_DARWIN_SERVER_ROOT = "/usr/local/etc/nginx"
|
||||
LINUX_SERVER_ROOT = "/etc/nginx"
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ def select_vhost_multiple(vhosts):
|
||||
return list()
|
||||
tags_list = [vhost.display_repr()+"\n" for vhost in vhosts]
|
||||
# Remove the extra newline from the last entry
|
||||
if len(tags_list):
|
||||
if tags_list:
|
||||
tags_list[-1] = tags_list[-1][:-1]
|
||||
code, names = zope.component.getUtility(interfaces.IDisplay).checklist(
|
||||
"Which server blocks would you like to modify?",
|
||||
|
||||
@@ -4,13 +4,13 @@ import logging
|
||||
import os
|
||||
|
||||
from acme import challenges
|
||||
from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-module
|
||||
|
||||
from certbot import errors
|
||||
from certbot.plugins import common
|
||||
|
||||
from certbot_nginx import obj
|
||||
from certbot_nginx import nginxparser
|
||||
from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-module
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -204,3 +204,4 @@ class NginxHttp01(common.ChallengePerformer):
|
||||
' ', '$1', ' ', 'break']]
|
||||
self.configurator.parser.add_server_directives(vhost,
|
||||
rewrite_directive, insert_at_top=True)
|
||||
return None
|
||||
|
||||
@@ -84,7 +84,7 @@ class Addr(common.Addr):
|
||||
port = tup[2]
|
||||
|
||||
# The rest of the parts are options; we only care about ssl and default
|
||||
while len(parts) > 0:
|
||||
while parts:
|
||||
nextpart = parts.pop()
|
||||
if nextpart == 'ssl':
|
||||
ssl = True
|
||||
@@ -120,7 +120,7 @@ class Addr(common.Addr):
|
||||
def __repr__(self):
|
||||
return "Addr(" + self.__str__() + ")"
|
||||
|
||||
def __hash__(self):
|
||||
def __hash__(self): # pylint: disable=useless-super-delegation
|
||||
# Python 3 requires explicit overridden for __hash__
|
||||
# See certbot-apache/certbot_apache/obj.py for more information
|
||||
return super(Addr, self).__hash__()
|
||||
@@ -224,15 +224,17 @@ class VirtualHost(object): # pylint: disable=too-few-public-methods
|
||||
for a in self.addrs:
|
||||
if a.ipv6:
|
||||
return True
|
||||
return False
|
||||
|
||||
def ipv4_enabled(self):
|
||||
"""Return true if one or more of the listen directives in vhost are IPv4
|
||||
only"""
|
||||
if self.addrs is None or len(self.addrs) == 0:
|
||||
if not self.addrs:
|
||||
return True
|
||||
for a in self.addrs:
|
||||
if not a.ipv6:
|
||||
return True
|
||||
return False
|
||||
|
||||
def display_repr(self):
|
||||
"""Return a representation of VHost to be used in dialog"""
|
||||
@@ -250,7 +252,7 @@ def _find_directive(directives, directive_name, match_content=None):
|
||||
"""Find a directive of type directive_name in directives. If match_content is given,
|
||||
Searches for `match_content` in the directive arguments.
|
||||
"""
|
||||
if not directives or isinstance(directives, six.string_types) or len(directives) == 0:
|
||||
if not directives or isinstance(directives, six.string_types):
|
||||
return None
|
||||
|
||||
# If match_content is None, just match on directive type. Otherwise, match on
|
||||
|
||||
@@ -4,8 +4,8 @@ import functools
|
||||
import glob
|
||||
import logging
|
||||
import os
|
||||
import pyparsing
|
||||
import re
|
||||
import pyparsing
|
||||
|
||||
import six
|
||||
|
||||
@@ -52,6 +52,7 @@ class NginxParser(object):
|
||||
:param str filepath: The path to the files to parse, as a glob
|
||||
|
||||
"""
|
||||
# pylint: disable=too-many-nested-blocks
|
||||
filepath = self.abs_path(filepath)
|
||||
trees = self._parse_files(filepath)
|
||||
for tree in trees:
|
||||
@@ -82,8 +83,7 @@ class NginxParser(object):
|
||||
"""
|
||||
if not os.path.isabs(path):
|
||||
return os.path.normpath(os.path.join(self.root, path))
|
||||
else:
|
||||
return os.path.normpath(path)
|
||||
return os.path.normpath(path)
|
||||
|
||||
def _build_addr_to_ssl(self):
|
||||
"""Builds a map from address to whether it listens on ssl in any server block
|
||||
@@ -381,7 +381,7 @@ class NginxParser(object):
|
||||
if only_directives is not None:
|
||||
new_directives = nginxparser.UnspacedList([])
|
||||
for directive in raw_in_parsed[1]:
|
||||
if len(directive) > 0 and directive[0] in only_directives:
|
||||
if directive and directive[0] in only_directives:
|
||||
new_directives.append(directive)
|
||||
raw_in_parsed[1] = new_directives
|
||||
|
||||
@@ -394,7 +394,7 @@ class NginxParser(object):
|
||||
addr.default = False
|
||||
addr.ipv6only = False
|
||||
for directive in enclosing_block[new_vhost.path[-1]][1]:
|
||||
if len(directive) > 0 and directive[0] == 'listen':
|
||||
if directive and directive[0] == 'listen':
|
||||
# Exclude one-time use parameters which will cause an error if repeated.
|
||||
# https://nginx.org/en/docs/http/ngx_http_core_module.html#listen
|
||||
exclude = set(('default_server', 'default', 'setfib', 'fastopen', 'backlog',
|
||||
@@ -465,19 +465,19 @@ def get_best_match(target_name, names):
|
||||
elif _regex_match(target_name, name):
|
||||
regex.append(name)
|
||||
|
||||
if len(exact) > 0:
|
||||
if exact:
|
||||
# There can be more than one exact match; e.g. eff.org, .eff.org
|
||||
match = min(exact, key=len)
|
||||
return ('exact', match)
|
||||
if len(wildcard_start) > 0:
|
||||
if wildcard_start:
|
||||
# Return the longest wildcard
|
||||
match = max(wildcard_start, key=len)
|
||||
return ('wildcard_start', match)
|
||||
if len(wildcard_end) > 0:
|
||||
if wildcard_end:
|
||||
# Return the longest wildcard
|
||||
match = max(wildcard_end, key=len)
|
||||
return ('wildcard_end', match)
|
||||
if len(regex) > 0:
|
||||
if regex:
|
||||
# Just return the first one for now
|
||||
match = regex[0]
|
||||
return ('regex', match)
|
||||
@@ -522,10 +522,7 @@ def _regex_match(target_name, name):
|
||||
# After tilde is a perl-compatible regex
|
||||
try:
|
||||
regex = re.compile(name[1:])
|
||||
if re.match(regex, target_name):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
return re.match(regex, target_name)
|
||||
except re.error: # pragma: no cover
|
||||
# perl-compatible regexes are sometimes not recognized by python
|
||||
return False
|
||||
|
||||
@@ -13,6 +13,7 @@ logger = logging.getLogger(__name__)
|
||||
COMMENT = " managed by Certbot"
|
||||
COMMENT_BLOCK = ["#", COMMENT]
|
||||
|
||||
|
||||
class Parsable(object):
|
||||
""" Abstract base class for "Parsable" objects whose underlying representation
|
||||
is a tree of lists.
|
||||
@@ -112,6 +113,7 @@ class Parsable(object):
|
||||
"""
|
||||
return [elem.dump(include_spaces) for elem in self._data]
|
||||
|
||||
|
||||
class Statements(Parsable):
|
||||
""" A group or list of "Statements". A Statement is either a Block or a Sentence.
|
||||
|
||||
@@ -142,24 +144,23 @@ class Statements(Parsable):
|
||||
if self.parent is not None:
|
||||
self._trailing_whitespace = "\n" + self.parent.get_tabs()
|
||||
|
||||
def parse(self, parse_this, add_spaces=False):
|
||||
def parse(self, raw_list, add_spaces=False):
|
||||
""" Parses a list of statements.
|
||||
Expects all elements in `parse_this` to be parseable by `type(self).parsing_hooks`,
|
||||
with an optional whitespace string at the last index of `parse_this`.
|
||||
Expects all elements in `raw_list` to be parseable by `type(self).parsing_hooks`,
|
||||
with an optional whitespace string at the last index of `raw_list`.
|
||||
"""
|
||||
if not isinstance(parse_this, list):
|
||||
if not isinstance(raw_list, list):
|
||||
raise errors.MisconfigurationError("Statements parsing expects a list!")
|
||||
# If there's a trailing whitespace in the list of statements, keep track of it.
|
||||
if len(parse_this) > 0 and isinstance(parse_this[-1], six.string_types) \
|
||||
and parse_this[-1].isspace():
|
||||
self._trailing_whitespace = parse_this[-1]
|
||||
parse_this = parse_this[:-1]
|
||||
self._data = [parse_raw(elem, self, add_spaces) for elem in parse_this]
|
||||
if raw_list and isinstance(raw_list[-1], six.string_types) and raw_list[-1].isspace():
|
||||
self._trailing_whitespace = raw_list[-1]
|
||||
raw_list = raw_list[:-1]
|
||||
self._data = [parse_raw(elem, self, add_spaces) for elem in raw_list]
|
||||
|
||||
def get_tabs(self):
|
||||
""" Takes a guess at the tabbing of all contained Statements by retrieving the
|
||||
tabbing of the first Statement."""
|
||||
if len(self._data) > 0:
|
||||
if self._data:
|
||||
return self._data[0].get_tabs()
|
||||
return ""
|
||||
|
||||
@@ -179,6 +180,7 @@ class Statements(Parsable):
|
||||
|
||||
# ======== End overridden functions
|
||||
|
||||
|
||||
def _space_list(list_):
|
||||
""" Inserts whitespace between adjacent non-whitespace tokens. """
|
||||
spaced_statement = [] # type: List[str]
|
||||
@@ -188,6 +190,7 @@ def _space_list(list_):
|
||||
spaced_statement.insert(0, " ")
|
||||
return spaced_statement
|
||||
|
||||
|
||||
class Sentence(Parsable):
|
||||
""" A list of words. Non-whitespace words are typically separated with whitespace tokens. """
|
||||
|
||||
@@ -205,15 +208,15 @@ class Sentence(Parsable):
|
||||
return isinstance(lists, list) and len(lists) > 0 and \
|
||||
all([isinstance(elem, six.string_types) for elem in lists])
|
||||
|
||||
def parse(self, parse_this, add_spaces=False):
|
||||
def parse(self, raw_list, add_spaces=False):
|
||||
""" Parses a list of string types into this object.
|
||||
If add_spaces is set, adds whitespace tokens between adjacent non-whitespace tokens."""
|
||||
if add_spaces:
|
||||
parse_this = _space_list(parse_this)
|
||||
if not isinstance(parse_this, list) or \
|
||||
any([not isinstance(elem, six.string_types) for elem in parse_this]):
|
||||
raw_list = _space_list(raw_list)
|
||||
if not isinstance(raw_list, list) or \
|
||||
any([not isinstance(elem, six.string_types) for elem in raw_list]):
|
||||
raise errors.MisconfigurationError("Sentence parsing expects a list of string types.")
|
||||
self._data = parse_this
|
||||
self._data = raw_list
|
||||
|
||||
def iterate(self, expanded=False, match=None):
|
||||
""" Simply yields itself. """
|
||||
@@ -255,6 +258,7 @@ class Sentence(Parsable):
|
||||
def __contains__(self, word):
|
||||
return word in self.words
|
||||
|
||||
|
||||
class Block(Parsable):
|
||||
""" Any sort of bloc, denoted by a block name and curly braces, like so:
|
||||
The parsed block:
|
||||
@@ -297,26 +301,26 @@ class Block(Parsable):
|
||||
for elem in self.contents.iterate(expanded, match):
|
||||
yield elem
|
||||
|
||||
def parse(self, parse_this, add_spaces=False):
|
||||
def parse(self, raw_list, add_spaces=False):
|
||||
""" Parses a list that resembles a block.
|
||||
|
||||
The assumptions that this routine makes are:
|
||||
1. the first element of `parse_this` is a valid Sentence.
|
||||
2. the second element of `parse_this` is a valid Statement.
|
||||
1. the first element of `raw_list` is a valid Sentence.
|
||||
2. the second element of `raw_list` is a valid Statement.
|
||||
If add_spaces is set, we call it recursively on `names` and `contents`, and
|
||||
add an extra trailing space to `names` (to separate the block's opening bracket
|
||||
and the block name).
|
||||
"""
|
||||
if not Block.should_parse(parse_this):
|
||||
if not Block.should_parse(raw_list):
|
||||
raise errors.MisconfigurationError("Block parsing expects a list of length 2. "
|
||||
"First element should be a list of string types (the bloc names), "
|
||||
"and second should be another list of statements (the bloc content).")
|
||||
self.names = Sentence(self)
|
||||
if add_spaces:
|
||||
parse_this[0].append(" ")
|
||||
self.names.parse(parse_this[0], add_spaces)
|
||||
raw_list[0].append(" ")
|
||||
self.names.parse(raw_list[0], add_spaces)
|
||||
self.contents = Statements(self)
|
||||
self.contents.parse(parse_this[1], add_spaces)
|
||||
self.contents.parse(raw_list[1], add_spaces)
|
||||
self._data = [self.names, self.contents]
|
||||
|
||||
def get_tabs(self):
|
||||
|
||||
@@ -17,9 +17,6 @@ from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-m
|
||||
class NginxParserTest(util.NginxTest): #pylint: disable=too-many-public-methods
|
||||
"""Nginx Parser Test."""
|
||||
|
||||
def setUp(self):
|
||||
super(NginxParserTest, self).setUp()
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.temp_dir)
|
||||
shutil.rmtree(self.config_dir)
|
||||
|
||||
@@ -1,21 +1,19 @@
|
||||
"""Common utilities for certbot_nginx."""
|
||||
import copy
|
||||
import os
|
||||
import pkg_resources
|
||||
import shutil
|
||||
import tempfile
|
||||
import unittest
|
||||
import shutil
|
||||
import warnings
|
||||
|
||||
import josepy as jose
|
||||
import mock
|
||||
import pkg_resources
|
||||
import zope.component
|
||||
|
||||
from certbot import configuration
|
||||
|
||||
from certbot.tests import util as test_util
|
||||
|
||||
from certbot.plugins import common
|
||||
from certbot.tests import util as test_util
|
||||
|
||||
from certbot_nginx import configurator
|
||||
from certbot_nginx import nginxparser
|
||||
@@ -131,8 +129,7 @@ def contains_at_depth(haystack, needle, n):
|
||||
return False
|
||||
if n == 0:
|
||||
return needle in haystack
|
||||
else:
|
||||
for item in haystack:
|
||||
if contains_at_depth(item, needle, n - 1):
|
||||
return True
|
||||
return False
|
||||
for item in haystack:
|
||||
if contains_at_depth(item, needle, n - 1):
|
||||
return True
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user