|
2 | 2 | """ Setup .env with local eccodes definition """ |
3 | 3 |
|
4 | 4 | import os |
| 5 | +import shutil |
5 | 6 | import subprocess |
6 | 7 |
|
7 | | -base_path = os.getcwd() |
8 | | - |
9 | | -eccodes_definition_path = os.path.join(base_path, "resource", "eccodes") |
10 | | -cosmo_definition_path = os.path.join(base_path, "resource", "eccodes-cosmo-resources") |
11 | | - |
12 | | -# Clone repositories |
13 | | -subprocess.run( |
14 | | - [ |
15 | | - "git", |
16 | | - "clone", |
17 | | - "--depth", |
18 | | - "1", |
19 | | - "-b", |
20 | | - "2.39.0", |
21 | | - "https://github.com/ecmwf/eccodes.git", |
22 | | - eccodes_definition_path, |
23 | | - ], |
24 | | - check=True, |
25 | | -) |
26 | | -subprocess.run( |
27 | | - [ |
28 | | - "git", |
29 | | - "clone", |
30 | | - "--depth", |
31 | | - "1", |
32 | | - "-b", |
33 | | - "v2.36.0.3", |
34 | | - "https://github.com/COSMO-ORG/eccodes-cosmo-resources.git", |
35 | | - cosmo_definition_path, |
36 | | - ], |
37 | | - check=True, |
38 | | -) |
39 | | - |
40 | | -# Remove deprecated directory |
41 | | -deprecated_path = os.path.join(eccodes_definition_path, "deprecated") |
42 | | -if os.path.exists(deprecated_path): |
43 | | - for root, dirs, files in os.walk(deprecated_path, topdown=False): |
44 | | - for name in files: |
45 | | - os.remove(os.path.join(root, name)) |
46 | | - for name in dirs: |
47 | | - os.rmdir(os.path.join(root, name)) |
48 | | - os.rmdir(deprecated_path) |
49 | | - |
50 | | -# Remove existing .env file |
51 | | -ENV_FILE = ".env" |
52 | | -if os.path.exists(ENV_FILE): |
53 | | - os.remove(ENV_FILE) |
54 | | - |
55 | | -# Write to .env file |
56 | | -ENV_CONTENT = ( |
57 | | - f"ECCODES_DEFINITION_PATH={eccodes_definition_path}/definitions:" |
58 | | - f"{cosmo_definition_path}/definitions\n" |
59 | | -) |
60 | | -with open(ENV_FILE, "w", encoding="utf-8") as file: |
61 | | - file.write(ENV_CONTENT) |
| 8 | + |
| 9 | +def main(): |
| 10 | + base_path = os.getcwd() |
| 11 | + |
| 12 | + resource_path = os.path.join(base_path, "resource") |
| 13 | + |
| 14 | + eccodes_definition_path = os.path.join(resource_path, "eccodes") |
| 15 | + cosmo_definition_path = os.path.join(resource_path, "eccodes-cosmo-resources") |
| 16 | + |
| 17 | + # remove existing resource |
| 18 | + if os.path.exists(resource_path): |
| 19 | + shutil.rmtree(resource_path) |
| 20 | + |
| 21 | + # Clone repositories |
| 22 | + subprocess.run( |
| 23 | + [ |
| 24 | + "git", |
| 25 | + "clone", |
| 26 | + "--depth", |
| 27 | + "1", |
| 28 | + "-b", |
| 29 | + "2.39.0", |
| 30 | + "https://github.com/ecmwf/eccodes.git", |
| 31 | + eccodes_definition_path, |
| 32 | + ], |
| 33 | + check=True, |
| 34 | + ) |
| 35 | + subprocess.run( |
| 36 | + [ |
| 37 | + "git", |
| 38 | + "clone", |
| 39 | + "--depth", |
| 40 | + "1", |
| 41 | + "-b", |
| 42 | + "v2.36.0.3", |
| 43 | + "https://github.com/COSMO-ORG/eccodes-cosmo-resources.git", |
| 44 | + cosmo_definition_path, |
| 45 | + ], |
| 46 | + check=True, |
| 47 | + ) |
| 48 | + |
| 49 | + # Remove deprecated directory |
| 50 | + deprecated_path = os.path.join(eccodes_definition_path, "deprecated") |
| 51 | + if os.path.exists(deprecated_path): |
| 52 | + for root, dirs, files in os.walk(deprecated_path, topdown=False): |
| 53 | + for name in files: |
| 54 | + os.remove(os.path.join(root, name)) |
| 55 | + for name in dirs: |
| 56 | + os.rmdir(os.path.join(root, name)) |
| 57 | + os.rmdir(deprecated_path) |
| 58 | + |
| 59 | + # Remove existing .env file |
| 60 | + env_file = ".env" |
| 61 | + if os.path.exists(env_file): |
| 62 | + os.remove(env_file) |
| 63 | + |
| 64 | + # Write to .env file |
| 65 | + env_content = ( |
| 66 | + f"ECCODES_DEFINITION_PATH={eccodes_definition_path}/definitions:" |
| 67 | + f"{cosmo_definition_path}/definitions\n" |
| 68 | + ) |
| 69 | + with open(env_file, "w", encoding="utf-8") as file: |
| 70 | + file.write(env_content) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + main() |
0 commit comments