mainPalio4.py 37 KB

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