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
commands:
- 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
test-flake8:

View file

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