A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

build.py 4.6 KiB

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