Skip to content

Commit

Permalink
Bug 1696251: Allow mach commands as stand-alone functions and adapt e…
Browse files Browse the repository at this point in the history
…xisting commands. r=mhentges,webdriver-reviewers,perftest-reviewers,sparky,whimboo

This removes the `@CommandProvider` decorator and the need to implement
mach commands inside subclasses of `MachCommandBase`, and moves all
existing commands out from classes to module level functions.

Differential Revision: https://phabricator.services.mozilla.com/D121512
  • Loading branch information
alopezz committed Sep 21, 2021
1 parent 78a6181 commit a8e7083
Show file tree
Hide file tree
Showing 67 changed files with 13,103 additions and 13,501 deletions.
422 changes: 209 additions & 213 deletions build/valgrind/mach_commands.py

Large diffs are not rendered by default.

148 changes: 73 additions & 75 deletions devtools/shared/css/generated/mach_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
from mozbuild import shellutil
from mozbuild.base import (
MozbuildObject,
MachCommandBase,
BinaryNotFoundException,
)
from mach.decorators import (
CommandProvider,
Command,
)

Expand All @@ -38,87 +36,87 @@ def stringify(obj):
return json.dumps(obj, sort_keys=True, indent=2, separators=(",", ": "))


@CommandProvider
class MachCommands(MachCommandBase):
@Command(
"devtools-css-db",
category="post-build",
description="Rebuild the devtool's static css properties database.",
@Command(
"devtools-css-db",
category="post-build",
description="Rebuild the devtool's static css properties database.",
)
def generate_css_db(command_context):
"""Generate the static css properties database for devtools and write it to file."""

print("Re-generating the css properties database...")
db = get_properties_db_from_xpcshell(command_context)
if not db:
return 1

output_template(
command_context,
{
"preferences": stringify(db["preferences"]),
"cssProperties": stringify(db["cssProperties"]),
"pseudoElements": stringify(db["pseudoElements"]),
},
)
def generate_css_db(self, command_context):
"""Generate the static css properties database for devtools and write it to file."""

print("Re-generating the css properties database...")
db = self.get_properties_db_from_xpcshell(command_context)
if not db:
return 1

self.output_template(
command_context,
{
"preferences": stringify(db["preferences"]),
"cssProperties": stringify(db["cssProperties"]),
"pseudoElements": stringify(db["pseudoElements"]),
},
)

def get_properties_db_from_xpcshell(self, command_context):
"""Generate the static css properties db for devtools from an xpcshell script."""
build = MozbuildObject.from_environment()

# Get the paths
script_path = resolve_path(
command_context.topsrcdir,
"devtools/shared/css/generated/generate-properties-db.js",
def get_properties_db_from_xpcshell(command_context):
"""Generate the static css properties db for devtools from an xpcshell script."""
build = MozbuildObject.from_environment()

# Get the paths
script_path = resolve_path(
command_context.topsrcdir,
"devtools/shared/css/generated/generate-properties-db.js",
)
gre_path = resolve_path(command_context.topobjdir, "dist/bin")
browser_path = resolve_path(command_context.topobjdir, "dist/bin/browser")
try:
xpcshell_path = build.get_binary_path(what="xpcshell")
except BinaryNotFoundException as e:
command_context.log(
logging.ERROR, "devtools-css-db", {"error": str(e)}, "ERROR: {error}"
)
gre_path = resolve_path(command_context.topobjdir, "dist/bin")
browser_path = resolve_path(command_context.topobjdir, "dist/bin/browser")
try:
xpcshell_path = build.get_binary_path(what="xpcshell")
except BinaryNotFoundException as e:
command_context.log(
logging.ERROR, "devtools-css-db", {"error": str(e)}, "ERROR: {error}"
)
command_context.log(
logging.INFO, "devtools-css-db", {"help": e.help()}, "{help}"
)
return None

print(browser_path)

sub_env = dict(os.environ)
if sys.platform.startswith("linux"):
sub_env["LD_LIBRARY_PATH"] = gre_path

# Run the xcpshell script, and set the appdir flag to the browser path so that
# we have the proper dependencies for requiring the loader.
contents = subprocess.check_output(
[xpcshell_path, "-g", gre_path, "-a", browser_path, script_path],
env=sub_env,
command_context.log(
logging.INFO, "devtools-css-db", {"help": e.help()}, "{help}"
)
# Extract just the output between the delimiters as the xpcshell output can
# have extra output that we don't want.
contents = contents.decode().split("DEVTOOLS_CSS_DB_DELIMITER")[1]
return None

return json.loads(contents)
print(browser_path)

def output_template(self, command_context, substitutions):
"""Output a the properties-db.js from a template."""
js_template_path = resolve_path(
command_context.topsrcdir,
"devtools/shared/css/generated/properties-db.js.in",
)
destination_path = resolve_path(
command_context.topsrcdir, "devtools/shared/css/generated/properties-db.js"
)
sub_env = dict(os.environ)
if sys.platform.startswith("linux"):
sub_env["LD_LIBRARY_PATH"] = gre_path

# Run the xcpshell script, and set the appdir flag to the browser path so that
# we have the proper dependencies for requiring the loader.
contents = subprocess.check_output(
[xpcshell_path, "-g", gre_path, "-a", browser_path, script_path],
env=sub_env,
)
# Extract just the output between the delimiters as the xpcshell output can
# have extra output that we don't want.
contents = contents.decode().split("DEVTOOLS_CSS_DB_DELIMITER")[1]

return json.loads(contents)


def output_template(command_context, substitutions):
"""Output a the properties-db.js from a template."""
js_template_path = resolve_path(
command_context.topsrcdir,
"devtools/shared/css/generated/properties-db.js.in",
)
destination_path = resolve_path(
command_context.topsrcdir, "devtools/shared/css/generated/properties-db.js"
)

with open(js_template_path, "rb") as handle:
js_template = handle.read().decode()
with open(js_template_path, "rb") as handle:
js_template = handle.read().decode()

preamble = "/* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT */\n\n"
contents = string.Template(js_template).substitute(substitutions)
preamble = "/* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT */\n\n"
contents = string.Template(js_template).substitute(substitutions)

with open(destination_path, "wb") as destination:
destination.write(preamble.encode() + contents.encode())
with open(destination_path, "wb") as destination:
destination.write(preamble.encode() + contents.encode())

print("The database was successfully generated at " + destination_path)
print("The database was successfully generated at " + destination_path)
95 changes: 45 additions & 50 deletions dom/bindings/mach_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@

from mach.decorators import (
CommandArgument,
CommandProvider,
Command,
)

from mozbuild.base import MachCommandBase
from mozbuild.util import mkdir


Expand All @@ -23,51 +21,48 @@ def get_test_parser():
return runtests.get_parser


@CommandProvider
class WebIDLProvider(MachCommandBase):
@Command(
"webidl-example",
category="misc",
description="Generate example files for a WebIDL interface.",
)
@CommandArgument(
"interface", nargs="+", help="Interface(s) whose examples to generate."
)
def webidl_example(self, command_context, interface):
from mozwebidlcodegen import BuildSystemWebIDL

manager = command_context._spawn(BuildSystemWebIDL).manager
for i in interface:
manager.generate_example_files(i)

@Command(
"webidl-parser-test",
category="testing",
parser=get_test_parser,
description="Run WebIDL tests (Interface Browser parser).",
)
def webidl_test(self, command_context, **kwargs):
sys.path.insert(
0, os.path.join(command_context.topsrcdir, "other-licenses", "ply")
)

# Ensure the topobjdir exists. On a Taskcluster test run there won't be
# an objdir yet.
mkdir(command_context.topobjdir)

# Make sure we drop our cached grammar bits in the objdir, not
# wherever we happen to be running from.
os.chdir(command_context.topobjdir)

if kwargs["verbose"] is None:
kwargs["verbose"] = False

# Now we're going to create the cached grammar file in the
# objdir. But we're going to try loading it as a python
# module, so we need to make sure the objdir is in our search
# path.
sys.path.insert(0, command_context.topobjdir)

import runtests

return runtests.run_tests(kwargs["tests"], verbose=kwargs["verbose"])
@Command(
"webidl-example",
category="misc",
description="Generate example files for a WebIDL interface.",
)
@CommandArgument(
"interface", nargs="+", help="Interface(s) whose examples to generate."
)
def webidl_example(command_context, interface):
from mozwebidlcodegen import BuildSystemWebIDL

manager = command_context._spawn(BuildSystemWebIDL).manager
for i in interface:
manager.generate_example_files(i)


@Command(
"webidl-parser-test",
category="testing",
parser=get_test_parser,
description="Run WebIDL tests (Interface Browser parser).",
)
def webidl_test(command_context, **kwargs):
sys.path.insert(0, os.path.join(command_context.topsrcdir, "other-licenses", "ply"))

# Ensure the topobjdir exists. On a Taskcluster test run there won't be
# an objdir yet.
mkdir(command_context.topobjdir)

# Make sure we drop our cached grammar bits in the objdir, not
# wherever we happen to be running from.
os.chdir(command_context.topobjdir)

if kwargs["verbose"] is None:
kwargs["verbose"] = False

# Now we're going to create the cached grammar file in the
# objdir. But we're going to try loading it as a python
# module, so we need to make sure the objdir is in our search
# path.
sys.path.insert(0, command_context.topobjdir)

import runtests

return runtests.run_tests(kwargs["tests"], verbose=kwargs["verbose"])
Loading

0 comments on commit a8e7083

Please sign in to comment.