main_sys_update.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import zipfile
  2. import urllib.request
  3. from .tool.func import *
  4. def main_sys_update():
  5. with get_db_connect() as conn:
  6. curs = conn.cursor()
  7. if admin_check(conn) != 1:
  8. return re_error(conn, '/error/3')
  9. if flask.request.method == 'POST':
  10. admin_check(conn, None, 'update')
  11. curs.execute(db_change('select data from other where name = "update"'))
  12. up_data = curs.fetchall()
  13. up_data = up_data[0][0] if up_data and up_data[0][0] in ['stable', 'beta', 'dev', 'dont_use'] else 'stable'
  14. print('Update')
  15. if platform.system() == 'Linux':
  16. ok = []
  17. ok += [os.system('git remote rm origin')]
  18. ok += [os.system('git remote add origin https://github.com/opennamu/opennamu.git')]
  19. ok += [os.system('git fetch --depth=1 origin ' + up_data)]
  20. ok += [os.system('git reset --hard origin/' + up_data)]
  21. for for_a in ok[1:]:
  22. if for_a != 0:
  23. break
  24. else:
  25. return redirect(conn, '/restart')
  26. print('Error : update failed')
  27. elif platform.system() == 'Windows':
  28. os.system('rd /s /q route')
  29. url = 'https://github.com/opennamu/opennamu/archive/' + up_data + '.zip'
  30. local_filename = 'update.zip'
  31. response = requests.get(url, stream = True)
  32. if response.status_code == 200:
  33. with open(local_filename, 'wb') as f:
  34. for chunk in response.iter_content(chunk_size = 1024 * 1024):
  35. if chunk:
  36. f.write(chunk)
  37. zipfile.ZipFile('update.zip').extractall('')
  38. ok = os.system('xcopy /y /s /r opennamu-' + up_data + ' .')
  39. if ok == 0:
  40. os.system('rd /s /q opennamu-' + up_data)
  41. os.system('del update.zip')
  42. return redirect(conn, '/restart')
  43. print('Error : update failed')
  44. return re_error(conn, '/error/34')
  45. else:
  46. return easy_minify(conn, flask.render_template(skin_check(conn),
  47. imp = [get_lang(conn, 'update'), wiki_set(conn), wiki_custom(conn), wiki_css([0, 0])],
  48. data = get_lang(conn, 'update_warning') + '''
  49. <hr class="main_hr">
  50. <ul class="opennamu_ul">
  51. <li id="ver_send_2">''' + get_lang(conn, 'version') + ''' : </li>
  52. <li id="ver_send">''' + get_lang(conn, 'lastest') + ''' : </li>
  53. </ul>
  54. <a href="https://github.com/openNAMU/openNAMU">(Beta)</a> <a href="https://github.com/openNAMU/openNAMU/tree/stable">(Stable)</a>
  55. <hr class="main_hr">
  56. <form method="post">
  57. <button type="submit">''' + get_lang(conn, 'update') + '''</button>
  58. </form>
  59. <!-- JS : opennamu_do_insert_version -->
  60. ''',
  61. menu = [['manager', get_lang(conn, 'return')]]
  62. ))