|
| 1 | +import datetime |
| 2 | +import pathlib |
| 3 | +import re |
| 4 | +import subprocess |
| 5 | + |
| 6 | +import aodn_cloud_optimised.lib.DataQuery |
| 7 | + |
| 8 | +# Regex pattern to find and capture the version string |
| 9 | +VERSION_PATTERN = re.compile(r'^__version__\s*=\s*"(.*)"\s*$') |
| 10 | + |
| 11 | + |
| 12 | +def get_file_content(ref: str, file_path: str) -> str | None: |
| 13 | + """ |
| 14 | + Retrieves the content of a specific file at a given git reference. |
| 15 | + """ |
| 16 | + |
| 17 | + # Use git show to get the file content |
| 18 | + result = subprocess.run( |
| 19 | + ["git", "show", f"{ref}:{file_path}"], |
| 20 | + capture_output=True, |
| 21 | + check=True, |
| 22 | + text=True, |
| 23 | + ) |
| 24 | + return result.stdout |
| 25 | + |
| 26 | + |
| 27 | +def extract_version(content: str) -> str | None: |
| 28 | + """ |
| 29 | + Extracts the __version__ string from the file content using a regex. |
| 30 | + """ |
| 31 | + |
| 32 | + for line in content.splitlines(): |
| 33 | + match = VERSION_PATTERN.match(line.strip()) |
| 34 | + if match: |
| 35 | + |
| 36 | + # Return the captured group (the version string) |
| 37 | + return match.group(1) |
| 38 | + |
| 39 | + return None |
| 40 | + |
| 41 | + |
| 42 | +def main(): |
| 43 | + """ |
| 44 | + validation of Data Query global vars. |
| 45 | +
|
| 46 | + These often get tinkered with in local development of notebooks. |
| 47 | +
|
| 48 | + We must enforce they are set back to normal before commits. |
| 49 | + """ |
| 50 | + |
| 51 | + # Check DataQuery.py is in the git diff |
| 52 | + stdout = subprocess.run( |
| 53 | + args=["git", "diff", "--cached", "--name-only"], |
| 54 | + check=True, |
| 55 | + text=True, |
| 56 | + capture_output=True, |
| 57 | + ).stdout |
| 58 | + |
| 59 | + # Exit if DataQuery.py is not in the changes |
| 60 | + if "aodn_cloud_optimised/lib/DataQuery.py" not in stdout.split("\n"): |
| 61 | + exit() |
| 62 | + |
| 63 | + # Extract versions |
| 64 | + staged_content = get_file_content( |
| 65 | + ref="", file_path="aodn_cloud_optimised/lib/DataQuery.py" |
| 66 | + ) |
| 67 | + staged_version = extract_version(staged_content) |
| 68 | + head_content = get_file_content( |
| 69 | + ref="HEAD", file_path="aodn_cloud_optimised/lib/DataQuery.py" |
| 70 | + ) |
| 71 | + head_version = extract_version(head_content) |
| 72 | + |
| 73 | + if staged_version == head_version: |
| 74 | + raise ValueError( |
| 75 | + f"DataQuery.__version__ must be updated. Bump from `{head_version}`" |
| 76 | + ) |
| 77 | + |
| 78 | + # Check the variables align to expected |
| 79 | + assert aodn_cloud_optimised.lib.DataQuery.REGION == "ap-southeast-2" |
| 80 | + assert ( |
| 81 | + aodn_cloud_optimised.lib.DataQuery.ENDPOINT_URL |
| 82 | + == "https://s3.ap-southeast-2.amazonaws.com" |
| 83 | + ) |
| 84 | + assert ( |
| 85 | + aodn_cloud_optimised.lib.DataQuery.BUCKET_OPTIMISED_DEFAULT |
| 86 | + == "aodn-cloud-optimised" |
| 87 | + ) |
| 88 | + assert aodn_cloud_optimised.lib.DataQuery.ROOT_PREFIX_CLOUD_OPTIMISED_PATH == "" |
| 89 | + assert aodn_cloud_optimised.lib.DataQuery.DEFAULT_TIME == datetime.datetime( |
| 90 | + 1900, 1, 1 |
| 91 | + ) |
| 92 | + |
| 93 | + print(aodn_cloud_optimised.lib.DataQuery.__version__) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + main() |
0 commit comments