annotate check.py @ 0:4de886e6300d draft

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