main_sys_update.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. if platform.system() == 'Linux' or platform.system() == 'Darwin':
  23. ok = []
  24. ok += [os.system('git remote rm origin')]
  25. ok += [os.system('git remote add origin https://github.com/opennamu/opennamu.git')]
  26. ok += [os.system('git fetch --depth=1 origin ' + up_data)]
  27. ok += [os.system('git reset --hard origin/' + up_data)]
  28. for for_a in ok[1:]:
  29. if for_a != 0:
  30. break
  31. else:
  32. linux_exe_chmod()
  33. await main_sys_restart_do()
  34. print('Error : update failed')
  35. elif platform.system() == 'Windows':
  36. os.system('rd /s /q route')
  37. urllib.request.urlretrieve('https://github.com/opennamu/opennamu/archive/' + up_data + '.zip', 'update.zip')
  38. zipfile.ZipFile('update.zip').extractall('')
  39. ok = os.system('xcopy /y /s /r opennamu-' + up_data + ' .')
  40. if ok == 0:
  41. os.system('rd /s /q opennamu-' + up_data)
  42. os.system('del update.zip')
  43. await main_sys_restart_do()
  44. print('Error : update failed')
  45. return await re_error(conn, 34)
  46. else:
  47. return easy_minify(conn, flask.render_template(skin_check(conn),
  48. imp = [get_lang(conn, 'update'), wiki_set(conn), await wiki_custom(conn), wiki_css([0, 0])],
  49. data = get_lang(conn, 'update_warning') + '''
  50. <hr class="main_hr">
  51. <ul>
  52. <li id="ver_send_2">''' + get_lang(conn, 'version') + ''' : </li>
  53. <li id="ver_send">''' + get_lang(conn, 'lastest') + ''' : </li>
  54. </ul>
  55. <a href="https://github.com/openNAMU/openNAMU">(Beta)</a> <a href="https://github.com/openNAMU/openNAMU/tree/stable">(Stable)</a>
  56. <hr class="main_hr">
  57. <form method="post">
  58. <button type="submit">''' + get_lang(conn, 'update') + '''</button>
  59. </form>
  60. <!-- JS : opennamu_do_insert_version -->
  61. ''',
  62. menu = [['manager', get_lang(conn, 'return')]]
  63. ))