11import os
22import glob
3+ import logging
4+
5+ # Initialize logger for instance_handler module
6+ logger = logging .getLogger (__name__ )
7+ logger .setLevel (logging .INFO ) # Or desired level, if not set at root logger
8+
39
410def modify_instance_files (instance_path , file_names , new_type ):
5- """Modifies file types in .bstk files to toggle R/W status, and logs changes in detail."""
11+ """Modifies file types in .bstk files to toggle R/W status, and logs changes in detail.
12+
13+ Args:
14+ instance_path (str): Path to the BlueStacks instance directory.
15+ file_names (list): List of file names to modify within the .bstk files.
16+ new_type (str): The new file type to set ("Normal" or "Readonly").
17+ """
618 bstk_files = ["Android.bstk.in" ]
7- instance_bstk = glob .glob (os .path .join (instance_path , "*.bstk" ))
19+ instance_bstk_files = glob .glob (os .path .join (instance_path , "*.bstk" ))
820
9- if not instance_bstk :
10- print (f"Error: No .bstk file found in { instance_path } " )
21+ if not instance_bstk_files :
22+ error_msg = f"Error: No .bstk file found in instance path: { instance_path } "
23+ logger .error (error_msg )
24+ print (error_msg )
1125 return
1226
13- bstk_files .append (os .path .basename (instance_bstk [0 ]))
27+ bstk_files .append (os .path .basename (instance_bstk_files [0 ])) # Take the first one found
28+
29+ logger .debug (f"Modifying instance files in path: { instance_path } , BSTK files: { bstk_files } , Target files: { file_names } , New type: { new_type } " )
1430
1531 for bstk_file in bstk_files :
1632 bstk_path = os .path .join (instance_path , bstk_file )
1733 changed_entries = [] # Store (filename, old_value, new_value)
1834
1935 try :
36+ logger .debug (f"Processing BSTK file: { bstk_path } " )
2037 with open (bstk_path , "r" ) as f :
2138 content = f .readlines ()
2239
@@ -29,36 +46,63 @@ def modify_instance_files(instance_path, file_names, new_type):
2946 old_value = "Readonly"
3047 line = line .replace ("Readonly" , new_type )
3148 changed_entries .append ((matched_file_name , old_value , new_type ))
49+ logger .debug (f"Modified line for file '{ matched_file_name } ': from 'Readonly' to '{ new_type } '" )
3250 elif "Normal" in line :
3351 old_value = "Normal"
3452 line = line .replace ("Normal" , new_type )
3553 changed_entries .append ((matched_file_name , old_value , new_type ))
54+ logger .debug (f"Modified line for file '{ matched_file_name } ': from 'Normal' to '{ new_type } '" )
3655 f .write (line )
3756
3857 if changed_entries :
3958 for (file_name , old_val , new_val ) in changed_entries :
40- print (f"Successfully updated '{ file_name } ' from '{ old_val } ' to '{ new_val } ' in { bstk_path } " )
59+ logger . info (f"Successfully updated file type for '{ file_name } ' in { bstk_path } : from '{ old_val } ' to '{ new_val } '" )
4160 else :
42- print (f"No changes made in { bstk_path } " )
61+ logger . info (f"No target file types ('Readonly' or 'Normal' with filenames { file_names } ) found to modify in { bstk_path } . " )
4362
4463 except FileNotFoundError :
45- print (f"Error: File not found at { bstk_path } " )
64+ error_msg = f"BSTK file not found at { bstk_path } "
65+ logger .error (error_msg )
66+ print (error_msg )
4667 except Exception as e :
47- print (f"Error modifying file: { e } " )
68+ error_msg = f"Error modifying BSTK file { bstk_path } : { e } "
69+ logger .exception (error_msg )
70+ print (error_msg )
71+
4872
4973def is_instance_readonly (instance_path ):
50- """Checks if instance files are set to 'Readonly'."""
51- for bstk_file in ["Android.bstk.in" , f"{ os .path .basename (instance_path )} .bstk" ]:
74+ """Checks if instance files are set to 'Readonly'.
75+
76+ Args:
77+ instance_path (str): Path to the BlueStacks instance directory.
78+
79+ Returns:
80+ bool: True if 'Readonly' is found in any checked .bstk file, False otherwise.
81+ """
82+ bstk_files_to_check = ["Android.bstk.in" ] # Define filenames to check directly
83+ instance_bstk_files = glob .glob (os .path .join (instance_path , "*.bstk" ))
84+ if instance_bstk_files : # Only add if found, avoid index error if glob returns empty list
85+ bstk_files_to_check .append (os .path .basename (instance_bstk_files [0 ]))
86+
87+ logger .debug (f"Checking readonly status for instance path: { instance_path } , BSTK files to check: { bstk_files_to_check } " )
88+
89+ for bstk_file in bstk_files_to_check :
5290 bstk_path = os .path .join (instance_path , bstk_file )
5391 try :
5492 with open (bstk_path , "r" ) as f :
5593 for line in f :
5694 if "Readonly" in line :
95+ logger .debug (f"'Readonly' found in { bstk_path } " )
5796 return True
58- return False
97+ logger . debug ( f"'Readonly' not found in { bstk_path } " ) # Log even when not found to confirm check
5998 except FileNotFoundError :
60- print (f"Error: File not found at { bstk_path } " )
61- return False
99+ error_msg = f"BSTK file not found at { bstk_path } "
100+ logger .error (error_msg )
101+ print (error_msg )
102+ return False # If one file not found, consider it not readonly for safety (or adjust logic)
62103 except Exception as e :
63- print (f"Error reading file: { e } " )
64- return False
104+ error_msg = f"Error reading BSTK file { bstk_path } : { e } "
105+ logger .exception (error_msg )
106+ print (error_msg )
107+ return False
108+ return False # Readonly not found in any checked files
0 commit comments