mainPalio4.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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):
  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. gradle = os.path.join(path_dest, "app/build.gradle")
  168. with open(gradle, "r") as f:
  169. lines = f.readlines()
  170. with open(gradle, "w") as f:
  171. counter = 0
  172. for line in lines:
  173. if counter == 0:
  174. if "fordigisales" in line:
  175. counter = 3
  176. continue
  177. if "cx-button-libs" in line:
  178. continue
  179. if "cx-tsel-client" in line:
  180. continue
  181. if "AP5NjpoELAt7gHYMtnsrm9hxdGk" in line:
  182. continue
  183. else:
  184. f.write(line)
  185. else:
  186. counter = counter - 1
  187. if package != "io.nexilis.digipos" and package != "io.nexilis.digisales":
  188. colorValue = os.path.join(path_dest, "app/src/main/res/values/colors.xml")
  189. with open(colorValue, "r") as c:
  190. lines = c.readlines()
  191. with open(colorValue, "w") as c:
  192. for line in lines:
  193. if "fordigisales" in line:
  194. continue
  195. else:
  196. c.write(line)
  197. # if package == "io.nexilis.digipos" or package == "io.nexilis.digisales":
  198. # with open(gradle, "w") as f:
  199. # counter = 0
  200. # for line in lines:
  201. # if counter == 0:
  202. # if "forallapps" in line:
  203. # counter = 3
  204. # continue
  205. # if "nexilis-libs" in line:
  206. # continue
  207. # if "temp-nexilis-client" in line:
  208. # continue
  209. # if "AP6ZuWCxBVTzLGiUjfacryBiwPQ" in line:
  210. # continue
  211. # else:
  212. # f.write(line)
  213. # else:
  214. # counter = counter - 1
  215. # else:
  216. # pass
  217. if package == "com.nexilis.persija" or package == "io.newuniverse.GoToMalls" or package == "io.qmera.mylab":
  218. string_res = os.path.join(path_dest, "app/src/main/res/values/strings.xml")
  219. string_en_res = os.path.join(path_dest, "app/src/main/res/values-en/strings.xml")
  220. string_id_res = os.path.join(path_dest, "app/src/main/res/values-in/strings.xml")
  221. with open(string_res, "r") as f:
  222. file_source = f.read()
  223. replaced = file_source.replace("Nexilis", "Qmera")
  224. with open(string_res, "w") as f:
  225. f.write(replaced)
  226. with open(string_en_res, "r") as f:
  227. file_source = f.read()
  228. replaced = file_source.replace("Nexilis", "Qmera")
  229. with open(string_en_res, "w") as f:
  230. f.write(replaced)
  231. with open(string_id_res, "r") as f:
  232. file_source = f.read()
  233. replaced = file_source.replace("Nexilis", "Qmera")
  234. with open(string_id_res, "w") as f:
  235. f.write(replaced)
  236. old_img_powered = "app/src/main/res/drawable/pb_powered_button.png"
  237. old_img_powered = os.path.join(path_dest, old_img_powered)
  238. old_new_img_powered = "app/src/main/res/drawable/pb_powered_button_temp.png"
  239. old_new_img_powered = os.path.join(path_dest, old_new_img_powered)
  240. shutil.move(old_img_powered, old_new_img_powered)
  241. img_powered = "app/src/main/res/drawable/pb_powered_button1.png"
  242. img_powered = os.path.join(path_dest, img_powered)
  243. new_img_powered = "app/src/main/res/drawable/pb_powered_button.png"
  244. new_img_powered = os.path.join(path_dest, new_img_powered)
  245. shutil.move(img_powered, new_img_powered)
  246. def change_logo(path_dest, logo, logo_float=None):
  247. img_path = "app/src/main/res/drawable/ic_launcher.png"
  248. img_path = os.path.join(path_dest, img_path)
  249. img_notif = "app/src/main/res/drawable/pb_ball.png"
  250. img_notif = os.path.join(path_dest, img_notif)
  251. img_path_float = "app/src/main/res/drawable/pb_button.png"
  252. img_path_float = os.path.join(path_dest, img_path_float)
  253. if isinstance(logo, str):
  254. logo = requests.get('https://newuniverse.io/dashboardv2/uploads/logo/{}'.format(logo))
  255. with open(img_path, "wb") as f:
  256. f.write(logo.content)
  257. with open(img_path, "rb") as f:
  258. logo = Image.open(f)
  259. logo = logo.resize((512, 512))
  260. if logo_float:
  261. logo_float = requests.get('https://newuniverse.io/dashboardv2/uploads/logofloat/{}'.format(logo_float))
  262. with open(img_path_float, "wb") as f:
  263. f.write(logo_float.content)
  264. with open(img_path_float, "rb") as f:
  265. logo_float = Image.open(f)
  266. logo_float = logo_float.resize((150, 150))
  267. else:
  268. logo = Image.open(logo)
  269. logo = logo.resize((512, 512))
  270. if logo_float:
  271. logo_float = Image.open(logo_float)
  272. logo_float = logo_float.resize((150, 150))
  273. logo.save(img_path, "PNG")
  274. if logo_float:
  275. logo_float.save(img_path_float, "PNG")
  276. logo_float.save(img_notif, "PNG")
  277. def change_tab(path_dest, tabs, tab_icon, package, tab3_mode, tab_amount):
  278. default_tab_icon = ["tab{}.png".format(x) for x in tabs]
  279. for i, icon in enumerate(default_tab_icon):
  280. if not tab_icon[i]:
  281. continue
  282. img_path = "app/src/main/res/drawable"
  283. img_path = os.path.join(path_dest, img_path, icon)
  284. if isinstance(tab_icon[i], str):
  285. logo = requests.get('https://newuniverse.io/dashboardv2/uploads/tab_icon/{}'.format(tab_icon[i]))
  286. with open(img_path, "wb") as f:
  287. f.write(logo.content)
  288. with open(img_path, "rb") as f:
  289. logo = Image.open(f)
  290. logo = logo.resize((150, 150))
  291. logo.save(img_path, "PNG")
  292. else:
  293. logo = Image.open(tab_icon[i])
  294. logo = logo.resize((150, 150))
  295. logo.save(img_path, "PNG")
  296. path_package_id = package.replace(".", "/")
  297. code_path = "app/src/main/java/"
  298. sobj_code_path = os.path.join(path_dest, code_path, path_package_id, "SObj.java")
  299. print(sobj_code_path)
  300. with open(sobj_code_path, "r") as f:
  301. file_source = f.read()
  302. replaced = file_source.replace("1,2,3,4", ",".join(tabs))
  303. with open(sobj_code_path, "w") as f:
  304. f.write(replaced)
  305. main_code_path = os.path.join(path_dest, code_path, path_package_id, "MAB.java")
  306. with open(main_code_path, "r") as f:
  307. file_source = f.read()
  308. replaced = file_source.replace('tab3 = "0"', 'tab3 = "{}"'.format(tab3_mode))
  309. with open(main_code_path, "w") as f:
  310. f.write(replaced)
  311. prefs_code_path = os.path.join(path_dest, code_path, path_package_id, "util/PrefsUtil.java")
  312. with open(prefs_code_path, "r") as f:
  313. file_source = f.read()
  314. replaced = file_source.replace('DEFAULT_TAB_AMOUNT = "4"', 'DEFAULT_TAB_AMOUNT = "{}"'.format(tab_amount))
  315. with open(prefs_code_path, "w") as f:
  316. f.write(replaced)
  317. def change_fb(path_dest, fb_order, package, fb_icon):
  318. fb_order_list = [int(x) for x in fb_order.split(",")]
  319. default_fb_icon = ["pb_button_chat.png", "pb_button_call.png", "pb_button_cc.png", "pb_button_stream.png",
  320. "nexilis_fb_04.png"]
  321. for x,i in enumerate(fb_order_list):
  322. if not fb_icon[x]:
  323. continue
  324. img_path = "app/src/main/res/drawable-nodpi"
  325. if i-1 in range(-len(default_fb_icon), len(default_fb_icon)):
  326. img_path = os.path.join(path_dest, img_path, default_fb_icon[i-1])
  327. if isinstance(fb_icon[x], str):
  328. logo = requests.get('https://newuniverse.io/dashboardv2/uploads/fb_icon/{}'.format(fb_icon[x]))
  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((150, 150))
  334. logo.save(img_path, "PNG")
  335. else:
  336. logo = Image.open(fb_icon[x])
  337. logo = logo.resize((150, 150))
  338. logo.save(img_path, "PNG")
  339. path_package_id = package.replace(".", "/")
  340. code_path = "app/src/main/java/"
  341. main_code_path = os.path.join(path_dest, code_path, path_package_id, "MAB.java")
  342. with open(main_code_path, "r") as f:
  343. file_source = f.read()
  344. replaced = file_source.replace('dockedPlacement = "1,2,3,4,5"', 'dockedPlacement = "{}"'.format(fb_order))
  345. with open(main_code_path, "w") as f:
  346. f.write(replaced)
  347. def change_background(path_dest, background):
  348. # if isinstance(background, str):
  349. # background = background.split(",")
  350. # for i,b in enumerate(background):
  351. # n = i+1
  352. # img_path = "app/src/main/res/drawable-nodpi/pb_lbackground_{}.png".format(n)
  353. # img_path = os.path.join(path_dest, img_path)
  354. # logo = requests.get('https://newuniverse.io/dashboardv2/uploads/background/{}'.format(b))
  355. # with open(img_path, "wb") as f:
  356. # f.write(logo.content)
  357. # with open(img_path, "rb") as f:
  358. # logo = Image.open(f)
  359. # logo = logo.resize((600, 1250))
  360. # logo.save(img_path, "PNG")
  361. # else:
  362. # img_path = "app/src/main/res/drawable-nodpi/pb_lbackground_1.png"
  363. # logo = Image.open(background)
  364. # logo = logo.resize((600, 1250))
  365. # logo.save(img_path, "PNG")
  366. pass
  367. def change_access(path_dest, access_model, package):
  368. access = ["CPAAS_MODE_FLOATING", "CPAAS_MODE_DOCKED", "CPAAS_MODE_BURGER"]
  369. path_package_id = package.replace(".", "/")
  370. code_path = "app/src/main/java/"
  371. code_path = os.path.join(path_dest, code_path, path_package_id, "util", "PrefsUtil.java")
  372. print(code_path)
  373. with open(code_path, "r") as f:
  374. file_source = f.read()
  375. replaced = file_source.replace("= CPAAS_MODE_DOCKED", "= {}".format(access[access_model]))
  376. with open(code_path, "w") as f:
  377. f.write(replaced)
  378. def change_certificate(path_dest, key, keyfile, keytool):
  379. keyfile_name = "{}.keystore".format(key["alias"])
  380. keyfile_path = os.path.join(path_dest, keyfile_name)
  381. if keyfile:
  382. keyfile.save(keyfile_path)
  383. else:
  384. vprint("keytool run")
  385. os.chdir(path_dest)
  386. vprint("current working directory: ", os.getcwd())
  387. dname = "CN={}, OU={}, O={}, L={}, S={}, C={}".format(key["common_name"], key["organization_unit"],
  388. key["organization_name"], key["locality_name"],
  389. key["state_name"], key["country"])
  390. cmd = [keytool, "-genkey", "-v", "-keystore", keyfile_path, "-alias", key["alias"], "-keyalg", "RSA",
  391. "-keysize", "2048",
  392. "-validity", "10000", "-dname", dname, "-storepass", key["store_password"], "-keypass",
  393. key["key_password"]]
  394. vprint(cmd)
  395. subprocess.run(cmd)
  396. vprint("keytool end")
  397. build_gradle = os.path.join(path_dest, 'app/build.gradle')
  398. with open(build_gradle, "r") as f:
  399. file_source = f.read()
  400. replaced = file_source.replace("allyourbase", key["store_password"])
  401. replaced = replaced.replace("arebelongto", key["key_password"])
  402. replaced = replaced.replace("key-qmeralite", key["alias"])
  403. with open(build_gradle, "w") as f:
  404. f.write(replaced)
  405. def change_huawei_file(path_dest, huawei_file, package):
  406. huaweifile_name = "agconnect-services.json"
  407. huaweifile_path = os.path.join(path_dest, "app/{}".format(huaweifile_name))
  408. if huawei_file:
  409. huawei_file.save(huaweifile_path)
  410. else:
  411. gradle = os.path.join(path_dest, "build.gradle")
  412. with open(gradle, "r") as f:
  413. lines = f.readlines()
  414. with open(gradle, "w") as f:
  415. for line in lines:
  416. if "//huawei" in line:
  417. continue
  418. else:
  419. f.write(line)
  420. gradle_app = os.path.join(path_dest, "app/build.gradle")
  421. with open(gradle_app, "r") as f:
  422. lines = f.readlines()
  423. with open(gradle_app, "w") as f:
  424. for line in lines:
  425. if "//huawei" in line:
  426. continue
  427. else:
  428. f.write(line)
  429. manifest_path = os.path.join(path_dest, "app/src/main/AndroidManifest.xml")
  430. with open(manifest_path, "r") as f:
  431. lines = f.readlines()
  432. with open(manifest_path, "w") as f:
  433. counter = 0
  434. for line in lines:
  435. if counter == 0:
  436. if "<!-- huawei -->" in line:
  437. counter = 11
  438. continue
  439. else:
  440. f.write(line)
  441. else:
  442. counter = counter - 1
  443. path_package_id = package.replace(".", "/")
  444. push_service_file = os.path.join(path_dest, "app/src/main/java",path_package_id,"MyPushService.java")
  445. os.remove(push_service_file)
  446. def run_build(path_dest):
  447. gradlew = os.path.join(path_dest, "gradlew")
  448. error = ""
  449. ret = subprocess.run([gradlew, 'assembleRelease'], capture_output=True)
  450. if ret.returncode == 0:
  451. ret = subprocess.run([gradlew, 'bundleRelease'], capture_output=True)
  452. if ret.returncode != 0:
  453. error = "{}\n".format(ret.stderr.decode())
  454. vprint(error)
  455. else:
  456. error = "{}\n".format(ret.stderr.decode())
  457. vprint(error)
  458. return ret.returncode, error
  459. def deliver_apk(path_dest, package_id, key, key_exists):
  460. apk_dir = os.path.join(path_dest, 'app/build/outputs/apk/release/app-release.apk')
  461. aab_dir = os.path.join(path_dest, 'app/build/outputs/bundle/release/app-release.aab')
  462. keystore_name = '{}.keystore'.format(key["alias"])
  463. keystore_dir = os.path.join(path_dest, keystore_name)
  464. timenow = time.time()
  465. apk_name = "{}{}.apk".format(package_id, timenow)
  466. aab_name = "{}{}.aab".format(package_id, timenow)
  467. zip_name = "{}{}.zip".format(package_id, timenow)
  468. new_apk_dir = os.path.join(app.apk_folder, apk_name)
  469. new_aab_dir = os.path.join(app.apk_folder, aab_name)
  470. new_dir = os.path.join(app.apk_folder, zip_name)
  471. vprint(apk_dir)
  472. shutil.move(apk_dir, new_apk_dir)
  473. try:
  474. shutil.move(aab_dir, new_aab_dir)
  475. with ZipFile(new_dir, 'w') as zip_file:
  476. zip_file.write(new_apk_dir, os.path.basename(new_apk_dir))
  477. zip_file.write(new_aab_dir, os.path.basename(new_aab_dir))
  478. if not key_exists:
  479. zip_file.write(keystore_dir, os.path.basename(keystore_dir))
  480. os.remove(new_apk_dir)
  481. os.remove(new_aab_dir)
  482. project_path = os.path.join(app.temp_folder, package_id)
  483. shutil.rmtree(project_path)
  484. return {"status": "0", "name": zip_name}
  485. except Exception as e:
  486. return {"status": "4", "message": "Deliver APK & AAB failed\n{}".format(e)}
  487. def change_version(path_dest, version_code, version_name):
  488. build_gradle = os.path.join(path_dest, 'app/build.gradle')
  489. with open(build_gradle, "r") as f:
  490. file_source = f.read()
  491. replaced = file_source.replace("versionCode 3", "versionCode {}".format(version_code))
  492. replaced = replaced.replace('versionName "3.0"', 'versionName "{}"'.format(version_name))
  493. with open(build_gradle, "w") as f:
  494. f.write(replaced)
  495. @app.route('/', methods=["GET", "POST"])
  496. def build_apk():
  497. vprint('==============================================================')
  498. if request.method == 'POST':
  499. logo = None
  500. logo_float = None
  501. app_name = "NexilisLite"
  502. package_id = "com.app.nexilis"
  503. acc = None
  504. url = None
  505. keystore = None
  506. huawei_file = None
  507. key_exists = False
  508. # tabs = ["1", "2", "3", "4"]
  509. fb_order = "1,2,3,4,5"
  510. tabs = []
  511. tab3_mode = "0"
  512. tab_amount = 6
  513. tab_icon = [None, None, None, None, None, None]
  514. fb_icon = [None, None, None, None, None]
  515. background = None
  516. version_code = "1"
  517. version_name = "1.0.0"
  518. font = 0
  519. enable_sms = 0
  520. key = {"alias": "nexilislite", "store_password": "allyourbase", "key_password": "allyourbase",
  521. "common_name": "all", "organization_unit": "your",
  522. "organization_name": "base", "locality_name": "are", "state_name": "belong", "country": "to"}
  523. try:
  524. if 'logo' in request.files:
  525. logo = request.files['logo']
  526. vprint(type(logo))
  527. elif 'logo' in request.form:
  528. logo = request.form['logo']
  529. vprint(type(logo))
  530. if 'logofloat' in request.files:
  531. logo_float = request.files['logofloat']
  532. vprint(type(logo_float))
  533. elif 'logofloat' in request.form:
  534. logo_float = request.form['logofloat']
  535. vprint(type(logo_float))
  536. if 'app_name' in request.files:
  537. app_name = request.files['app_name']
  538. vprint(app_name)
  539. elif 'app_name' in request.form:
  540. app_name = request.form['app_name']
  541. vprint(app_name)
  542. if 'package_id' in request.files:
  543. package_id = request.files['package_id']
  544. vprint(package_id)
  545. elif 'package_id' in request.form:
  546. package_id = request.form['package_id']
  547. vprint(package_id)
  548. if 'acc' in request.files:
  549. acc = request.files['acc']
  550. vprint(acc)
  551. elif 'acc' in request.form:
  552. acc = request.form['acc']
  553. vprint(acc)
  554. if 'url' in request.files:
  555. url = request.files['url']
  556. vprint(url)
  557. elif 'url' in request.form:
  558. url = request.form['url']
  559. vprint(url)
  560. if 'keystore' in request.files:
  561. keystore = request.files['keystore']
  562. key_exists = True
  563. elif 'keystore' in request.form:
  564. keystore = request.form['keystore']
  565. key_exists = True
  566. if 'huawei_file' in request.files:
  567. huawei_file = request.files['huawei_file']
  568. elif 'huawei_file' in request.form:
  569. huawei_file = request.form['huawei_file']
  570. if 'alias' in request.form:
  571. if request.form['alias']:
  572. key["alias"] = request.form['alias']
  573. if 'store_password' in request.form:
  574. if request.form['store_password']:
  575. key["store_password"] = request.form['store_password']
  576. if 'key_password' in request.form:
  577. if request.form['key_password']:
  578. key["key_password"] = request.form['key_password']
  579. if keystore:
  580. if 'common_name' in request.form:
  581. if request.form['common_name']:
  582. key["common_name"] = request.form['common_name']
  583. if 'organization_unit' in request.form:
  584. if request.form['organization_unit']:
  585. key["organization_unit"] = request.form['organization_unit']
  586. if 'organization_name' in request.form:
  587. if request.form['organization_name']:
  588. key["organization_name"] = request.form['organization_name']
  589. if 'locality_name' in request.form:
  590. if request.form['locality_name']:
  591. key["locality_name"] = request.form['locality_name']
  592. if 'state_name' in request.form:
  593. if request.form['state_name']:
  594. key["state_name"] = request.form['state_name']
  595. if 'country' in request.form:
  596. if request.form['country']:
  597. key["country"] = request.form['country']
  598. tabs.append(request.form["tab1"])
  599. tabs.append(request.form["tab2"])
  600. if request.form['tab3']:
  601. tabs.append(request.form["tab3"])
  602. if request.form['tab4']:
  603. tabs.append(request.form["tab4"])
  604. if 'tab1_icon' in request.files:
  605. tab_icon[0] = request.files['tab1_icon']
  606. elif 'tab1_icon' in request.form:
  607. tab_icon[0] = request.form['tab1_icon']
  608. if 'tab2_icon' in request.files:
  609. tab_icon[1] = request.files['tab2_icon']
  610. elif 'tab2_icon' in request.form:
  611. tab_icon[1] = request.form['tab2_icon']
  612. if 'tab3_icon' in request.files:
  613. tab_icon[2] = request.files['tab3_icon']
  614. elif 'tab3_icon' in request.form:
  615. tab_icon[2] = request.form['tab3_icon']
  616. if 'tab4_icon' in request.files:
  617. tab_icon[3] = request.files['tab4_icon']
  618. elif 'tab4_icon' in request.form:
  619. tab_icon[3] = request.form['tab4_icon']
  620. if 'fb1_icon' in request.files:
  621. fb_icon[0] = request.files['fb1_icon']
  622. elif 'fb1_icon' in request.form:
  623. fb_icon[0] = request.form['fb1_icon']
  624. if 'fb2_icon' in request.files:
  625. fb_icon[1] = request.files['fb2_icon']
  626. elif 'fb2_icon' in request.form:
  627. fb_icon[1] = request.form['fb2_icon']
  628. if 'fb3_icon' in request.files:
  629. fb_icon[2] = request.files['fb3_icon']
  630. elif 'fb3_icon' in request.form:
  631. fb_icon[2] = request.form['fb3_icon']
  632. if 'fb4_icon' in request.files:
  633. fb_icon[3] = request.files['fb4_icon']
  634. elif 'fb4_icon' in request.form:
  635. fb_icon[3] = request.form['fb4_icon']
  636. if 'fb5_icon' in request.files:
  637. fb_icon[4] = request.files['fb5_icon']
  638. elif 'fb5_icon' in request.form:
  639. fb_icon[4] = request.form['fb5_icon']
  640. if 'fb_icon' in request.form:
  641. fb_icon = request.form['fb_icon'].split(",")
  642. if 'fb_order' in request.form:
  643. fb_order = request.form['fb_order']
  644. vprint("fb_icon: {}".format(fb_icon))
  645. access_model = int(request.form['access_model'])
  646. if 'tab3_mode' in request.form:
  647. tab3_mode = request.form['tab3_mode']
  648. if 'tab_amount' in request.form:
  649. tab_amount = int(request.form['tab_amount'])
  650. if 'font' in request.form:
  651. font = int(request.form['font'])
  652. if 'background' in request.files:
  653. background = request.files['background']
  654. elif 'background' in request.form:
  655. background = request.form['background']
  656. if 'version_code' in request.form:
  657. version_code = request.form['version_code']
  658. if 'version_name' in request.form:
  659. version_name = request.form['version_name']
  660. else:
  661. version_name = "1.0.{}".format(version_code)
  662. if 'enable_sms' in request.form:
  663. enable_sms = int(request.form['enable_sms'])
  664. except BaseException as e:
  665. vprint(traceback.format_exc())
  666. return {"status": "1", "message": "Parameter mismatch\n{}\n".format(str(e))}
  667. try:
  668. path_dest, c_code_path = create_folder(package_id)
  669. vprint("path_dest: " + path_dest)
  670. vprint("c_code_path: " + c_code_path)
  671. if acc:
  672. change_acc(c_code_path, acc, enable_sms)
  673. if url:
  674. change_url(c_code_path, url)
  675. change_name(path_dest, app_name, enable_sms)
  676. change_certificate(path_dest, key, keystore, app.keytool)
  677. change_package(path_dest, package_id)
  678. change_version(path_dest, version_code, version_name)
  679. change_font(path_dest, font, package_id)
  680. if logo:
  681. change_logo(path_dest, logo, logo_float)
  682. if background:
  683. change_background(path_dest, background)
  684. change_fb(path_dest, fb_order, package_id, fb_icon)
  685. change_access(path_dest, access_model, package_id)
  686. change_tab(path_dest, tabs, tab_icon, package_id, tab3_mode, tab_amount)
  687. change_huawei_file(path_dest, huawei_file, package_id)
  688. except BaseException as e:
  689. vprint(traceback.format_exc())
  690. return {"status": "2", "message": "Process failure\n{}\n".format(str(e))}
  691. os.chdir(path_dest)
  692. return_code, error = run_build(path_dest)
  693. if return_code == 0:
  694. return deliver_apk(path_dest, package_id, key, key_exists)
  695. return {"status": "3", "message": "Build failed\n{}".format(str(error))}
  696. else:
  697. if 'e' in request.args:
  698. return request.args['e']
  699. return "Hello World!"
  700. if __name__ == '__main__':
  701. app.run(host='0.0.0.0', port=8072, debug=app.verbose, ssl_context=app.ssl)