-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathutils.py
123 lines (99 loc) · 3.17 KB
/
utils.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
# utils.py
import re
import os
import json
import boto3
from datetime import datetime
def write_log(message, log_file_path):
"""
Write a log message to the log file.
Args:
message (str): The message to log.
log_file_path (str): The path to the log file.
"""
with open(log_file_path, "a") as log_file:
log_file.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - {message}\n")
def transform_function_name(func_name):
"""
Transform a CamelCase function name to snake_case.
Exception : s3:ListAllMyBuckets -> list_buckets
Args:
func_name (str): The CamelCase function name.
Returns:
str: The snake_case function name.
"""
if func_name == "ListAllMyBuckets":
return "list_buckets"
else:
return re.sub(r'([a-z0-9])([A-Z])', r'\1_\2', re.sub(r'([A-Z]+)([A-Z][a-z])', r'\1_\2', func_name)).lower()
def json_serial(obj):
"""
JSON serializer for objects not serializable by default.
Args:
obj (any): The object to serialize.
Returns:
str: The serialized object.
Raises:
TypeError: If the object is not serializable.
"""
if isinstance(obj, datetime):
return obj.isoformat()
raise TypeError("Type not serializable")
def is_empty(value):
"""
Check if a value is empty. This includes None, empty strings, empty lists, and empty dictionaries.
Args:
value (any): The value to check.
Returns:
bool: True if the value is empty, False otherwise.
"""
if value is None:
return True
if isinstance(value, str) and not value.strip(): # Check empty string
return True
if isinstance(value, (list, dict)) and len(value) == 0: # Check empty list or dict
return True
return False
def ensure_directory_exists(directory):
"""
Ensure that a directory exists. If it does not exist, create it.
Args:
directory (str): The path to the directory.
"""
if not os.path.exists(directory):
os.makedirs(directory)
def create_log_file(log_dir):
"""
Create a timestamped log file in the specified directory.
Args:
log_dir (str): The directory where the log file will be created.
Returns:
str: The path to the created log file.
"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
return os.path.join(log_dir, f"log_{timestamp}.log")
def get_all_regions():
"""
Retrieve all AWS regions.
Returns:
list: A list of all AWS regions.
"""
ec2 = boto3.client('ec2')
response = ec2.describe_regions()
return response['Regions']
def test_region_connectivity(region, log_file_path):
"""
Test connectivity to a specific AWS region.
Args:
region (str): The AWS region to test.
log_file_path (str): The path to the log file.
Returns:
bool: True if the region is reachable, False otherwise.
"""
ec2 = boto3.client('ec2', region_name=region)
try:
ec2.describe_availability_zones()
return True
except Exception as e:
write_log(f"Could not connect to the endpoint URL for region {region}: {e}", log_file_path)
return False