Index: tools/jsondiff.py |
diff --git a/tools/jsondiff.py b/tools/jsondiff.py |
index dd89c6d8dc7d54f6a37ff872d7d0d9bba37e8c57..76958c6428db15291a9a2152c99597830d4977ae 100755 |
--- a/tools/jsondiff.py |
+++ b/tools/jsondiff.py |
@@ -48,14 +48,17 @@ class GMDiffer(object): |
def _GetFileContentsAsString(self, filepath): |
"""Returns the full contents of a file, as a single string. |
- If the filename looks like a URL, download its contents...""" |
- if filepath.startswith('http:') or filepath.startswith('https:'): |
+ If the filename looks like a URL, download its contents. |
+ If the filename is None, return None.""" |
+ if filepath is None: |
+ return None |
+ elif filepath.startswith('http:') or filepath.startswith('https:'): |
return urllib2.urlopen(filepath).read() |
else: |
return open(filepath, 'r').read() |
- def _GetExpectedResults(self, filepath): |
- """Returns the dictionary of expected results from a JSON file, |
+ def _GetExpectedResults(self, contents): |
+ """Returns the dictionary of expected results from a JSON string, |
in this form: |
{ |
@@ -75,7 +78,6 @@ class GMDiffer(object): |
returned dictionary. |
""" |
result_dict = {} |
- contents = self._GetFileContentsAsString(filepath) |
json_dict = gm_json.LoadFromString(contents) |
all_expectations = json_dict[gm_json.JSONKEY_EXPECTEDRESULTS] |
for test_name in all_expectations.keys(): |
@@ -86,18 +88,18 @@ class GMDiffer(object): |
num_allowed_digests = len(allowed_digests) |
if num_allowed_digests > 1: |
raise ValueError( |
- 'test %s in file %s has %d allowed digests' % ( |
- test_name, filepath, num_allowed_digests)) |
+ 'test %s has %d allowed digests' % ( |
+ test_name, num_allowed_digests)) |
digest_pair = allowed_digests[0] |
if digest_pair[0] != gm_json.JSONKEY_HASHTYPE_BITMAP_64BITMD5: |
raise ValueError( |
- 'test %s in file %s has unsupported hashtype %s' % ( |
- test_name, filepath, digest_pair[0])) |
+ 'test %s has unsupported hashtype %s' % ( |
+ test_name, digest_pair[0])) |
result_dict[test_name] = digest_pair[1] |
return result_dict |
- def _GetActualResults(self, filepath): |
- """Returns the dictionary of actual results from a JSON file, |
+ def _GetActualResults(self, contents): |
+ """Returns the dictionary of actual results from a JSON string, |
in this form: |
{ |
@@ -116,7 +118,6 @@ class GMDiffer(object): |
returned dictionary. |
""" |
result_dict = {} |
- contents = self._GetFileContentsAsString(filepath) |
json_dict = gm_json.LoadFromString(contents) |
all_result_types = json_dict[gm_json.JSONKEY_ACTUALRESULTS] |
for result_type in all_result_types.keys(): |
@@ -126,8 +127,8 @@ class GMDiffer(object): |
digest_pair = results_of_this_type[test_name] |
if digest_pair[0] != gm_json.JSONKEY_HASHTYPE_BITMAP_64BITMD5: |
raise ValueError( |
- 'test %s in file %s has unsupported hashtype %s' % ( |
- test_name, filepath, digest_pair[0])) |
+ 'test %s has unsupported hashtype %s' % ( |
+ test_name, digest_pair[0])) |
result_dict[test_name] = digest_pair[1] |
return result_dict |
@@ -152,11 +153,22 @@ class GMDiffer(object): |
If newfile is not specified, then 'new' is the actual results within |
oldfile. |
""" |
- old_results = self._GetExpectedResults(oldfile) |
- if newfile: |
- new_results = self._GetExpectedResults(newfile) |
+ return self.GenerateDiffDictFromStrings(self._GetFileContentsAsString(oldfile), |
+ self._GetFileContentsAsString(newfile)) |
+ |
+ def GenerateDiffDictFromStrings(self, oldjson, newjson=None): |
+ """Generate a dictionary showing the diffs: |
+ old = expectations within oldjson |
+ new = expectations within newjson |
+ |
+ If newfile is not specified, then 'new' is the actual results within |
+ oldfile. |
+ """ |
+ old_results = self._GetExpectedResults(oldjson) |
+ if newjson: |
+ new_results = self._GetExpectedResults(newjson) |
else: |
- new_results = self._GetActualResults(oldfile) |
+ new_results = self._GetActualResults(oldjson) |
return self._DictionaryDiff(old_results, new_results) |