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