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.

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