mainPalio4.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. import time
  2. import traceback
  3. from flask import Flask, request
  4. import requests
  5. from PIL import ImageFile, Image
  6. import os
  7. import shutil
  8. import subprocess
  9. from zipfile import ZipFile
  10. ImageFile.SAFEBLOCK = 2048 * 2048
  11. app = Flask(__name__)
  12. app.base_project = "/apps/3ps/build_apk/PalioLiteDev"
  13. # app.base_project = "/Users/easysoft/Documents/PalioLite"
  14. app.temp_folder = "/apps/3ps/build_apk/BuildApkDev"
  15. # app.temp_folder = "/Users/easysoft/BuildApk"
  16. app.apk_folder = "/var/www/html/palio.io/dashboardv2/uploads"
  17. # app.apk_folder = "/Users/easysoft/"
  18. app.verbose = True
  19. app.ssl = (
  20. '/etc/ssl/star.newuniverse.io/star.newuniverse.io.crt', '/etc/ssl/star.newuniverse.io/STAR_newuniverse_io.key')
  21. # app.ssl =('/etc/ssl/STAR_palio_io/STAR_palio_io.crt', '/etc/ssl/STAR_palio_io/STAR_palio_io.pem')
  22. # app.ssl = None
  23. app.keytool = '/usr/bin/keytool'
  24. app.base_project_name = os.path.basename(app.base_project)
  25. def vprint(*data):
  26. if app.verbose:
  27. print(*data)
  28. def create_folder(package_id):
  29. path = os.path.join(app.temp_folder, package_id)
  30. if not os.path.exists(path):
  31. os.mkdir(path)
  32. else:
  33. shutil.rmtree(path)
  34. os.mkdir(path)
  35. vprint(path)
  36. path_dest = os.path.join(path, app.base_project_name)
  37. if not os.path.exists(path_dest):
  38. shutil.copytree(app.base_project, path_dest)
  39. code_path = "app/src/main/java/"
  40. orig_code = "com/example/qmeralitesamplecode"
  41. path_package_id = package_id.replace(".", "/")
  42. c_code_path = os.path.join(path_dest, code_path, path_package_id)
  43. orig_code_path = os.path.join(path_dest, code_path, orig_code)
  44. if not os.path.exists(c_code_path):
  45. shutil.copytree(orig_code_path, c_code_path)
  46. shutil.rmtree(orig_code_path)
  47. return path_dest, c_code_path
  48. def change_acc(c_code_path, acc, enable_sms):
  49. main_activity = os.path.join(c_code_path, "MAB.java")
  50. with open(main_activity, "r") as f:
  51. file_source = f.read()
  52. replaced = file_source.replace("***REPLACE***WITH***YOUR***QMERA***ACCOUNT***", acc)
  53. if enable_sms == 1:
  54. replaced = replaced.replace("isEnabledSMS = false", "isEnabledSMS = true")
  55. with open(main_activity, "w") as f:
  56. f.write(replaced)
  57. def change_url(c_code_path, url):
  58. main_activity = os.path.join(c_code_path, "MAB.java")
  59. with open(main_activity, "r") as f:
  60. file_source = f.read()
  61. replaced = file_source.replace("https://www.google.com", url)
  62. with open(main_activity, "w") as f:
  63. f.write(replaced)
  64. def change_name(path_dest, name, enable_sms):
  65. manifest = os.path.join(path_dest, "app/src/main/AndroidManifest.xml")
  66. string_res = os.path.join(path_dest, "app/src/main/res/values/strings.xml")
  67. with open(manifest, "r") as f:
  68. lines = f.readlines()
  69. # file_source = f.read()
  70. with open(manifest, "w") as f:
  71. for line in lines:
  72. if "NexilisLite" in line:
  73. line = line.replace("NexilisLite", name)
  74. if enable_sms == 0:
  75. if "SMS" not in line:
  76. f.write(line)
  77. else:
  78. f.write(line)
  79. with open(string_res, "r") as f:
  80. file_source = f.read()
  81. replaced = file_source.replace("Nexilis Sport", name)
  82. with open(string_res, "w") as f:
  83. f.write(replaced)
  84. def change_font(path_dest, font, package):
  85. if font == 1:
  86. path_package_id = package.replace(".", "/")
  87. code_path = "app/src/main/java/"
  88. code_path = os.path.join(path_dest, code_path, path_package_id)
  89. res_path = "app/src/main/res/"
  90. res_path = os.path.join(path_dest, res_path)
  91. javas = [os.path.join(dp, f) for dp, dn, filenames in os.walk(code_path) for f in filenames]
  92. res = [os.path.join(dp, f) for dp, dn, filenames in os.walk(res_path) for f in filenames if "xml" in f]
  93. button_apps_code_path = os.path.join(path_dest, "palio-button-app/src/main/java")
  94. button_apps_res_path = os.path.join(path_dest, "palio-button-app/src/main/res-pba/")
  95. lib_javas = [os.path.join(dp, f) for dp, dn, filenames in os.walk(button_apps_code_path) for f in filenames]
  96. lib_res = [os.path.join(dp, f) for dp, dn, filenames in os.walk(button_apps_res_path) for f in filenames if
  97. "xml" in f]
  98. for j in javas:
  99. print(j)
  100. if "ForumFragment.java" in j:
  101. with open(j, "r") as f:
  102. file_source = f.read()
  103. replaced = file_source.replace("pb_poppins", "roboto")
  104. with open(j, "w") as f:
  105. f.write(replaced)
  106. for j in res:
  107. print(j)
  108. with open(j, "r") as f:
  109. file_source = f.read()
  110. replaced = file_source.replace("pb_poppins", "roboto")
  111. with open(j, "w") as f:
  112. f.write(replaced)
  113. for j in lib_javas:
  114. print(j)
  115. with open(j, "r") as f:
  116. file_source = f.read()
  117. replaced = file_source.replace("pb_poppins", "roboto")
  118. with open(j, "w") as f:
  119. f.write(replaced)
  120. for j in lib_res:
  121. print(j)
  122. with open(j, "r") as f:
  123. file_source = f.read()
  124. replaced = file_source.replace("pb_poppins", "roboto")
  125. with open(j, "w") as f:
  126. f.write(replaced)
  127. def change_package(path_dest, package, enable_location):
  128. build_gradle = os.path.join(path_dest, 'app/build.gradle')
  129. with open(build_gradle, "r") as f:
  130. file_source = f.read()
  131. replaced = file_source.replace("com.example.qmeralitesamplecode", package)
  132. with open(build_gradle, "w") as f:
  133. f.write(replaced)
  134. proguard = os.path.join(path_dest, 'app/proguard-rules.pro')
  135. with open(proguard, "r") as f:
  136. file_source = f.read()
  137. replaced = file_source.replace("com.example.qmeralitesamplecode", package)
  138. with open(proguard, "w") as f:
  139. f.write(replaced)
  140. manifest = os.path.join(path_dest, "app/src/main/AndroidManifest.xml")
  141. with open(manifest, "r") as f:
  142. file_source = f.read()
  143. replaced = file_source.replace("com.example.qmeralitesamplecode", package)
  144. with open(manifest, "w") as f:
  145. f.write(replaced)
  146. path_package_id = package.replace(".", "/")
  147. code_path = "app/src/main/java/"
  148. code_path = os.path.join(path_dest, code_path, path_package_id)
  149. res_path = "app/src/main/res/"
  150. res_path = os.path.join(path_dest, res_path)
  151. javas = [os.path.join(dp, f) for dp, dn, filenames in os.walk(code_path) for f in filenames]
  152. res = [os.path.join(dp, f) for dp, dn, filenames in os.walk(res_path) for f in filenames if "xml" in f]
  153. for j in javas:
  154. print(j)
  155. with open(j, "r") as f:
  156. file_source = f.read()
  157. replaced = file_source.replace("com.example.qmeralitesamplecode", package)
  158. with open(j, "w") as f:
  159. f.write(replaced)
  160. for j in res:
  161. print(j)
  162. with open(j, "r") as f:
  163. file_source = f.read()
  164. replaced = file_source.replace("com.example.qmeralitesamplecode", package)
  165. with open(j, "w") as f:
  166. f.write(replaced)
  167. queue_page = os.path.join(path_dest, code_path, "QueueBank.java")
  168. with open(queue_page, "r") as f:
  169. lines = f.readlines()
  170. with open(queue_page, "w") as f:
  171. counter = 0
  172. for line in lines:
  173. if counter == 0:
  174. if enable_location == 0 and "LocationOn1" in line:
  175. counter = 9
  176. if enable_location == 0 and "LocationOn2" in line:
  177. counter = 17
  178. else:
  179. f.write(line)
  180. else:
  181. counter = counter - 1
  182. manifest = os.path.join(path_dest, "app/src/main/AndroidManifest.xml")
  183. with open(manifest, "r") as f:
  184. lines = f.readlines()
  185. with open(manifest, "w") as f:
  186. counter = 0
  187. for line in lines:
  188. if counter == 0:
  189. if enable_location == 0 and "<!-- LocationOn/Off -->" in line:
  190. counter = 3
  191. else:
  192. f.write(line)
  193. else:
  194. counter = counter - 1
  195. if package.__contains__('digipos') or package.__contains__('digisales'):
  196. settings_gradle = os.path.join(path_dest, "settings.gradle")
  197. with open(settings_gradle, "r") as f:
  198. lines = f.readlines()
  199. with open(settings_gradle, "w") as f:
  200. counter = 0
  201. for line in lines:
  202. if counter == 0:
  203. if "forallapps" in line:
  204. counter = 5
  205. else:
  206. f.write(line)
  207. else:
  208. counter = counter - 1
  209. mab_path = os.path.join(path_dest, code_path, "MAB.java")
  210. with open(mab_path, "r") as f:
  211. lines = f.readlines()
  212. with open(mab_path, "w") as f:
  213. counter1 = 0
  214. counter2 = 0
  215. for line in lines:
  216. if "fordigisales1" in line:
  217. counter1 = 132
  218. elif "fordigisales2" in line:
  219. counter2 = 27
  220. elif counter1 > 0:
  221. counter1 = counter1 - 1
  222. elif counter2 > 0:
  223. counter2 = counter2 - 1
  224. else:
  225. f.write(line)
  226. notif_center = os.path.join(path_dest, code_path, "notificationCenterTab/NotificationCenterFragment.java")
  227. with open(notif_center, "r") as f:
  228. lines = f.readlines()
  229. with open(notif_center, "w") as f:
  230. counter = 0
  231. for line in lines:
  232. if counter == 0:
  233. if "fordigisales" in line:
  234. counter = 14
  235. else:
  236. f.write(line)
  237. else:
  238. counter = counter - 1
  239. else:
  240. settings_gradle = os.path.join(path_dest, "settings.gradle")
  241. with open(settings_gradle, "r") as f:
  242. lines = f.readlines()
  243. with open(settings_gradle, "w") as f:
  244. counter = 0
  245. for line in lines:
  246. if counter == 0:
  247. if "fordigisales" in line:
  248. counter = 5
  249. else:
  250. f.write(line)
  251. else:
  252. counter = counter - 1
  253. colorValue = os.path.join(path_dest, "app/src/main/res/values/colors.xml")
  254. with open(colorValue, "r") as c:
  255. lines = c.readlines()
  256. with open(colorValue, "w") as c:
  257. for lineColor in lines:
  258. if "fordigisales" in lineColor:
  259. continue
  260. else:
  261. c.write(lineColor)
  262. if package == "com.nexilis.persija" or package == "io.newuniverse.GoToMalls" or package == "io.qmera.mylab":
  263. string_res = os.path.join(path_dest, "app/src/main/res/values/strings.xml")
  264. string_en_res = os.path.join(path_dest, "app/src/main/res/values-en/strings.xml")
  265. string_id_res = os.path.join(path_dest, "app/src/main/res/values-in/strings.xml")
  266. with open(string_res, "r") as f:
  267. file_source = f.read()
  268. replaced = file_source.replace("Nexilis", "Qmera")
  269. with open(string_res, "w") as f:
  270. f.write(replaced)
  271. with open(string_en_res, "r") as f:
  272. file_source = f.read()
  273. replaced = file_source.replace("Nexilis", "Qmera")
  274. with open(string_en_res, "w") as f:
  275. f.write(replaced)
  276. with open(string_id_res, "r") as f:
  277. file_source = f.read()
  278. replaced = file_source.replace("Nexilis", "Qmera")
  279. with open(string_id_res, "w") as f:
  280. f.write(replaced)
  281. old_img_powered = "app/src/main/res/drawable/pb_powered_button.png"
  282. old_img_powered = os.path.join(path_dest, old_img_powered)
  283. old_new_img_powered = "app/src/main/res/drawable/pb_powered_button_temp.png"
  284. old_new_img_powered = os.path.join(path_dest, old_new_img_powered)
  285. shutil.move(old_img_powered, old_new_img_powered)
  286. img_powered = "app/src/main/res/drawable/pb_powered_button1.png"
  287. img_powered = os.path.join(path_dest, img_powered)
  288. new_img_powered = "app/src/main/res/drawable/pb_powered_button.png"
  289. new_img_powered = os.path.join(path_dest, new_img_powered)
  290. shutil.move(img_powered, new_img_powered)
  291. if package == "com.telkomsel.smb":
  292. string_res = os.path.join(path_dest, "app/src/main/res/values/strings.xml")
  293. string_en_res = os.path.join(path_dest, "app/src/main/res/values-en/strings.xml")
  294. string_id_res = os.path.join(path_dest, "app/src/main/res/values-in/strings.xml")
  295. with open(string_res, "r") as f:
  296. file_source = f.read()
  297. replaced = file_source.replace("Nexilis", "Telkomsel")
  298. with open(string_res, "w") as f:
  299. f.write(replaced)
  300. with open(string_en_res, "r") as f:
  301. file_source = f.read()
  302. replaced = file_source.replace("Nexilis", "Telkomsel")
  303. with open(string_en_res, "w") as f:
  304. f.write(replaced)
  305. with open(string_id_res, "r") as f:
  306. file_source = f.read()
  307. replaced = file_source.replace("Nexilis", "Telkomsel")
  308. with open(string_id_res, "w") as f:
  309. f.write(replaced)
  310. old_img_powered = "app/src/main/res/drawable/pb_powered_button.png"
  311. old_img_powered = os.path.join(path_dest, old_img_powered)
  312. old_new_img_powered = "app/src/main/res/drawable/pb_powered_button_temp.png"
  313. old_new_img_powered = os.path.join(path_dest, old_new_img_powered)
  314. shutil.move(old_img_powered, old_new_img_powered)
  315. img_powered = "app/src/main/res/drawable/pb_powered_button2.png"
  316. img_powered = os.path.join(path_dest, img_powered)
  317. new_img_powered = "app/src/main/res/drawable/pb_powered_button.png"
  318. new_img_powered = os.path.join(path_dest, new_img_powered)
  319. shutil.move(img_powered, new_img_powered)
  320. def change_logo(path_dest, logo, logo_float=None):
  321. img_path = "app/src/main/res/drawable/ic_launcher.png"
  322. img_path = os.path.join(path_dest, img_path)
  323. img_notif = "app/src/main/res/drawable/pb_ball.png"
  324. img_notif = os.path.join(path_dest, img_notif)
  325. img_path_float = "app/src/main/res/drawable/pb_button.png"
  326. img_path_float = os.path.join(path_dest, img_path_float)
  327. if isinstance(logo, str):
  328. logo = requests.get('https://newuniverse.io/dashboardv2/uploads/logo/{}'.format(logo))
  329. with open(img_path, "wb") as f:
  330. f.write(logo.content)
  331. with open(img_path, "rb") as f:
  332. logo = Image.open(f)
  333. logo = logo.resize((512, 512))
  334. if logo_float:
  335. logo_float = requests.get('https://newuniverse.io/dashboardv2/uploads/logofloat/{}'.format(logo_float))
  336. with open(img_path_float, "wb") as f:
  337. f.write(logo_float.content)
  338. with open(img_path_float, "rb") as f:
  339. logo_float = Image.open(f)
  340. logo_float = logo_float.resize((150, 150))
  341. else:
  342. logo = Image.open(logo)
  343. logo = logo.resize((512, 512))
  344. if logo_float:
  345. logo_float = Image.open(logo_float)
  346. logo_float = logo_float.resize((150, 150))
  347. logo.save(img_path, "PNG")
  348. if logo_float:
  349. logo_float.save(img_path_float, "PNG")
  350. logo_float.save(img_notif, "PNG")
  351. def change_tab(path_dest, tabs, tab_icon, package, tab3_mode, tab_amount):
  352. default_tab_icon = ["tab{}.png".format(x) for x in tabs]
  353. for i, icon in enumerate(default_tab_icon):
  354. if not tab_icon[i]:
  355. continue
  356. img_path = "app/src/main/res/drawable"
  357. img_path = os.path.join(path_dest, img_path, icon)
  358. if isinstance(tab_icon[i], str):
  359. logo = requests.get('https://newuniverse.io/dashboardv2/uploads/tab_icon/{}'.format(tab_icon[i]))
  360. with open(img_path, "wb") as f:
  361. f.write(logo.content)
  362. with open(img_path, "rb") as f:
  363. logo = Image.open(f)
  364. logo = logo.resize((150, 150))
  365. logo.save(img_path, "PNG")
  366. else:
  367. logo = Image.open(tab_icon[i])
  368. logo = logo.resize((150, 150))
  369. logo.save(img_path, "PNG")
  370. path_package_id = package.replace(".", "/")
  371. code_path = "app/src/main/java/"
  372. sobj_code_path = os.path.join(path_dest, code_path, path_package_id, "SObj.java")
  373. print(sobj_code_path)
  374. with open(sobj_code_path, "r") as f:
  375. file_source = f.read()
  376. replaced = file_source.replace("1,2,3,4", ",".join(tabs))
  377. with open(sobj_code_path, "w") as f:
  378. f.write(replaced)
  379. main_code_path = os.path.join(path_dest, code_path, path_package_id, "MAB.java")
  380. with open(main_code_path, "r") as f:
  381. file_source = f.read()
  382. replaced = file_source.replace('tab3 = "0"', 'tab3 = "{}"'.format(tab3_mode))
  383. with open(main_code_path, "w") as f:
  384. f.write(replaced)
  385. prefs_code_path = os.path.join(path_dest, code_path, path_package_id, "util/PrefsUtil.java")
  386. with open(prefs_code_path, "r") as f:
  387. file_source = f.read()
  388. replaced = file_source.replace('DEFAULT_TAB_AMOUNT = "4"', 'DEFAULT_TAB_AMOUNT = "{}"'.format(tab_amount))
  389. with open(prefs_code_path, "w") as f:
  390. f.write(replaced)
  391. def change_fb(path_dest, fb_order, package, fb_icon):
  392. fb_order_list = [int(x) for x in fb_order.split(",")]
  393. default_fb_icon = ["pb_button_chat.png", "pb_button_call.png", "pb_button_cc.png", "pb_button_stream.png",
  394. "nexilis_fb_04.png"]
  395. for x,i in enumerate(fb_order_list):
  396. if not fb_icon[x]:
  397. continue
  398. img_path = "app/src/main/res/drawable-nodpi"
  399. if i-1 in range(-len(default_fb_icon), len(default_fb_icon)):
  400. img_path = os.path.join(path_dest, img_path, default_fb_icon[i-1])
  401. if isinstance(fb_icon[x], str):
  402. logo = requests.get('https://newuniverse.io/dashboardv2/uploads/fb_icon/{}'.format(fb_icon[x]))
  403. with open(img_path, "wb") as f:
  404. f.write(logo.content)
  405. with open(img_path, "rb") as f:
  406. logo = Image.open(f)
  407. logo = logo.resize((150, 150))
  408. logo.save(img_path, "PNG")
  409. else:
  410. logo = Image.open(fb_icon[x])
  411. logo = logo.resize((150, 150))
  412. logo.save(img_path, "PNG")
  413. path_package_id = package.replace(".", "/")
  414. code_path = "app/src/main/java/"
  415. main_code_path = os.path.join(path_dest, code_path, path_package_id, "MAB.java")
  416. with open(main_code_path, "r") as f:
  417. file_source = f.read()
  418. replaced = file_source.replace('dockedPlacement = "1,2,3,4,5"', 'dockedPlacement = "{}"'.format(fb_order))
  419. with open(main_code_path, "w") as f:
  420. f.write(replaced)
  421. def change_background(path_dest, background):
  422. # if isinstance(background, str):
  423. # background = background.split(",")
  424. # for i,b in enumerate(background):
  425. # n = i+1
  426. # img_path = "app/src/main/res/drawable-nodpi/pb_lbackground_{}.png".format(n)
  427. # img_path = os.path.join(path_dest, img_path)
  428. # logo = requests.get('https://newuniverse.io/dashboardv2/uploads/background/{}'.format(b))
  429. # with open(img_path, "wb") as f:
  430. # f.write(logo.content)
  431. # with open(img_path, "rb") as f:
  432. # logo = Image.open(f)
  433. # logo = logo.resize((600, 1250))
  434. # logo.save(img_path, "PNG")
  435. # else:
  436. # img_path = "app/src/main/res/drawable-nodpi/pb_lbackground_1.png"
  437. # logo = Image.open(background)
  438. # logo = logo.resize((600, 1250))
  439. # logo.save(img_path, "PNG")
  440. pass
  441. def change_access(path_dest, access_model, package):
  442. access = ["CPAAS_MODE_FLOATING", "CPAAS_MODE_DOCKED", "CPAAS_MODE_BURGER"]
  443. path_package_id = package.replace(".", "/")
  444. code_path = "app/src/main/java/"
  445. code_path = os.path.join(path_dest, code_path, path_package_id, "util", "PrefsUtil.java")
  446. print(code_path)
  447. with open(code_path, "r") as f:
  448. file_source = f.read()
  449. replaced = file_source.replace("= CPAAS_MODE_DOCKED", "= {}".format(access[access_model]))
  450. with open(code_path, "w") as f:
  451. f.write(replaced)
  452. def change_certificate(path_dest, key, keyfile, keytool):
  453. keyfile_name = "{}.keystore".format(key["alias"])
  454. keyfile_path = os.path.join(path_dest, keyfile_name)
  455. if keyfile:
  456. keyfile.save(keyfile_path)
  457. else:
  458. vprint("keytool run")
  459. os.chdir(path_dest)
  460. vprint("current working directory: ", os.getcwd())
  461. dname = "CN={}, OU={}, O={}, L={}, S={}, C={}".format(key["common_name"], key["organization_unit"],
  462. key["organization_name"], key["locality_name"],
  463. key["state_name"], key["country"])
  464. cmd = [keytool, "-genkey", "-v", "-keystore", keyfile_path, "-alias", key["alias"], "-keyalg", "RSA",
  465. "-keysize", "2048",
  466. "-validity", "10000", "-dname", dname, "-storepass", key["store_password"], "-keypass",
  467. key["key_password"]]
  468. vprint(cmd)
  469. subprocess.run(cmd)
  470. vprint("keytool end")
  471. build_gradle = os.path.join(path_dest, 'app/build.gradle')
  472. with open(build_gradle, "r") as f:
  473. file_source = f.read()
  474. replaced = file_source.replace("allyourbase", key["store_password"])
  475. replaced = replaced.replace("arebelongto", key["key_password"])
  476. replaced = replaced.replace("key-qmeralite", key["alias"])
  477. with open(build_gradle, "w") as f:
  478. f.write(replaced)
  479. def change_huawei_file(path_dest, huawei_file, package_id):
  480. huaweifile_name = "agconnect-services.json"
  481. huaweifile_path = os.path.join(path_dest, "app/{}".format(huaweifile_name))
  482. if huawei_file:
  483. huawei_file.save(huaweifile_path)
  484. path_package_id = package_id.replace(".", "/")
  485. code_path = "app/src/main/java/"
  486. main_code_path = os.path.join(path_dest, code_path, path_package_id, "MAB.java")
  487. with open(main_code_path, "r") as f:
  488. file_source = f.read()
  489. replaced = file_source.replace('isHMSEnabled = false', 'isHMSEnabled = true')
  490. with open(main_code_path, "w") as f:
  491. f.write(replaced)
  492. def change_fms_file(path_dest, fms_enable):
  493. build_gradle = os.path.join(path_dest, 'app/build.gradle')
  494. with open(build_gradle, "r") as f:
  495. lines = f.readlines()
  496. with open(build_gradle, "w") as f:
  497. if fms_enable == 0:
  498. for line in lines:
  499. if "withfcm" in line:
  500. continue
  501. else:
  502. f.write(line)
  503. else:
  504. for line in lines:
  505. if "-nofcm" in line:
  506. continue
  507. else:
  508. f.write(line)
  509. def change_adblock_file(path_dest, package, use_adblock):
  510. if use_adblock == 0:
  511. build_gradle = os.path.join(path_dest, 'app/build.gradle')
  512. with open(build_gradle, "r") as f:
  513. lines = f.readlines()
  514. with open(build_gradle, "w") as f:
  515. for line in lines:
  516. if "removeAdblock" in line:
  517. continue
  518. else:
  519. f.write(line)
  520. path_package_id = package.replace(".", "/")
  521. code_path = "app/src/main/java/"
  522. code_path = os.path.join(path_dest, code_path, path_package_id)
  523. javas = [os.path.join(dp, f) for dp, dn, filenames in os.walk(code_path) for f in filenames]
  524. for j in javas:
  525. print(j)
  526. with open(j, "r") as f:
  527. lines = f.readlines()
  528. with open(j, "w") as f:
  529. counter = 0
  530. for line in lines:
  531. if counter == 0:
  532. if "removeAdblock" in line:
  533. continue
  534. elif "import org.adblockplus.libadblockplus.android" in line:
  535. continue
  536. elif "removeAdbMultiLine" in line:
  537. counter = 11
  538. elif "extends AdblockWebView" in line:
  539. replaced = line.replace("extends AdblockWebView", "extends WebView")
  540. f.write(replaced)
  541. else:
  542. f.write(line)
  543. else:
  544. counter = counter - 1
  545. def run_build(path_dest):
  546. gradlew = os.path.join(path_dest, "gradlew")
  547. error = ""
  548. ret = subprocess.run([gradlew, 'clean', 'assembleRelease'], capture_output=True)
  549. if ret.returncode == 0:
  550. ret = subprocess.run([gradlew, 'bundleRelease'], capture_output=True)
  551. if ret.returncode != 0:
  552. error = "{}\n".format(ret.stderr.decode())
  553. vprint(error)
  554. else:
  555. error = "{}\n".format(ret.stderr.decode())
  556. vprint(error)
  557. return ret.returncode, error
  558. def deliver_apk(path_dest, package_id, key, key_exists):
  559. apk_dir = os.path.join(path_dest, 'app/build/outputs/apk/release/app-release.apk')
  560. aab_dir = os.path.join(path_dest, 'app/build/outputs/bundle/release/app-release.aab')
  561. keystore_name = '{}.keystore'.format(key["alias"])
  562. keystore_dir = os.path.join(path_dest, keystore_name)
  563. timenow = time.time()
  564. apk_name = "{}{}.apk".format(package_id, timenow)
  565. aab_name = "{}{}.aab".format(package_id, timenow)
  566. zip_name = "{}{}.zip".format(package_id, timenow)
  567. new_apk_dir = os.path.join(app.apk_folder, apk_name)
  568. new_aab_dir = os.path.join(app.apk_folder, aab_name)
  569. new_dir = os.path.join(app.apk_folder, zip_name)
  570. vprint(apk_dir)
  571. shutil.move(apk_dir, new_apk_dir)
  572. try:
  573. shutil.move(aab_dir, new_aab_dir)
  574. with ZipFile(new_dir, 'w') as zip_file:
  575. zip_file.write(new_apk_dir, os.path.basename(new_apk_dir))
  576. zip_file.write(new_aab_dir, os.path.basename(new_aab_dir))
  577. if not key_exists:
  578. zip_file.write(keystore_dir, os.path.basename(keystore_dir))
  579. os.remove(new_apk_dir)
  580. os.remove(new_aab_dir)
  581. project_path = os.path.join(app.temp_folder, package_id)
  582. shutil.rmtree(project_path)
  583. return {"status": "0", "name": zip_name}
  584. except Exception as e:
  585. return {"status": "4", "message": "Deliver APK & AAB failed\n{}".format(e)}
  586. def change_version(path_dest, version_code, version_name):
  587. build_gradle = os.path.join(path_dest, 'app/build.gradle')
  588. with open(build_gradle, "r") as f:
  589. file_source = f.read()
  590. replaced = file_source.replace("versionCode 3", "versionCode {}".format(version_code))
  591. replaced = replaced.replace('versionName "3.0"', 'versionName "{}"'.format(version_name))
  592. with open(build_gradle, "w") as f:
  593. f.write(replaced)
  594. @app.route('/', methods=["GET", "POST"])
  595. def build_apk():
  596. vprint('==============================================================')
  597. if request.method == 'POST':
  598. logo = None
  599. logo_float = None
  600. app_name = "NexilisLite"
  601. package_id = "com.app.nexilis"
  602. acc = None
  603. url = None
  604. keystore = None
  605. huawei_file = None
  606. fms_enable = 1
  607. use_adblock = 0
  608. key_exists = False
  609. # tabs = ["1", "2", "3", "4"]
  610. fb_order = "1,2,3,4,5"
  611. tabs = []
  612. tab3_mode = "0"
  613. tab_amount = 6
  614. tab_icon = [None, None, None, None, None, None]
  615. fb_icon = [None, None, None, None, None]
  616. background = None
  617. version_code = "1"
  618. version_name = "1.0.0"
  619. font = 0
  620. enable_sms = 0
  621. enable_location = 0
  622. key = {"alias": "nexilislite", "store_password": "allyourbase", "key_password": "allyourbase",
  623. "common_name": "all", "organization_unit": "your",
  624. "organization_name": "base", "locality_name": "are", "state_name": "belong", "country": "to"}
  625. try:
  626. if 'logo' in request.files:
  627. logo = request.files['logo']
  628. vprint(type(logo))
  629. elif 'logo' in request.form:
  630. logo = request.form['logo']
  631. vprint(type(logo))
  632. if 'logofloat' in request.files:
  633. logo_float = request.files['logofloat']
  634. vprint(type(logo_float))
  635. elif 'logofloat' in request.form:
  636. logo_float = request.form['logofloat']
  637. vprint(type(logo_float))
  638. if 'app_name' in request.files:
  639. app_name = request.files['app_name']
  640. vprint(app_name)
  641. elif 'app_name' in request.form:
  642. app_name = request.form['app_name']
  643. vprint(app_name)
  644. if 'package_id' in request.files:
  645. package_id = request.files['package_id']
  646. vprint(package_id)
  647. elif 'package_id' in request.form:
  648. package_id = request.form['package_id']
  649. vprint(package_id)
  650. if 'acc' in request.files:
  651. acc = request.files['acc']
  652. vprint(acc)
  653. elif 'acc' in request.form:
  654. acc = request.form['acc']
  655. vprint(acc)
  656. if 'url' in request.files:
  657. url = request.files['url']
  658. vprint(url)
  659. elif 'url' in request.form:
  660. url = request.form['url']
  661. vprint(url)
  662. if 'keystore' in request.files:
  663. keystore = request.files['keystore']
  664. key_exists = True
  665. elif 'keystore' in request.form:
  666. keystore = request.form['keystore']
  667. key_exists = True
  668. if 'huawei_file' in request.files:
  669. huawei_file = request.files['huawei_file']
  670. elif 'huawei_file' in request.form:
  671. huawei_file = request.form['huawei_file']
  672. if 'fms_enable' in request.form:
  673. fms_enable = int(request.form['fms_enable'])
  674. if 'use_adblock' in request.form:
  675. use_adblock = int(request.form['use_adblock'])
  676. if 'alias' in request.form:
  677. if request.form['alias']:
  678. key["alias"] = request.form['alias']
  679. if 'store_password' in request.form:
  680. if request.form['store_password']:
  681. key["store_password"] = request.form['store_password']
  682. if 'key_password' in request.form:
  683. if request.form['key_password']:
  684. key["key_password"] = request.form['key_password']
  685. if keystore:
  686. if 'common_name' in request.form:
  687. if request.form['common_name']:
  688. key["common_name"] = request.form['common_name']
  689. if 'organization_unit' in request.form:
  690. if request.form['organization_unit']:
  691. key["organization_unit"] = request.form['organization_unit']
  692. if 'organization_name' in request.form:
  693. if request.form['organization_name']:
  694. key["organization_name"] = request.form['organization_name']
  695. if 'locality_name' in request.form:
  696. if request.form['locality_name']:
  697. key["locality_name"] = request.form['locality_name']
  698. if 'state_name' in request.form:
  699. if request.form['state_name']:
  700. key["state_name"] = request.form['state_name']
  701. if 'country' in request.form:
  702. if request.form['country']:
  703. key["country"] = request.form['country']
  704. tabs.append(request.form["tab1"])
  705. tabs.append(request.form["tab2"])
  706. if request.form['tab3']:
  707. tabs.append(request.form["tab3"])
  708. if request.form['tab4']:
  709. tabs.append(request.form["tab4"])
  710. if 'tab1_icon' in request.files:
  711. tab_icon[0] = request.files['tab1_icon']
  712. elif 'tab1_icon' in request.form:
  713. tab_icon[0] = request.form['tab1_icon']
  714. if 'tab2_icon' in request.files:
  715. tab_icon[1] = request.files['tab2_icon']
  716. elif 'tab2_icon' in request.form:
  717. tab_icon[1] = request.form['tab2_icon']
  718. if 'tab3_icon' in request.files:
  719. tab_icon[2] = request.files['tab3_icon']
  720. elif 'tab3_icon' in request.form:
  721. tab_icon[2] = request.form['tab3_icon']
  722. if 'tab4_icon' in request.files:
  723. tab_icon[3] = request.files['tab4_icon']
  724. elif 'tab4_icon' in request.form:
  725. tab_icon[3] = request.form['tab4_icon']
  726. if 'fb1_icon' in request.files:
  727. fb_icon[0] = request.files['fb1_icon']
  728. elif 'fb1_icon' in request.form:
  729. fb_icon[0] = request.form['fb1_icon']
  730. if 'fb2_icon' in request.files:
  731. fb_icon[1] = request.files['fb2_icon']
  732. elif 'fb2_icon' in request.form:
  733. fb_icon[1] = request.form['fb2_icon']
  734. if 'fb3_icon' in request.files:
  735. fb_icon[2] = request.files['fb3_icon']
  736. elif 'fb3_icon' in request.form:
  737. fb_icon[2] = request.form['fb3_icon']
  738. if 'fb4_icon' in request.files:
  739. fb_icon[3] = request.files['fb4_icon']
  740. elif 'fb4_icon' in request.form:
  741. fb_icon[3] = request.form['fb4_icon']
  742. if 'fb5_icon' in request.files:
  743. fb_icon[4] = request.files['fb5_icon']
  744. elif 'fb5_icon' in request.form:
  745. fb_icon[4] = request.form['fb5_icon']
  746. if 'fb_icon' in request.form:
  747. fb_icon = request.form['fb_icon'].split(",")
  748. if 'fb_order' in request.form:
  749. fb_order = request.form['fb_order']
  750. vprint("fb_icon: {}".format(fb_icon))
  751. access_model = int(request.form['access_model'])
  752. if 'tab3_mode' in request.form:
  753. tab3_mode = request.form['tab3_mode']
  754. if 'tab_amount' in request.form:
  755. tab_amount = int(request.form['tab_amount'])
  756. if 'font' in request.form:
  757. font = int(request.form['font'])
  758. if 'background' in request.files:
  759. background = request.files['background']
  760. elif 'background' in request.form:
  761. background = request.form['background']
  762. if 'version_code' in request.form:
  763. version_code = request.form['version_code']
  764. if 'version_name' in request.form:
  765. version_name = request.form['version_name']
  766. else:
  767. version_name = "1.0.{}".format(version_code)
  768. if 'enable_sms' in request.form:
  769. enable_sms = int(request.form['enable_sms'])
  770. if 'enable_location' in request.form:
  771. enable_location = int(request.form['enable_location'])
  772. except BaseException as e:
  773. vprint(traceback.format_exc())
  774. return {"status": "1", "message": "Parameter mismatch\n{}\n".format(str(e))}
  775. try:
  776. path_dest, c_code_path = create_folder(package_id)
  777. vprint("path_dest: " + path_dest)
  778. vprint("c_code_path: " + c_code_path)
  779. if acc:
  780. change_acc(c_code_path, acc, enable_sms)
  781. if url:
  782. change_url(c_code_path, url)
  783. change_name(path_dest, app_name, enable_sms)
  784. change_certificate(path_dest, key, keystore, app.keytool)
  785. change_package(path_dest, package_id, enable_location)
  786. change_version(path_dest, version_code, version_name)
  787. change_font(path_dest, font, package_id)
  788. if logo:
  789. change_logo(path_dest, logo, logo_float)
  790. if background:
  791. change_background(path_dest, background)
  792. change_fb(path_dest, fb_order, package_id, fb_icon)
  793. change_access(path_dest, access_model, package_id)
  794. change_tab(path_dest, tabs, tab_icon, package_id, tab3_mode, tab_amount)
  795. change_huawei_file(path_dest, huawei_file, package_id)
  796. change_fms_file(path_dest, fms_enable)
  797. change_adblock_file(path_dest, package_id, use_adblock)
  798. except BaseException as e:
  799. vprint(traceback.format_exc())
  800. return {"status": "2", "message": "Process failure\n{}\n".format(str(e))}
  801. os.chdir(path_dest)
  802. return_code, error = run_build(path_dest)
  803. if return_code == 0:
  804. return deliver_apk(path_dest, package_id, key, key_exists)
  805. return {"status": "3", "message": "Build failed\n{}".format(str(error))}
  806. else:
  807. if 'e' in request.args:
  808. return request.args['e']
  809. return "Hello World!"
  810. if __name__ == '__main__':
  811. app.run(host='0.0.0.0', port=8092, debug=app.verbose, ssl_context=app.ssl)