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

286 行
8.9 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # License: MIT. See LICENSE
  3. import hashlib
  4. import json
  5. import os
  6. import frappe
  7. from frappe.model.base_document import get_controller
  8. from frappe.modules import get_module_path, scrub_dt_dn
  9. from frappe.query_builder import DocType
  10. from frappe.utils import get_datetime, now
  11. def caclulate_hash(path: str) -> str:
  12. """Calculate md5 hash of the file in binary mode
  13. Args:
  14. path (str): Path to the file to be hashed
  15. Returns:
  16. str: The calculated hash
  17. """
  18. hash_md5 = hashlib.md5()
  19. with open(path, "rb") as f:
  20. for chunk in iter(lambda: f.read(4096), b""):
  21. hash_md5.update(chunk)
  22. return hash_md5.hexdigest()
  23. ignore_values = {
  24. "Report": ["disabled", "prepared_report", "add_total_row"],
  25. "Print Format": ["disabled"],
  26. "Notification": ["enabled"],
  27. "Print Style": ["disabled"],
  28. "Module Onboarding": ["is_complete"],
  29. "Onboarding Step": ["is_complete", "is_skipped"],
  30. }
  31. ignore_doctypes = [""]
  32. def import_files(module, dt=None, dn=None, force=False, pre_process=None, reset_permissions=False):
  33. if type(module) is list:
  34. out = []
  35. for m in module:
  36. out.append(import_file(m[0], m[1], m[2], force=force, pre_process=pre_process, reset_permissions=reset_permissions))
  37. return out
  38. else:
  39. return import_file(module, dt, dn, force=force, pre_process=pre_process, reset_permissions=reset_permissions)
  40. def import_file(module, dt, dn, force=False, pre_process=None, reset_permissions=False):
  41. """Sync a file from txt if modifed, return false if not updated"""
  42. path = get_file_path(module, dt, dn)
  43. ret = import_file_by_path(path, force, pre_process=pre_process, reset_permissions=reset_permissions)
  44. return ret
  45. def get_file_path(module, dt, dn):
  46. dt, dn = scrub_dt_dn(dt, dn)
  47. path = os.path.join(get_module_path(module), os.path.join(dt, dn, f"{dn}.json"))
  48. return path
  49. def import_file_by_path(path: str,force: bool = False,data_import: bool = False,pre_process = None,ignore_version: bool = None,reset_permissions: bool = False):
  50. """Import file from the given path
  51. Some conditions decide if a file should be imported or not.
  52. Evaluation takes place in the order they are mentioned below.
  53. - Check if `force` is true. Import the file. If not, move ahead.
  54. - Get `db_modified_timestamp`(value of the modified field in the database for the file).
  55. If the return is `none,` this file doesn't exist in the DB, so Import the file. If not, move ahead.
  56. - Check if there is a hash in DB for that file. If there is, Calculate the Hash of the file to import and compare it with the one in DB if they are not equal.
  57. Import the file. If Hash doesn't exist, move ahead.
  58. - Check if `db_modified_timestamp` is older than the timestamp in the file; if it is, we import the file.
  59. If timestamp comparison happens for doctypes, that means the Hash for it doesn't exist.
  60. So, even if the timestamp is newer on DB (When comparing timestamps), we import the file and add the calculated Hash to the DB.
  61. So in the subsequent imports, we can use hashes to compare. As a precautionary measure, the timestamp is updated to the current time as well.
  62. Args:
  63. path (str): Path to the file.
  64. force (bool, optional): Load the file without checking any conditions. Defaults to False.
  65. data_import (bool, optional): [description]. Defaults to False.
  66. pre_process ([type], optional): Any preprocesing that may need to take place on the doc. Defaults to None.
  67. ignore_version (bool, optional): ignore current version. Defaults to None.
  68. reset_permissions (bool, optional): reset permissions for the file. Defaults to False.
  69. Returns:
  70. [bool]: True if import takes place. False if it wasn't imported.
  71. """
  72. frappe.flags.dt = frappe.flags.dt or []
  73. try:
  74. docs = read_doc_from_file(path)
  75. except IOError:
  76. print(f"{path} missing")
  77. return
  78. calculated_hash = caclulate_hash(path)
  79. if docs:
  80. if not isinstance(docs, list):
  81. docs = [docs]
  82. for doc in docs:
  83. # modified timestamp in db, none if doctype's first import
  84. db_modified_timestamp = frappe.db.get_value(doc["doctype"], doc["name"], "modified")
  85. is_db_timestamp_latest = db_modified_timestamp and (
  86. get_datetime(doc.get("modified")) <= get_datetime(db_modified_timestamp)
  87. )
  88. if not force or db_modified_timestamp:
  89. try:
  90. stored_hash = None
  91. if doc["doctype"] == "DocType":
  92. stored_hash = frappe.db.get_value(doc["doctype"], doc["name"], "migration_hash")
  93. except Exception:
  94. frappe.flags.dt += [doc["doctype"]]
  95. # if hash exists and is equal no need to update
  96. if stored_hash and stored_hash == calculated_hash:
  97. continue
  98. # if hash doesn't exist, check if db timestamp is same as json timestamp, add hash if from doctype
  99. if is_db_timestamp_latest and doc["doctype"] != "DocType":
  100. continue
  101. import_doc(
  102. docdict=doc,
  103. force=force,
  104. data_import=data_import,
  105. pre_process=pre_process,
  106. ignore_version=ignore_version,
  107. reset_permissions=reset_permissions,
  108. path=path,
  109. )
  110. if doc["doctype"] == "DocType":
  111. doctype_table = DocType("DocType")
  112. frappe.qb.update(
  113. doctype_table
  114. ).set(
  115. doctype_table.migration_hash, calculated_hash
  116. ).where(
  117. doctype_table.name == doc["name"]
  118. ).run()
  119. new_modified_timestamp = doc.get("modified")
  120. # if db timestamp is newer, hash must have changed, must update db timestamp
  121. if is_db_timestamp_latest and doc["doctype"] == "DocType":
  122. new_modified_timestamp = now()
  123. if new_modified_timestamp:
  124. update_modified(new_modified_timestamp, doc)
  125. return True
  126. def is_timestamp_changed(doc):
  127. # check if timestamps match
  128. db_modified = frappe.db.get_value(doc["doctype"], doc["name"], "modified")
  129. return not (db_modified and get_datetime(doc.get("modified")) == get_datetime(db_modified))
  130. def read_doc_from_file(path):
  131. doc = None
  132. if os.path.exists(path):
  133. with open(path, "r") as f:
  134. try:
  135. doc = json.loads(f.read())
  136. except ValueError:
  137. print("bad json: {0}".format(path))
  138. raise
  139. else:
  140. raise IOError("%s missing" % path)
  141. return doc
  142. def update_modified(original_modified, doc):
  143. # since there is a new timestamp on the file, update timestamp in
  144. if doc["doctype"] == doc["name"] and doc["name"] != "DocType":
  145. singles_table = DocType("Singles")
  146. frappe.qb.update(
  147. singles_table
  148. ).set(
  149. singles_table.value,original_modified
  150. ).where(
  151. singles_table["field"] == "modified", # singles_table.field is a method of pypika Selectable
  152. ).where(
  153. singles_table.doctype == doc["name"]
  154. ).run()
  155. else:
  156. doctype_table = DocType(doc['doctype'])
  157. frappe.qb.update(doctype_table
  158. ).set(
  159. doctype_table.modified, original_modified
  160. ).where(
  161. doctype_table.name == doc["name"]
  162. ).run()
  163. def import_doc(docdict, force=False, data_import=False, pre_process=None, ignore_version=None, reset_permissions=False, path=None):
  164. frappe.flags.in_import = True
  165. docdict["__islocal"] = 1
  166. controller = get_controller(docdict["doctype"])
  167. if controller and hasattr(controller, "prepare_for_import") and callable(getattr(controller, "prepare_for_import")):
  168. controller.prepare_for_import(docdict)
  169. doc = frappe.get_doc(docdict)
  170. reset_tree_properties(doc)
  171. load_code_properties(doc, path)
  172. doc.run_method("before_import")
  173. doc.flags.ignore_version = ignore_version
  174. if pre_process:
  175. pre_process(doc)
  176. if frappe.db.exists(doc.doctype, doc.name):
  177. delete_old_doc(doc, reset_permissions)
  178. doc.flags.ignore_links = True
  179. if not data_import:
  180. doc.flags.ignore_validate = True
  181. doc.flags.ignore_permissions = True
  182. doc.flags.ignore_mandatory = True
  183. doc.insert()
  184. frappe.flags.in_import = False
  185. return doc
  186. def load_code_properties(doc, path):
  187. """Load code files stored in separate files with extensions"""
  188. if path:
  189. if hasattr(doc, "get_code_fields"):
  190. dirname, filename = os.path.split(path)
  191. for key, extn in doc.get_code_fields().items():
  192. codefile = os.path.join(dirname, filename.split(".")[0] + "." + extn)
  193. if os.path.exists(codefile):
  194. with open(codefile, "r") as txtfile:
  195. doc.set(key, txtfile.read())
  196. def delete_old_doc(doc, reset_permissions):
  197. ignore = []
  198. old_doc = frappe.get_doc(doc.doctype, doc.name)
  199. if doc.doctype in ignore_values:
  200. # update ignore values
  201. for key in ignore_values.get(doc.doctype) or []:
  202. doc.set(key, old_doc.get(key))
  203. # update ignored docs into new doc
  204. for df in doc.meta.get_table_fields():
  205. if df.options in ignore_doctypes and not reset_permissions:
  206. doc.set(df.fieldname, [])
  207. ignore.append(df.options)
  208. # delete old
  209. frappe.delete_doc(doc.doctype, doc.name, force=1, ignore_doctypes=ignore, for_reload=True)
  210. doc.flags.ignore_children_type = ignore
  211. def reset_tree_properties(doc):
  212. # Note on Tree DocTypes:
  213. # The tree structure is maintained in the database via the fields "lft" and
  214. # "rgt". They are automatically set and kept up-to-date. Importing them
  215. # would destroy any existing tree structure.
  216. if getattr(doc.meta, "is_tree", None) and any([doc.lft, doc.rgt]):
  217. print('Ignoring values of `lft` and `rgt` for {} "{}"'.format(doc.doctype, doc.name))
  218. doc.lft = None
  219. doc.rgt = None