Add define HOST_XXX to support pal.h
[platform/core/dotnet/launcher.git] / tools / CandidateMCJ / candidate_mcj.py
1 #!/usr/bin/env python3
2
3 import subprocess, os, argparse
4
5 from time import sleep
6 from getpass import getpass
7
8 # Default .NET application used in TV
9 pre_app_lists = [
10         "com.samsung.tv.csfs",
11         "com.samsung.tv.mycontents",
12         "com.samsung.tv.samsung-health",
13         "com.samsung.tv.searchall",
14         "com.samsung.tv.social-chat-app",
15         "com.samsung.tv.store"]
16
17 # Default system module used for candidate process in TV
18 pre_system_module_lists = [
19         "System.Private.CoreLib",
20         "Tizen.NUI",
21         "System.Net.Http",
22         "System.Text.Json"]
23
24 tizen_profile_data_path = "/home/owner/data/.__tizen_candidate_profile_data"
25
26 def cmd(command):
27     return subprocess.run((f"sdb -s {serial_num} " + command).split(), encoding="utf-8", stdout=subprocess.PIPE)
28
29
30 def download_mcj_edit():
31     if not os.path.exists("mcj-edit.py"):
32         print("Download mcj-edit.py: Start")
33
34         user_id = input("Username for 'https://github.sec.samsung.net': ")
35         password = getpass(f"Password for 'https://{user_id}@github.sec.samsung.net': ")
36         subprocess.Popen((f"curl -L -O https://raw.github.sec.samsung.net/dotnet/runtime/release/6.0-tizen/src/coreclr/tools/mcj-edit/mcj-edit.py -u {user_id}:{password} -v").split(), encoding="utf-8", stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout
37         sleep(3)
38         with open("mcj-edit.py", 'r') as f:
39             if "404: Not Found" == f.readline():
40                 print("\nremote: Invalid username or password.")
41                 os.remove("mcj-edit.py")
42                 exit(1)
43
44         print("Download mcj-edit.py: End")
45
46
47 def get_profile():
48     print("Get profile data: Start")
49
50     for app in app_ids:
51         print(f"Kill appId: {app}")
52         cmd(f"shell app_launcher -t {app}")
53         cmd(f"shell dotnettool --rm-app-profile {app}")
54         sleep(3)
55
56     cmd(f"shell cp {tizen_profile_data_path} {tizen_profile_data_path}.origin")
57     cmd(f"shell rm {tizen_profile_data_path}")
58     cmd(f'shell killall dotnet-hydra-loader dotnet-loader')
59     sleep(30)
60     
61     for app in app_ids:
62         print(f"Launch appId: {app}")
63         cmd(f"shell app_launcher -s {app}")
64         sleep(30)
65         cmd(f"shell app_launcher -t {app}")
66         cmd(f"pull /home/owner/apps_rw/{app}/data/.__tizen_specific_profile_data {app}")
67         sleep(3)
68
69     print("Get profile data: End")
70
71
72 def split_profile():
73     print("Split profile data: Start")
74
75     system_module_path = ""
76     with open("system_module", 'w') as f:
77         for module in sys_modules:
78             f.write(module+"\n")
79             print(f"System modules: {module}")
80
81     for app in app_ids:
82         print(f"Split appId: {app}")
83         subprocess.run((f"python3 mcj-edit.py split -i {app} --system-modules-list system_module").split(), encoding="utf-8", stdout=subprocess.PIPE)
84         sleep(3)
85
86     os.remove("system_module")
87
88     print("Split profile data: End")
89
90
91 def set_profile():
92     cmd(f"shell chsmack -a User::App::Shared {tizen_profile_data_path}")
93     cmd(f"shell chown owner:users {tizen_profile_data_path}")
94     cmd(f"shell chmod 644 {tizen_profile_data_path}")
95     cmd(f"shell rm {tizen_profile_data_path}.origin")
96     os.remove("mcj-edit.py")
97
98     for app in app_ids:
99         subprocess.run((f"rm {app}").split(), encoding="utf-8", stdout=subprocess.PIPE)
100         subprocess.run((f"rm {app}.app").split(), encoding="utf-8", stdout=subprocess.PIPE)
101         subprocess.run((f"rm {app}.sys").split(), encoding="utf-8", stdout=subprocess.PIPE)
102
103
104 def merge_profile():
105     print("Merge profile data: Start")
106
107     app_sys_lists = ""
108     for app in app_ids:
109         app_sys_lists += f"-i {app}.sys "
110
111     subprocess.run((f"python3 mcj-edit.py merge " + app_sys_lists + "-o .__tizen_candidate_profile_data").split(), encoding="utf-8", stdout=subprocess.PIPE)
112     sleep(3)
113
114     print("Merge profile data: End")
115
116     if not os.path.exists(f".__tizen_candidate_profile_data"):
117         cmd(f"shell cp {tizen_profile_data_path}.origin {tizen_profile_data_path}")
118         set_profile()
119         print("Candidate profile data generation: FAIL")
120         exit(1)
121     else:
122         cmd(f"push .__tizen_candidate_profile_data /home/owner/data/")
123         set_profile()
124         print("Candidate profile data generation: SUCCESS")
125
126
127 def read_serial():
128     global serial_num
129
130     raw = subprocess.run(("sdb devices").split(), encoding="utf-8", stdout=subprocess.PIPE).stdout
131     lines = [l for l in raw.splitlines() if not l.startswith("* ")]
132     if len(lines) <= 1:
133         serial_num = None
134     elif len(lines) == 2:
135         serial_num = lines[1].split("   ")[0].split(":")[0].strip()
136     else:
137         serials = []
138         for idx in range(1, len(lines)):
139             serial = lines[idx].split(" ")[0].split(":")[0].replace("device", "").strip()
140             serials.append(serial)
141             print(f"[{idx}] {serial} - {lines[idx].split('      ')[2].strip()}")
142         choice = input(f"Select a device [1-{len(lines) - 1}]: ")
143         serial_num = serials[int(choice) - 1].strip() if choice.isdigit() else None
144     return serial_num
145
146
147 def main():
148     epilog = \
149    '''Reference link :
150    https://github.sec.samsung.net/dotnet/runtime/blob/release/6.0-tizen/src/coreclr/tools/mcj-edit/README.md
151    '''
152
153     global app_ids, sys_modules
154
155     parser = argparse.ArgumentParser(description="Profile data generation tools to use in candidate process", epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter)
156     parser.add_argument("-a", metavar="APP_ID", nargs="*", type=str, help='target app ids (default: predefined 6 applications)', default=pre_app_lists)
157     parser.add_argument("-s", metavar="SYS_MODULE", nargs="*", type=str, help='system module lists (default: predefined 4 system modules)', default=pre_system_module_lists)
158     args = parser.parse_args()
159     app_ids = args.a
160     sys_modules = args.s
161
162     serial = read_serial()
163     if serial is None:
164         print("No connected device(s).")
165         exit(1)
166
167     cmd(f"root on")
168     cmd(f"shell mount -o remount,rw /")
169
170     download_mcj_edit();
171     get_profile()
172     split_profile()
173     merge_profile()
174
175
176 if __name__ == "__main__":
177     try:
178         main()
179     except KeyboardInterrupt:
180         exit(1)