diff --git a/python/mozbuild/mozbuild/frontend/emitter.py b/python/mozbuild/mozbuild/frontend/emitter.py index 2e0498e3f6a8..bdfa6acd49e5 100644 --- a/python/mozbuild/mozbuild/frontend/emitter.py +++ b/python/mozbuild/mozbuild/frontend/emitter.py @@ -110,18 +110,6 @@ class TreeMetadataEmitter(LoggingMixin): self.populate_logger() self.config = config - - mozinfo.find_and_update_from_json(config.topobjdir) - - # Python 2.6 doesn't allow unicode keys to be used for keyword - # arguments. This gross hack works around the problem until we - # rid ourselves of 2.6. - self.info = {} - for k, v in mozinfo.info.items(): - if isinstance(k, unicode): - k = k.encode('ascii') - self.info[k] = v - self._libs = OrderedDefaultDict(list) self._binaries = OrderedDict() self._compile_dirs = set() diff --git a/testing/mozbase/mozinfo/mozinfo/mozinfo.py b/testing/mozbase/mozinfo/mozinfo/mozinfo.py index 9cff8a777f8e..5d934a7f0c80 100755 --- a/testing/mozbase/mozinfo/mozinfo/mozinfo.py +++ b/testing/mozbase/mozinfo/mozinfo/mozinfo.py @@ -183,13 +183,14 @@ def sanitize(info): def update(new_info): - """ - Update the info. + """Adds information from json file to the global symbol table. + + Given the parameter new_info, this method will look for the file. + If found, the file is read into a buffer and assumed to be json. :param new_info: Either a dict containing the new info or a path/url to a json file containing the new info. """ - PY3 = sys.version_info[0] == 3 if PY3: string_types = str, @@ -215,16 +216,29 @@ def update(new_info): globals()['isUnix'] = True -def find_and_update_from_json(*dirs): - """ - Find a mozinfo.json file, load it, and update the info with the - contents. +def find_and_update_from_json(*dirs, **kwargs): + """Find a mozinfo.json file, load it, and update global symbol table. - :param dirs: Directories in which to look for the file. They will be - searched after first looking in the root of the objdir - if the current script is being run from a Mozilla objdir. + This method will first check the relevant objdir directory for the + necessary mozinfo.json file, if the current script is being run from a + Mozilla objdir. - Returns the full path to mozinfo.json if it was found, or None otherwise. + If the objdir directory did not supply the necessary data, this method + will then look for the required mozinfo.json file from the provided + tuple of directories. + + If file is found, the global symbols table is updated via a helper method. + + If no valid files are found, an exception is raised. + + :param tuple dirs: Directories in which to look for the file. + :param dict kwargs: optional values: + raise_exception: if this value is provided, the default + behavior of raising an exception is + overridden. + :returns: EnvironmentError: default behavior. + None: if exception raising is suppressed. + json_path: string representation of path. """ # First, see if we're in an objdir try: @@ -240,6 +254,8 @@ def find_and_update_from_json(*dirs): except (BuildEnvironmentNotFoundException, MozconfigFindException): pass + raise_exception = kwargs.get('raise_exception', True) + for d in dirs: d = _os.path.abspath(d) json_path = _os.path.join(d, "mozinfo.json") @@ -247,7 +263,10 @@ def find_and_update_from_json(*dirs): update(json_path) return json_path - return None + if raise_exception: + raise EnvironmentError('{}: could not find any mozinfo.json.'.format(__name__)) + else: + return None def output_to_file(path): diff --git a/testing/mozbase/mozinfo/tests/test.py b/testing/mozbase/mozinfo/tests/test.py index b3d4e3efbe85..013c279848ff 100644 --- a/testing/mozbase/mozinfo/tests/test.py +++ b/testing/mozbase/mozinfo/tests/test.py @@ -75,6 +75,13 @@ class TestMozinfo(unittest.TestCase): self.assertEqual(mozinfo.find_and_update_from_json(self.tempdir), j) self.assertEqual(mozinfo.info["foo"], "abcdefg") + def test_find_and_update_file_no_argument(self): + """Test that mozinfo.find_and_update_from_json can + handle not being given any arguments. + """ + with self.assertRaises(EnvironmentError): + self.assertEqual(mozinfo.find_and_update_from_json()) + def test_find_and_update_file_invalid_json(self): """Test that mozinfo.find_and_update_from_json can handle invalid JSON""" @@ -83,6 +90,42 @@ class TestMozinfo(unittest.TestCase): f.write('invalid{"json":') self.assertRaises(ValueError, mozinfo.find_and_update_from_json, self.tempdir) + def test_find_and_update_file_raise_exception(self): + """Test that mozinfo.find_and_update_from_json raises + an IOError exception if a True boolean value is + provided as the only argument. + """ + with self.assertRaises(EnvironmentError): + mozinfo.find_and_update_from_json(raise_exception=True) + + def test_find_and_update_file_raise_exception_multiple_arguments(self): + """Test that mozinfo.find_and_update_from_json raises + an IOError exception if a True boolean value is + provided as last positional argument. + """ + with self.assertRaises(EnvironmentError): + mozinfo.find_and_update_from_json(self.tempdir, raise_exception=True) + + def test_find_and_update_file_suppress_exception(self): + """Test that mozinfo.find_and_update_from_json suppresses + an IOError exception if a False boolean value is + provided as the only argument. + """ + self.assertEqual(mozinfo.find_and_update_from_json( + raise_exception=False), None) + + def test_find_and_update_file_suppress_exception_multiple_arguments(self): + """Test that mozinfo.find_and_update_from_json suppresses + an IOError exception if a False boolean value is + provided as last positional argument. + """ + j = os.path.join(self.tempdir, "mozinfo.json") + with open(j, "w") as f: + f.write(json.dumps({"foo": "abcdefg"})) + self.assertEqual(mozinfo.find_and_update_from_json( + self.tempdir, raise_exception=False), j) + self.assertEqual(mozinfo.info["foo"], "abcdefg") + def test_find_and_update_file_mozbuild(self): """Test that mozinfo.find_and_update_from_json can find mozinfo.json using the mozbuild module."""