Merge list_properties.py into build_properties_rs.py

This commit is contained in:
Simon Sapin 2016-04-18 15:55:36 +02:00
parent eda711307c
commit b7eb720c74
5 changed files with 84 additions and 84 deletions

View file

@ -3,10 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::process::{Command, Stdio, exit};
use std::process::{Command, exit};
#[cfg(windows)]
fn find_python() -> String {
@ -31,25 +28,13 @@ fn find_python() -> String {
}
fn main() {
let python = match env::var("PYTHON") {
Ok(python_path) => python_path,
Err(_) => find_python(),
};
let style = Path::new(file!()).parent().unwrap();
let mako = style.join("Mako-0.9.1.zip");
let template = style.join("properties.mako.rs");
let python = env::var("PYTHON").ok().unwrap_or_else(find_python);
let product = if cfg!(feature = "gecko") { "gecko" } else { "servo" };
let result = Command::new(python)
.env("PYTHONPATH", &mako)
.env("TEMPLATE", &template)
.env("PRODUCT", product)
.arg("generate_properties_rs.py")
.stderr(Stdio::inherit())
.output()
let status = Command::new(python)
.args(&["generate_properties_rs.py", product, "rust"])
.status()
.unwrap();
if !result.status.success() {
if !status.success() {
exit(1)
}
let out = env::var("OUT_DIR").unwrap();
File::create(&Path::new(&out).join("properties.rs")).unwrap().write_all(&result.stdout).unwrap();
}

View file

@ -2,16 +2,67 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import json
import os.path
import re
import sys
BASE = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(BASE, "Mako-0.9.1.zip"))
from mako import exceptions
from mako.template import Template
try:
template = Template(open(os.environ['TEMPLATE'], 'rb').read(),
input_encoding='utf8')
print(template.render(PRODUCT=os.environ['PRODUCT']).encode('utf8'))
except:
sys.stderr.write(exceptions.text_error_template().render().encode('utf8'))
def main():
usage = "Usage: %s [ servo | gecko ] [ rust | html ]" % sys.argv[0]
if len(sys.argv) < 3:
abort(usage)
product = sys.argv[1]
output = sys.argv[2]
if product not in ["servo", "gecko"] or output not in ["rust", "html"]:
abort(usage)
template, rust = render("properties.mako.rs", PRODUCT=product)
if output == "rust":
write(os.environ["OUT_DIR"], "properties.rs", rust)
elif output == "html":
write_html(template)
def abort(message):
sys.stderr.write(message + b"\n")
sys.exit(1)
def render(name, **context):
try:
template = Template(open(os.path.join(BASE, name), "rb").read(), input_encoding="utf8")
return template, template.render(**context).encode("utf8")
except:
abort(exceptions.text_error_template().render().encode("utf8"))
def write(directory, filename, content):
if not os.path.exists(directory):
os.makedirs(directory)
open(os.path.join(directory, filename), "wb").write(content)
def write_html(template):
properties = dict(
(p.name, {
"flag": p.experimental,
"shorthand": hasattr(p, "sub_properties")
})
for p in template.module.LONGHANDS + template.module.SHORTHANDS
)
_, html = render("properties.html.mako", properties=properties)
doc_servo = os.path.join(BASE, "..", "..", "target", "doc", "servo")
write(doc_servo, "css-properties.json", json.dumps(properties, indent=4))
write(doc_servo, "css-properties.html", html)
if __name__ == "__main__":
main()

View file

@ -1,41 +0,0 @@
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os.path
import sys
import json
style = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(style, "Mako-0.9.1.zip"))
from mako.template import Template
template = Template(filename=os.path.join(style, "properties.mako.rs"), input_encoding='utf8')
template.render(PRODUCT='servo')
properties = dict(
(p.name, {
"flag": p.experimental,
"shorthand": hasattr(p, "sub_properties")
})
for p in template.module.LONGHANDS + template.module.SHORTHANDS
)
json_dump = json.dumps(properties, indent=4)
#
# Resolve path to doc directory and write CSS properties and JSON.
#
servo_doc_path = os.path.abspath(os.path.join(style, '../', '../', 'target', 'doc', 'servo'))
# Ensure ./target/doc/servo exists
if not os.path.exists(servo_doc_path):
os.makedirs(servo_doc_path)
with open(os.path.join(servo_doc_path, 'css-properties.json'), "w") as out_file:
out_file.write(json_dump)
html_template = Template(filename=os.path.join(style, "properties.html.mako"), input_encoding='utf8')
with open(os.path.join(servo_doc_path, 'css-properties.html'), "w") as out_file:
out_file.write(html_template.render(properties=properties))

View file

@ -12,7 +12,7 @@ cd "$(dirname $0)/../.."
# etc/doc.servo.org/index.html overwrites $(mach rust-root)/doc/index.html
cp etc/doc.servo.org/* target/doc/
python components/style/list_properties.py
python components/style/build_properties_rs.py servo html
ghp-import -n target/doc
git push -qf https://${TOKEN}@github.com/servo/doc.servo.org.git gh-pages

View file

@ -141,20 +141,7 @@ class MachCommands(CommandBase):
@CommandArgument('test_name', nargs=argparse.REMAINDER,
help="Only run tests that match this pattern or file path")
def test_unit(self, test_name=None, package=None):
subprocess.check_output([
sys.executable,
path.join(self.context.topdir, "components", "style", "list_properties.py")
])
this_file = os.path.dirname(__file__)
servo_doc_path = os.path.abspath(os.path.join(this_file, '../', '../', 'target', 'doc', 'servo'))
with open(os.path.join(servo_doc_path, 'css-properties.json'), 'r') as property_file:
properties = json.loads(property_file.read())
assert len(properties) >= 100
assert "margin-top" in properties
assert "margin" in properties
check_css_properties_json(self.context.topdir)
if test_name is None:
test_name = []
@ -670,3 +657,21 @@ testing/web-platform/mozilla/tests for Servo-only tests""" % reference_path)
if editor:
proc.wait()
def check_css_properties_json(topdir):
filename = path.join(topdir, "target", "doc", "servo", "css-properties.json")
if path.exists(filename):
os.remove(filename)
subprocess.check_call([
sys.executable,
path.join(topdir, "components", "style", "build_properties_rs.py"),
"servo",
"html",
])
properties = json.load(open(filename))
assert len(properties) >= 100
assert "margin-top" in properties
assert "margin" in properties