taskPalio.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. from io import BytesIO
  2. import os
  3. import shutil
  4. import subprocess
  5. import time
  6. from zipfile import ZipFile
  7. import base64
  8. import requests
  9. from werkzeug.datastructures import FileStorage
  10. from PIL import Image
  11. from celery import Celery
  12. import project_config
  13. def vprint(*data):
  14. if project_config.verbose:
  15. print(*data)
  16. celery = Celery(__name__)
  17. celery.conf.broker_url = os.environ.get("CELERY_BROKER_URL", "redis://localhost:6379")
  18. celery.conf.result_backend = os.environ.get("CELERY_RESULT_BACKEND", "redis://localhost:6379")
  19. celery.conf.task_track_started = True
  20. celery.conf.task_time_limit = 1200
  21. # celery.conf.task_serializer = 'pickle'
  22. # celery.conf.result_serializer = 'pickle'
  23. def create_folder(package_id):
  24. path = os.path.join(project_config.temp_folder, package_id)
  25. if not os.path.exists(path):
  26. os.mkdir(path)
  27. else:
  28. shutil.rmtree(path)
  29. os.mkdir(path)
  30. vprint(path)
  31. path_dest = os.path.join(path, project_config.base_project_name)
  32. if not os.path.exists(path_dest):
  33. shutil.copytree(project_config.base_project, path_dest)
  34. code_path = "app/src/main/java/"
  35. orig_code = "com/example/qmeralitesamplecode"
  36. path_package_id = package_id.replace(".", "/")
  37. c_code_path = os.path.join(path_dest, code_path, path_package_id)
  38. orig_code_path = os.path.join(path_dest, code_path, orig_code)
  39. if not os.path.exists(c_code_path):
  40. shutil.copytree(orig_code_path, c_code_path)
  41. shutil.rmtree(orig_code_path)
  42. return path_dest, c_code_path
  43. def change_acc(c_code_path, acc, enable_sms):
  44. main_activity = os.path.join(c_code_path, "MAB.java")
  45. with open(main_activity, "r") as f:
  46. file_source = f.read()
  47. replaced = file_source.replace("***REPLACE***WITH***YOUR***QMERA***ACCOUNT***", acc)
  48. if enable_sms == 1:
  49. replaced = replaced.replace("isEnabledSMS = false", "isEnabledSMS = true")
  50. with open(main_activity, "w") as f:
  51. f.write(replaced)
  52. def change_url(c_code_path, url):
  53. main_activity = os.path.join(c_code_path, "MAB.java")
  54. with open(main_activity, "r") as f:
  55. file_source = f.read()
  56. replaced = file_source.replace("https://www.google.com", url)
  57. with open(main_activity, "w") as f:
  58. f.write(replaced)
  59. def change_name(path_dest, name, enable_sms):
  60. manifest = os.path.join(path_dest, "app/src/main/AndroidManifest.xml")
  61. string_res = os.path.join(path_dest, "app/src/main/res/values/strings.xml")
  62. with open(manifest, "r") as f:
  63. lines = f.readlines()
  64. # file_source = f.read()
  65. with open(manifest, "w") as f:
  66. for line in lines:
  67. if "NexilisLite" in line:
  68. line = line.replace("NexilisLite", name)
  69. if enable_sms == 0:
  70. if "SMS" not in line:
  71. f.write(line)
  72. else:
  73. f.write(line)
  74. with open(string_res, "r") as f:
  75. file_source = f.read()
  76. replaced = file_source.replace("Nexilis Sport", name)
  77. with open(string_res, "w") as f:
  78. f.write(replaced)
  79. if enable_sms == 0:
  80. with open(manifest, "r") as f:
  81. lines = f.readlines()
  82. with open(manifest, "w") as f:
  83. counter = 0
  84. for line in lines:
  85. if counter == 0:
  86. if "<receiver" in line:
  87. counter = 6
  88. else:
  89. f.write(line)
  90. else:
  91. counter = counter - 1
  92. def change_font(path_dest, font, package):
  93. if font == 1:
  94. path_package_id = package.replace(".", "/")
  95. code_path = "app/src/main/java/"
  96. code_path = os.path.join(path_dest, code_path, path_package_id)
  97. res_path = "app/src/main/res/"
  98. res_path = os.path.join(path_dest, res_path)
  99. javas = [os.path.join(dp, f) for dp, dn, filenames in os.walk(code_path) for f in filenames]
  100. res = [os.path.join(dp, f) for dp, dn, filenames in os.walk(res_path) for f in filenames if "xml" in f]
  101. button_apps_code_path = os.path.join(path_dest, "palio-button-app/src/main/java")
  102. button_apps_res_path = os.path.join(path_dest, "palio-button-app/src/main/res-pba/")
  103. lib_javas = [os.path.join(dp, f) for dp, dn, filenames in os.walk(button_apps_code_path) for f in filenames]
  104. lib_res = [os.path.join(dp, f) for dp, dn, filenames in os.walk(button_apps_res_path) for f in filenames if
  105. "xml" in f]
  106. for j in javas:
  107. print(j)
  108. if "ForumFragment.java" in j:
  109. with open(j, "r") as f:
  110. file_source = f.read()
  111. replaced = file_source.replace("pb_poppins", "roboto")
  112. with open(j, "w") as f:
  113. f.write(replaced)
  114. for j in res:
  115. print(j)
  116. with open(j, "r") as f:
  117. file_source = f.read()
  118. replaced = file_source.replace("pb_poppins", "roboto")
  119. with open(j, "w") as f:
  120. f.write(replaced)
  121. for j in lib_javas:
  122. print(j)
  123. with open(j, "r") as f:
  124. file_source = f.read()
  125. replaced = file_source.replace("pb_poppins", "roboto")
  126. with open(j, "w") as f:
  127. f.write(replaced)
  128. for j in lib_res:
  129. print(j)
  130. with open(j, "r") as f:
  131. file_source = f.read()
  132. replaced = file_source.replace("pb_poppins", "roboto")
  133. with open(j, "w") as f:
  134. f.write(replaced)
  135. def change_package(path_dest, package, enable_location):
  136. build_gradle = os.path.join(path_dest, 'app/build.gradle')
  137. with open(build_gradle, "r") as f:
  138. file_source = f.read()
  139. replaced = file_source.replace("com.example.qmeralitesamplecode", package)
  140. with open(build_gradle, "w") as f:
  141. f.write(replaced)
  142. proguard = os.path.join(path_dest, 'app/proguard-rules.pro')
  143. with open(proguard, "r") as f:
  144. file_source = f.read()
  145. replaced = file_source.replace("com.example.qmeralitesamplecode", package)
  146. with open(proguard, "w") as f:
  147. f.write(replaced)
  148. manifest = os.path.join(path_dest, "app/src/main/AndroidManifest.xml")
  149. with open(manifest, "r") as f:
  150. file_source = f.read()
  151. replaced = file_source.replace("com.example.qmeralitesamplecode", package)
  152. with open(manifest, "w") as f:
  153. f.write(replaced)
  154. path_package_id = package.replace(".", "/")
  155. code_path = "app/src/main/java/"
  156. code_path = os.path.join(path_dest, code_path, path_package_id)
  157. res_path = "app/src/main/res/"
  158. res_path = os.path.join(path_dest, res_path)
  159. javas = [os.path.join(dp, f) for dp, dn, filenames in os.walk(code_path) for f in filenames]
  160. res = [os.path.join(dp, f) for dp, dn, filenames in os.walk(res_path) for f in filenames if "xml" in f]
  161. for j in javas:
  162. print(j)
  163. with open(j, "r") as f:
  164. file_source = f.read()
  165. replaced = file_source.replace("com.example.qmeralitesamplecode", package)
  166. with open(j, "w") as f:
  167. f.write(replaced)
  168. for j in res:
  169. print(j)
  170. with open(j, "r") as f:
  171. file_source = f.read()
  172. replaced = file_source.replace("com.example.qmeralitesamplecode", package)
  173. with open(j, "w") as f:
  174. f.write(replaced)
  175. queue_page = os.path.join(path_dest, code_path, "QueueBank.java")
  176. with open(queue_page, "r") as f:
  177. lines = f.readlines()
  178. with open(queue_page, "w") as f:
  179. counter = 0
  180. for line in lines:
  181. if counter == 0:
  182. if enable_location == 0 and "LocationOn1" in line:
  183. counter = 9
  184. if enable_location == 0 and "LocationOn2" in line:
  185. counter = 17
  186. else:
  187. f.write(line)
  188. else:
  189. counter = counter - 1
  190. manifest = os.path.join(path_dest, "app/src/main/AndroidManifest.xml")
  191. with open(manifest, "r") as f:
  192. lines = f.readlines()
  193. with open(manifest, "w") as f:
  194. counter = 0
  195. for line in lines:
  196. if counter == 0:
  197. if enable_location == 0 and "<!-- LocationOn/Off -->" in line:
  198. counter = 3
  199. else:
  200. f.write(line)
  201. else:
  202. counter = counter - 1
  203. if package.__contains__('digipos') or package.__contains__('digisales'):
  204. settings_gradle = os.path.join(path_dest, "settings.gradle")
  205. with open(settings_gradle, "r") as f:
  206. lines = f.readlines()
  207. with open(settings_gradle, "w") as f:
  208. counter = 0
  209. for line in lines:
  210. if counter == 0:
  211. if "forallapps" in line:
  212. counter = 5
  213. else:
  214. f.write(line)
  215. else:
  216. counter = counter - 1
  217. mab_path = os.path.join(path_dest, code_path, "MAB.java")
  218. with open(mab_path, "r") as f:
  219. lines = f.readlines()
  220. with open(mab_path, "w") as f:
  221. counter1 = 0
  222. counter2 = 0
  223. for line in lines:
  224. if "fordigisales1" in line:
  225. counter1 = 132
  226. elif "fordigisales2" in line:
  227. counter2 = 46
  228. elif counter1 > 0:
  229. counter1 = counter1 - 1
  230. elif counter2 > 0:
  231. counter2 = counter2 - 1
  232. else:
  233. f.write(line)
  234. notif_center = os.path.join(path_dest, code_path, "notificationCenterTab/NotificationCenterFragment.java")
  235. with open(notif_center, "r") as f:
  236. lines = f.readlines()
  237. with open(notif_center, "w") as f:
  238. counter = 0
  239. for line in lines:
  240. if counter == 0:
  241. if "fordigisales" in line:
  242. counter = 14
  243. else:
  244. f.write(line)
  245. else:
  246. counter = counter - 1
  247. else:
  248. settings_gradle = os.path.join(path_dest, "settings.gradle")
  249. with open(settings_gradle, "r") as f:
  250. lines = f.readlines()
  251. with open(settings_gradle, "w") as f:
  252. counter = 0
  253. for line in lines:
  254. if counter == 0:
  255. if "fordigisales" in line:
  256. counter = 5
  257. else:
  258. f.write(line)
  259. else:
  260. counter = counter - 1
  261. colorValue = os.path.join(path_dest, "app/src/main/res/values/colors.xml")
  262. with open(colorValue, "r") as c:
  263. lines = c.readlines()
  264. with open(colorValue, "w") as c:
  265. for lineColor in lines:
  266. if "fordigisales" in lineColor:
  267. continue
  268. else:
  269. c.write(lineColor)
  270. if package == "com.nexilis.persija" or package == "io.newuniverse.GoToMalls" or package == "io.qmera.mylab":
  271. string_res = os.path.join(path_dest, "app/src/main/res/values/strings.xml")
  272. string_en_res = os.path.join(path_dest, "app/src/main/res/values-en/strings.xml")
  273. string_id_res = os.path.join(path_dest, "app/src/main/res/values-in/strings.xml")
  274. with open(string_res, "r") as f:
  275. file_source = f.read()
  276. replaced = file_source.replace("Nexilis", "Qmera")
  277. with open(string_res, "w") as f:
  278. f.write(replaced)
  279. with open(string_en_res, "r") as f:
  280. file_source = f.read()
  281. replaced = file_source.replace("Nexilis", "Qmera")
  282. with open(string_en_res, "w") as f:
  283. f.write(replaced)
  284. with open(string_id_res, "r") as f:
  285. file_source = f.read()
  286. replaced = file_source.replace("Nexilis", "Qmera")
  287. with open(string_id_res, "w") as f:
  288. f.write(replaced)
  289. old_img_powered = "app/src/main/res/drawable/pb_powered_button.png"
  290. old_img_powered = os.path.join(path_dest, old_img_powered)
  291. old_new_img_powered = "app/src/main/res/drawable/pb_powered_button_temp.png"
  292. old_new_img_powered = os.path.join(path_dest, old_new_img_powered)
  293. shutil.move(old_img_powered, old_new_img_powered)
  294. img_powered = "app/src/main/res/drawable/pb_powered_button1.png"
  295. img_powered = os.path.join(path_dest, img_powered)
  296. new_img_powered = "app/src/main/res/drawable/pb_powered_button.png"
  297. new_img_powered = os.path.join(path_dest, new_img_powered)
  298. shutil.move(img_powered, new_img_powered)
  299. if package == "com.telkomsel.smb":
  300. string_res = os.path.join(path_dest, "app/src/main/res/values/strings.xml")
  301. string_en_res = os.path.join(path_dest, "app/src/main/res/values-en/strings.xml")
  302. string_id_res = os.path.join(path_dest, "app/src/main/res/values-in/strings.xml")
  303. with open(string_res, "r") as f:
  304. file_source = f.read()
  305. replaced = file_source.replace("Nexilis", "Telkomsel")
  306. with open(string_res, "w") as f:
  307. f.write(replaced)
  308. with open(string_en_res, "r") as f:
  309. file_source = f.read()
  310. replaced = file_source.replace("Nexilis", "Telkomsel")
  311. with open(string_en_res, "w") as f:
  312. f.write(replaced)
  313. with open(string_id_res, "r") as f:
  314. file_source = f.read()
  315. replaced = file_source.replace("Nexilis", "Telkomsel")
  316. with open(string_id_res, "w") as f:
  317. f.write(replaced)
  318. old_img_powered = "app/src/main/res/drawable/pb_powered_button.png"
  319. old_img_powered = os.path.join(path_dest, old_img_powered)
  320. old_new_img_powered = "app/src/main/res/drawable/pb_powered_button_temp.png"
  321. old_new_img_powered = os.path.join(path_dest, old_new_img_powered)
  322. shutil.move(old_img_powered, old_new_img_powered)
  323. img_powered = "app/src/main/res/drawable/pb_powered_button2.png"
  324. img_powered = os.path.join(path_dest, img_powered)
  325. new_img_powered = "app/src/main/res/drawable/pb_powered_button.png"
  326. new_img_powered = os.path.join(path_dest, new_img_powered)
  327. shutil.move(img_powered, new_img_powered)
  328. def change_logo(path_dest, logo, logo_float=None):
  329. img_path = "app/src/main/res/drawable/ic_launcher.png"
  330. img_path = os.path.join(path_dest, img_path)
  331. img_notif = "app/src/main/res/drawable/pb_ball.png"
  332. img_notif = os.path.join(path_dest, img_notif)
  333. img_path_float = "app/src/main/res/drawable/pb_button.png"
  334. img_path_float = os.path.join(path_dest, img_path_float)
  335. if isinstance(logo, str):
  336. logo = requests.get('https://digixplatform.com/dashboardv2/uploads/logo/{}'.format(logo), verify=False)
  337. with open(img_path, "wb") as f:
  338. f.write(logo.content)
  339. with open(img_path, "rb") as f:
  340. logo = Image.open(f)
  341. logo = logo.resize((512, 512))
  342. if logo_float:
  343. logo_float = requests.get('https://digixplatform.com/dashboardv2/uploads/logofloat/{}'.format(logo_float),
  344. verify=False)
  345. with open(img_path_float, "wb") as f:
  346. f.write(logo_float.content)
  347. with open(img_path_float, "rb") as f:
  348. logo_float = Image.open(f)
  349. logo_float = logo_float.resize((150, 150))
  350. else:
  351. logo = Image.open(BytesIO(logo))
  352. logo = logo.resize((512, 512))
  353. if logo_float:
  354. logo_float = Image.open(BytesIO(logo_float))
  355. logo_float = logo_float.resize((150, 150))
  356. logo.save(img_path, "PNG")
  357. if logo_float:
  358. logo_float.save(img_path_float, "PNG")
  359. logo_float.save(img_notif, "PNG")
  360. def change_tab(path_dest, tabs, tab_icon, package, tab3_mode, tab_amount):
  361. default_tab_icon = ["tab{}.png".format(x) for x in tabs]
  362. for i, icon in enumerate(default_tab_icon):
  363. if not tab_icon[i]:
  364. continue
  365. img_path = "app/src/main/res/drawable"
  366. img_path = os.path.join(path_dest, img_path, icon)
  367. if isinstance(tab_icon[i], str):
  368. logo = requests.get('https://digixplatform.com/dashboardv2/uploads/tab_icon/{}'.format(tab_icon[i]),
  369. verify=False)
  370. with open(img_path, "wb") as f:
  371. f.write(logo.content)
  372. with open(img_path, "rb") as f:
  373. logo = Image.open(f)
  374. logo = logo.resize((150, 150))
  375. logo.save(img_path, "PNG")
  376. else:
  377. logo = Image.open(BytesIO(tab_icon[i]))
  378. logo = logo.resize((150, 150))
  379. logo.save(img_path, "PNG")
  380. path_package_id = package.replace(".", "/")
  381. code_path = "app/src/main/java/"
  382. sobj_code_path = os.path.join(path_dest, code_path, path_package_id, "SObj.java")
  383. print(sobj_code_path)
  384. with open(sobj_code_path, "r") as f:
  385. file_source = f.read()
  386. replaced = file_source.replace("1,2,3,4", ",".join(tabs))
  387. with open(sobj_code_path, "w") as f:
  388. f.write(replaced)
  389. main_code_path = os.path.join(path_dest, code_path, path_package_id, "MAB.java")
  390. with open(main_code_path, "r") as f:
  391. file_source = f.read()
  392. replaced = file_source.replace('tab3 = "0"', 'tab3 = "{}"'.format(tab3_mode))
  393. with open(main_code_path, "w") as f:
  394. f.write(replaced)
  395. prefs_code_path = os.path.join(path_dest, code_path, path_package_id, "util/PrefsUtil.java")
  396. with open(prefs_code_path, "r") as f:
  397. file_source = f.read()
  398. replaced = file_source.replace('DEFAULT_TAB_AMOUNT = "4"', 'DEFAULT_TAB_AMOUNT = "{}"'.format(tab_amount))
  399. with open(prefs_code_path, "w") as f:
  400. f.write(replaced)
  401. def change_fb(path_dest, fb_order, package, fb_icon):
  402. fb_order_list = [int(x) for x in fb_order.split(",")]
  403. default_fb_icon = ["pb_button_chat.png", "pb_button_call.png", "pb_button_cc.png", "pb_button_stream.png",
  404. "nexilis_fb_04.png"]
  405. for x, i in enumerate(fb_order_list):
  406. if not fb_icon[x]:
  407. continue
  408. img_path = "app/src/main/res/drawable-nodpi"
  409. if i - 1 in range(-len(default_fb_icon), len(default_fb_icon)):
  410. img_path = os.path.join(path_dest, img_path, default_fb_icon[i - 1])
  411. if isinstance(fb_icon[x], str):
  412. logo = requests.get('https://digixplatform.com/dashboardv2/uploads/fb_icon/{}'.format(fb_icon[x]),
  413. verify=False)
  414. with open(img_path, "wb") as f:
  415. f.write(logo.content)
  416. with open(img_path, "rb") as f:
  417. logo = Image.open(f)
  418. logo = logo.resize((150, 150))
  419. logo.save(img_path, "PNG")
  420. else:
  421. logo = Image.open(BytesIO(fb_icon[x]))
  422. logo = logo.resize((150, 150))
  423. logo.save(img_path, "PNG")
  424. path_package_id = package.replace(".", "/")
  425. code_path = "app/src/main/java/"
  426. main_code_path = os.path.join(path_dest, code_path, path_package_id, "MAB.java")
  427. with open(main_code_path, "r") as f:
  428. file_source = f.read()
  429. replaced = file_source.replace('dockedPlacement = "1,2,3,4,5"', 'dockedPlacement = "{}"'.format(fb_order))
  430. with open(main_code_path, "w") as f:
  431. f.write(replaced)
  432. def change_background(path_dest, background):
  433. # if isinstance(background, str):
  434. # background = background.split(",")
  435. # for i,b in enumerate(background):
  436. # n = i+1
  437. # img_path = "app/src/main/res/drawable-nodpi/pb_lbackground_{}.png".format(n)
  438. # img_path = os.path.join(path_dest, img_path)
  439. # logo = requests.get('https://digixplatform.com/dashboardv2/uploads/background/{}'.format(b), verify=False)
  440. # with open(img_path, "wb") as f:
  441. # f.write(logo.content)
  442. # with open(img_path, "rb") as f:
  443. # logo = Image.open(f)
  444. # logo = logo.resize((600, 1250))
  445. # logo.save(img_path, "PNG")
  446. # else:
  447. # img_path = "app/src/main/res/drawable-nodpi/pb_lbackground_1.png"
  448. # logo = Image.open(BytesIO(background))
  449. # logo = logo.resize((600, 1250))
  450. # logo.save(img_path, "PNG")
  451. pass
  452. def change_access(path_dest, access_model, package):
  453. access = ["CPAAS_MODE_FLOATING", "CPAAS_MODE_DOCKED", "CPAAS_MODE_BURGER"]
  454. path_package_id = package.replace(".", "/")
  455. code_path = "app/src/main/java/"
  456. code_path = os.path.join(path_dest, code_path, path_package_id, "util", "PrefsUtil.java")
  457. print(code_path)
  458. with open(code_path, "r") as f:
  459. file_source = f.read()
  460. replaced = file_source.replace("= CPAAS_MODE_DOCKED", "= {}".format(access[access_model]))
  461. with open(code_path, "w") as f:
  462. f.write(replaced)
  463. def change_certificate(path_dest, key, keyfile, keytool):
  464. keyfile_name = "{}.keystore".format(key["alias"])
  465. keyfile_path = os.path.join(path_dest, keyfile_name)
  466. if keyfile:
  467. if isinstance(keyfile, FileStorage):
  468. keyfile.save(keyfile_path)
  469. else:
  470. with open(keyfile_path, 'wb') as f:
  471. f.write(base64.b64decode(bytes(keyfile, 'ascii')))
  472. else:
  473. vprint("keytool run")
  474. os.chdir(path_dest)
  475. vprint("current working directory: ", os.getcwd())
  476. dname = "CN={}, OU={}, O={}, L={}, S={}, C={}".format(key["common_name"], key["organization_unit"],
  477. key["organization_name"], key["locality_name"],
  478. key["state_name"], key["country"])
  479. cmd = [keytool, "-genkey", "-v", "-storetype", "JKS", "-keystore", keyfile_path, "-alias", key["alias"], "-keyalg", "RSA",
  480. "-keysize", "2048",
  481. "-validity", "10000", "-dname", dname, "-storepass", key["store_password"], "-keypass",
  482. key["key_password"]]
  483. vprint(cmd)
  484. subprocess.run(cmd)
  485. vprint("keytool end")
  486. build_gradle = os.path.join(path_dest, 'app/build.gradle')
  487. with open(build_gradle, "r") as f:
  488. file_source = f.read()
  489. replaced = file_source.replace("allyourbase", key["store_password"])
  490. replaced = replaced.replace("arebelongto", key["key_password"])
  491. replaced = replaced.replace("key-qmeralite", key["alias"])
  492. with open(build_gradle, "w") as f:
  493. f.write(replaced)
  494. def change_huawei_file(path_dest, huawei_file, package_id):
  495. huaweifile_name = "agconnect-services.json"
  496. huaweifile_path = os.path.join(path_dest, "app/{}".format(huaweifile_name))
  497. if huawei_file:
  498. if isinstance(huawei_file, FileStorage):
  499. huawei_file.save(huaweifile_path)
  500. else:
  501. with open(huaweifile_path, 'wb') as f:
  502. f.write(base64.b64decode(bytes(huawei_file, 'ascii')))
  503. path_package_id = package_id.replace(".", "/")
  504. code_path = "app/src/main/java/"
  505. main_code_path = os.path.join(path_dest, code_path, path_package_id, "MAB.java")
  506. with open(main_code_path, "r") as f:
  507. file_source = f.read()
  508. replaced = file_source.replace('isHMSEnabled = false', 'isHMSEnabled = true')
  509. with open(main_code_path, "w") as f:
  510. f.write(replaced)
  511. def change_fms_file(path_dest, fms_enable):
  512. build_gradle = os.path.join(path_dest, 'app/build.gradle')
  513. with open(build_gradle, "r") as f:
  514. lines = f.readlines()
  515. with open(build_gradle, "w") as f:
  516. if fms_enable == 0:
  517. for line in lines:
  518. if "withfcm" in line:
  519. continue
  520. else:
  521. f.write(line)
  522. else:
  523. for line in lines:
  524. if "-nofcm" in line:
  525. continue
  526. else:
  527. f.write(line)
  528. def change_adblock_file(path_dest, package, use_adblock):
  529. if use_adblock == 0:
  530. build_gradle = os.path.join(path_dest, 'app/build.gradle')
  531. with open(build_gradle, "r") as f:
  532. lines = f.readlines()
  533. with open(build_gradle, "w") as f:
  534. for line in lines:
  535. if "removeAdblock" in line:
  536. continue
  537. else:
  538. f.write(line)
  539. path_package_id = package.replace(".", "/")
  540. code_path = "app/src/main/java/"
  541. code_path = os.path.join(path_dest, code_path, path_package_id)
  542. javas = [os.path.join(dp, f) for dp, dn, filenames in os.walk(code_path) for f in filenames]
  543. for j in javas:
  544. print(j)
  545. with open(j, "r") as f:
  546. lines = f.readlines()
  547. with open(j, "w") as f:
  548. counter = 0
  549. for line in lines:
  550. if counter == 0:
  551. if "removeAdblock" in line:
  552. continue
  553. elif "import org.adblockplus.libadblockplus.android" in line:
  554. continue
  555. elif "removeAdbMultiLine" in line:
  556. counter = 11
  557. elif "extends AdblockWebView" in line:
  558. replaced = line.replace("extends AdblockWebView", "extends WebView")
  559. f.write(replaced)
  560. else:
  561. f.write(line)
  562. else:
  563. counter = counter - 1
  564. def run_build(path_dest):
  565. gradlew = os.path.join(path_dest, "gradlew")
  566. error = ""
  567. ret = subprocess.run([gradlew, 'clean', 'assembleRelease'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  568. if ret.returncode == 0:
  569. ret = subprocess.run([gradlew, 'bundleRelease'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  570. if ret.returncode != 0:
  571. error = "{}\n".format(ret.stderr.decode())
  572. vprint(error)
  573. else:
  574. error = "{}\n".format(ret.stderr.decode())
  575. vprint(error)
  576. return ret.returncode, error
  577. def deliver_apk(path_dest, package_id, key, key_exists):
  578. apk_dir = os.path.join(path_dest, 'app/build/outputs/apk/release/app-release.apk')
  579. aab_dir = os.path.join(path_dest, 'app/build/outputs/bundle/release/app-release.aab')
  580. keystore_name = '{}.keystore'.format(key["alias"])
  581. keystore_dir = os.path.join(path_dest, keystore_name)
  582. timenow = time.time()
  583. apk_name = "{}{}.apk".format(package_id, timenow)
  584. aab_name = "{}{}.aab".format(package_id, timenow)
  585. zip_name = "{}{}.zip".format(package_id, timenow)
  586. new_apk_dir = os.path.join(project_config.apk_folder, apk_name)
  587. new_aab_dir = os.path.join(project_config.apk_folder, aab_name)
  588. new_dir = os.path.join(project_config.apk_folder, zip_name)
  589. vprint(apk_dir)
  590. shutil.move(apk_dir, new_apk_dir)
  591. try:
  592. shutil.move(aab_dir, new_aab_dir)
  593. with ZipFile(new_dir, 'w') as zip_file:
  594. zip_file.write(new_apk_dir, os.path.basename(new_apk_dir))
  595. zip_file.write(new_aab_dir, os.path.basename(new_aab_dir))
  596. if not key_exists:
  597. zip_file.write(keystore_dir, os.path.basename(keystore_dir))
  598. os.remove(new_apk_dir)
  599. os.remove(new_aab_dir)
  600. project_path = os.path.join(project_config.temp_folder, package_id)
  601. shutil.rmtree(project_path)
  602. return {"status": "0", "name": zip_name}
  603. except Exception as e:
  604. return {"status": "4", "message": "Deliver APK & AAB failed"}
  605. def change_version(path_dest, version_code, version_name):
  606. build_gradle = os.path.join(path_dest, 'app/build.gradle')
  607. with open(build_gradle, "r") as f:
  608. file_source = f.read()
  609. replaced = file_source.replace("versionCode 3", "versionCode {}".format(version_code))
  610. replaced = replaced.replace('versionName "3.0"', 'versionName "{}"'.format(version_name))
  611. with open(build_gradle, "w") as f:
  612. f.write(replaced)
  613. @celery.task(name="create_task")
  614. def create_task(package_id, enable_sms, app_name, key, keystore, access_model,
  615. version_code, version_name, enable_location, font, fb_order, fb_icon, tabs, tab_icon, tab3_mode,
  616. tab_amount, huawei_file, fms_enable, use_adblock, key_exists,
  617. acc=None, logo=None, logo_float=None, background=None, url=None):
  618. path_dest, c_code_path = create_folder(package_id)
  619. vprint("path_dest: " + path_dest)
  620. vprint("c_code_path: " + c_code_path)
  621. if acc:
  622. change_acc(c_code_path, acc, enable_sms)
  623. if url:
  624. change_url(c_code_path, url)
  625. change_name(path_dest, app_name, enable_sms)
  626. change_certificate(path_dest, key, keystore, project_config.keytool)
  627. change_package(path_dest, package_id, enable_location)
  628. change_version(path_dest, version_code, version_name)
  629. change_font(path_dest, font, package_id)
  630. if logo:
  631. change_logo(path_dest, logo, logo_float)
  632. if background:
  633. change_background(path_dest, background)
  634. change_fb(path_dest, fb_order, package_id, fb_icon)
  635. change_access(path_dest, access_model, package_id)
  636. change_tab(path_dest, tabs, tab_icon, package_id, tab3_mode, tab_amount)
  637. change_huawei_file(path_dest, huawei_file, package_id)
  638. change_fms_file(path_dest, fms_enable)
  639. change_adblock_file(path_dest, package_id, use_adblock)
  640. os.chdir(path_dest)
  641. return_code, error = run_build(path_dest)
  642. if return_code == 0:
  643. return deliver_apk(path_dest, package_id, key, key_exists)
  644. return {"status": "3", "message": "Build failed"}