main_sys_update.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import zipfile
  2. import urllib.request
  3. from .tool.func import *
  4. from .main_sys_restart import main_sys_restart_do
  5. async def main_sys_update(golang_process):
  6. with get_db_connect() as conn:
  7. curs = conn.cursor()
  8. if await acl_check('', 'owner_auth', '', '') == 1:
  9. return await re_error(conn, 3)
  10. if flask.request.method == 'POST':
  11. await acl_check(tool = 'owner_auth', memo = 'update')
  12. curs.execute(db_change('select data from other where name = "update"'))
  13. up_data = curs.fetchall()
  14. up_data = up_data[0][0] if up_data and up_data[0][0] in ['stable', 'beta', 'dev', 'dont_use'] else 'stable'
  15. print('Update')
  16. if golang_process.poll() is None:
  17. golang_process.terminate()
  18. try:
  19. golang_process.wait(timeout = 5)
  20. except subprocess.TimeoutExpired:
  21. golang_process.kill()
  22. try:
  23. golang_process.wait(timeout = 5)
  24. except subprocess.TimeoutExpired:
  25. print('Golang process not terminated properly.')
  26. if platform.system() == 'Linux' or platform.system() == 'Darwin':
  27. ok = []
  28. ok += [os.system('git remote rm origin')]
  29. ok += [os.system('git remote add origin https://github.com/opennamu/opennamu.git')]
  30. ok += [os.system('git fetch --depth=1 origin ' + up_data)]
  31. ok += [os.system('git reset --hard origin/' + up_data)]
  32. for for_a in ok[1:]:
  33. if for_a != 0:
  34. break
  35. else:
  36. linux_exe_chmod()
  37. threading.Thread(target = main_sys_restart_do).start()
  38. return flask.Response(await get_lang("warning_restart"), status = 200)
  39. print('Error : update failed')
  40. elif platform.system() == 'Windows':
  41. os.system('rd /s /q route')
  42. urllib.request.urlretrieve('https://github.com/opennamu/opennamu/archive/' + up_data + '.zip', 'update.zip')
  43. zipfile.ZipFile('update.zip').extractall('')
  44. ok = os.system('xcopy /y /s /r opennamu-' + up_data + ' .')
  45. if ok == 0:
  46. os.system('rd /s /q opennamu-' + up_data)
  47. os.system('del update.zip')
  48. threading.Thread(target = main_sys_restart_do).start()
  49. return flask.Response(await get_lang("warning_restart"), status = 200)
  50. print('Error : update failed')
  51. return await re_error(conn, 34)
  52. else:
  53. return easy_minify(flask.render_template(await skin_check(),
  54. imp = [await get_lang('update'), await wiki_set(), await wiki_custom(conn), wiki_css([0, 0])],
  55. data = await get_lang('update_warning') + '''
  56. <hr class="main_hr">
  57. <ul>
  58. <li id="ver_send_2">''' + await get_lang('version') + ''' : </li>
  59. <li id="ver_send">''' + await get_lang('lastest') + ''' : </li>
  60. </ul>
  61. <a href="https://github.com/openNAMU/openNAMU">(Beta)</a> <a href="https://github.com/openNAMU/openNAMU/tree/stable">(Stable)</a>
  62. <hr class="main_hr">
  63. <form method="post">
  64. <button type="submit">''' + await get_lang('update') + '''</button>
  65. </form>
  66. <!-- JS : opennamu_do_insert_version -->
  67. ''',
  68. menu = [['manager', await get_lang('return')]]
  69. ))