import lxml.html as lh import lxml.etree as et import os import shutil base_path = "/mnt/hd/web/1bpm.net" top_path = os.path.join(base_path, "apps.csound") apps_top_path = os.path.join(top_path, "app") twirl_path = os.path.join(apps_top_path, "twirl") apps = ["twist", "twigs", "twine"] def compile_apps(): for app in apps: compile(app) def compile(app): print "Compiling {}".format(app) app_path = os.path.join(apps_top_path, app) target_dir = "{}/{}".format(base_path, app) doc = lh.parse(os.path.join(app_path, "index.html")) root = doc.getroot() head = root.xpath("//head")[0] script_data = "" css_data = "" post_scripts = [] for script in root.xpath("//script"): src = script.attrib.get("src") if src: src = src.replace("https://apps.csound.1bpm.net/", "../../") path = os.path.join(app_path, src) with open(path, "r") as f: script_data += f.read() + "\n" else: post_scripts.append(script) script.getparent().remove(script) new_script = et.fromstring("".format(app)) head.append(new_script) for ps in post_scripts: head.append(ps) for css in root.xpath("//link"): href = css.attrib.get("href") if href: href = href.replace("https://apps.csound.1bpm.net/", "../../") path = os.path.join(app_path, href) with open(path, "r") as f: css_data += f.read() + "\n" css.getparent().remove(css) new_css = et.fromstring("".format(app)) head.append(new_css) doc.write(os.path.join(target_dir, "index.html"), method="html", encoding="UTF-8") with open(os.path.join(target_dir, "{}.js".format(app)), "w") as f: f.write(script_data) with open(os.path.join(target_dir, "{}.css".format(app)), "w") as f: f.write(css_data) dups = [ [twirl_path, "twirl"], [os.path.join(twirl_path, "font"), "font"], [os.path.join(top_path, "udo"), "udo"], [os.path.join(top_path, "code"), "code"], [os.path.join(app_path, "{}.csd".format(app)), "{}.csd".format(app)] ] for l in dups: target = os.path.join(target_dir, l[1]) if os.path.islink(target) or os.path.isfile(target): os.unlink(target) elif os.path.isdir(target): shutil.rmtree(target) if os.path.isfile(l[0]): shutil.copy2(l[0], target) elif os.path.isdir(l[0]): shutil.copytree(l[0], target) #os.symlink(l[0], target) copies = ["documentation.html", "developer_documentation.html"] for c in copies: item = os.path.join(app_path, c) if os.path.exists(item): target = os.path.join(target_dir, c) if os.path.exists(target): os.unlink(target) shutil.copy(item, target) if __name__ == "__main__": compile_apps()