Skip to content

Commit d68ec9d

Browse files
committed
Fix permissions on state handler files
1 parent 0c39b1d commit d68ec9d

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

investing_algorithm_framework/infrastructure/services/aws/state_handler.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import logging
3+
import stat
34
import boto3
45
from botocore.exceptions import NoCredentialsError, PartialCredentialsError
56
from investing_algorithm_framework.domain import OperationalException, \
@@ -34,6 +35,34 @@ def initialize(self):
3435

3536
self.s3_client = boto3.client("s3")
3637

38+
def _fix_permissions(self, target_directory: str):
39+
"""
40+
Fix permissions on downloaded files to make them writable.
41+
42+
Args:
43+
target_directory (str): Directory to fix permissions for
44+
"""
45+
try:
46+
# Fix directory permissions
47+
os.chmod(target_directory, 0o755)
48+
49+
# Recursively fix all files and subdirectories
50+
for root, dirs, files in os.walk(target_directory):
51+
# Fix subdirectories
52+
for dir_name in dirs:
53+
dir_path = os.path.join(root, dir_name)
54+
os.chmod(dir_path, 0o755)
55+
56+
# Fix files - make them readable and writable
57+
for file_name in files:
58+
file_path = os.path.join(root, file_name)
59+
# Set to 0o644 (rw-r--r--)
60+
os.chmod(file_path, 0o644)
61+
62+
logger.info(f"Permissions fixed for {target_directory}")
63+
except Exception as e:
64+
logger.warning(f"Error fixing permissions: {e}")
65+
3766
def save(self, source_directory: str):
3867
"""
3968
Save the state to AWS S3.
@@ -54,7 +83,7 @@ def save(self, source_directory: str):
5483
file_path = os.path.join(root, file_name)
5584

5685
# Construct the S3 object key (relative path in the bucket)
57-
s3_key = os.path.relpath(file_path, source_directory)\
86+
s3_key = os.path.relpath(file_path, source_directory) \
5887
.replace("\\", "/")
5988

6089
# Upload the file
@@ -103,6 +132,9 @@ def load(self, target_directory: str):
103132
self.bucket_name, s3_key, file_path
104133
)
105134

135+
# Fix permissions on all downloaded files
136+
self._fix_permissions(target_directory)
137+
106138
except (NoCredentialsError, PartialCredentialsError) as ex:
107139
logger.error(f"Error loading state from AWS S3: {ex}")
108140
raise OperationalException(

0 commit comments

Comments
 (0)