Skip to content

Commit

Permalink
ruff formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Hilgers committed May 28, 2024
1 parent 0e0ed63 commit 58153c2
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 63 deletions.
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 18 additions & 4 deletions qrcloak/qrcloak-android/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -42,6 +46,7 @@ def format_versions(mv):
joined = ",\n".join(lines)
return f"dict(\n{joined}\n)"


boms = []
artifacts = []
versions = []
Expand All @@ -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),
)
)
46 changes: 25 additions & 21 deletions qrcloak/qrcloak-android/script2.py
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down Expand Up @@ -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"]),
Expand All @@ -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
Expand All @@ -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 ""

Expand All @@ -139,17 +146,14 @@ 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)
version_ref = get_version_ref(version)

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}")



34 changes: 25 additions & 9 deletions tools/unzip_jars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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()
71 changes: 53 additions & 18 deletions tools/workflow_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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"
Expand All @@ -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()
Loading

0 comments on commit 58153c2

Please sign in to comment.