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.

build.py 4.8 KiB

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