From bee3598ba6dc0b0cd0baac009cd4966697f9b536 Mon Sep 17 00:00:00 2001 From: Nathan Ruiz Date: Fri, 13 Sep 2024 17:22:05 +0000 Subject: [PATCH] ci/lint: add python linting Enabled the ruff python linter in CI, and resolved the issues that were detected by it. Ruff was used due to it's faster checking compared with other linters like pylint. This was added to resolve the python linting request in #13608. --- .github/workflows/lint.yml | 6 ++++++ TOOLS/docutils-wrapper.py | 2 +- TOOLS/dylib_unhell.py | 20 ++++++++++---------- TOOLS/macos-sdk-version.py | 4 +--- TOOLS/matroska.py | 21 +++++++++++---------- TOOLS/stats-conv.py | 2 +- ci/lint-commit-msg.py | 6 +++++- 7 files changed, 35 insertions(+), 26 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 425e8d1a07..3aece2f4d7 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -33,3 +33,9 @@ jobs: steps: - uses: actions/checkout@v4 - uses: lunarmodules/luacheck@v1 + + python-lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: chartboost/ruff-action@v1 diff --git a/TOOLS/docutils-wrapper.py b/TOOLS/docutils-wrapper.py index 31ba976e64..8e4ce57dc5 100755 --- a/TOOLS/docutils-wrapper.py +++ b/TOOLS/docutils-wrapper.py @@ -58,7 +58,7 @@ try: proc = subprocess.run(argv, check=True) if depfile is not None: convert_depfile(output, depfile) -except: +except Exception: remove(output) if depfile is not None: remove(depfile) diff --git a/TOOLS/dylib_unhell.py b/TOOLS/dylib_unhell.py index c885969284..ed2136554d 100755 --- a/TOOLS/dylib_unhell.py +++ b/TOOLS/dylib_unhell.py @@ -16,13 +16,13 @@ def is_user_lib(objfile, libname): return not sys_re.match(libname) and \ not usr_re.match(libname) and \ not exe_re.match(libname) and \ - not "libobjc." in libname and \ - not "libSystem." in libname and \ - not "libc." in libname and \ - not "libgcc." in libname and \ + "libobjc." not in libname and \ + "libSystem." not in libname and \ + "libc." not in libname and \ + "libgcc." not in libname and \ not os.path.basename(libname) == 'Python' and \ - not os.path.basename(objfile) in libname and \ - not "libswift" in libname + os.path.basename(objfile) not in libname and \ + "libswift" not in libname def otool(objfile, rapths): command = "otool -L '%s' | grep -e '\t' | awk '{ print $1 }'" % objfile @@ -46,7 +46,7 @@ def get_rapths(objfile): try: result = subprocess.check_output(command, shell = True, universal_newlines=True) - except: + except Exception: return rpaths for line in result.splitlines(): @@ -90,9 +90,9 @@ def resolve_lib_path(objfile, lib, rapths): def check_vulkan_max_version(version): try: - result = subprocess.check_output("pkg-config vulkan --max-version=" + version, shell = True) + subprocess.check_output("pkg-config vulkan --max-version=" + version, shell = True) return True - except: + except Exception: return False def get_homebrew_prefix(): @@ -100,7 +100,7 @@ def get_homebrew_prefix(): result = "/opt/homebrew" try: result = subprocess.check_output("brew --prefix", universal_newlines=True, shell=True, stderr=subprocess.DEVNULL).strip() - except: + except Exception: pass return result diff --git a/TOOLS/macos-sdk-version.py b/TOOLS/macos-sdk-version.py index 12e1071d75..eadfda414d 100755 --- a/TOOLS/macos-sdk-version.py +++ b/TOOLS/macos-sdk-version.py @@ -3,9 +3,7 @@ # This checks for the sdk path, the sdk version, and # the sdk build version. -import re import os -import string import subprocess import sys from shutil import which @@ -33,7 +31,7 @@ def find_macos_sdk(): try: sdk_version = check_output([xcodebuild, '-sdk', 'macosx', '-version', 'ProductVersion'], encoding="UTF-8", stderr=subprocess.DEVNULL) - except: + except Exception: pass if not isinstance(sdk_version, str): diff --git a/TOOLS/matroska.py b/TOOLS/matroska.py index 6abece6e95..445e222372 100755 --- a/TOOLS/matroska.py +++ b/TOOLS/matroska.py @@ -4,6 +4,10 @@ Generate C definitions for parsing Matroska files. Can also be used to directly parse Matroska files and display their contents. """ +import sys +from math import ldexp +from binascii import hexlify + # # This file is part of mpv. # @@ -242,14 +246,11 @@ elements_matroska = ( ) -import sys -from math import ldexp -from binascii import hexlify - def byte2num(s): return int(hexlify(s), 16) -class EOF(Exception): pass +class EOF(Exception): + pass def camelcase_to_words(name): parts = [] @@ -291,9 +292,9 @@ class MatroskaElement(object): elementd = {} elementlist = [] -def parse_elems(l, namespace): +def parse_elems(elements, namespace): subelements = [] - for el in l: + for el in elements: if isinstance(el, str): name, hexid, eltype = [x.strip() for x in el.split(',')] hexid = hexid.lower() @@ -328,10 +329,10 @@ def generate_C_header(out): continue printf(out) printf(out, 'struct {0.structname} {{'.format(el)) - l = max(len(subel.valname) for subel, multiple in el.subelements)+1 + length = max(len(subel.valname) for subel, multiple in el.subelements)+1 for subel, multiple in el.subelements: - printf(out, ' {e.valname:{l}} {star}{e.fieldname};'.format( - e=subel, l=l, star=' *'[multiple])) + printf(out, ' {e.valname:{length}} {star}{e.fieldname};'.format( + e=subel, length=length, star=' *'[multiple])) printf(out) for subel, multiple in el.subelements: printf(out, ' int n_{0.fieldname};'.format(subel)) diff --git a/TOOLS/stats-conv.py b/TOOLS/stats-conv.py index 0a9cf4ee9e..774e77bdcc 100755 --- a/TOOLS/stats-conv.py +++ b/TOOLS/stats-conv.py @@ -161,7 +161,7 @@ for cur in ax: for e in G.sevents: cur = ax[1 if e.type == "value" else 0] - if not cur in G.curveno: + if cur not in G.curveno: G.curveno[cur] = 0 args = {'name': e.name,'antialias':True} color = mkColor(colors[G.curveno[cur] % len(colors)]) diff --git a/ci/lint-commit-msg.py b/ci/lint-commit-msg.py index 3ceaa24fbf..36497b66e4 100755 --- a/ci/lint-commit-msg.py +++ b/ci/lint-commit-msg.py @@ -1,5 +1,9 @@ #!/usr/bin/env python3 -import os, sys, json, subprocess, re +import os +import sys +import json +import subprocess +import re from typing import Dict, Tuple, Callable, Optional def call(cmd) -> str: