annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
1 import os
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
2 import subprocess
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
3 import sys
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
4
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
5
f3c64a15ee7e 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):
f3c64a15ee7e 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
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
8 # potentially dangerous substrings
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
9 forbidden_substrings = ["rm",
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
10 "rm -rf",
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
11 "sudo",
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
12 "dd if=",
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
13 "curl",
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
14 "wget",
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
15 ";",
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
16 "&&"]
f3c64a15ee7e 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:
f3c64a15ee7e 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:
f3c64a15ee7e 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}'"
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
20 return False, message
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
21
f3c64a15ee7e 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'
f3c64a15ee7e 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"):
f3c64a15ee7e 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'"
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
25
f3c64a15ee7e 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
f3c64a15ee7e 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()
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
28
f3c64a15ee7e 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
f3c64a15ee7e 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"):
f3c64a15ee7e 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
f3c64a15ee7e 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):
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
33 message = (
f3c64a15ee7e 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 "
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
35 "'--dataset-url'"
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
36 )
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
37 return False, message
f3c64a15ee7e 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"):
f3c64a15ee7e 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
f3c64a15ee7e 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):
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
41 message = (
f3c64a15ee7e 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 "
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
43 "'--dataset-url'"
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
44 )
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
45 return False, message
f3c64a15ee7e 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"):
f3c64a15ee7e 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."
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
48 return False, message
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
49 else:
f3c64a15ee7e 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'"
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
51
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
52 return True, None
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
53
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
54
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
55 def main():
f3c64a15ee7e 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
f3c64a15ee7e 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:
f3c64a15ee7e 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>")
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
59 sys.exit(1)
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
60
f3c64a15ee7e 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
f3c64a15ee7e 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]
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
63
f3c64a15ee7e 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
f3c64a15ee7e 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):
f3c64a15ee7e 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.")
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
67 sys.exit(1)
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
68
f3c64a15ee7e 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
f3c64a15ee7e 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:
f3c64a15ee7e 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()
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
72
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
73 # Validate the command
f3c64a15ee7e 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)
f3c64a15ee7e 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:
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
76 print(error_message)
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
77 sys.exit(1)
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
78
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
79 # Execute the command
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
80 try:
f3c64a15ee7e 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)
f3c64a15ee7e 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:
f3c64a15ee7e 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}")
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
84 sys.exit(1)
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
85
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
86
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
87 if __name__ == "__main__":
f3c64a15ee7e planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/ocean commit a7e53c429cf93485aba692b928defe6ee01633d6
ecology
parents:
diff changeset
88 main()