app.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import uuid
  2. import os
  3. import subprocess
  4. import zipfile
  5. import urllib.request
  6. import gzip
  7. from io import BytesIO
  8. import time
  9. from flask import Flask # using flask as the framework
  10. from flask import render_template
  11. from flask import request
  12. # Directory of this file
  13. this_path = os.path.dirname(os.path.realpath(__file__))
  14. # Where the user requested tile are stored
  15. output_path = os.path.join(this_path, '..', 'userRequestFirmware')
  16. # Where the data database is
  17. tile_path = os.path.join(this_path, '..', 'data', 'tiles')
  18. # The output folder for all gzipped build requests
  19. app = Flask(__name__, static_url_path='/builds', static_folder=output_path,)
  20. def compressFiles(fileList, uuidkey):
  21. # create a zip file comprised of dat.gz tiles
  22. zipthis = os.path.join(output_path, uuidkey + '.zip')
  23. # create output dirs if needed
  24. try:
  25. os.makedirs(output_path)
  26. except OSError:
  27. pass
  28. try:
  29. os.makedirs(tile_path)
  30. except OSError:
  31. pass
  32. try:
  33. with zipfile.ZipFile(zipthis, 'w') as terrain_zip:
  34. for fn in fileList:
  35. if not os.path.exists(fn):
  36. #download if required
  37. print("Downloading " + os.path.basename(fn))
  38. g = urllib.request.urlopen('https://terrain.ardupilot.org/data/tiles/' +
  39. os.path.basename(fn))
  40. print("Downloaded " + os.path.basename(fn))
  41. with open(fn, 'b+w') as f:
  42. f.write(g.read())
  43. # need to decompress file and pass to zip
  44. with gzip.open(fn, 'r') as f_in:
  45. myio = BytesIO(f_in.read())
  46. print("Decomp " + os.path.basename(fn))
  47. # and add file to zip
  48. terrain_zip.writestr(os.path.basename(fn)[:-3], myio.read(),
  49. compress_type=zipfile.ZIP_DEFLATED)
  50. except Exception as ex:
  51. print("Unexpected error: {0}".format(ex))
  52. return False
  53. return True
  54. @app.route('/')
  55. def index():
  56. return render_template('index.html')
  57. @app.route('/generate', methods=['GET', 'POST'])
  58. def generate():
  59. if request.method == 'POST':
  60. # request.form['username']
  61. features = []
  62. for i in range(1,8):
  63. value = request.form["option" + str(i)]
  64. features.append(value)
  65. undefine = "undef " + value.split()[1]
  66. features.insert(0,undefine)
  67. extra_hwdef = '\n'.join(features)
  68. print("features: ", features)
  69. print("running...")
  70. file = open('extra_hwdef.dat',"w")
  71. file.write(extra_hwdef)
  72. file.close()
  73. git_hash = subprocess.check_output(['git', 'rev-parse', 'master'])
  74. git_hash = git_hash[:len(git_hash)-1]
  75. md5sum = subprocess.check_output(['md5sum', 'extra_hwdef.dat'])
  76. md5sum = md5sum[:len(md5sum)-18]
  77. builddir = '/tmp/build'
  78. sourcedir = '/Users/willpiper/Documents/GitHub/ardupilot/'
  79. appdir = os.path.abspath(os.curdir)
  80. board = 'Beastf7'
  81. subprocess.run(['./waf', 'configure',
  82. '--board', board,
  83. '--out', builddir,
  84. '--extra-hwdef', os.path.join(appdir, 'extra_hwdef.dat')],
  85. cwd = sourcedir)
  86. subprocess.run(['./waf', 'copter'], cwd = sourcedir)
  87. print("git hash: ", git_hash)
  88. print("md5sum: ", md5sum)
  89. print("token: ", git_hash+md5sum)
  90. # UUID for this terrain generation
  91. uuidkey = str(uuid.uuid1())
  92. # remove duplicates
  93. #filelist = list(dict.fromkeys(filelist))
  94. #print(filelist)
  95. #compress
  96. #success = compressFiles(filelist, uuidkey)
  97. # as a cleanup, remove any generated terrain older than 24H
  98. #for f in os.listdir(output_path):
  99. # if os.stat(os.path.join(output_path, f)).st_mtime < time.time() - 24 * 60 * 60:
  100. # print("Removing old file: " + str(os.path.join(output_path, f)))
  101. # os.remove(os.path.join(output_path, f))
  102. #if success:
  103. # print("Generated " + "/terrain/" + uuidkey + ".zip")
  104. # return render_template('generate.html', urlkey="/terrain/" + uuidkey + ".zip",
  105. # uuidkey=uuidkey, outsideLat=outsideLat)
  106. #else:
  107. # print("Failed " + "/terrain/" + uuidkey + ".zip")
  108. # return render_template('generate.html', error="Cannot generate terrain",
  109. # uuidkey=uuidkey)
  110. else:
  111. print("Bad get")
  112. return render_template('generate.html', error="Need to use POST, not GET")
  113. if __name__ == "__main__":
  114. app.run()