uraniborg/s/p/automate_observation.py Updated copyright year and bug …#3
uraniborg/s/p/automate_observation.py Updated copyright year and bug …#3billy-lau wants to merge 1 commit intoandroid:mainfrom
Conversation
…fix. Fix a bug that was introduced when prebuilt binaries were removed, and the directory that the script relied on to create symlinks is now gone. Also made sure that the Android SDK's location is known and exists before proceeding to call gradle to build Hubble APK. Change-Id: Ia37be790e26ff352635215fd211447f8ebc0bdd3
There was a problem hiding this comment.
Code Review
This pull request introduces an ensure_android_sdk function to automate Android SDK detection and configuration, updates the copyright year, and ensures symlink directories are created. Feedback focuses on increasing the robustness of the SDK detection logic by validating paths, improving the parsing and writing of local.properties to handle whitespace and missing newlines, and advising against interactive input to ensure compatibility with CI/CD environments.
| if os.environ.get("ANDROID_HOME"): | ||
| logger.debug("ANDROID_HOME is set to %s", os.environ.get("ANDROID_HOME")) | ||
| return True | ||
| if os.environ.get("ANDROID_SDK_ROOT"): | ||
| logger.debug("ANDROID_SDK_ROOT is set to %s", os.environ.get("ANDROID_SDK_ROOT")) | ||
| return True |
There was a problem hiding this comment.
The environment variable check should verify that the directory actually exists, consistent with how local.properties and default locations are handled. This ensures the build doesn't fail later due to an invalid path in the environment.
| if os.environ.get("ANDROID_HOME"): | |
| logger.debug("ANDROID_HOME is set to %s", os.environ.get("ANDROID_HOME")) | |
| return True | |
| if os.environ.get("ANDROID_SDK_ROOT"): | |
| logger.debug("ANDROID_SDK_ROOT is set to %s", os.environ.get("ANDROID_SDK_ROOT")) | |
| return True | |
| for env_var in ["ANDROID_HOME", "ANDROID_SDK_ROOT"]: | |
| val = os.environ.get(env_var) | |
| if val: | |
| logger.debug("%s is set to %s", env_var, val) | |
| if os.path.exists(val): | |
| return True | |
| logger.warning("%s points to non-existent directory: %s", env_var, val) |
| if line.startswith("sdk.dir="): | ||
| sdk_dir = line.split("=")[1].strip() | ||
| logger.debug("Found sdk.dir in local.properties: %s", sdk_dir) | ||
| if os.path.exists(sdk_dir): | ||
| return True | ||
| else: | ||
| logger.warning("sdk.dir in local.properties points to non-existent directory: %s", sdk_dir) |
There was a problem hiding this comment.
The parsing of local.properties is brittle. It doesn't handle potential whitespace around the = sign and might fail if the value itself contains an = character.
| if line.startswith("sdk.dir="): | |
| sdk_dir = line.split("=")[1].strip() | |
| logger.debug("Found sdk.dir in local.properties: %s", sdk_dir) | |
| if os.path.exists(sdk_dir): | |
| return True | |
| else: | |
| logger.warning("sdk.dir in local.properties points to non-existent directory: %s", sdk_dir) | |
| parts = line.split("=", 1) | |
| if len(parts) == 2 and parts[0].strip() == "sdk.dir": | |
| sdk_dir = parts[1].strip() | |
| logger.debug("Found sdk.dir in local.properties: %s", sdk_dir) | |
| if os.path.exists(sdk_dir): | |
| return True | |
| else: | |
| logger.warning("sdk.dir in local.properties points to non-existent directory: %s", sdk_dir) |
| if not sdk_path: | ||
| logger.warning("Android SDK location not found in environment or default locations.") | ||
| try: | ||
| user_input = input("Please enter the path to your Android SDK: ").strip() |
| lines = [l for l in lines if not l.startswith("sdk.dir=")] | ||
| lines.append(f"sdk.dir={sdk_path}\n") |
There was a problem hiding this comment.
When appending to local.properties, if the existing file does not end with a newline character, the new entry will be appended to the last line, making it invalid. Additionally, the filtering logic should be more robust against leading whitespace.
lines = [l for l in lines if not l.strip().startswith("sdk.dir=")]
if lines and not lines[-1].endswith("\n"):
lines[-1] += "\n"
lines.append(f"sdk.dir={sdk_path}\n")
…fix.
Fix a bug that was introduced when prebuilt binaries were removed, and the directory that the script relied on to create symlinks is now gone.
Also made sure that the Android SDK's location is known and exists before proceeding to call gradle to build Hubble APK.
Change-Id: Ia37be790e26ff352635215fd211447f8ebc0bdd3