Bug 1293253 - part 7 - factor out a Cargo.toml-for-context parsing function; r=chmanchester
authorNathan Froyd <froydnj@mozilla.com>
Mon, 28 Nov 2016 11:20:39 -0500
changeset 324484 fcc7e0fd607c0ec2270744f26560be13ff4bc0e9
parent 324483 0d91224e1389d87439ee8af1f1f0d398049aaa3c
child 324485 b3505dd036cfa0db0c3a2c2047c1b3b6b458566e
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewerschmanchester
bugs1293253
milestone53.0a1
Bug 1293253 - part 7 - factor out a Cargo.toml-for-context parsing function; r=chmanchester We'll need a function to do this for Rust program definitions, so we might as well use one that's already there for Rust library definitions.
python/mozbuild/mozbuild/frontend/emitter.py
--- a/python/mozbuild/mozbuild/frontend/emitter.py
+++ b/python/mozbuild/mozbuild/frontend/emitter.py
@@ -433,20 +433,26 @@ class TreeMetadataEmitter(LoggingMixin):
         # context more or less truthful about where the external library is.
         context = Context(config=self.config)
         context.add_source(mozpath.join(self.config.topsrcdir, dir, 'dummy'))
         if force_static:
             return ExternalStaticLibrary(context, name)
         else:
             return ExternalSharedLibrary(context, name)
 
-    def _parse_cargo_file(self, toml_file):
-        """Parse toml_file and return a Python object representation of it."""
-        with open(toml_file, 'r') as f:
-            return pytoml.load(f)
+    def _parse_cargo_file(self, context):
+        """Parse the Cargo.toml file in context and return a Python object
+        representation of it.  Raise a SandboxValidationError if the Cargo.toml
+        file does not exist.  Return a tuple of (config, cargo_file)."""
+        cargo_file = mozpath.join(context.srcdir, 'Cargo.toml')
+        if not os.path.exists(cargo_file):
+            raise SandboxValidationError(
+                'No Cargo.toml file found in %s' % cargo_file, context)
+        with open(cargo_file, 'r') as f:
+            return pytoml.load(f), cargo_file
 
     def _verify_deps(self, context, crate_dir, crate_name, dependencies, description='Dependency'):
         """Verify that a crate's dependencies all specify local paths."""
         for dep_crate_name, values in dependencies.iteritems():
             # A simple version number.
             if isinstance(values, (str, unicode)):
                 raise SandboxValidationError(
                     '%s %s of crate %s does not list a path' % (description, dep_crate_name, crate_name),
@@ -467,22 +473,17 @@ class TreeMetadataEmitter(LoggingMixin):
 
             if not os.path.exists(mozpath.join(context.config.topsrcdir, crate_dir, dep_path)):
                 raise SandboxValidationError(
                     '%s %s of crate %s refers to a non-existent path' % (description, dep_crate_name, crate_name),
                     context)
 
     def _rust_library(self, context, libname, static_args):
         # We need to note any Rust library for linking purposes.
-        cargo_file = mozpath.join(context.srcdir, 'Cargo.toml')
-        if not os.path.exists(cargo_file):
-            raise SandboxValidationError(
-                'No Cargo.toml file found in %s' % cargo_file, context)
-
-        config = self._parse_cargo_file(cargo_file)
+        config, cargo_file = self._parse_cargo_file(context)
         crate_name = config['package']['name']
 
         if crate_name != libname:
             raise SandboxValidationError(
                 'library %s does not match Cargo.toml-defined package %s' % (libname, crate_name),
                 context)
 
         # Check that the [lib.crate-type] field is correct