|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- #! /usr/bin/env python
- # -*- coding: utf-8 -*-
-
- import logging
- import os
- import shutil
- import subprocess
-
- page_src = """#! /data/project/copyvios/env/bin/python
- # -*- coding: utf-8 -*-
- import os
- import sys
-
- os.chdir("..")
- sys.path.insert(0, ".")
-
- from mako.template import Template
- from mako.lookup import TemplateLookup
-
- from copyvios.cookies import parse_cookies
-
- def main(environ, start_response):
- lookup = TemplateLookup(directories=["{{pages_dir}}"],
- input_encoding="utf8")
- template = Template(filename="{{src}}", module_directory="{{temp_dir}}",
- lookup=lookup, format_exceptions=True)
- headers = [("Content-Type", "text/html")]
- cookies = parse_cookies(environ)
- page = template.render(environ=environ, headers=headers,
- cookies=cookies).encode("utf8")
- start_response("200 OK", headers)
- return [page]
-
- if __name__ == "__main__":
- from flup.server.fcgi import WSGIServer
- WSGIServer(main).run()
- """
-
- class Builder(object):
- def __init__(self):
- self.build_dir = "www"
- self.static_dir = "static"
- self.pages_dir = "pages"
- self.support_dir = "pages/support"
- self.temp_dir = "temp"
-
- self.root = logging.getLogger("builder")
- self.root.addHandler(logging.NullHandler())
- self._pages = []
-
- def _enable_logging(self):
- handler = logging.StreamHandler()
- handler.setFormatter(_LogFormatter())
- self.root.addHandler(handler)
- self.root.setLevel(logging.DEBUG)
-
- def _replace_file(self, name, program, logger):
- logger.debug("{0} {1}".format(program, name))
- replacement = subprocess.check_output([program, name])
- os.remove(name)
- with open(name, "w") as fp:
- fp.write(replacement)
-
- def _gen_page(self, page, base):
- if not page.endswith(".mako"):
- base.warn("Skipping {0} (not endswith('.mako'))".format(page))
- return
-
- logger = base.getChild(page.rsplit(".", 1)[0])
- self._pages.append(page.rsplit(".", 1)[0])
- src = os.path.join(self.pages_dir, page)
- dest = os.path.join(self.build_dir, page.replace(".mako", ".py"))
-
- logger.debug("build {0} -> {1}".format(src, dest))
- content = page_src.replace("{{src}}", src)
- content = content.replace("{{pages_dir}}", self.pages_dir)
- content = content.replace("{{temp_dir}}", self.temp_dir)
- with open(dest, "w") as fp:
- fp.write(content)
-
- logger.debug("chmod 0755 {0}".format(dest))
- os.chmod(dest, 0755)
-
- def clean(self):
- logger = self.root.getChild("clean")
- targets = (self.build_dir, self.temp_dir)
-
- for target in targets:
- if os.path.exists(target):
- logger.debug("rm -r {0}".format(target))
- shutil.rmtree(target)
-
- logger.debug("mkdir {0}".format(target))
- os.mkdir(target)
-
- def gen_static(self):
- logger = self.root.getChild("static")
- dest = os.path.join(self.build_dir, "static")
-
- logger.debug("copytree {0} -> {1}".format(self.static_dir, dest))
- shutil.copytree(self.static_dir, dest)
- for dirpath, dirnames, filenames in os.walk(dest):
- for filename in filenames:
- name = os.path.join(dirpath, filename)
- if name.endswith(".js"):
- self._replace_file(name, "uglifyjs", logger)
- elif name.endswith(".css"):
- self._replace_file(name, "uglifycss", logger)
-
- def gen_pages(self):
- logger = self.root.getChild("pages")
- pages = os.listdir(self.pages_dir)
- for page in pages:
- if not os.path.isfile(os.path.join(self.pages_dir, page)):
- continue
- self._gen_page(page, logger)
-
- def build(self):
- self._enable_logging()
- self.root.info("Building project...")
- self.clean()
- self.gen_static()
- self.gen_pages()
- self.root.info("Done!")
-
-
- class _LogFormatter(logging.Formatter):
- def __init__(self):
- fmt = "[%(asctime)s %(lvl)s] %(name)s: %(message)s"
- datefmt = "%Y-%m-%d %H:%M:%S"
- _format = super(_LogFormatter, self).format
- self.format = lambda record: _format(self.colorize(record))
- super(_LogFormatter, self).__init__(fmt=fmt, datefmt=datefmt)
-
- def colorize(self, record):
- l = record.levelname.ljust(8)
- if record.levelno == logging.DEBUG:
- record.lvl = l.join(("\x1b[34m", "\x1b[0m")) # Blue
- if record.levelno == logging.INFO:
- record.lvl = l.join(("\x1b[32m", "\x1b[0m")) # Green
- if record.levelno == logging.WARNING:
- record.lvl = l.join(("\x1b[33m", "\x1b[0m")) # Yellow
- if record.levelno == logging.ERROR:
- record.lvl = l.join(("\x1b[31m", "\x1b[0m")) # Red
- if record.levelno == logging.CRITICAL:
- record.lvl = l.join(("\x1b[1m\x1b[31m", "\x1b[0m")) # Bold red
- return record
-
-
- if __name__ == "__main__":
- Builder().build()
|