-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
145 lines (123 loc) · 4.84 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
import json
import sys
def check_folder_or_file(input_paths):
"""
Check if the input is a folder, a single file, or multiple files/folders.
Returns a list of files if they are folders, or a list with a single file/folder if it's a single file/folder.
If an input path is '.', it lists all files in the current directory recursively.
Parameters:
input_paths (str): A space-separated string of paths to files or folders.
Returns:
list: List of files if they are folders, or a list with a single file/folder if it's a single file/folder.
str: Error message if any input does not exist.
"""
input_paths = input_paths.split() # Split string into list of objects
files = []
for input_path in input_paths:
if input_path == '.':
# List all files in the current directory and its subdirectories recursively
for root, _, filenames in os.walk('.'):
for filename in filenames:
files.append(os.path.join(root, filename))
else:
# Check if the input_path exists
if not os.path.exists(input_path):
return f"Error: {input_path} does not exist."
# Check if the input_path is a directory
if os.path.isdir(input_path):
# List all files in the directory and its subdirectories recursively
for root, _, filenames in os.walk(input_path):
for filename in filenames:
files.append(os.path.join(root, filename))
else:
# If it's not a directory, assume it's a single file or folder
files.append(input_path)
return files
def read_file(file_path):
"""
Read the content of a file and find all JSON contents within the file.
Parameters:
file_path (str): The path to the file to read.
Returns:
list: List of JSON contents if found, None if not found.
"""
try:
with open(file_path, 'r') as file:
content = file.read()
json_contents = []
# Find JSON content starting with '{' and ending with '}'
json_start = content.find("{")
while json_start != -1:
json_end = content.find("}", json_start + 1)
if json_end != -1:
json_string = content[json_start:json_end + 1]
json_contents.append(json_string)
json_start = content.find("{", json_start + 1)
if json_contents:
return json_contents
else:
return None
except Exception as e:
return None
def validate_json(json_contents, file_name):
"""
Check the validity of multiple JSON contents and return result information.
Parameters:
json_contents (list): List of JSON contents to check.
file_name (str): The file name.
Returns:
str: Validation result and file name.
"""
if json_contents:
valid = True
for index, json_content in enumerate(json_contents):
try:
# Check the current JSON content
json.loads(json_content)
except json.JSONDecodeError as e:
valid = False
print(f"File '{file_name}', JSON #{index + 1} is not valid: {str(e)}")
if valid:
# If the JSON is valid, return True
return True
else:
# If the JSON is invalid, return False
return False
else:
return f"No JSON content found in '{file_name}'."
def main():
input_paths = os.getenv("INPUT_FILE_OR_DIR")
if not input_paths:
print("The INPUT_FILE_OR_DIR environment variable is not set.")
exit(1)
# Check if objects in input_paths exist
invalid_inputs = check_folder_or_file(input_paths)
if isinstance(invalid_inputs, str):
# If the function returns an error message, print the error message and exit with the error code
print(invalid_inputs)
exit(1)
# Set a variable that tracks whether all JSON is valid or not
all_json_valid = True
if not invalid_inputs:
print("No valid files or folders found.")
exit(1)
else:
for file_path in invalid_inputs:
json_contents = read_file(file_path)
if json_contents is not None:
result = validate_json(json_contents, os.path.basename(file_path))
if result is True:
print(f"File '{file_path}' is valid.\n")
else:
print(f"File '{file_path}' is not valid.\n")
if not result:
all_json_valid = False
else:
print(f"No JSON content found in '{file_path}'.\n")
if all_json_valid:
exit(0)
else:
exit(1)
if __name__ == "__main__":
main()