Commit 8cdb208b authored by Luke Mewburn's avatar Luke Mewburn
Browse files

testing: reformat *.py with ruff

parent b37f94f0
Loading
Loading
Loading
Loading
+61 −63
Original line number Diff line number Diff line
@@ -8,24 +8,30 @@ from pycrate_asn1c.asnproc import *

import lint_asn1


def reconstrainInteger(filename):
    Path('temp.asn').write_text(Path(filename).read_text().replace("18446744073709551615", "65536"))
    return 'temp.asn'
    Path("temp.asn").write_text(
        Path(filename).read_text().replace("18446744073709551615", "65536")
    )
    return "temp.asn"


filesWithBigInts = [
    'testing/dependencies/asn/stubs/LI-PS-PDU.asn',
    'testing/dependencies/asn/IPAccessPDU.asn'
    "testing/dependencies/asn/stubs/LI-PS-PDU.asn",
    "testing/dependencies/asn/IPAccessPDU.asn",
]

asn1c_path = ""
change_path_to_unix = False


def fix_path(path):
    if change_path_to_unix:
        return "./" + path.replace("\\", "/")
    else:
        return path


def syntaxCheckASN(fileList):
    """
    Performs ASN syntax checking on a list of filenames (or pathlib Paths)
@@ -41,33 +47,28 @@ def syntaxCheckASN (fileList):
        try:
            if file.as_posix() in filesWithBigInts:
                newFile = reconstrainInteger(str(file))
                p = run([asn1c_path, '-E', fix_path(newFile)], capture_output=True)
                p = run([asn1c_path, "-E", fix_path(newFile)], capture_output=True)
                Path(newFile).unlink()
            else:
                p = run([asn1c_path, '-E', fix_path(str(file))], capture_output=True)
            if (p.returncode != 0):
                p = run([asn1c_path, "-E", fix_path(str(file))], capture_output=True)
            if p.returncode != 0:
                errorMessage = p.stderr.decode().splitlines()[0]
                if errorMessage.startswith('   Value "18446744073709551615" at line'):
                    results[str(file)] = { 'ok' : True}
                    results[str(file)] = {"ok": True}
                    continue
                results[str(file)] = {
                    'ok'   : False,
                    'code' : p.returncode,
                    'message'  : p.stderr.decode().splitlines()[0]
                    "ok": False,
                    "code": p.returncode,
                    "message": p.stderr.decode().splitlines()[0],
                }
            else:
                results[str(file)] = {
                    'ok'   : True
                }            
                results[str(file)] = {"ok": True}
        except Exception as ex:
            raise ex
            results[str(file)] = {
                'ok'   : False,
                'code' : -1,
                'message'  : f"{ex!r}"
            }
            results[str(file)] = {"ok": False, "code": -1, "message": f"{ex!r}"}
    return results


def compileAllTargets(compileTargets):
    """
    Attempts to compile a set of compile targets using the pycrate ASN1 tools
@@ -101,19 +102,14 @@ def compileAllTargets (compileTargets):
                logging.debug(f"  Loading {filename}")
            compile_text(fileTexts, filenames=fileNames)
            results[str(firstTarget)] = {
                'ok' : True,
                "ok": True,
            }
        except Exception as ex:
            results[str(firstTarget)] = {
                'ok'   : False,
                'code' : -1,
                'message'  : f"{ex!r}"
            }
            results[str(firstTarget)] = {"ok": False, "code": -1, "message": f"{ex!r}"}
            continue
    return results



def processResults(results, stageName):
    """
    Counts the number of errors and writes out the output per filename
@@ -123,7 +119,7 @@ def processResults (results, stageName):
    :returns: The number of files which had errors
    """
    print("")
    errorCount = sum([1 for r in results.values() if not r['ok']])
    errorCount = sum([1 for r in results.values() if not r["ok"]])
    logging.info(f"{errorCount} {stageName} errors encountered")

    print(f"{'-':-<60}")
@@ -131,9 +127,9 @@ def processResults (results, stageName):
    print(f"{'-':-<60}")
    for filename, result in results.items():
        print(f" {filename:.<55}{'..OK' if result['ok'] else 'FAIL'}")
        if not result['ok']:
            if isinstance(result['message'], list):
                for thing in result['message']:
        if not result["ok"]:
            if isinstance(result["message"], list):
                for thing in result["message"]:
                    print(f"    {thing['message']}")
            else:
                print(f"    {result['message']}")
@@ -149,38 +145,40 @@ if __name__ == "__main__":
    loglevel = os.environ.get("LOGLEVEL", "WARNING").upper()
    logging.basicConfig(level=loglevel)

    logging.info ('Searching for ASN1C')
    logging.info("Searching for ASN1C")
    asn1c_path = which("asn1c")
    if asn1c_path is None:
        raise Exception("No asn1c executable found. Please install asn1c")
    logging.info(f"asn1c found at {asn1c_path}")
    if asn1c_path.lower().endswith("bat"):
        logging.info (f"asn1c is a batch file, so assume path separators need to be changed")
        logging.info(
            f"asn1c is a batch file, so assume path separators need to be changed"
        )
        change_path_to_unix = True

    logging.info("Searching for ASN.1 files")
    fileList = list(Path(".").rglob("*.asn1")) + list(Path(".").rglob("*.asn"))
    logging.info(f'{len(fileList)} ASN.1 files found')
    logging.info(f"{len(fileList)} ASN.1 files found")
    for file in fileList:
        logging.debug(f'  {file}')
        logging.debug(f"  {file}")

    ignoreList = Path('testing/asn_ignore.txt').read_text().splitlines()
    ignoreList = Path("testing/asn_ignore.txt").read_text().splitlines()
    ignoredFiles = []
    for ignore in ignoreList:
        logging.debug(f'Ignoring pattern {ignore}')
        logging.debug(f"Ignoring pattern {ignore}")
        for file in fileList:
            if ignore in str(file):
                ignoredFiles.append(file)
                logging.debug(f" Ignoring {str(file)} as contains {ignore}")
    ignoredFiles = list(set(ignoredFiles))
    logging.info(f'{len(ignoredFiles)} files ignored')
    logging.info(f"{len(ignoredFiles)} files ignored")
    for file in ignoredFiles:
        logging.debug(f'  {file}')
        logging.debug(f"  {file}")

    fileList = [file for file in fileList if file not in ignoredFiles]
    logging.info(f'{len(fileList)} files to process')
    logging.info(f"{len(fileList)} files to process")
    for file in fileList:
        logging.debug(f'  {file}')
        logging.debug(f"  {file}")

    if len(fileList) == 0:
        logging.warning("No files specified")
@@ -192,7 +190,7 @@ if __name__ == "__main__":
        exit(-1)

    logging.info("Getting compile targets")
    compileTargets = json.loads(Path('testing/asn_compile_targets.json').read_text())
    compileTargets = json.loads(Path("testing/asn_compile_targets.json").read_text())
    logging.info(f"{len(compileTargets)} compile targets found")

    compileResults = compileAllTargets(compileTargets)
+18 −16
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ from pathlib import Path
from difflib import *
import subprocess


def lint(file: Path):
    completed = subprocess.run(["xmllint", str(file)], capture_output=True, text=True)

@@ -27,7 +28,6 @@ def lint(file : Path):
        return len(diff)



if __name__ == "__main__":
    root = Path("./")

@@ -47,10 +47,12 @@ if __name__ == "__main__":
        files_with_errors += 1 if new_errors > 0 else 0

    print("-------------------------------------------------")
    print (f"Files: {len(files)} ({files_with_errors} with errors)  Total errors: {errors}")
    print(
        f"Files: {len(files)} ({files_with_errors} with errors)  Total errors: {errors}"
    )
    print("-------------------------------------------------")

    if (files_with_errors > 0):
    if files_with_errors > 0:
        exit(-1)
    else:
        exit(0)