Mercurial > repos > ecology > argo_getdata
comparison check.py @ 2:f3c64a15ee7e draft default tip
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
author | ecology |
---|---|
date | Tue, 22 Oct 2024 15:55:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1:28c8f085d1b4 | 2:f3c64a15ee7e |
---|---|
1 import os | |
2 import subprocess | |
3 import sys | |
4 | |
5 | |
6 def validate_command(command): | |
7 # Example validation: Ensure the command does not contain | |
8 # potentially dangerous substrings | |
9 forbidden_substrings = ["rm", | |
10 "rm -rf", | |
11 "sudo", | |
12 "dd if=", | |
13 "curl", | |
14 "wget", | |
15 ";", | |
16 "&&"] | |
17 for substring in forbidden_substrings: | |
18 if substring in command: | |
19 message = f"Error: Command has forbidden substring '{substring}'" | |
20 return False, message | |
21 | |
22 # Check if the command starts with 'copernicusmarine' | |
23 if not command.startswith("copernicusmarine"): | |
24 return False, "Error: Command must start with 'copernicusmarine'" | |
25 | |
26 # Remove 'copernicusmarine' from the start | |
27 command = command[len("copernicusmarine"):].strip() | |
28 | |
29 # Check for specific commands and their arguments | |
30 if command.startswith("subset"): | |
31 # Check for required arguments for 'subset' command | |
32 if not ("--dataset-id" in command or "--dataset-url" in command): | |
33 message = ( | |
34 "Error: 'subset' command must have '--dataset-id' or " | |
35 "'--dataset-url'" | |
36 ) | |
37 return False, message | |
38 elif command.startswith("get"): | |
39 # Check for required arguments for 'get' command | |
40 if not ("--dataset-id" in command or "--dataset-url" in command): | |
41 message = ( | |
42 "Error: 'get' command must have '--dataset-id' or " | |
43 "'--dataset-url'" | |
44 ) | |
45 return False, message | |
46 elif command.startswith("login") or command.startswith("describe"): | |
47 message = "This tool only accepts 'subset' and 'get' commands." | |
48 return False, message | |
49 else: | |
50 return False, "Error: Command must be 'subset' or 'get'" | |
51 | |
52 return True, None | |
53 | |
54 | |
55 def main(): | |
56 # Check if a filename argument is provided | |
57 if len(sys.argv) != 2: | |
58 print("Usage: python check.py <config_file>") | |
59 sys.exit(1) | |
60 | |
61 # Get the filename from the command line argument | |
62 config_file = sys.argv[1] | |
63 | |
64 # Check if the file exists | |
65 if not os.path.isfile(config_file): | |
66 print(f"Error: File '{config_file}' does not exist.") | |
67 sys.exit(1) | |
68 | |
69 # Read the content of the file | |
70 with open(config_file, "r") as file: | |
71 command = file.read().strip() | |
72 | |
73 # Validate the command | |
74 is_valid, error_message = validate_command(command) | |
75 if not is_valid: | |
76 print(error_message) | |
77 sys.exit(1) | |
78 | |
79 # Execute the command | |
80 try: | |
81 subprocess.run(command, shell=True, check=True) | |
82 except subprocess.CalledProcessError as e: | |
83 print(f"Error: Command failed with exit code {e.returncode}") | |
84 sys.exit(1) | |
85 | |
86 | |
87 if __name__ == "__main__": | |
88 main() |