main.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import flask
  2. from flask import Flask, request
  3. from PIL import ImageFile, Image
  4. import os
  5. import shutil
  6. import subprocess
  7. ImageFile.SAFEBLOCK = 2048*2048
  8. app = Flask(__name__)
  9. app.base_project = "/Users/easysoft/Documents/PalioBrowserGen"
  10. app.base_project_name = os.path.basename(app.base_project)
  11. app.temp_folder = "/Users/easysoft/BuildApk"
  12. app.verbose = True
  13. def vprint(*data):
  14. if app.verbose:
  15. print(data)
  16. def create_folder(package_id):
  17. path = os.path.join(app.temp_folder, package_id)
  18. if not os.path.exists(path):
  19. os.mkdir(path)
  20. else:
  21. shutil.rmtree(path)
  22. os.mkdir(path)
  23. vprint(path)
  24. path_dest = os.path.join(path,app.base_project_name)
  25. if not os.path.exists(path_dest):
  26. shutil.copytree(app.base_project, path_dest)
  27. code_path = "app/src/main/java/"
  28. orig_code = "io/palio/browser"
  29. path_package_id = package_id.replace(".","/")
  30. c_code_path = os.path.join(path_dest, code_path, path_package_id)
  31. orig_code_path = os.path.join(path_dest, code_path, orig_code)
  32. if not os.path.exists(c_code_path):
  33. shutil.copytree(orig_code_path,c_code_path)
  34. shutil.rmtree(orig_code_path)
  35. return path_dest, c_code_path
  36. def change_url(c_code_path, url):
  37. main_activity = os.path.join(c_code_path, "MainActivity.java")
  38. with open(main_activity, "r") as f:
  39. file_source = f.read()
  40. replaced = file_source.replace("https://www.google.com",url)
  41. with open(main_activity, "w") as f:
  42. f.write(replaced)
  43. def change_name(path_dest, name):
  44. manifest = os.path.join(path_dest, "app/src/main/AndroidManifest.xml")
  45. with open(manifest, "r") as f:
  46. file_source = f.read()
  47. replaced = file_source.replace("Palio Brow", name)
  48. with open(manifest, "w") as f:
  49. f.write(replaced)
  50. def change_package(path_dest, package):
  51. build_gradle = os.path.join(path_dest, 'app/build.gradle')
  52. with open(build_gradle, "r") as f:
  53. file_source = f.read()
  54. replaced = file_source.replace("io.palio.browser", package)
  55. with open(build_gradle, "w") as f:
  56. f.write(replaced)
  57. manifest = os.path.join(path_dest, "app/src/main/AndroidManifest.xml")
  58. with open(manifest, "r") as f:
  59. file_source = f.read()
  60. replaced = file_source.replace("io.palio.browser", package)
  61. with open(manifest, "w") as f:
  62. f.write(replaced)
  63. path_package_id = package.replace(".", "/")
  64. code_path = "app/src/main/java/"
  65. code_path = os.path.join(path_dest,code_path,path_package_id)
  66. javas = [os.path.join(dp, f) for dp, dn, filenames in os.walk(code_path) for f in filenames]
  67. for j in javas:
  68. with open(j, "r") as f:
  69. file_source = f.read()
  70. replaced = file_source.replace("io.palio.browser", package)
  71. with open(j, "w") as f:
  72. f.write(replaced)
  73. def change_logo(path_dest, logo):
  74. img_path = "app/src/main/res/drawable/pb_palio_ball.png"
  75. img_path = os.path.join(path_dest, img_path)
  76. logo = Image.open(logo)
  77. logo = logo.resize((150,150))
  78. logo.save(img_path,"PNG")
  79. def run_build(path_dest):
  80. gradlew = os.path.join(path_dest,"gradlew")
  81. ret = subprocess.run([gradlew, 'assembleRelease'])
  82. return ret.returncode
  83. def deliver_apk(path_dest):
  84. apk_dir = os.path.join(path_dest,'app/build/outputs/apk/release')
  85. vprint(apk_dir)
  86. try:
  87. return flask.send_from_directory(apk_dir,'app-release.apk')
  88. except Exception as e:
  89. return str(e)
  90. @app.route('/', methods=["GET","POST"])
  91. def build_apk():
  92. if request.method == 'POST':
  93. logo = request.files['logo']
  94. app_name = request.form['app_name']
  95. package_id = request.form['package_id']
  96. home_url = request.form['url']
  97. path_dest, c_code_path = create_folder(package_id)
  98. vprint("path_dest: " + path_dest)
  99. vprint("c_code_path: "+c_code_path)
  100. change_url(c_code_path, home_url)
  101. change_name(path_dest, app_name)
  102. change_package(path_dest,package_id)
  103. change_logo(path_dest,logo)
  104. os.chdir(path_dest)
  105. return_code = run_build(path_dest)
  106. if(return_code == 0):
  107. return deliver_apk(path_dest)
  108. return "h"
  109. else:
  110. if 'e' in request.args:
  111. return request.args['e']
  112. return "Hello World!"
  113. if __name__ == '__main__':
  114. app.run(host='0.0.0.0', port=8090, debug=app.verbose)