Fix some pylint warnings and configure pre-commit to run all tests on commit
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Benjamin Renard 2023-01-19 14:08:49 +01:00
parent 4dce16df85
commit 0040605d88
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
3 changed files with 79 additions and 51 deletions

31
.pre-commit-config.yaml Normal file
View file

@ -0,0 +1,31 @@
# Pre-commit hooks to run tests and ensure code is cleaned.
# See https://pre-commit.com for more information
repos:
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
require_serial: true
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
args: ['--max-line-length=100']
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
args: ['--keep-percent-format', '--py37-plus']
- repo: https://github.com/psf/black
rev: 22.12.0
hooks:
- id: black
args: ['--target-version', 'py37', '--line-length', '100']
- repo: https://github.com/PyCQA/isort
rev: 5.11.4
hooks:
- id: isort
args: ['--profile', 'black', '--line-length', '100']

View file

@ -9,7 +9,7 @@ pipeline:
image: debian:stable-slim image: debian:stable-slim
commands: commands:
- DEBIAN_FRONTEND=noninteractive apt-get -qq update < /dev/null > /dev/null - DEBIAN_FRONTEND=noninteractive apt-get -qq update < /dev/null > /dev/null
- DEBIAN_FRONTEND=noninteractive apt-get -qq -y install --no-install-recommends python3-requests pylint3 < /dev/null > /dev/null - DEBIAN_FRONTEND=noninteractive apt-get -qq -y install --no-install-recommends python3-requests python3-xmltodict pylint3 < /dev/null > /dev/null
- python3 -m pylint check_forgejo_upgrade - python3 -m pylint check_forgejo_upgrade
test-flake8: test-flake8:

View file

@ -29,81 +29,78 @@ import xmltodict
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-d', '--debug', action='store_true') parser.add_argument("-d", "--debug", action="store_true")
parser.add_argument("-p", "--path", type=str, help="Forgejo bin path", default="forgejo")
parser.add_argument( parser.add_argument(
'-p', '--path', type=str, "-U",
help='Forgejo bin path', default='forgejo') "--url",
type=str,
help="Forgejo releases RSS URL",
default="https://forgejo.org/releases/rss.xml",
)
parser.add_argument( parser.add_argument(
'-U', '--url', type=str, "--rc",
help='Forgejo releases RSS URL', action="store_true",
default='https://forgejo.org/releases/rss.xml') dest="include_rc",
parser.add_argument( help=("Allow release candidate (default: only stable release are " "considered)"),
'--rc', action='store_true', dest='include_rc',
help=(
'Allow release candidate (default: only stable release are '
'considered)')
) )
options = parser.parse_args() options = parser.parse_args()
logging.basicConfig(level=logging.DEBUG if options.debug else logging.WARNING) logging.basicConfig(level=logging.DEBUG if options.debug else logging.WARNING)
current = None CURRENT = None
cmd = [options.path, '--version'] cmd = [options.path, "--version"]
logging.debug( logging.debug("Command use to retreive current version of Forgejo: %s", " ".join(cmd))
'Command use to retreive current version of Forgejo: %s', OUTPUT = None
' '.join(cmd)) EXCEPTION = None
out = None
exc = None
try: try:
out = subprocess.check_output(cmd) OUTPUT = subprocess.check_output(cmd)
logging.debug('Output:\n%s', out) logging.debug("Output:\n%s", OUTPUT)
m = re.search('version ([^ ]+) built', out.decode('utf8', errors='ignore')) m = re.search("version ([^ ]+) built", OUTPUT.decode("utf8", errors="ignore"))
if m: if m:
current = m.group(1) CURRENT = m.group(1)
except Exception as err: except Exception as err: # pylint: disable=broad-except
exc = err EXCEPTION = err
logging.debug('Current version: %s', current) logging.debug("Current version: %s", CURRENT)
if not current: if not CURRENT:
print('UNKNOWN - Fail to retreive current Forgejo') print("UNKNOWN - Fail to retreive current Forgejo")
print(f'Command: {" ".join(cmd)}') print(f'Command: {" ".join(cmd)}')
print('Output:') print("Output:")
print(out if out else "") print(OUTPUT if OUTPUT else "")
print('Exception:') print("Exception:")
print(exc if exc else "") print(EXCEPTION if EXCEPTION else "")
sys.exit(3) sys.exit(3)
latest = None LATEST = None
try: try:
logging.debug('Get releases RSS feed from %s...', options.url) logging.debug("Get releases RSS feed from %s...", options.url)
r = requests.get(options.url) r = requests.get(options.url)
logging.debug('Data retreive:\n%s', r.text) logging.debug("Data retreive:\n%s", r.text)
data = xmltodict.parse(r.text) data = xmltodict.parse(r.text)
for item in data['rss']['channel']['item']: for item in data["rss"]["channel"]["item"]:
version = re.sub('^v', '', item['title']) version = re.sub("^v", "", item["title"])
if not options.include_rc and '-rc' in version: if not options.include_rc and "-rc" in version:
logging.debug('Ignore release candidate %s', version) logging.debug("Ignore release candidate %s", version)
continue continue
latest = version LATEST = version
break break
except Exception: except Exception: # pylint: disable=broad-except
pass pass
logging.debug('Latest version: %s', current) logging.debug("Latest version: %s", LATEST)
if not latest: if not LATEST:
print( print("UNKNOWN - Fail to retreive latest Forgejo release from the project " "RSS feed")
'UNKNOWN - Fail to retreive latest Forgejo release from the project ' print(f"Current version: {CURRENT}")
'RSS feed')
print(f'Current version: {current}')
sys.exit(3) sys.exit(3)
if latest == current: if LATEST == CURRENT:
print(f'OK - The latest release of Forgejo is currently used ({latest})') print(f"OK - The latest release of Forgejo is currently used ({LATEST})")
sys.exit(0) sys.exit(0)
print( print(
'WARNING - The version of Forgejo currently used is not the latest ' "WARNING - The version of Forgejo currently used is not the latest " f"({CURRENT} vs {LATEST})"
f'({current} vs {latest})') )
sys.exit(1) sys.exit(1)