-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
58 lines (43 loc) · 1.82 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
#!/usr/bin/env python3
import os
import re
import sys
def is_substring_in_string(substring, string):
pattern = r'(?<![a-zA-Z0-9])' + re.escape(str.upper(substring)) + r'(?![a-zA-Z0-9])'
match = re.search(pattern, str.upper(string))
return match is not None
def extract_matching_blocks(content, keyword):
# Simplified pattern to match both resource and data blocks
block_pattern = r'(?:^|\n)((data|resource|module|variable|provider)\s+"[^"]+"(\s+"[^"]+")?\s*\{.*?\n\})'
extracted_blocks = []
matches = re.findall(block_pattern, content, re.DOTALL | re.MULTILINE)
for match in matches:
# Extracting the full block, which is the first element of the tuple
block = match[0]
if is_substring_in_string(keyword, block):
extracted_blocks.append(block)
return extracted_blocks
def main():
if len(sys.argv) != 3:
print("Usage: ./extract_resources.py <keyword> <path_to_directory>")
sys.exit(1)
keyword = sys.argv[1]
path = sys.argv[2]
extracted_resources = []
# Get all .tf files in the current directory
tf_files = [f"{path}/{f}" for f in os.listdir(path) if f.endswith('.tf')]
for tf_file in tf_files:
with open(tf_file, 'r') as file:
content = file.read()
matching_blocks = extract_matching_blocks(content, keyword)
extracted_resources.extend(matching_blocks)
# Removing the matched blocks from original file
for block in matching_blocks:
content = content.replace(block, "")
with open(tf_file, 'w') as file:
file.write(content)
# Write the extracted resources to a new .tf file
with open(f"{keyword}.tf", "w") as output_file:
output_file.write("\n\n".join(extracted_resources))
if __name__ == "__main__":
main()