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

182 行
5.9 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import frappe, os
  5. def sync_statics(rebuild=False):
  6. s = sync()
  7. s.verbose = True
  8. s.start(rebuild)
  9. frappe.db.commit()
  10. # while True:
  11. # s.start(rebuild)
  12. # frappe.db.commit()
  13. # time.sleep(2)
  14. # rebuild = False
  15. class sync(object):
  16. def __init__(self, verbose=False, path=None):
  17. self.verbose = verbose
  18. def start(self, rebuild=False, path="www"):
  19. self.path = path
  20. self.synced = []
  21. self.synced_paths = []
  22. self.updated = 0
  23. if rebuild:
  24. frappe.db.sql("delete from `tabWeb Page` where ifnull(template_path, '')!=''")
  25. for app in frappe.get_installed_apps():
  26. # print "Syncing for {0}".format(app)
  27. self.sync_for_app(app)
  28. self.cleanup()
  29. def sync_for_app(self, app):
  30. self.statics_path = frappe.get_app_path(app, self.path)
  31. if os.path.exists(self.statics_path):
  32. for basepath, folders, files in os.walk(self.statics_path):
  33. self.sync_folder(basepath, folders, files, app)
  34. def sync_folder(self, basepath, folders, files, app):
  35. self.get_index_txt(basepath, files)
  36. index_found = self.sync_index_page(basepath, files, app)
  37. if not index_found and basepath!=self.statics_path:
  38. # not synced either by generator or by index.html
  39. return
  40. if self.index:
  41. self.sync_using_given_index(basepath, folders, files, app)
  42. else:
  43. self.sync_alphabetically(basepath, folders, [filename for filename in files if filename.endswith('html') or filename.endswith('md')], app)
  44. def get_index_txt(self, basepath, files):
  45. self.index = []
  46. if "index.txt" in files:
  47. with open(os.path.join(basepath, "index.txt"), "r") as indexfile:
  48. self.index = indexfile.read().splitlines()
  49. def sync_index_page(self, basepath, files, app):
  50. for extn in ("md", "html"):
  51. fname = "index." + extn
  52. if fname in files:
  53. self.sync_file(fname, os.path.join(basepath, fname), None, app)
  54. return True
  55. def sync_using_given_index(self, basepath, folders, files, app):
  56. for i, page_name in enumerate(self.index):
  57. if page_name in folders:
  58. # for folder, sync inner index first (so that idx is set)
  59. for extn in ("md", "html"):
  60. path = os.path.join(basepath, page_name, "index." + extn)
  61. if os.path.exists(path):
  62. self.sync_file("index." + extn, path, i, app)
  63. break
  64. # other files
  65. if page_name + ".md" in files:
  66. self.sync_file(page_name + ".md", os.path.join(basepath, page_name + ".md"), i, app)
  67. elif page_name + ".html" in files:
  68. self.sync_file(page_name + ".html", os.path.join(basepath, page_name + ".html"), i, app)
  69. else:
  70. if page_name not in folders:
  71. print page_name + " not found in " + basepath
  72. def sync_alphabetically(self, basepath, folders, files, app):
  73. files.sort()
  74. for fname in files:
  75. page_name = fname.rsplit(".", 1)[0]
  76. if not (page_name=="index" and basepath!=self.statics_path):
  77. self.sync_file(fname, os.path.join(basepath, fname), None, app)
  78. def sync_file(self, fname, template_path, priority, app):
  79. route = os.path.relpath(template_path, self.statics_path).rsplit(".", 1)[0]
  80. if fname.rsplit(".", 1)[0]=="index" and \
  81. os.path.dirname(template_path) != self.statics_path:
  82. route = os.path.dirname(route)
  83. parent_web_page = frappe.db.sql("""select name from `tabWeb Page` where
  84. page_name=%s and ifnull(parent_website_route, '')=ifnull(%s, '')""",
  85. (os.path.basename(os.path.dirname(route)), os.path.dirname(os.path.dirname(route))))
  86. parent_web_page = parent_web_page and parent_web_page[0][0] or ""
  87. page_name = os.path.basename(route)
  88. published = 1
  89. idx = priority
  90. if (parent_web_page, page_name) in self.synced:
  91. return
  92. with open(template_path, "r") as f:
  93. content = unicode(f.read().strip(), "utf-8")
  94. title = self.get_title(template_path, content)
  95. relative_template_path = os.path.join(app, os.path.relpath(template_path, frappe.get_app_path(app)))
  96. if not frappe.db.get_value("Web Page", {"template_path":relative_template_path}):
  97. web_page = frappe.new_doc("Web Page")
  98. web_page.page_name = page_name
  99. web_page.parent_web_page = parent_web_page
  100. web_page.template_path = relative_template_path
  101. web_page.main_section = content
  102. web_page.title = title
  103. web_page.published = published
  104. web_page.idx = idx
  105. web_page.from_website_sync = True
  106. web_page.insert()
  107. if self.verbose: print "Inserted: " + web_page.name
  108. else:
  109. web_page = frappe.get_doc("Web Page", {"template_path":relative_template_path})
  110. dirty = False
  111. for key in ("parent_web_page", "title", "published", "idx"):
  112. if web_page.get(key) != locals().get(key):
  113. web_page.set(key, locals().get(key))
  114. dirty = True
  115. if web_page.template_path != relative_template_path:
  116. web_page.template_path = relative_template_path
  117. dirty = True
  118. if dirty:
  119. web_page.from_website_sync = True
  120. web_page.save()
  121. if self.verbose: print "Updated: " + web_page.name
  122. self.synced.append((parent_web_page, page_name))
  123. def get_title(self, fpath, content):
  124. title = os.path.basename(fpath).rsplit(".", 1)[0]
  125. if title =="index":
  126. title = os.path.basename(os.path.dirname(fpath))
  127. title = title.replace("-", " ").replace("_", " ").title()
  128. if content.startswith("# "):
  129. title = content.splitlines()[0][2:]
  130. if "<!-- title:" in content:
  131. title = content.split("<!-- title:", 1)[1].split("-->", 1)[0].strip()
  132. return title
  133. def cleanup(self):
  134. if self.synced:
  135. # delete static web pages that are not in immediate list
  136. for static_page in frappe.db.sql("""select name, page_name, parent_web_page
  137. from `tabWeb Page` where ifnull(template_path,'')!=''""", as_dict=1):
  138. if (static_page.parent_web_page, static_page.page_name) not in self.synced:
  139. frappe.delete_doc("Web Page", static_page.name, force=1)
  140. else:
  141. # delete all static web pages
  142. frappe.delete_doc("Web Page", frappe.db.sql_list("""select name
  143. from `tabWeb Page`
  144. where ifnull(template_path,'')!=''"""), force=1)