Created
August 10, 2012 16:55
-
-
Save danielrichman/3315597 to your computer and use it in GitHub Desktop.
unused genpayload packing (wasn't worth the benefits)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
coffee --join js/genpayload.js --compile coffee/*.coffee && | |
coffee --join js/specs.js --compile spec/*.coffee && | |
python misc/make_test_docs.py && | |
python misc/pack_files.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bs4 import BeautifulSoup | |
from os.path import dirname, join | |
from subprocess import Popen, PIPE | |
import re | |
directory = join(dirname(__file__), "..") | |
def load_html(): | |
with open(join(directory, "genpayload.html")) as f: | |
return BeautifulSoup(f) | |
def add_tag(soup, html): | |
new = BeautifulSoup(html) | |
soup.head.append(new) | |
def dump_html(soup): | |
with open(join(directory, "packed.html"), "w") as f: | |
f.write(str(soup)) | |
def add_notice(soup): | |
add_tag(soup, "<!-- packed.css, packed.js contain dependencies and " + | |
"genpayload code under various open source licenses. " + | |
"See https://github.com/ukhas/genpayload -->\n\n") | |
def replace_scripts(soup): | |
sources = [] | |
for s in list(soup.find_all('script')): | |
sources.append(s["src"]) | |
s.decompose() | |
with open(join(directory, "packed.js"), "w") as out: | |
out.write("/* Concatenated dependencies and genpayload code, please " + | |
"see https://github.com/ukhas/genpayload/tree/master/js */\n\n") | |
out.flush() | |
p = Popen(('uglifyjs', '--no-copyright'), stdout=out, stdin=PIPE) | |
p.stdin.write(concat(sources)) | |
p.stdin.close() | |
p.wait() | |
add_tag(soup, "<script type='text/javascript' src='packed.js'></script>") | |
def replace_css(soup): | |
sources = [] | |
for s in list(soup.find_all('link')): | |
if s["rel"] != ["stylesheet"]: | |
continue | |
sources.append(s["href"]) | |
s.decompose() | |
data = concat(sources) | |
comments = re.compile(r"/\*.*?\*/", flags=re.DOTALL) | |
data = comments.sub(" ", data) | |
data = data.replace("\t", " ") | |
data = re.sub(r"\n+", "\n", data) | |
data = re.sub(r" +", " ", data) | |
# for jquery-ui | |
data = data.replace("url(images/ui-", "url(css/redmond/images/ui-") | |
with open(join(directory, "packed.css"), "w") as out: | |
out.write("/* Concatenated habitat-template, jquery-ui and " + | |
"genpayload css, please see " + | |
"https://github.com/ukhas/genpayload */\n\n") | |
out.write(data) | |
add_tag(soup, "<link rel='stylesheet' href='packed.css' />") | |
def main(): | |
soup = load_html() | |
add_notice(soup) | |
replace_css(soup) | |
replace_scripts(soup) | |
dump_html(soup) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment