您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

120 行
3.5 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. """
  5. Execute Patch Files
  6. To run directly
  7. python lib/wnf.py patch patch1, patch2 etc
  8. python lib/wnf.py patch -f patch1, patch2 etc
  9. where patch1, patch2 is module name
  10. """
  11. import webnotes
  12. class PatchError(Exception): pass
  13. def run_all(patch_list=None):
  14. """run all pending patches"""
  15. if webnotes.conn.table_exists("__PatchLog"):
  16. executed = [p[0] for p in webnotes.conn.sql("""select patch from `__PatchLog`""")]
  17. else:
  18. executed = [p[0] for p in webnotes.conn.sql("""select patch from `tabPatch Log`""")]
  19. import patches.patch_list
  20. for patch in (patch_list or patches.patch_list.patch_list):
  21. if patch not in executed:
  22. if not run_single(patchmodule = patch):
  23. log(patch + ': failed: STOPPED')
  24. raise PatchError(patch)
  25. def reload_doc(args):
  26. import webnotes.modules
  27. run_single(method = webnotes.modules.reload_doc, methodargs = args)
  28. def run_single(patchmodule=None, method=None, methodargs=None, force=False):
  29. from webnotes import conf
  30. # don't write txt files
  31. conf.developer_mode = 0
  32. if force or method or not executed(patchmodule):
  33. return execute_patch(patchmodule, method, methodargs)
  34. else:
  35. return True
  36. def execute_patch(patchmodule, method=None, methodargs=None):
  37. """execute the patch"""
  38. success = False
  39. block_user(True)
  40. webnotes.conn.begin()
  41. try:
  42. log('Executing %s in %s' % (patchmodule or str(methodargs), webnotes.conn.cur_db_name))
  43. if patchmodule:
  44. if patchmodule.startswith("execute:"):
  45. exec patchmodule.split("execute:")[1] in globals()
  46. else:
  47. webnotes.get_method(patchmodule + ".execute")()
  48. update_patch_log(patchmodule)
  49. elif method:
  50. method(**methodargs)
  51. webnotes.conn.commit()
  52. success = True
  53. except Exception, e:
  54. webnotes.conn.rollback()
  55. tb = webnotes.getTraceback()
  56. log(tb)
  57. import os
  58. if webnotes.request:
  59. add_to_patch_log(tb)
  60. block_user(False)
  61. if success:
  62. log('Success')
  63. return success
  64. def add_to_patch_log(tb):
  65. """add error log to patches/patch.log"""
  66. import conf, os
  67. # TODO use get_site_base_path
  68. with open(os.path.join(os.path.dirname(conf.__file__), 'app', 'patches','patch.log'),'a') as patchlog:
  69. patchlog.write('\n\n' + tb)
  70. def update_patch_log(patchmodule):
  71. """update patch_file in patch log"""
  72. if webnotes.conn.table_exists("__PatchLog"):
  73. webnotes.conn.sql("""INSERT INTO `__PatchLog` VALUES (%s, now())""", \
  74. patchmodule)
  75. else:
  76. webnotes.doc({"doctype": "Patch Log", "patch": patchmodule}).insert()
  77. def executed(patchmodule):
  78. """return True if is executed"""
  79. if webnotes.conn.table_exists("__PatchLog"):
  80. done = webnotes.conn.sql("""select patch from __PatchLog where patch=%s""", patchmodule)
  81. else:
  82. done = webnotes.conn.get_value("Patch Log", {"patch": patchmodule})
  83. if done:
  84. print "Patch %s executed in %s" % (patchmodule, webnotes.conn.cur_db_name)
  85. return done
  86. def block_user(block):
  87. """stop/start execution till patch is run"""
  88. webnotes.conn.begin()
  89. msg = "Patches are being executed in the system. Please try again in a few moments."
  90. webnotes.conn.set_global('__session_status', block and 'stop' or None)
  91. webnotes.conn.set_global('__session_status_message', block and msg or None)
  92. webnotes.conn.commit()
  93. def setup():
  94. webnotes.conn.sql("""CREATE TABLE IF NOT EXISTS `__PatchLog` (
  95. patch TEXT, applied_on DATETIME) engine=InnoDB""")
  96. def log(msg):
  97. if getattr(webnotes.local, "patch_log_list", None) is None:
  98. webnotes.local.patch_log_list = []
  99. webnotes.local.patch_log_list.append(msg)