mainPalio4.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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/PalioLite"
  13. # app.base_project = "/Users/easysoft/Documents/PalioLite"
  14. app.temp_folder = "/apps/3ps/build_apk/BuildApk"
  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 = 128
  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(res_path, "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. def change_logo(path_dest, logo, logo_float=None):
  292. img_path = "app/src/main/res/drawable/ic_launcher.png"
  293. img_path = os.path.join(path_dest, img_path)
  294. img_notif = "app/src/main/res/drawable/pb_ball.png"
  295. img_notif = os.path.join(path_dest, img_notif)
  296. img_path_float = "app/src/main/res/drawable/pb_button.png"
  297. img_path_float = os.path.join(path_dest, img_path_float)
  298. if isinstance(logo, str):
  299. logo = requests.get('https://newuniverse.io/dashboardv2/uploads/logo/{}'.format(logo))
  300. with open(img_path, "wb") as f:
  301. f.write(logo.content)
  302. with open(img_path, "rb") as f:
  303. logo = Image.open(f)
  304. logo = logo.resize((512, 512))
  305. if logo_float:
  306. logo_float = requests.get('https://newuniverse.io/dashboardv2/uploads/logofloat/{}'.format(logo_float))
  307. with open(img_path_float, "wb") as f:
  308. f.write(logo_float.content)
  309. with open(img_path_float, "rb") as f:
  310. logo_float = Image.open(f)
  311. logo_float = logo_float.resize((150, 150))
  312. else:
  313. logo = Image.open(logo)
  314. logo = logo.resize((512, 512))
  315. if logo_float:
  316. logo_float = Image.open(logo_float)
  317. logo_float = logo_float.resize((150, 150))
  318. logo.save(img_path, "PNG")
  319. if logo_float:
  320. logo_float.save(img_path_float, "PNG")
  321. logo_float.save(img_notif, "PNG")
  322. def change_tab(path_dest, tabs, tab_icon, package, tab3_mode, tab_amount):
  323. default_tab_icon = ["tab{}.png".format(x) for x in tabs]
  324. for i, icon in enumerate(default_tab_icon):
  325. if not tab_icon[i]:
  326. continue
  327. img_path = "app/src/main/res/drawable"
  328. img_path = os.path.join(path_dest, img_path, icon)
  329. if isinstance(tab_icon[i], str):
  330. logo = requests.get('https://newuniverse.io/dashboardv2/uploads/tab_icon/{}'.format(tab_icon[i]))
  331. with open(img_path, "wb") as f:
  332. f.write(logo.content)
  333. with open(img_path, "rb") as f:
  334. logo = Image.open(f)
  335. logo = logo.resize((150, 150))
  336. logo.save(img_path, "PNG")
  337. else:
  338. logo = Image.open(tab_icon[i])
  339. logo = logo.resize((150, 150))
  340. logo.save(img_path, "PNG")
  341. path_package_id = package.replace(".", "/")
  342. code_path = "app/src/main/java/"
  343. sobj_code_path = os.path.join(path_dest, code_path, path_package_id, "SObj.java")
  344. print(sobj_code_path)
  345. with open(sobj_code_path, "r") as f:
  346. file_source = f.read()
  347. replaced = file_source.replace("1,2,3,4", ",".join(tabs))
  348. with open(sobj_code_path, "w") as f:
  349. f.write(replaced)
  350. main_code_path = os.path.join(path_dest, code_path, path_package_id, "MAB.java")
  351. with open(main_code_path, "r") as f:
  352. file_source = f.read()
  353. replaced = file_source.replace('tab3 = "0"', 'tab3 = "{}"'.format(tab3_mode))
  354. with open(main_code_path, "w") as f:
  355. f.write(replaced)
  356. prefs_code_path = os.path.join(path_dest, code_path, path_package_id, "util/PrefsUtil.java")
  357. with open(prefs_code_path, "r") as f:
  358. file_source = f.read()
  359. replaced = file_source.replace('DEFAULT_TAB_AMOUNT = "4"', 'DEFAULT_TAB_AMOUNT = "{}"'.format(tab_amount))
  360. with open(prefs_code_path, "w") as f:
  361. f.write(replaced)
  362. def change_fb(path_dest, fb_order, package, fb_icon):
  363. fb_order_list = [int(x) for x in fb_order.split(",")]
  364. default_fb_icon = ["pb_button_chat.png", "pb_button_call.png", "pb_button_cc.png", "pb_button_stream.png",
  365. "nexilis_fb_04.png"]
  366. for x,i in enumerate(fb_order_list):
  367. if not fb_icon[x]:
  368. continue
  369. img_path = "app/src/main/res/drawable-nodpi"
  370. if i-1 in range(-len(default_fb_icon), len(default_fb_icon)):
  371. img_path = os.path.join(path_dest, img_path, default_fb_icon[i-1])
  372. if isinstance(fb_icon[x], str):
  373. logo = requests.get('https://newuniverse.io/dashboardv2/uploads/fb_icon/{}'.format(fb_icon[x]))
  374. with open(img_path, "wb") as f:
  375. f.write(logo.content)
  376. with open(img_path, "rb") as f:
  377. logo = Image.open(f)
  378. logo = logo.resize((150, 150))
  379. logo.save(img_path, "PNG")
  380. else:
  381. logo = Image.open(fb_icon[x])
  382. logo = logo.resize((150, 150))
  383. logo.save(img_path, "PNG")
  384. path_package_id = package.replace(".", "/")
  385. code_path = "app/src/main/java/"
  386. main_code_path = os.path.join(path_dest, code_path, path_package_id, "MAB.java")
  387. with open(main_code_path, "r") as f:
  388. file_source = f.read()
  389. replaced = file_source.replace('dockedPlacement = "1,2,3,4,5"', 'dockedPlacement = "{}"'.format(fb_order))
  390. with open(main_code_path, "w") as f:
  391. f.write(replaced)
  392. def change_background(path_dest, background):
  393. # if isinstance(background, str):
  394. # background = background.split(",")
  395. # for i,b in enumerate(background):
  396. # n = i+1
  397. # img_path = "app/src/main/res/drawable-nodpi/pb_lbackground_{}.png".format(n)
  398. # img_path = os.path.join(path_dest, img_path)
  399. # logo = requests.get('https://newuniverse.io/dashboardv2/uploads/background/{}'.format(b))
  400. # with open(img_path, "wb") as f:
  401. # f.write(logo.content)
  402. # with open(img_path, "rb") as f:
  403. # logo = Image.open(f)
  404. # logo = logo.resize((600, 1250))
  405. # logo.save(img_path, "PNG")
  406. # else:
  407. # img_path = "app/src/main/res/drawable-nodpi/pb_lbackground_1.png"
  408. # logo = Image.open(background)
  409. # logo = logo.resize((600, 1250))
  410. # logo.save(img_path, "PNG")
  411. pass
  412. def change_access(path_dest, access_model, package):
  413. access = ["CPAAS_MODE_FLOATING", "CPAAS_MODE_DOCKED", "CPAAS_MODE_BURGER"]
  414. path_package_id = package.replace(".", "/")
  415. code_path = "app/src/main/java/"
  416. code_path = os.path.join(path_dest, code_path, path_package_id, "util", "PrefsUtil.java")
  417. print(code_path)
  418. with open(code_path, "r") as f:
  419. file_source = f.read()
  420. replaced = file_source.replace("= CPAAS_MODE_DOCKED", "= {}".format(access[access_model]))
  421. with open(code_path, "w") as f:
  422. f.write(replaced)
  423. def change_certificate(path_dest, key, keyfile, keytool):
  424. keyfile_name = "{}.keystore".format(key["alias"])
  425. keyfile_path = os.path.join(path_dest, keyfile_name)
  426. if keyfile:
  427. keyfile.save(keyfile_path)
  428. else:
  429. vprint("keytool run")
  430. os.chdir(path_dest)
  431. vprint("current working directory: ", os.getcwd())
  432. dname = "CN={}, OU={}, O={}, L={}, S={}, C={}".format(key["common_name"], key["organization_unit"],
  433. key["organization_name"], key["locality_name"],
  434. key["state_name"], key["country"])
  435. cmd = [keytool, "-genkey", "-v", "-keystore", keyfile_path, "-alias", key["alias"], "-keyalg", "RSA",
  436. "-keysize", "2048",
  437. "-validity", "10000", "-dname", dname, "-storepass", key["store_password"], "-keypass",
  438. key["key_password"]]
  439. vprint(cmd)
  440. subprocess.run(cmd)
  441. vprint("keytool end")
  442. build_gradle = os.path.join(path_dest, 'app/build.gradle')
  443. with open(build_gradle, "r") as f:
  444. file_source = f.read()
  445. replaced = file_source.replace("allyourbase", key["store_password"])
  446. replaced = replaced.replace("arebelongto", key["key_password"])
  447. replaced = replaced.replace("key-qmeralite", key["alias"])
  448. with open(build_gradle, "w") as f:
  449. f.write(replaced)
  450. def change_huawei_file(path_dest, huawei_file, package_id):
  451. huaweifile_name = "agconnect-services.json"
  452. huaweifile_path = os.path.join(path_dest, "app/{}".format(huaweifile_name))
  453. if huawei_file:
  454. huawei_file.save(huaweifile_path)
  455. path_package_id = package_id.replace(".", "/")
  456. code_path = "app/src/main/java/"
  457. main_code_path = os.path.join(path_dest, code_path, path_package_id, "MAB.java")
  458. with open(main_code_path, "r") as f:
  459. file_source = f.read()
  460. replaced = file_source.replace('isHMSEnabled = false', 'isHMSEnabled = true')
  461. with open(main_code_path, "w") as f:
  462. f.write(replaced)
  463. def change_fms_file(path_dest, fms_enable, package_id):
  464. if fms_enable == 0:
  465. path_package_id = package_id.replace(".", "/")
  466. code_path = "app/src/main/java/"
  467. main_code_path = os.path.join(path_dest, code_path, path_package_id, "MAB.java")
  468. with open(main_code_path, "r") as f:
  469. file_source = f.read()
  470. replaced = file_source.replace('isFMSEnabled = true', 'isFMSEnabled = false')
  471. with open(main_code_path, "w") as f:
  472. f.write(replaced)
  473. def run_build(path_dest):
  474. gradlew = os.path.join(path_dest, "gradlew")
  475. error = ""
  476. ret = subprocess.run([gradlew, 'assembleRelease'], capture_output=True)
  477. if ret.returncode == 0:
  478. ret = subprocess.run([gradlew, 'bundleRelease'], capture_output=True)
  479. if ret.returncode != 0:
  480. error = "{}\n".format(ret.stderr.decode())
  481. vprint(error)
  482. else:
  483. error = "{}\n".format(ret.stderr.decode())
  484. vprint(error)
  485. return ret.returncode, error
  486. def deliver_apk(path_dest, package_id, key, key_exists):
  487. apk_dir = os.path.join(path_dest, 'app/build/outputs/apk/release/app-release.apk')
  488. aab_dir = os.path.join(path_dest, 'app/build/outputs/bundle/release/app-release.aab')
  489. keystore_name = '{}.keystore'.format(key["alias"])
  490. keystore_dir = os.path.join(path_dest, keystore_name)
  491. timenow = time.time()
  492. apk_name = "{}{}.apk".format(package_id, timenow)
  493. aab_name = "{}{}.aab".format(package_id, timenow)
  494. zip_name = "{}{}.zip".format(package_id, timenow)
  495. new_apk_dir = os.path.join(app.apk_folder, apk_name)
  496. new_aab_dir = os.path.join(app.apk_folder, aab_name)
  497. new_dir = os.path.join(app.apk_folder, zip_name)
  498. vprint(apk_dir)
  499. shutil.move(apk_dir, new_apk_dir)
  500. try:
  501. shutil.move(aab_dir, new_aab_dir)
  502. with ZipFile(new_dir, 'w') as zip_file:
  503. zip_file.write(new_apk_dir, os.path.basename(new_apk_dir))
  504. zip_file.write(new_aab_dir, os.path.basename(new_aab_dir))
  505. if not key_exists:
  506. zip_file.write(keystore_dir, os.path.basename(keystore_dir))
  507. os.remove(new_apk_dir)
  508. os.remove(new_aab_dir)
  509. project_path = os.path.join(app.temp_folder, package_id)
  510. shutil.rmtree(project_path)
  511. return {"status": "0", "name": zip_name}
  512. except Exception as e:
  513. return {"status": "4", "message": "Deliver APK & AAB failed\n{}".format(e)}
  514. def change_version(path_dest, version_code, version_name):
  515. build_gradle = os.path.join(path_dest, 'app/build.gradle')
  516. with open(build_gradle, "r") as f:
  517. file_source = f.read()
  518. replaced = file_source.replace("versionCode 3", "versionCode {}".format(version_code))
  519. replaced = replaced.replace('versionName "3.0"', 'versionName "{}"'.format(version_name))
  520. with open(build_gradle, "w") as f:
  521. f.write(replaced)
  522. @app.route('/', methods=["GET", "POST"])
  523. def build_apk():
  524. vprint('==============================================================')
  525. if request.method == 'POST':
  526. logo = None
  527. logo_float = None
  528. app_name = "NexilisLite"
  529. package_id = "com.app.nexilis"
  530. acc = None
  531. url = None
  532. keystore = None
  533. huawei_file = None
  534. fms_enable = 1
  535. key_exists = False
  536. # tabs = ["1", "2", "3", "4"]
  537. fb_order = "1,2,3,4,5"
  538. tabs = []
  539. tab3_mode = "0"
  540. tab_amount = 6
  541. tab_icon = [None, None, None, None, None, None]
  542. fb_icon = [None, None, None, None, None]
  543. background = None
  544. version_code = "1"
  545. version_name = "1.0.0"
  546. font = 0
  547. enable_sms = 0
  548. enable_location = 0
  549. key = {"alias": "nexilislite", "store_password": "allyourbase", "key_password": "allyourbase",
  550. "common_name": "all", "organization_unit": "your",
  551. "organization_name": "base", "locality_name": "are", "state_name": "belong", "country": "to"}
  552. try:
  553. if 'logo' in request.files:
  554. logo = request.files['logo']
  555. vprint(type(logo))
  556. elif 'logo' in request.form:
  557. logo = request.form['logo']
  558. vprint(type(logo))
  559. if 'logofloat' in request.files:
  560. logo_float = request.files['logofloat']
  561. vprint(type(logo_float))
  562. elif 'logofloat' in request.form:
  563. logo_float = request.form['logofloat']
  564. vprint(type(logo_float))
  565. if 'app_name' in request.files:
  566. app_name = request.files['app_name']
  567. vprint(app_name)
  568. elif 'app_name' in request.form:
  569. app_name = request.form['app_name']
  570. vprint(app_name)
  571. if 'package_id' in request.files:
  572. package_id = request.files['package_id']
  573. vprint(package_id)
  574. elif 'package_id' in request.form:
  575. package_id = request.form['package_id']
  576. vprint(package_id)
  577. if 'acc' in request.files:
  578. acc = request.files['acc']
  579. vprint(acc)
  580. elif 'acc' in request.form:
  581. acc = request.form['acc']
  582. vprint(acc)
  583. if 'url' in request.files:
  584. url = request.files['url']
  585. vprint(url)
  586. elif 'url' in request.form:
  587. url = request.form['url']
  588. vprint(url)
  589. if 'keystore' in request.files:
  590. keystore = request.files['keystore']
  591. key_exists = True
  592. elif 'keystore' in request.form:
  593. keystore = request.form['keystore']
  594. key_exists = True
  595. if 'huawei_file' in request.files:
  596. huawei_file = request.files['huawei_file']
  597. elif 'huawei_file' in request.form:
  598. huawei_file = request.form['huawei_file']
  599. if 'fms_enable' in request.form:
  600. fms_enable = int(request.form['fms_enable'])
  601. if 'alias' in request.form:
  602. if request.form['alias']:
  603. key["alias"] = request.form['alias']
  604. if 'store_password' in request.form:
  605. if request.form['store_password']:
  606. key["store_password"] = request.form['store_password']
  607. if 'key_password' in request.form:
  608. if request.form['key_password']:
  609. key["key_password"] = request.form['key_password']
  610. if keystore:
  611. if 'common_name' in request.form:
  612. if request.form['common_name']:
  613. key["common_name"] = request.form['common_name']
  614. if 'organization_unit' in request.form:
  615. if request.form['organization_unit']:
  616. key["organization_unit"] = request.form['organization_unit']
  617. if 'organization_name' in request.form:
  618. if request.form['organization_name']:
  619. key["organization_name"] = request.form['organization_name']
  620. if 'locality_name' in request.form:
  621. if request.form['locality_name']:
  622. key["locality_name"] = request.form['locality_name']
  623. if 'state_name' in request.form:
  624. if request.form['state_name']:
  625. key["state_name"] = request.form['state_name']
  626. if 'country' in request.form:
  627. if request.form['country']:
  628. key["country"] = request.form['country']
  629. tabs.append(request.form["tab1"])
  630. tabs.append(request.form["tab2"])
  631. if request.form['tab3']:
  632. tabs.append(request.form["tab3"])
  633. if request.form['tab4']:
  634. tabs.append(request.form["tab4"])
  635. if 'tab1_icon' in request.files:
  636. tab_icon[0] = request.files['tab1_icon']
  637. elif 'tab1_icon' in request.form:
  638. tab_icon[0] = request.form['tab1_icon']
  639. if 'tab2_icon' in request.files:
  640. tab_icon[1] = request.files['tab2_icon']
  641. elif 'tab2_icon' in request.form:
  642. tab_icon[1] = request.form['tab2_icon']
  643. if 'tab3_icon' in request.files:
  644. tab_icon[2] = request.files['tab3_icon']
  645. elif 'tab3_icon' in request.form:
  646. tab_icon[2] = request.form['tab3_icon']
  647. if 'tab4_icon' in request.files:
  648. tab_icon[3] = request.files['tab4_icon']
  649. elif 'tab4_icon' in request.form:
  650. tab_icon[3] = request.form['tab4_icon']
  651. if 'fb1_icon' in request.files:
  652. fb_icon[0] = request.files['fb1_icon']
  653. elif 'fb1_icon' in request.form:
  654. fb_icon[0] = request.form['fb1_icon']
  655. if 'fb2_icon' in request.files:
  656. fb_icon[1] = request.files['fb2_icon']
  657. elif 'fb2_icon' in request.form:
  658. fb_icon[1] = request.form['fb2_icon']
  659. if 'fb3_icon' in request.files:
  660. fb_icon[2] = request.files['fb3_icon']
  661. elif 'fb3_icon' in request.form:
  662. fb_icon[2] = request.form['fb3_icon']
  663. if 'fb4_icon' in request.files:
  664. fb_icon[3] = request.files['fb4_icon']
  665. elif 'fb4_icon' in request.form:
  666. fb_icon[3] = request.form['fb4_icon']
  667. if 'fb5_icon' in request.files:
  668. fb_icon[4] = request.files['fb5_icon']
  669. elif 'fb5_icon' in request.form:
  670. fb_icon[4] = request.form['fb5_icon']
  671. if 'fb_icon' in request.form:
  672. fb_icon = request.form['fb_icon'].split(",")
  673. if 'fb_order' in request.form:
  674. fb_order = request.form['fb_order']
  675. vprint("fb_icon: {}".format(fb_icon))
  676. access_model = int(request.form['access_model'])
  677. if 'tab3_mode' in request.form:
  678. tab3_mode = request.form['tab3_mode']
  679. if 'tab_amount' in request.form:
  680. tab_amount = int(request.form['tab_amount'])
  681. if 'font' in request.form:
  682. font = int(request.form['font'])
  683. if 'background' in request.files:
  684. background = request.files['background']
  685. elif 'background' in request.form:
  686. background = request.form['background']
  687. if 'version_code' in request.form:
  688. version_code = request.form['version_code']
  689. if 'version_name' in request.form:
  690. version_name = request.form['version_name']
  691. else:
  692. version_name = "1.0.{}".format(version_code)
  693. if 'enable_sms' in request.form:
  694. enable_sms = int(request.form['enable_sms'])
  695. if 'enable_location' in request.form:
  696. enable_sms = int(request.form['enable_location'])
  697. except BaseException as e:
  698. vprint(traceback.format_exc())
  699. return {"status": "1", "message": "Parameter mismatch\n{}\n".format(str(e))}
  700. try:
  701. path_dest, c_code_path = create_folder(package_id)
  702. vprint("path_dest: " + path_dest)
  703. vprint("c_code_path: " + c_code_path)
  704. if acc:
  705. change_acc(c_code_path, acc, enable_sms)
  706. if url:
  707. change_url(c_code_path, url)
  708. change_name(path_dest, app_name, enable_sms, enable_location)
  709. change_certificate(path_dest, key, keystore, app.keytool)
  710. change_package(path_dest, package_id, enable_location)
  711. change_version(path_dest, version_code, version_name)
  712. change_font(path_dest, font, package_id)
  713. if logo:
  714. change_logo(path_dest, logo, logo_float)
  715. if background:
  716. change_background(path_dest, background)
  717. change_fb(path_dest, fb_order, package_id, fb_icon)
  718. change_access(path_dest, access_model, package_id)
  719. change_tab(path_dest, tabs, tab_icon, package_id, tab3_mode, tab_amount)
  720. change_huawei_file(path_dest, huawei_file, package_id)
  721. change_fms_file(path_dest, fms_enable, package_id)
  722. except BaseException as e:
  723. vprint(traceback.format_exc())
  724. return {"status": "2", "message": "Process failure\n{}\n".format(str(e))}
  725. os.chdir(path_dest)
  726. return_code, error = run_build(path_dest)
  727. if return_code == 0:
  728. return deliver_apk(path_dest, package_id, key, key_exists)
  729. return {"status": "3", "message": "Build failed\n{}".format(str(error))}
  730. else:
  731. if 'e' in request.args:
  732. return request.args['e']
  733. return "Hello World!"
  734. if __name__ == '__main__':
  735. app.run(host='0.0.0.0', port=8072, debug=app.verbose, ssl_context=app.ssl)