You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

168 line
4.8 KiB

  1. from __future__ import unicode_literals
  2. """
  3. Sync's doctype and docfields from txt files to database
  4. perms will get synced only if none exist
  5. """
  6. import webnotes
  7. import os
  8. import conf
  9. def sync_all(force=0):
  10. modules = []
  11. modules += sync_core_doctypes(force)
  12. modules += sync_modules(force)
  13. try:
  14. webnotes.clear_cache()
  15. except Exception, e:
  16. if e[0]!=1146: raise e
  17. return modules
  18. def sync_core_doctypes(force=0):
  19. # doctypes
  20. return walk_and_sync(os.path.join(os.path.dirname(os.path.abspath(conf.__file__)), 'lib'), force)
  21. def sync_modules(force=0):
  22. return walk_and_sync(os.path.join(os.path.dirname(os.path.abspath(conf.__file__)), 'app'), force)
  23. def walk_and_sync(start_path, force=0):
  24. """walk and sync all doctypes and pages"""
  25. from webnotes.modules import reload_doc
  26. modules = []
  27. document_type = ['page', 'workflow', 'module_def', 'report', 'workflow_state', 'workflow_action']
  28. for path, folders, files in os.walk(start_path):
  29. if os.path.basename(os.path.dirname(path)) in (['doctype'] + document_type):
  30. for f in files:
  31. if f.endswith(".txt"):
  32. # great grand-parent folder is module_name
  33. module_name = path.split(os.sep)[-3]
  34. if not module_name in modules:
  35. modules.append(module_name)
  36. # grand parent folder is doctype
  37. doctype = path.split(os.sep)[-2]
  38. # parent folder is the name
  39. name = path.split(os.sep)[-1]
  40. if doctype == 'doctype':
  41. sync(module_name, name, force)
  42. elif doctype in document_type:
  43. if reload_doc(module_name, doctype, name, force):
  44. print module_name + ' | ' + doctype + ' | ' + name
  45. return modules
  46. # docname in small letters with underscores
  47. def sync(module_name, docname, force=0):
  48. """sync doctype from file if modified"""
  49. with open(get_file_path(module_name, docname), 'r') as f:
  50. from webnotes.modules.utils import peval_doclist
  51. doclist = peval_doclist(f.read())
  52. if merge_doctype(doclist, force):
  53. print module_name, '|', docname
  54. #raise Exception
  55. return doclist[0].get('name')
  56. def merge_doctype(doclist, force=False):
  57. modified = doclist[0]['modified']
  58. if not doclist:
  59. raise Exception('ModelWrapper could not be evaluated')
  60. db_modified = str(webnotes.conn.get_value(doclist[0].get('doctype'),
  61. doclist[0].get('name'), 'modified'))
  62. if modified == db_modified and not force:
  63. return
  64. webnotes.conn.begin()
  65. delete_doctype_docfields(doclist)
  66. save_doctype_docfields(doclist)
  67. save_perms_if_none_exist(doclist)
  68. webnotes.conn.sql("""UPDATE `tab{doctype}`
  69. SET modified=%s WHERE name=%s""".format(doctype=doclist[0]['doctype']),
  70. (modified, doclist[0]['name']))
  71. webnotes.conn.commit()
  72. return True
  73. def get_file_path(module_name, docname):
  74. if not (module_name and docname):
  75. raise Exception('No Module Name or DocName specified')
  76. module = __import__(module_name)
  77. module_init_path = os.path.abspath(module.__file__)
  78. module_path = os.sep.join(module_init_path.split(os.sep)[:-1])
  79. return os.sep.join([module_path, 'doctype', docname, docname + '.txt'])
  80. def delete_doctype_docfields(doclist):
  81. parent = doclist[0].get('name')
  82. if not parent: raise Exception('Could not determine parent')
  83. webnotes.conn.sql("DELETE FROM `tabDocType` WHERE name=%s", parent)
  84. webnotes.conn.sql("DELETE FROM `tabDocField` WHERE parent=%s", parent)
  85. def save_doctype_docfields(doclist):
  86. from webnotes.model.doc import Document
  87. parent_doc = Document(fielddata=doclist[0])
  88. parent_doc.save(1, check_links=0,
  89. ignore_fields=1)
  90. idx = 1
  91. for d in doclist:
  92. if d.get('doctype') != 'DocField': continue
  93. d['idx'] = idx
  94. Document(fielddata=d).save(1, check_links=0, ignore_fields=1)
  95. idx += 1
  96. update_schema(parent_doc.name)
  97. def update_schema(docname):
  98. from webnotes.model.db_schema import updatedb
  99. updatedb(docname)
  100. webnotes.clear_cache(doctype=docname)
  101. def save_perms_if_none_exist(doclist):
  102. if not webnotes.conn.sql("""select count(*) from tabDocPerm
  103. where parent=%s""", doclist[0].name)[0][0]:
  104. for d in doclist:
  105. if d.get('doctype') != 'DocPerm': continue
  106. webnotes.doc(fielddata=d).save(1, check_links=0, ignore_fields=1)
  107. def sync_install(force=1):
  108. # sync all doctypes
  109. modules = sync_all(force)
  110. # load install docs
  111. load_install_docs(modules)
  112. def load_install_docs(modules):
  113. if isinstance(modules, basestring): modules = [modules]
  114. for module_name in modules:
  115. module = __import__(module_name)
  116. if hasattr(module, 'install_docs'):
  117. webnotes.conn.begin()
  118. for data in module.install_docs:
  119. if data.get('name'):
  120. if not webnotes.conn.exists(data['doctype'], data.get('name')):
  121. create_doc(data)
  122. elif not webnotes.conn.exists(data):
  123. create_doc(data)
  124. webnotes.conn.commit()
  125. if hasattr(module, 'module_init'):
  126. module.module_init()
  127. def create_doc(data):
  128. from webnotes.model.doc import Document
  129. d = Document(data['doctype'])
  130. d.fields.update(data)
  131. d.save()
  132. print 'Created %(doctype)s %(name)s' % d.fields