cleanup and refactoring

This commit is contained in:
Robert Kaussow 2020-04-11 13:17:01 +02:00
parent 2b3385b13c
commit 76ddfb5a41
9 changed files with 111 additions and 63 deletions

View File

@ -6,7 +6,7 @@ local PythonVersion(pyversion="3.5") = {
PY_COLORS: 1 PY_COLORS: 1
}, },
commands: [ commands: [
"pip install -r test-requirements.txt -qq", "pip install -r dev-requirements.txt -qq",
"pip install -qq .", "pip install -qq .",
"git-batch --help", "git-batch --help",
], ],
@ -25,13 +25,13 @@ local PipelineLint = {
steps: [ steps: [
{ {
name: "flake8", name: "flake8",
image: "python:3.7", image: "python:3.8",
pull: "always", pull: "always",
environment: { environment: {
PY_COLORS: 1 PY_COLORS: 1
}, },
commands: [ commands: [
"pip install -r test-requirements.txt -qq", "pip install -r dev-requirements.txt -qq",
"pip install -qq .", "pip install -qq .",
"flake8 ./gitbatch", "flake8 ./gitbatch",
], ],
@ -73,13 +73,13 @@ local PipelineSecurity = {
steps: [ steps: [
{ {
name: "bandit", name: "bandit",
image: "python:3.7", image: "python:3.8",
pull: "always", pull: "always",
environment: { environment: {
PY_COLORS: 1 PY_COLORS: 1
}, },
commands: [ commands: [
"pip install -r test-requirements.txt -qq", "pip install -r dev-requirements.txt -qq",
"pip install -qq .", "pip install -qq .",
"bandit -r ./gitbatch -x ./gitbatch/tests", "bandit -r ./gitbatch -x ./gitbatch/tests",
], ],
@ -103,7 +103,7 @@ local PipelineBuildContainer(arch="amd64") = {
steps: [ steps: [
{ {
name: "build", name: "build",
image: "python:3.7", image: "python:3.8",
pull: "always", pull: "always",
commands: [ commands: [
"python setup.py bdist_wheel", "python setup.py bdist_wheel",

View File

@ -9,9 +9,9 @@ platform:
steps: steps:
- name: flake8 - name: flake8
pull: always pull: always
image: python:3.7 image: python:3.8
commands: commands:
- pip install -r test-requirements.txt -qq - pip install -r dev-requirements.txt -qq
- pip install -qq . - pip install -qq .
- flake8 ./gitbatch - flake8 ./gitbatch
environment: environment:
@ -36,7 +36,7 @@ steps:
pull: always pull: always
image: python:3.5 image: python:3.5
commands: commands:
- pip install -r test-requirements.txt -qq - pip install -r dev-requirements.txt -qq
- pip install -qq . - pip install -qq .
- git-batch --help - git-batch --help
environment: environment:
@ -48,7 +48,7 @@ steps:
pull: always pull: always
image: python:3.6 image: python:3.6
commands: commands:
- pip install -r test-requirements.txt -qq - pip install -r dev-requirements.txt -qq
- pip install -qq . - pip install -qq .
- git-batch --help - git-batch --help
environment: environment:
@ -60,7 +60,7 @@ steps:
pull: always pull: always
image: python:3.7 image: python:3.7
commands: commands:
- pip install -r test-requirements.txt -qq - pip install -r dev-requirements.txt -qq
- pip install -qq . - pip install -qq .
- git-batch --help - git-batch --help
environment: environment:
@ -72,7 +72,7 @@ steps:
pull: always pull: always
image: python:3.8 image: python:3.8
commands: commands:
- pip install -r test-requirements.txt -qq - pip install -r dev-requirements.txt -qq
- pip install -qq . - pip install -qq .
- git-batch --help - git-batch --help
environment: environment:
@ -100,9 +100,9 @@ platform:
steps: steps:
- name: bandit - name: bandit
pull: always pull: always
image: python:3.7 image: python:3.8
commands: commands:
- pip install -r test-requirements.txt -qq - pip install -r dev-requirements.txt -qq
- pip install -qq . - pip install -qq .
- bandit -r ./gitbatch -x ./gitbatch/tests - bandit -r ./gitbatch -x ./gitbatch/tests
environment: environment:
@ -128,7 +128,7 @@ platform:
steps: steps:
- name: build - name: build
pull: always pull: always
image: python:3.7 image: python:3.8
commands: commands:
- python setup.py bdist_wheel - python setup.py bdist_wheel
@ -184,7 +184,7 @@ platform:
steps: steps:
- name: build - name: build
pull: always pull: always
image: python:3.7 image: python:3.8
commands: commands:
- python setup.py bdist_wheel - python setup.py bdist_wheel
@ -240,7 +240,7 @@ platform:
steps: steps:
- name: build - name: build
pull: always pull: always
image: python:3.7 image: python:3.8
commands: commands:
- python setup.py bdist_wheel - python setup.py bdist_wheel
@ -366,6 +366,6 @@ depends_on:
--- ---
kind: signature kind: signature
hmac: c8a46e748269c79caf345ebfcb117a5bd78b6e62a40de8ce492a165a0fa7789d hmac: b0b80a9e961c9d56a25b583c0e5404b09039a725be5e635670d7c2ef346add5e
... ...

18
.flake8
View File

@ -1,8 +1,18 @@
[flake8] [flake8]
# Temp disable Docstring checks D101, D102, D103, D107 ignore = D102, D103, D107, D202, W503
ignore = E501, W503, F401, N813, D101, D102, D103, D107 max-line-length = 99
max-line-length = 110
inline-quotes = double inline-quotes = double
exclude = .git,.tox,__pycache__,build,dist,tests,*.pyc,*.egg-info,.cache,.eggs,env* exclude =
.git
.tox
__pycache__
build
dist
tests
*.pyc
*.egg-info
.cache
.eggs
env*
application-import-names = ansiblelater application-import-names = ansiblelater
format = ${cyan}%(path)s:%(row)d:%(col)d${reset}: ${red_bold}%(code)s${reset} %(text)s format = ${cyan}%(path)s:%(row)d:%(col)d${reset}: ${red_bold}%(code)s${reset} %(text)s

View File

@ -1,19 +1,18 @@
# open issue
# https://gitlab.com/pycqa/flake8-docstrings/issues/36
pydocstyle<4.0.0 pydocstyle<4.0.0
flake8 flake8
flake8-colors flake8-colors
flake8-blind-except flake8-blind-except
flake8-builtins flake8-builtins
flake8-colors
flake8-docstrings<=3.0.0 flake8-docstrings<=3.0.0
flake8-isort flake8-isort
flake8-logging-format flake8-logging-format
flake8-polyfill flake8-polyfill
flake8-quotes flake8-quotes
flake8-eradicate; python_version >= "3.6"
pep8-naming pep8-naming
wheel wheel
pytest pytest
pytest-mock pytest-mock
pytest-cov pytest-cov
bandit bandit
yapf

View File

@ -2,9 +2,7 @@
"""Main program.""" """Main program."""
import argparse import argparse
import logging
import os import os
import sys
from collections import defaultdict from collections import defaultdict
from urllib.parse import urlparse from urllib.parse import urlparse
@ -17,6 +15,7 @@ from gitbatch.Utils import to_bool
class GitBatch: class GitBatch:
"""Handles git operations."""
def __init__(self): def __init__(self):
self.log = SingleLog() self.log = SingleLog()
@ -27,8 +26,11 @@ class GitBatch:
def _cli_args(self): def _cli_args(self):
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description=("Clone single branch from all repositories listed in a file")) description=("Clone single branch from all repositories listed in a file")
parser.add_argument("--version", action="version", version="%(prog)s {}".format(__version__)) )
parser.add_argument(
"--version", action="version", version="%(prog)s {}".format(__version__)
)
return parser.parse_args() return parser.parse_args()
@ -52,8 +54,11 @@ class GitBatch:
try: try:
url, branch, dest = [x.strip() for x in line.split(";")] url, branch, dest = [x.strip() for x in line.split(";")]
except ValueError as e: except ValueError as e:
self.log.sysexit_with_message("Wrong numer of delimiters in line {line_num}: {exp}".format( self.log.sysexit_with_message(
line_num=num, exp=e)) "Wrong numer of delimiters in line {line_num}: {exp}".format(
line_num=num, exp=e
)
)
if url: if url:
url_parts = urlparse(url) url_parts = urlparse(url)
@ -61,21 +66,21 @@ class GitBatch:
repo["url"] = url repo["url"] = url
repo["branch"] = branch or "master" repo["branch"] = branch or "master"
repo["name"] = os.path.basename(url_parts.path) repo["name"] = os.path.basename(url_parts.path)
repo["dest"] = normalize_path(dest) or normalize_path("./{}".format(repo["name"])) repo["dest"] = normalize_path(dest) or normalize_path(
"./{}".format(repo["name"])
)
repos.append(repo) repos.append(repo)
else: else:
self.log.sysexit_with_message("Repository Url is not set on line {line_num}".format( self.log.sysexit_with_message(
line_num=num)) "Repository Url is not set on line {line_num}".format(line_num=num)
)
return repos return repos
def _repos_clone(self, repos, ignore_existing): def _repos_clone(self, repos, ignore_existing):
for repo in repos: for repo in repos:
try: try:
options = [ options = ["--branch={}".format(repo["branch"]), "--single-branch"]
"--branch={}".format(repo["branch"]),
"--single-branch"
]
git.Repo.clone_from(repo["url"], repo["dest"], multi_options=options) git.Repo.clone_from(repo["url"], repo["dest"], multi_options=options)
except git.exc.GitCommandError as e: except git.exc.GitCommandError as e:
passed = False passed = False
@ -98,5 +103,8 @@ class GitBatch:
repos = self._repos_from_file(self.config["input_file"]) repos = self._repos_from_file(self.config["input_file"])
self._repos_clone(repos, self.config["ignore_existing"]) self._repos_clone(repos, self.config["ignore_existing"])
else: else:
self.log.sysexit_with_message("The given batch file at '{}' does not exist".format( self.log.sysexit_with_message(
os.path.relpath(os.path.join("./", self.config["input_file"])))) "The given batch file at '{}' does not exist".format(
os.path.relpath(os.path.join("./", self.config["input_file"]))
)
)

View File

@ -8,7 +8,6 @@ import sys
import colorama import colorama
from pythonjsonlogger import jsonlogger from pythonjsonlogger import jsonlogger
import gitbatch.Utils
from gitbatch.Utils import Singleton from gitbatch.Utils import Singleton
from gitbatch.Utils import to_bool from gitbatch.Utils import to_bool
@ -48,7 +47,7 @@ class LogFilter(object):
class MultilineFormatter(logging.Formatter): class MultilineFormatter(logging.Formatter):
"""Logging Formatter to reset color after newline characters.""" """Logging Formatter to reset color after newline characters."""
def format(self, record): # noqa def format(self, record): # noqa
record.msg = record.msg.replace("\n", "\n{}... ".format(colorama.Style.RESET_ALL)) record.msg = record.msg.replace("\n", "\n{}... ".format(colorama.Style.RESET_ALL))
return logging.Formatter.format(self, record) return logging.Formatter.format(self, record)
@ -56,12 +55,14 @@ class MultilineFormatter(logging.Formatter):
class MultilineJsonFormatter(jsonlogger.JsonFormatter): class MultilineJsonFormatter(jsonlogger.JsonFormatter):
"""Logging Formatter to remove newline characters.""" """Logging Formatter to remove newline characters."""
def format(self, record): # noqa def format(self, record): # noqa
record.msg = record.msg.replace("\n", " ") record.msg = record.msg.replace("\n", " ")
return jsonlogger.JsonFormatter.format(self, record) return jsonlogger.JsonFormatter.format(self, record)
class Log: class Log:
"""Base logging object."""
def __init__(self, level=logging.WARN, name="ansibledoctor", json=False): def __init__(self, level=logging.WARN, name="ansibledoctor", json=False):
self.logger = logging.getLogger(name) self.logger = logging.getLogger(name)
self.logger.setLevel(level) self.logger.setLevel(level)
@ -76,8 +77,11 @@ class Log:
handler = logging.StreamHandler(sys.stderr) handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.ERROR) handler.setLevel(logging.ERROR)
handler.addFilter(LogFilter(logging.ERROR)) handler.addFilter(LogFilter(logging.ERROR))
handler.setFormatter(MultilineFormatter( handler.setFormatter(
self.error(CONSOLE_FORMAT.format(colorama.Fore.RED, colorama.Style.RESET_ALL)))) MultilineFormatter(
self.error(CONSOLE_FORMAT.format(colorama.Fore.RED, colorama.Style.RESET_ALL))
)
)
if json: if json:
handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT)) handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT))
@ -88,8 +92,11 @@ class Log:
handler = logging.StreamHandler(sys.stdout) handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.WARN) handler.setLevel(logging.WARN)
handler.addFilter(LogFilter(logging.WARN)) handler.addFilter(LogFilter(logging.WARN))
handler.setFormatter(MultilineFormatter( handler.setFormatter(
self.warn(CONSOLE_FORMAT.format(colorama.Fore.YELLOW, colorama.Style.RESET_ALL)))) MultilineFormatter(
self.warn(CONSOLE_FORMAT.format(colorama.Fore.YELLOW, colorama.Style.RESET_ALL))
)
)
if json: if json:
handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT)) handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT))
@ -100,8 +107,11 @@ class Log:
handler = logging.StreamHandler(sys.stdout) handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO) handler.setLevel(logging.INFO)
handler.addFilter(LogFilter(logging.INFO)) handler.addFilter(LogFilter(logging.INFO))
handler.setFormatter(MultilineFormatter( handler.setFormatter(
self.info(CONSOLE_FORMAT.format(colorama.Fore.CYAN, colorama.Style.RESET_ALL)))) MultilineFormatter(
self.info(CONSOLE_FORMAT.format(colorama.Fore.CYAN, colorama.Style.RESET_ALL))
)
)
if json: if json:
handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT)) handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT))
@ -112,8 +122,11 @@ class Log:
handler = logging.StreamHandler(sys.stderr) handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.CRITICAL) handler.setLevel(logging.CRITICAL)
handler.addFilter(LogFilter(logging.CRITICAL)) handler.addFilter(LogFilter(logging.CRITICAL))
handler.setFormatter(MultilineFormatter( handler.setFormatter(
self.critical(CONSOLE_FORMAT.format(colorama.Fore.RED, colorama.Style.RESET_ALL)))) MultilineFormatter(
self.critical(CONSOLE_FORMAT.format(colorama.Fore.RED, colorama.Style.RESET_ALL))
)
)
if json: if json:
handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT)) handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT))
@ -124,8 +137,11 @@ class Log:
handler = logging.StreamHandler(sys.stderr) handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.DEBUG) handler.setLevel(logging.DEBUG)
handler.addFilter(LogFilter(logging.DEBUG)) handler.addFilter(LogFilter(logging.DEBUG))
handler.setFormatter(MultilineFormatter( handler.setFormatter(
self.critical(CONSOLE_FORMAT.format(colorama.Fore.BLUE, colorama.Style.RESET_ALL)))) MultilineFormatter(
self.critical(CONSOLE_FORMAT.format(colorama.Fore.BLUE, colorama.Style.RESET_ALL))
)
)
if json: if json:
handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT)) handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT))
@ -175,4 +191,6 @@ class Log:
class SingleLog(Log, metaclass=Singleton): class SingleLog(Log, metaclass=Singleton):
"""Singleton logger object."""
pass pass

View File

@ -2,7 +2,6 @@
"""Global utility methods and classes.""" """Global utility methods and classes."""
import os import os
import sys
from distutils.util import strtobool from distutils.util import strtobool
@ -16,6 +15,8 @@ def to_bool(string):
class Singleton(type): class Singleton(type):
"""Meta singleton class."""
_instances = {} _instances = {}
def __call__(cls, *args, **kwargs): def __call__(cls, *args, **kwargs):

View File

@ -10,11 +10,21 @@ default_section = THIRDPARTY
known_first_party = gitbatch known_first_party = gitbatch
sections = FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER sections = FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
force_single_line = true force_single_line = true
line_length = 110 line_length = 99
skip_glob = **/env/* skip_glob = **/.env*,**/env/*,**/docs/*
[yapf]
based_on_style = google
column_limit = 99
dedent_closing_brackets = true
coalesce_brackets = true
split_before_logical_operator = true
[tool:pytest] [tool:pytest]
filterwarnings = filterwarnings =
ignore::FutureWarning ignore::FutureWarning
ignore:.*collections.*:DeprecationWarning ignore:.*collections.*:DeprecationWarning
ignore:.*pep8.*:FutureWarning ignore:.*pep8.*:FutureWarning
[coverage:run]
omit = **/test/*

View File

@ -30,28 +30,31 @@ setup(
name=get_property("__project__", PACKAGE_NAME), name=get_property("__project__", PACKAGE_NAME),
version=get_property("__version__", PACKAGE_NAME), version=get_property("__version__", PACKAGE_NAME),
description="Clone single branch from all repositories listed in a file.", description="Clone single branch from all repositories listed in a file.",
keywords="git, batch, automation",
author=get_property("__author__", PACKAGE_NAME), author=get_property("__author__", PACKAGE_NAME),
author_email=get_property("__email__", PACKAGE_NAME), author_email=get_property("__email__", PACKAGE_NAME),
url=get_property("__url__", PACKAGE_NAME), url=get_property("__url__", PACKAGE_NAME),
license=get_property("__license__", PACKAGE_NAME), license=get_property("__license__", PACKAGE_NAME),
long_description=get_readme(), long_description=get_readme(),
long_description_content_type="text/markdown", long_description_content_type="text/markdown",
packages=find_packages(exclude=["*.tests", "tests", "tests.*"]), packages=find_packages(exclude=["*.test", "test", "test.*"]),
include_package_data=True, include_package_data=True,
zip_safe=False, zip_safe=False,
python_requires=">=3.5", python_requires=">=3.5,<4",
classifiers=[ classifiers=[
"Development Status :: 5 - Production/Stable", "Development Status :: 5 - Production/Stable",
"Environment :: Console", "Environment :: Console",
"License :: OSI Approved :: MIT License",
"Intended Audience :: Developers", "Intended Audience :: Developers",
"Intended Audience :: Information Technology", "Intended Audience :: Information Technology",
"Intended Audience :: System Administrators", "Intended Audience :: System Administrators",
"License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)",
"Natural Language :: English", "Natural Language :: English",
"Operating System :: POSIX", "Operating System :: POSIX",
"Programming Language :: Python :: 3 :: Only", "Programming Language :: Python :: 3",
"Topic :: System :: Installation/Setup", "Programming Language :: Python :: 3.5",
"Topic :: System :: Systems Administration", "Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Topic :: Utilities", "Topic :: Utilities",
"Topic :: Software Development", "Topic :: Software Development",
"Topic :: Software Development :: Documentation", "Topic :: Software Development :: Documentation",
@ -66,5 +69,4 @@ setup(
"git-batch = gitbatch.__main__:main" "git-batch = gitbatch.__main__:main"
] ]
}, },
test_suite="tests"
) )