From 58153c27d7cff43ca8c3b43a22dac4f81959bdde Mon Sep 17 00:00:00 2001 From: Felix Hilgers Date: Tue, 28 May 2024 22:46:44 +0200 Subject: [PATCH] ruff formatting --- flake.lock | 6 +-- flake.nix | 5 ++- qrcloak/qrcloak-android/script.py | 22 +++++++-- qrcloak/qrcloak-android/script2.py | 46 ++++++++++--------- tools/unzip_jars.py | 34 ++++++++++---- tools/workflow_setup.py | 71 ++++++++++++++++++++++-------- tools/zip.py | 26 ++++++++--- 7 files changed, 147 insertions(+), 63 deletions(-) diff --git a/flake.lock b/flake.lock index d1d1f55..f1fac54 100644 --- a/flake.lock +++ b/flake.lock @@ -191,11 +191,11 @@ ] }, "locked": { - "lastModified": 1707300477, - "narHash": "sha256-qQF0fEkHlnxHcrKIMRzOETnRBksUK048MXkX0SOmxvA=", + "lastModified": 1715940852, + "narHash": "sha256-wJqHMg/K6X3JGAE9YLM0LsuKrKb4XiBeVaoeMNlReZg=", "owner": "numtide", "repo": "treefmt-nix", - "rev": "ac599dab59a66304eb511af07b3883114f061b9d", + "rev": "2fba33a182602b9d49f0b2440513e5ee091d838b", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 0a73bb6..7ba99ea 100644 --- a/flake.nix +++ b/flake.nix @@ -108,7 +108,10 @@ alejandra.enable = true; rustfmt.enable = true; biome.enable = true; - ruff.enable = true; + ruff = { + check = true; + format = true; + }; shfmt.enable = true; buildifier = { enable = true; diff --git a/qrcloak/qrcloak-android/script.py b/qrcloak/qrcloak-android/script.py index 27d9b2e..0e8cdd9 100644 --- a/qrcloak/qrcloak-android/script.py +++ b/qrcloak/qrcloak-android/script.py @@ -24,16 +24,20 @@ use_repo(maven, "maven_deps") """.strip() + def quote(s): - return f"\"{s}\"" + return f'"{s}"' + def ident(s, by): return f"{by}{s}" + def format(ss): joined = ",\n".join(ss) return f"[\n{joined}\n]" + def format_versions(mv): lines = [] for key, val in mv.items(): @@ -42,6 +46,7 @@ def format_versions(mv): joined = ",\n".join(lines) return f"dict(\n{joined}\n)" + boms = [] artifacts = [] versions = [] @@ -61,12 +66,21 @@ def format_versions(mv): version_ref = library.get("version", {}).get("ref") version = versions.get(version_ref) if version_ref else None - full = f" \"{module}:{{}}\".format(maven_versions[\"{version_ref}\"])" if version_ref else f" \"{module}\"" + full = ( + f' "{module}:{{}}".format(maven_versions["{version_ref}"])' + if version_ref + else f' "{module}"' + ) if module == "androidx.compose:compose-bom": boms.append(full) else: artifacts.append(full) -print(TEMPLATE.format(artifacts = format(artifacts), boms = format(boms), versions = format_versions(versions))) - +print( + TEMPLATE.format( + artifacts=format(artifacts), + boms=format(boms), + versions=format_versions(versions), + ) +) diff --git a/qrcloak/qrcloak-android/script2.py b/qrcloak/qrcloak-android/script2.py index 276d97b..0420c49 100644 --- a/qrcloak/qrcloak-android/script2.py +++ b/qrcloak/qrcloak-android/script2.py @@ -1,17 +1,17 @@ versions = dict( - accompanist = "0.35.0-alpha", - activity = "1.9.0", - camera = "1.4.0-alpha05", - compose_bom = "2024.05.00", - compose_compiler = "1.5.13", - concurrent = "1.1.0", - core = "1.13.1", - datastore = "1.1.0", - fragment = "1.7.0", - lifecycle = "2.7.0", - mlkit = "17.2.0", - qrose = "1.0.1", - voyager = "1.0.0", + accompanist="0.35.0-alpha", + activity="1.9.0", + camera="1.4.0-alpha05", + compose_bom="2024.05.00", + compose_compiler="1.5.13", + concurrent="1.1.0", + core="1.13.1", + datastore="1.1.0", + fragment="1.7.0", + lifecycle="2.7.0", + mlkit="17.2.0", + qrose="1.0.1", + voyager="1.0.0", ) artifacts = [ @@ -83,7 +83,9 @@ "androidx.lifecycle:lifecycle-service:{}".format(versions["lifecycle"]), "androidx.lifecycle:lifecycle-viewmodel-ktx:{}".format(versions["lifecycle"]), "androidx.lifecycle:lifecycle-viewmodel-compose:{}".format(versions["lifecycle"]), - "androidx.lifecycle:lifecycle-viewmodel-savedstate:{}".format(versions["lifecycle"]), + "androidx.lifecycle:lifecycle-viewmodel-savedstate:{}".format( + versions["lifecycle"] + ), "io.github.alexzhirkevich:qrose:{}".format(versions["qrose"]), "androidx.camera:camera-camera2:{}".format(versions["camera"]), "androidx.camera:camera-core:{}".format(versions["camera"]), @@ -104,18 +106,22 @@ "androidx.compose:compose-bom:{}".format(versions["compose_bom"]), ] + def get_aar(splits): return "aar" if len(splits) > 2 and splits[2] == "aar" else None + def get_key(group, name): return f"{group}-{name}".replace(":", "-").replace(".", "-") + def get_version(splits): if get_aar(splits): return splits[3] if len(splits) > 3 else None else: return splits[2] if len(splits) > 2 else None + def get_version_ref(version): if not version: return None @@ -126,11 +132,12 @@ def get_version_ref(version): return None + def format_version(version, version_ref): if version_ref: - return f", version.ref = \"{version_ref}\"" + return f', version.ref = "{version_ref}"' elif version: - return f", version = \"{version}\"" + return f', version = "{version}"' else: return "" @@ -139,7 +146,7 @@ def format_version(version, version_ref): splits = artifact.split(":") group = splits[0] name = splits[1] - + aar = get_aar(splits) key = get_key(group, name) version = get_version(splits) @@ -147,9 +154,6 @@ def format_version(version, version_ref): name = f"{name}:{aar}" if aar else name formatted_version = format_version(version, version_ref) - value = f"{{ group = \"{group}\", name = \"{name}\"{formatted_version} }}" + value = f'{{ group = "{group}", name = "{name}"{formatted_version} }}' print(f"{key} = {value}") - - - diff --git a/tools/unzip_jars.py b/tools/unzip_jars.py index e0448a6..9c01f7b 100644 --- a/tools/unzip_jars.py +++ b/tools/unzip_jars.py @@ -2,19 +2,21 @@ import zipfile import argparse + def unzip_files(jar_files, output_directory): if not os.path.exists(output_directory): os.makedirs(output_directory) - + for jar_file in jar_files: - with zipfile.ZipFile(jar_file, 'r') as zip_ref: + with zipfile.ZipFile(jar_file, "r") as zip_ref: zip_ref.extractall(output_directory) + def rchmod(dir): os.chmod(dir, 0o755) for root, dirs, files in os.walk(dir): - # set perms on sub-directories + # set perms on sub-directories for momo in dirs: os.chmod(os.path.join(root, momo), 0o755) @@ -24,16 +26,30 @@ def rchmod(dir): def main(): - parser = argparse.ArgumentParser(description='Unzip all .jar files in a directory recursively and combine the extracted files into a new .jar file.') - parser.add_argument('inputs', nargs='+', type=str, help='The directory to search for .jar files') - parser.add_argument('--output_directory', required=True, type=str, help='The directory to extract the contents to') - parser.add_argument('--exclude', action='append', default=[], help='Glob patterns of files or directories to exclude') + parser = argparse.ArgumentParser( + description="Unzip all .jar files in a directory recursively and combine the extracted files into a new .jar file." + ) + parser.add_argument( + "inputs", nargs="+", type=str, help="The directory to search for .jar files" + ) + parser.add_argument( + "--output_directory", + required=True, + type=str, + help="The directory to extract the contents to", + ) + parser.add_argument( + "--exclude", + action="append", + default=[], + help="Glob patterns of files or directories to exclude", + ) args = parser.parse_args() unzip_files(args.inputs, args.output_directory) rchmod(args.output_directory) -if __name__ == '__main__': - main() +if __name__ == "__main__": + main() diff --git a/tools/workflow_setup.py b/tools/workflow_setup.py index 0621e07..842a481 100644 --- a/tools/workflow_setup.py +++ b/tools/workflow_setup.py @@ -6,11 +6,15 @@ from tqdm import tqdm from textwrap import dedent -custom_bar_format = '[{bar:39}] {percentage:3.0f}% {desc}' +custom_bar_format = "[{bar:39}] {percentage:3.0f}% {desc}" + def install_jre(): - subprocess.run(['sudo', 'apt-get', 'update'], check=True) - subprocess.run(['sudo', 'apt-get', 'install', '--yes', 'openjdk-17-jre'], check=True) + subprocess.run(["sudo", "apt-get", "update"], check=True) + subprocess.run( + ["sudo", "apt-get", "install", "--yes", "openjdk-17-jre"], check=True + ) + def check_sha256(filename, expected_sha256): sha256_hash = hashlib.sha256() @@ -19,25 +23,36 @@ def check_sha256(filename, expected_sha256): sha256_hash.update(byte_block) return sha256_hash.hexdigest() == expected_sha256 + class DownloadProgressBar(tqdm): def update_to(self, b=1, bsize=1, tsize=None): if tsize is not None: self.total = tsize - + previous = self.n current = min(b * bsize, tsize) self.update(current - previous) + def download_with_progress(url, filename): - with DownloadProgressBar(desc=filename, bar_format=custom_bar_format, ascii=" =") as t: + with DownloadProgressBar( + desc=filename, bar_format=custom_bar_format, ascii=" =" + ) as t: urllib.request.urlretrieve(url, filename, reporthook=t.update_to) t.close() + def unzip_with_progress(zip_file, extract_to): - with zipfile.ZipFile(zip_file, 'r') as zip_ref: + with zipfile.ZipFile(zip_file, "r") as zip_ref: total_files = len(zip_ref.infolist()) - with tqdm(total=total_files, unit='file', desc='Unzipping', bar_format=custom_bar_format, ascii=" =") as t: + with tqdm( + total=total_files, + unit="file", + desc="Unzipping", + bar_format=custom_bar_format, + ascii=" =", + ) as t: for file in zip_ref.infolist(): zip_ref.extract(file, extract_to) file_path = os.path.join(extract_to, file.filename) @@ -46,8 +61,9 @@ def unzip_with_progress(zip_file, extract_to): os.chmod(file_path, perm) t.update(1) + def main(): - os.environ['DEBIAN_FRONTEND'] = 'noninteractive' + os.environ["DEBIAN_FRONTEND"] = "noninteractive" install_jre() VERSION_LONG = "10406996" @@ -58,30 +74,49 @@ def main(): if not os.path.isdir(ANDROID_SDK_ROOT): url = f"https://dl.google.com/android/repository/{FILENAME}" download_with_progress(url, FILENAME) - + if not check_sha256(FILENAME, SHA256): raise ValueError("SHA256 checksum does not match") os.makedirs(f"{ANDROID_SDK_ROOT}/cmdline-tools/", exist_ok=True) - - unzip_with_progress(FILENAME, f"{ANDROID_SDK_ROOT}/tmp") - os.rename(f"{ANDROID_SDK_ROOT}/tmp/cmdline-tools", f"{ANDROID_SDK_ROOT}/cmdline-tools/latest") + unzip_with_progress(FILENAME, f"{ANDROID_SDK_ROOT}/tmp") - subprocess.run([f"{ANDROID_SDK_ROOT}/cmdline-tools/latest/bin/sdkmanager", "--licenses"], input=b'y\n' * 100, capture_output=True, check=True) - subprocess.run([f"{ANDROID_SDK_ROOT}/cmdline-tools/latest/bin/sdkmanager", "ndk;26.3.11579264", "build-tools;34.0.0", "platforms;android-34"], check=True) + os.rename( + f"{ANDROID_SDK_ROOT}/tmp/cmdline-tools", + f"{ANDROID_SDK_ROOT}/cmdline-tools/latest", + ) + + subprocess.run( + [f"{ANDROID_SDK_ROOT}/cmdline-tools/latest/bin/sdkmanager", "--licenses"], + input=b"y\n" * 100, + capture_output=True, + check=True, + ) + subprocess.run( + [ + f"{ANDROID_SDK_ROOT}/cmdline-tools/latest/bin/sdkmanager", + "ndk;26.3.11579264", + "build-tools;34.0.0", + "platforms;android-34", + ], + check=True, + ) ANDROID_HOME = ANDROID_SDK_ROOT ANDROID_NDK_HOME = f"{ANDROID_HOME}/ndk/26.3.11579264" - ci_bazelrc_content = dedent(f"""\ + ci_bazelrc_content = dedent( + f"""\ common --repo_env=ANDROID_HOME={ANDROID_HOME} common --repo_env=ANDROID_NDK_HOME={ANDROID_NDK_HOME} - """) + """ + ) - BUILD_WORKSPACE_DIRECTORY = os.getenv('BUILD_WORKSPACE_DIRECTORY', '.') - with open(os.path.join(BUILD_WORKSPACE_DIRECTORY, 'ci.bazelrc'), 'w') as f: + BUILD_WORKSPACE_DIRECTORY = os.getenv("BUILD_WORKSPACE_DIRECTORY", ".") + with open(os.path.join(BUILD_WORKSPACE_DIRECTORY, "ci.bazelrc"), "w") as f: f.write(ci_bazelrc_content) + if __name__ == "__main__": main() diff --git a/tools/zip.py b/tools/zip.py index ef6eded..e93bcc3 100644 --- a/tools/zip.py +++ b/tools/zip.py @@ -3,6 +3,7 @@ import zipfile import argparse + def should_exclude(file_path, excludes, base_directory): relative_path = os.path.relpath(file_path, base_directory) for pattern in excludes: @@ -10,8 +11,9 @@ def should_exclude(file_path, excludes, base_directory): return True return False + def zip_directory(directory, zip_filename, excludes): - with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as jar_file: + with zipfile.ZipFile(zip_filename, "w", zipfile.ZIP_DEFLATED) as jar_file: for root, dirs, files in os.walk(directory): for file in files: file_path = os.path.join(root, file) @@ -19,16 +21,26 @@ def zip_directory(directory, zip_filename, excludes): if not should_exclude(file_path, excludes, directory): jar_file.write(file_path, arcname) + def main(): - parser = argparse.ArgumentParser(description='Unzip all .jar files in a directory recursively and combine the extracted files into a new .jar file.') - parser.add_argument('input_directory', type=str, help='The directory to search for .jar files') - parser.add_argument('output_file', type=str, help='The path to the final .jar file') - parser.add_argument('--exclude', action='append', default=[], help='Glob patterns of files or directories to exclude') + parser = argparse.ArgumentParser( + description="Unzip all .jar files in a directory recursively and combine the extracted files into a new .jar file." + ) + parser.add_argument( + "input_directory", type=str, help="The directory to search for .jar files" + ) + parser.add_argument("output_file", type=str, help="The path to the final .jar file") + parser.add_argument( + "--exclude", + action="append", + default=[], + help="Glob patterns of files or directories to exclude", + ) args = parser.parse_args() zip_directory(args.input_directory, args.output_file, args.exclude) -if __name__ == '__main__': - main() +if __name__ == "__main__": + main()