mllmTeam commited on
Commit
a29d55f
·
verified ·
1 Parent(s): 32bb39f

Uploading screenshot_state_mapping.py

Browse files
MobileViews_Apps_CompleteTraces/screenshot_state_mapping.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import csv
4
+
5
+ def extract_node_data(utg_file_path, apk_folder_name):
6
+ """
7
+ Extract 'screen_id', 'state_str', 'structure_str', 'vh_json_id', and 'vh_xml_id' from the 'utg.js' file,
8
+ and prefix identifiers with the apk folder name for uniqueness.
9
+
10
+ Parameters:
11
+ - utg_file_path (str): The path to the utg.js file.
12
+ - apk_folder_name (str): The name of the APK folder for prefixing identifiers.
13
+
14
+ Returns:
15
+ - List of dictionaries containing 'screen_id', 'state_str', 'structure_str', 'vh_json_id', and 'vh_xml_id' for each node.
16
+ """
17
+ data = []
18
+ states_folder = os.path.join(os.path.dirname(utg_file_path), 'states')
19
+
20
+ try:
21
+ with open(utg_file_path, 'r') as file:
22
+ content = file.read()
23
+ content = content.replace("var utg =", "").strip() # Remove prefix for JSON parsing
24
+ utg_data = json.loads(content)
25
+
26
+ for node in utg_data.get("nodes", []):
27
+ image_path = node.get("image")
28
+
29
+ # Get the base name and suffix for mapping other files
30
+ base_name = os.path.splitext(os.path.basename(image_path))[0].replace("screen_", "")
31
+
32
+ # Map the corresponding state JSON and window dump files
33
+ vh_json_id = f"{apk_folder_name}/states/state_{base_name}.json"
34
+ vh_xml_id = f"{apk_folder_name}/states/window_dump_{base_name}.xml"
35
+
36
+ # Construct full paths for each file and verify if they exist
37
+ vh_json_path = os.path.join(states_folder, f"state_{base_name}.json")
38
+ vh_xml_path = os.path.join(states_folder, f"window_dump_{base_name}.xml")
39
+
40
+ # Only include the paths if the files exist
41
+ data.append({
42
+ "screen_id": f"{apk_folder_name}/{image_path}",
43
+ "state_str": node.get("state_str"),
44
+ "structure_str": node.get("structure_str"),
45
+ "vh_json_id": vh_json_id if os.path.isfile(vh_json_path) else "",
46
+ "vh_xml_id": vh_xml_id if os.path.isfile(vh_xml_path) else ""
47
+ })
48
+ except Exception as e:
49
+ print(f"Error processing {utg_file_path}: {e}")
50
+
51
+ return data
52
+
53
+ def process_single_folder(apk_folder_path):
54
+ """
55
+ Process a single APK folder containing a 'utg.js' file and save data to a CSV file.
56
+
57
+ Parameters:
58
+ - apk_folder_path (str): The path to the APK folder containing a utg.js file.
59
+ """
60
+ output_csv_path = os.path.join(apk_folder_path, "screenshot_state_mapping.csv")
61
+ utg_file_path = os.path.join(apk_folder_path, 'utg.js')
62
+ apk_folder_name = os.path.basename(apk_folder_path.rstrip('/'))
63
+
64
+ if os.path.isfile(utg_file_path):
65
+ data = extract_node_data(utg_file_path, apk_folder_name)
66
+
67
+ # Write data to CSV with specified column names
68
+ with open(output_csv_path, 'w', newline='') as csvfile:
69
+ fieldnames = ["screen_id", "state_str", "structure_str", "vh_json_id", "vh_xml_id"]
70
+ writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
71
+
72
+ writer.writeheader()
73
+ writer.writerows(data)
74
+
75
+ print(f"CSV file generated for {apk_folder_path} at {output_csv_path}")
76
+ else:
77
+ print(f"No utg.js file found in {apk_folder_path}")
78
+
79
+ def process_multiple_folders(parent_folder_path):
80
+ """
81
+ Process multiple APK folders within a parent folder, each containing a 'utg.js' file, and save all data to a single CSV file.
82
+
83
+ Parameters:
84
+ - parent_folder_path (str): The path to the parent folder containing multiple APK folders.
85
+ """
86
+ output_csv_path = os.path.join(parent_folder_path, "screenshot_state_mapping.csv")
87
+ all_data = []
88
+
89
+ # Iterate over subfolders (APK folders)
90
+ for apk_folder_name in os.listdir(parent_folder_path):
91
+ apk_folder_path = os.path.join(parent_folder_path, apk_folder_name)
92
+
93
+ # Ensure it's a directory containing a utg.js file
94
+ if os.path.isdir(apk_folder_path):
95
+ utg_file_path = os.path.join(apk_folder_path, 'utg.js')
96
+ if os.path.isfile(utg_file_path):
97
+ folder_data = extract_node_data(utg_file_path, apk_folder_name)
98
+ all_data.extend(folder_data)
99
+ else:
100
+ print(f"Skipping {apk_folder_path} - no utg.js file found")
101
+
102
+ # Write all data to a single CSV file
103
+ with open(output_csv_path, 'w', newline='') as csvfile:
104
+ fieldnames = ["screen_id", "state_str", "structure_str", "vh_json_id", "vh_xml_id"]
105
+ writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
106
+
107
+ writer.writeheader()
108
+ writer.writerows(all_data)
109
+
110
+ print(f"Overall CSV file generated at {output_csv_path}")
111
+
112
+
113
+ # Specify the folder path
114
+ folder_path = '/path/to/folder'
115
+
116
+ # Uncomment one of the following lines to run the desired function:
117
+ # For processing a single APK folder, uncomment this line:
118
+ # process_single_folder(folder_path)
119
+
120
+ # For processing multiple APK folders in a parent folder, uncomment this line:
121
+ # process_multiple_folders(folder_path)