Coverage for fiqus/utils/update_data_settings.py: 0%

20 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-12-27 02:39 +0100

1from ruamel.yaml import YAML 

2import argparse 

3import os 

4 

5def update_data_settings_getdp_path(file_path, env_variable): 

6 # Load the YAML file 

7 yaml = YAML() 

8 

9 with open(file_path, 'r') as file: 

10 config = yaml.load(file) 

11 

12 # Get the value of the environment variable 

13 env_value = os.environ.get(env_variable) 

14 if env_value is None: 

15 raise ValueError(f"Environment variable '{env_variable}' is not set.") 

16 

17 # Update the GetDP_path with the expanded value 

18 config['GetDP_path'] = "C:/cerngetdp/getdp_" + env_value + ".exe" 

19 # Save the updated YAML file, preserving formatting 

20 with open(file_path, 'w') as file: 

21 yaml.dump(config, file) 

22 

23 print(f"Updated 'GetDP_path' to use environment variable: {env_variable}") 

24 

25if __name__ == "__main__": 

26 # Parse command-line arguments 

27 parser = argparse.ArgumentParser(description="Update GetDP_path in a YAML file to use an environment variable.") 

28 parser.add_argument("file", help="Path to the YAML file.") 

29 parser.add_argument("env_variable", help="Name of the environment variable to use for GetDP_path.") 

30 args = parser.parse_args() 

31 

32 # Call the function with the provided arguments 

33 update_data_settings_getdp_path(args.file, args.env_variable)