rebaseline_server: use new intermediate JSON format as described in https://goto.google.com/ChangingRbsJson

There are still some places in the frontend (HTML+Javascript) code where
we assume we are handling expectations vs actuals, but there are just a few
and we should be able to remove them easily in a coming CL.
At that point, the frontend will work just as well for displaying any set
of image pairs.

BUG=skia:1919
NOTRY=True
R=rmistry@google.com

Author: epoger@google.com

Review URL: https://codereview.chromium.org/178253010

git-svn-id: http://skia.googlecode.com/svn/trunk@13598 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gm/gm_json.py b/gm/gm_json.py
index 1aac663..6f4b324 100644
--- a/gm/gm_json.py
+++ b/gm/gm_json.py
@@ -15,12 +15,14 @@
 import io
 import json
 import os
+import posixpath
+import re
 
 
 # Key strings used in GM results JSON files (both expected-results.json and
 # actual-results.json).
 #
-# These constants must be kept in sync with the kJsonKey_ constants in
+# NOTE: These constants must be kept in sync with the kJsonKey_ constants in
 # gm_expectations.cpp !
 
 
@@ -93,6 +95,14 @@
 # Pattern used to assemble each image's filename
 IMAGE_FILENAME_PATTERN = '(.+)_(.+)\.png'  # matches (testname, config)
 
+# Pattern used to create image URLs, relative to some base URL.
+GM_RELATIVE_URL_FORMATTER = '%s/%s/%s.png' # pass in (hash_type, test_name,
+                                           #          hash_digest)
+GM_RELATIVE_URL_PATTERN = '(.+)/(.+)/(.+).png' # matches (hash_type, test_name,
+                                               #          hash_digest)
+GM_RELATIVE_URL_RE = re.compile(GM_RELATIVE_URL_PATTERN)
+
+
 def CreateGmActualUrl(test_name, hash_type, hash_digest,
                       gm_actuals_root_url=GM_ACTUALS_ROOT_HTTP_URL):
   """Return the URL we can use to download a particular version of
@@ -104,10 +114,40 @@
   hash_digest: the hash digest of the image to retrieve
   gm_actuals_root_url: root url where actual images are stored
   """
-  # TODO(epoger): Maybe use url_or_path.join() so that, for testing, this can
-  # return either a URL or a local filepath?
-  return '%s/%s/%s/%s.png' % (gm_actuals_root_url, hash_type, test_name,
-                              hash_digest)
+  return posixpath.join(
+      gm_actuals_root_url, CreateGmRelativeUrl(
+          test_name=test_name, hash_type=hash_type, hash_digest=hash_digest))
+
+
+def CreateGmRelativeUrl(test_name, hash_type, hash_digest):
+  """Returns a relative URL pointing at a test result's image.
+
+  Returns the URL we can use to download a particular version of
+  the actually-generated image for this particular GM test,
+  relative to the URL root.
+
+  Args:
+    test_name: name of the test, e.g. 'perlinnoise'
+    hash_type: string indicating the hash type used to generate hash_digest,
+               e.g. JSONKEY_HASHTYPE_BITMAP_64BITMD5
+    hash_digest: the hash digest of the image to retrieve
+  """
+  return GM_RELATIVE_URL_FORMATTER % (hash_type, test_name, hash_digest)
+
+
+def SplitGmRelativeUrl(url):
+  """Splits the relative URL into (test_name, hash_type, hash_digest) tuple.
+
+  This is the inverse of CreateGmRelativeUrl().
+
+  Args:
+    url: a URL generated with CreateGmRelativeUrl().
+
+  Returns: (test_name, hash_type, hash_digest) tuple.
+  """
+  hash_type, test_name, hash_digest = GM_RELATIVE_URL_RE.match(url).groups()
+  return (test_name, hash_type, hash_digest)
+
 
 def LoadFromString(file_contents):
   """Loads the JSON summary written out by the GM tool.
@@ -119,6 +159,7 @@
   json_dict = json.loads(file_contents)
   return json_dict
 
+
 def LoadFromFile(file_path):
   """Loads the JSON summary written out by the GM tool.
      Returns a dictionary keyed by the values listed as JSONKEY_ constants
@@ -126,6 +167,7 @@
   file_contents = open(file_path, 'r').read()
   return LoadFromString(file_contents)
 
+
 def WriteToFile(json_dict, file_path):
   """Writes the JSON summary in json_dict out to file_path.