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.

sync.py 3.7 KiB

13 vuotta sitten
13 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. from webnotes.modules import reload_doc
  10. def sync_all(force=0):
  11. sync_for("lib", force)
  12. sync_for("app", force)
  13. webnotes.clear_cache()
  14. def sync_for(folder, force=0, sync_everything = False):
  15. return walk_and_sync(os.path.join(os.path.dirname(os.path.abspath(conf.__file__)),
  16. folder), force, sync_everything)
  17. def walk_and_sync(start_path, force=0, sync_everything = False):
  18. """walk and sync all doctypes and pages"""
  19. modules = []
  20. document_type = ['doctype', 'page', 'report']
  21. for path, folders, files in os.walk(start_path):
  22. if sync_everything or (os.path.basename(os.path.dirname(path)) in document_type):
  23. for f in files:
  24. if f.endswith(".txt"):
  25. doc_name = f.split(".txt")[0]
  26. if doc_name == os.path.basename(path):
  27. module_name = path.split(os.sep)[-3]
  28. doctype = path.split(os.sep)[-2]
  29. name = path.split(os.sep)[-1]
  30. if doctype == 'doctype':
  31. sync_doctype(module_name, name, force)
  32. else:
  33. if reload_doc(module_name, doctype, name, force):
  34. print module_name + ' | ' + doctype + ' | ' + name
  35. return modules
  36. # docname in small letters with underscores
  37. def sync_doctype(module_name, docname, force=0):
  38. """sync doctype from file if modified"""
  39. with open(get_file_path(module_name, docname), 'r') as f:
  40. from webnotes.modules.utils import peval_doclist
  41. doclist = peval_doclist(f.read())
  42. if merge_doctype(doclist, force):
  43. print module_name, '|', docname
  44. #raise Exception
  45. return doclist[0].get('name')
  46. def merge_doctype(doclist, force=False):
  47. modified = doclist[0]['modified']
  48. if not doclist:
  49. raise Exception('Bean could not be evaluated')
  50. db_modified = str(webnotes.conn.get_value(doclist[0].get('doctype'),
  51. doclist[0].get('name'), 'modified'))
  52. if modified == db_modified and not force:
  53. return
  54. webnotes.conn.begin()
  55. delete_doctype_docfields(doclist)
  56. save_doctype_docfields(doclist)
  57. save_perms_if_none_exist(doclist)
  58. webnotes.conn.sql("""UPDATE `tab{doctype}`
  59. SET modified=%s WHERE name=%s""".format(doctype=doclist[0]['doctype']),
  60. (modified, doclist[0]['name']))
  61. webnotes.conn.commit()
  62. return True
  63. def get_file_path(module_name, docname):
  64. if not (module_name and docname):
  65. raise Exception('No Module Name or DocName specified')
  66. module = __import__(module_name)
  67. module_init_path = os.path.abspath(module.__file__)
  68. module_path = os.sep.join(module_init_path.split(os.sep)[:-1])
  69. return os.sep.join([module_path, 'doctype', docname, docname + '.txt'])
  70. def delete_doctype_docfields(doclist):
  71. parent = doclist[0].get('name')
  72. if not parent: raise Exception('Could not determine parent')
  73. webnotes.conn.sql("DELETE FROM `tabDocType` WHERE name=%s", parent)
  74. webnotes.conn.sql("DELETE FROM `tabDocField` WHERE parent=%s", parent)
  75. def save_doctype_docfields(doclist):
  76. from webnotes.model.doc import Document
  77. parent_doc = Document(fielddata=doclist[0])
  78. parent_doc.save(1, check_links=0,
  79. ignore_fields=1)
  80. idx = 1
  81. for d in doclist:
  82. if d.get('doctype') != 'DocField': continue
  83. d['idx'] = idx
  84. Document(fielddata=d).save(1, check_links=0, ignore_fields=1)
  85. idx += 1
  86. update_schema(parent_doc.name)
  87. def update_schema(docname):
  88. from webnotes.model.db_schema import updatedb
  89. updatedb(docname)
  90. webnotes.clear_cache(doctype=docname)
  91. def save_perms_if_none_exist(doclist):
  92. if not webnotes.conn.sql("""select count(*) from tabDocPerm
  93. where parent=%s""", doclist[0].name)[0][0]:
  94. for d in doclist:
  95. if d.get('doctype') != 'DocPerm': continue
  96. webnotes.doc(fielddata=d).save(1, check_links=0, ignore_fields=1)