A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. template = Template(filename="{{src}}", module_directory="{{temp_dir}}",
  16. lookup=lookup, format_exceptions=True)
  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 = "pages/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("{{pages_dir}}", self.pages_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. if not os.path.isfile(os.path.join(self.pages_dir, page)):
  79. continue
  80. self._gen_page(page, logger)
  81. def gen_zws(self):
  82. logger = self.root.getChild("zws")
  83. target = self.rs_file
  84. if os.path.exists(target):
  85. logger.debug("rm {0}".format(target))
  86. os.remove(target)
  87. logger.debug("build rewrite.script")
  88. with open(target, "w") as fp:
  89. for page in self._pages:
  90. fp.write(rewrite_script_src.format(page))
  91. def build(self):
  92. self._enable_logging()
  93. self.root.info("Building project...")
  94. self.clean()
  95. self.gen_static()
  96. self.gen_pages()
  97. self.gen_zws()
  98. self.root.info("Done!")
  99. class _LogFormatter(logging.Formatter):
  100. def __init__(self):
  101. fmt = "[%(asctime)s %(lvl)s] %(name)s: %(message)s"
  102. datefmt = "%Y-%m-%d %H:%M:%S"
  103. _format = super(_LogFormatter, self).format
  104. self.format = lambda record: _format(self.colorize(record))
  105. super(_LogFormatter, self).__init__(fmt=fmt, datefmt=datefmt)
  106. def colorize(self, record):
  107. l = record.levelname.ljust(8)
  108. if record.levelno == logging.DEBUG:
  109. record.lvl = l.join(("\x1b[34m", "\x1b[0m")) # Blue
  110. if record.levelno == logging.INFO:
  111. record.lvl = l.join(("\x1b[32m", "\x1b[0m")) # Green
  112. if record.levelno == logging.WARNING:
  113. record.lvl = l.join(("\x1b[33m", "\x1b[0m")) # Yellow
  114. if record.levelno == logging.ERROR:
  115. record.lvl = l.join(("\x1b[31m", "\x1b[0m")) # Red
  116. if record.levelno == logging.CRITICAL:
  117. record.lvl = l.join(("\x1b[1m\x1b[31m", "\x1b[0m")) # Bold red
  118. return record
  119. if __name__ == "__main__":
  120. Builder().build()