A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
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.
 
 
 
 
 

150 lines
4.8 KiB

  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import logging
  4. import os
  5. import shutil
  6. page_src = """#! /usr/bin/env python
  7. # -*- coding: utf-8 -*-
  8. import os
  9. os.chdir("..")
  10. from mako.template import Template
  11. from mako.lookup import TemplateLookup
  12. def myapp(environ, start_response):
  13. start_response("200 OK", [("Content-Type", "text/html")])
  14. lookup = TemplateLookup(directories=["{{pages_dir}}"],
  15. input_encoding="utf8")
  16. template = Template(filename="{{src}}", module_directory="{{temp_dir}}",
  17. lookup=lookup, format_exceptions=True)
  18. return [template.render(environ=environ).encode("utf8")]
  19. if __name__ == "__main__":
  20. from flup.server.fcgi import WSGIServer
  21. WSGIServer(myapp).run()
  22. """
  23. rewrite_script_src = """match URL into $ with ^/~earwig/{0}(\?.*?)?$
  24. if matched then
  25. set URL = /~earwig/rewrite/{0}.fcgi$1
  26. goto END
  27. endif
  28. """
  29. class Builder(object):
  30. def __init__(self):
  31. self.build_dir = "www"
  32. self.static_dir = "static"
  33. self.pages_dir = "pages"
  34. self.support_dir = "pages/support"
  35. self.temp_dir = "temp"
  36. self.rs_file = "rewrite.script"
  37. self.root = logging.getLogger("builder")
  38. self.root.addHandler(logging.NullHandler())
  39. self._pages = []
  40. def _enable_logging(self):
  41. handler = logging.StreamHandler()
  42. handler.setFormatter(_LogFormatter())
  43. self.root.addHandler(handler)
  44. self.root.setLevel(logging.DEBUG)
  45. def _gen_page(self, page, base):
  46. if not page.endswith(".mako"):
  47. base.warn("Skipping {0} (not endswith('.mako'))".format(page))
  48. return
  49. logger = base.getChild(page.rsplit(".", 1)[0])
  50. self._pages.append(page.rsplit(".", 1)[0])
  51. src = os.path.join(self.pages_dir, page)
  52. dest = os.path.join(self.build_dir, page.replace(".mako", ".fcgi"))
  53. logger.debug("build {0} -> {1}".format(src, dest))
  54. content = page_src.replace("{{src}}", src)
  55. content = content.replace("{{pages_dir}}", self.pages_dir)
  56. content = content.replace("{{temp_dir}}", self.temp_dir)
  57. with open(dest, "w") as fp:
  58. fp.write(content)
  59. logger.debug("chmod 0755 {0}".format(dest))
  60. os.chmod(dest, 0755)
  61. def clean(self):
  62. logger = self.root.getChild("clean")
  63. targets = (self.build_dir, self.temp_dir)
  64. for target in targets:
  65. if os.path.exists(target):
  66. logger.debug("rm -r {0}".format(target))
  67. shutil.rmtree(target)
  68. logger.debug("mkdir {0}".format(target))
  69. os.mkdir(target)
  70. def gen_static(self):
  71. logger = self.root.getChild("static")
  72. dest = os.path.join(self.build_dir, "static")
  73. logger.debug("copytree {0} -> {1}".format(self.static_dir, dest))
  74. shutil.copytree(self.static_dir, dest)
  75. def gen_pages(self):
  76. logger = self.root.getChild("pages")
  77. pages = os.listdir(self.pages_dir)
  78. for page in pages:
  79. if not os.path.isfile(os.path.join(self.pages_dir, page)):
  80. continue
  81. self._gen_page(page, logger)
  82. def gen_zws(self):
  83. logger = self.root.getChild("zws")
  84. target = self.rs_file
  85. if os.path.exists(target):
  86. logger.debug("rm {0}".format(target))
  87. os.remove(target)
  88. logger.debug("build rewrite.script")
  89. with open(target, "w") as fp:
  90. for page in self._pages:
  91. fp.write(rewrite_script_src.format(page))
  92. def build(self):
  93. self._enable_logging()
  94. self.root.info("Building project...")
  95. self.clean()
  96. self.gen_static()
  97. self.gen_pages()
  98. self.gen_zws()
  99. self.root.info("Done!")
  100. class _LogFormatter(logging.Formatter):
  101. def __init__(self):
  102. fmt = "[%(asctime)s %(lvl)s] %(name)s: %(message)s"
  103. datefmt = "%Y-%m-%d %H:%M:%S"
  104. _format = super(_LogFormatter, self).format
  105. self.format = lambda record: _format(self.colorize(record))
  106. super(_LogFormatter, self).__init__(fmt=fmt, datefmt=datefmt)
  107. def colorize(self, record):
  108. l = record.levelname.ljust(8)
  109. if record.levelno == logging.DEBUG:
  110. record.lvl = l.join(("\x1b[34m", "\x1b[0m")) # Blue
  111. if record.levelno == logging.INFO:
  112. record.lvl = l.join(("\x1b[32m", "\x1b[0m")) # Green
  113. if record.levelno == logging.WARNING:
  114. record.lvl = l.join(("\x1b[33m", "\x1b[0m")) # Yellow
  115. if record.levelno == logging.ERROR:
  116. record.lvl = l.join(("\x1b[31m", "\x1b[0m")) # Red
  117. if record.levelno == logging.CRITICAL:
  118. record.lvl = l.join(("\x1b[1m\x1b[31m", "\x1b[0m")) # Bold red
  119. return record
  120. if __name__ == "__main__":
  121. Builder().build()