<commit_before># Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import unicode_literals, print_function
"""
	Execute Patch Files

	To run directly

	python lib/wnf.py patch patch1, patch2 etc
	python lib/wnf.py patch -f patch1, patch2 etc

	where patch1, patch2 is module name
"""
import frappe, frappe.permissions

# for patches
import os

class PatchError(Exception): pass

def run_all():
	"""run all pending patches"""
	executed = [p[0] for p in frappe.db.sql("""select patch from `tabPatch Log`""")]

	frappe.flags.final_patches = []
	for patch in get_all_patches():
		if patch and (patch not in executed):
			if not run_single(patchmodule = patch):
				log(patch + ': failed: STOPPED')
				raise PatchError(patch)

	# patches to be run in the end
	for patch in frappe.flags.final_patches:
		patch = patch.replace('finally:', '')
		if not run_single(patchmodule = patch):
			log(patch + ': failed: STOPPED')
			raise PatchError(patch)

def get_all_patches():
	patches = []
	for app in frappe.get_installed_apps():
		if app == "shopping_cart":
			continue
		# 3-to-4 fix
		if app=="webnotes":
			app="frappe"
		patches.extend(frappe.get_file_items(frappe.get_pymodule_path(app, "patches.txt")))

	return patches

def reload_doc(args):
	import frappe.modules
	run_single(method = frappe.modules.reload_doc, methodargs = args)

def run_single(patchmodule=None, method=None, methodargs=None, force=False):
	from frappe import conf

	# don't write txt files
	conf.developer_mode = 0

	if force or method or not executed(patchmodule):
		return execute_patch(patchmodule, method, methodargs)
	else:
		return True

def execute_patch(patchmodule, method=None, methodargs=None):
	"""execute the patch"""
	block_user(True)
	frappe.db.begin()

	try:
		log('Executing {patch} in {site} ({db})'.format(patch=patchmodule or str(methodargs),
			site=frappe.local.site, db=frappe.db.cur_db_name))
		if patchmodule:
			if patchmodule.startswith("finally:"):
				# run run patch at the end
				frappe.flags.final_patches.append(patchmodule)
			else:
				if patchmodule.startswith("execute:"):
					exec(patchmodule.split("execute:")[1],globals())
				else:
					frappe.get_attr(patchmodule.split()[0] + ".execute")()
				update_patch_log(patchmodule)
		elif method:
			method(**methodargs)

	except Exception:
		frappe.db.rollback()
		raise

	else:
		frappe.db.commit()
		block_user(False)
		log('Success')

	return True

def update_patch_log(patchmodule):
	"""update patch_file in patch log"""
	frappe.get_doc({"doctype": "Patch Log", "patch": patchmodule}).insert(ignore_permissions=True)

def executed(patchmodule):
	"""return True if is executed"""
	if patchmodule.startswith('finally:'):
		# patches are saved without the finally: tag
		patchmodule = patchmodule.replace('finally:', '')
	done = frappe.db.get_value("Patch Log", {"patch": patchmodule})
	# if done:
	# 	print "Patch %s already executed in %s" % (patchmodule, frappe.db.cur_db_name)
	return done

def block_user(block):
	"""stop/start execution till patch is run"""
	frappe.local.flags.in_patch = block
	frappe.db.begin()<commit_msg>time taken by the patch along with success message (#5524)<commit_after># Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import unicode_literals, print_function
"""
	Execute Patch Files

	To run directly

	python lib/wnf.py patch patch1, patch2 etc
	python lib/wnf.py patch -f patch1, patch2 etc

	where patch1, patch2 is module name
"""
import frappe, frappe.permissions, time

# for patches
import os

class PatchError(Exception): pass

def run_all():
	"""run all pending patches"""
	executed = [p[0] for p in frappe.db.sql("""select patch from `tabPatch Log`""")]

	frappe.flags.final_patches = []
	for patch in get_all_patches():
		if patch and (patch not in executed):
			if not run_single(patchmodule = patch):
				log(patch + ': failed: STOPPED')
				raise PatchError(patch)

	# patches to be run in the end
	for patch in frappe.flags.final_patches:
		patch = patch.replace('finally:', '')
		if not run_single(patchmodule = patch):
			log(patch + ': failed: STOPPED')
			raise PatchError(patch)

def get_all_patches():
	patches = []
	for app in frappe.get_installed_apps():
		if app == "shopping_cart":
			continue
		# 3-to-4 fix
		if app=="webnotes":
			app="frappe"
		patches.extend(frappe.get_file_items(frappe.get_pymodule_path(app, "patches.txt")))

	return patches

def reload_doc(args):
	import frappe.modules
	run_single(method = frappe.modules.reload_doc, methodargs = args)

def run_single(patchmodule=None, method=None, methodargs=None, force=False):
	from frappe import conf

	# don't write txt files
	conf.developer_mode = 0

	if force or method or not executed(patchmodule):
		return execute_patch(patchmodule, method, methodargs)
	else:
		return True

def execute_patch(patchmodule, method=None, methodargs=None):
	"""execute the patch"""
	block_user(True)
	frappe.db.begin()
	start_time = time.time()
	try:
		log('Executing {patch} in {site} ({db})'.format(patch=patchmodule or str(methodargs),
			site=frappe.local.site, db=frappe.db.cur_db_name))
		if patchmodule:
			if patchmodule.startswith("finally:"):
				# run run patch at the end
				frappe.flags.final_patches.append(patchmodule)
			else:
				if patchmodule.startswith("execute:"):
					exec(patchmodule.split("execute:")[1],globals())
				else:
					frappe.get_attr(patchmodule.split()[0] + ".execute")()
				update_patch_log(patchmodule)
		elif method:
			method(**methodargs)

	except Exception:
		frappe.db.rollback()
		raise

	else:
		frappe.db.commit()
		end_time = time.time()
		block_user(False)
		log('Success: Done in {time}s'.format(time = round(end_time - start_time, 3)))

	return True

def update_patch_log(patchmodule):
	"""update patch_file in patch log"""
	frappe.get_doc({"doctype": "Patch Log", "patch": patchmodule}).insert(ignore_permissions=True)

def executed(patchmodule):
	"""return True if is executed"""
	if patchmodule.startswith('finally:'):
		# patches are saved without the finally: tag
		patchmodule = patchmodule.replace('finally:', '')
	done = frappe.db.get_value("Patch Log", {"patch": patchmodule})
	# if done:
	# 	print "Patch %s already executed in %s" % (patchmodule, frappe.db.cur_db_name)
	return done

def block_user(block):
	"""stop/start execution till patch is run"""
	frappe.local.flags.in_patch = block
	frappe.db.begin()
{'fraction_non_alphanumeric': 0.12710424710424711, 'fraction_numerical': 0.005714285714285714, 'mean_word_length': 4.818508535489667, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4204583', 'n_tokens_mistral': 2375, 'n_tokens_neox': 2216, 'n_words': 579}