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.
 
 
 
 
 
 

189 rivejä
5.5 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import frappe, os, time, sys
  5. from frappe.utils import update_progress_bar
  6. # from frappe.website.sitemap import get_route_children, get_next
  7. def sync_statics(rebuild=False):
  8. s = sync()
  9. s.verbose = True
  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):
  17. self.verbose = verbose
  18. def start(self, rebuild=False):
  19. self.synced = []
  20. self.synced_paths = []
  21. self.to_insert = []
  22. self.to_update = []
  23. self.updated = 0
  24. self.rebuild = rebuild
  25. for app in frappe.get_installed_apps():
  26. self.sync_for_app(app)
  27. self.insert_and_update()
  28. self.cleanup()
  29. def sync_for_app(self, app):
  30. self.statics_path = frappe.get_app_path(app, "templates", "statics")
  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)
  34. def sync_folder(self, basepath, folders, files):
  35. self.get_index_txt(basepath, files)
  36. index_found = self.sync_index_page(basepath, files)
  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)
  42. else:
  43. self.sync_alphabetically(basepath, folders, [filename for filename in files if filename.endswith('html') or filename.endswith('md')])
  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):
  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)
  54. return True
  55. def sync_using_given_index(self, basepath, folders, files):
  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)
  63. break
  64. # other files
  65. for extn in ("md", "html"):
  66. fname = page_name + "." + extn
  67. if fname in files:
  68. self.sync_file(fname, os.path.join(basepath, fname), i)
  69. break
  70. elif page_name not in folders:
  71. print page_name + " not found in " + basepath
  72. def sync_alphabetically(self, basepath, folders, files):
  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)
  78. def sync_file(self, fname, template_path, priority):
  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. page_name = os.path.basename(route)
  84. parent_web_page = os.path.basename(os.path.dirname(route))
  85. if page_name in self.synced:
  86. return
  87. title = self.get_title(template_path)
  88. if not frappe.db.exists("Web Page", page_name):
  89. web_page = frappe.new_doc("Web Page")
  90. web_page.page_name = page_name
  91. web_page.parent_web_page = parent_web_page
  92. web_page.template_path = template_path
  93. web_page.title = title
  94. web_page.from_website_sync = True
  95. self.to_insert.append(web_page)
  96. else:
  97. web_page = frappe.get_doc("Web Page", page_name)
  98. dirty = False
  99. for key in ("parent_web_page", "title", "template_path"):
  100. if web_page.get(key) != locals().get(key):
  101. web_page.set(key, locals().get(key))
  102. dirty = True
  103. if dirty:
  104. web_page.from_website_sync = True
  105. self.to_update.append(web_page)
  106. self.synced.append(page_name)
  107. def get_title(self, fpath):
  108. title = os.path.basename(fpath).rsplit(".", 1)[0]
  109. if title =="index":
  110. title = os.path.basename(os.path.dirname(fpath))
  111. title = title.replace("-", " ").replace("_", " ").title()
  112. with open(fpath, "r") as f:
  113. content = unicode(f.read().strip(), "utf-8")
  114. if content.startswith("# "):
  115. title = content.splitlines()[0][2:]
  116. if "<!-- title:" in content:
  117. title = content.split("<!-- title:", 1)[1].split("-->", 1)[0].strip()
  118. return title
  119. def insert_and_update(self):
  120. if self.to_insert:
  121. l = len(self.to_insert)
  122. for i, page in enumerate(self.to_insert):
  123. if self.verbose:
  124. print "Inserting " + page.page_name
  125. else:
  126. update_progress_bar("Updating Static Pages", i, l)
  127. page.insert()
  128. if not self.verbose: print ""
  129. if self.to_update:
  130. for i, page in enumerate(self.to_update):
  131. if not self.verbose:
  132. print "Updating " + page.page_name
  133. else:
  134. sys.stdout.write("\rUpdating statics {0}/{1}".format(i+1, len(self.to_update)))
  135. sys.stdout.flush()
  136. page.save()
  137. if not self.verbose: print ""
  138. def cleanup(self):
  139. if self.synced:
  140. # delete static web pages that are not in immediate list
  141. frappe.delete_doc("Web Page", frappe.db.sql_list("""select name
  142. from `tabWeb Page`
  143. where ifnull(template_path,'')!=''
  144. and name not in ({})""".format(', '.join(["%s"]*len(self.synced))),
  145. tuple(self.synced)), force=1)
  146. else:
  147. # delete all static web pages
  148. frappe.delete_doc("Web Page", frappe.db.sql_list("""select name
  149. from `tabWeb Page`
  150. where ifnull(template_path,'')!=''"""), force=1)