A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

152 Zeilen
5.0 KiB

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