Add scm.*.GetEmail() to retrieve the user email. Use this email for try job emails.

TEST=not much
BUG=none

Review URL: http://codereview.chromium.org/399070

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@32720 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/scm.py b/scm.py
index 21363d3..97df0dc 100644
--- a/scm.py
+++ b/scm.py
@@ -70,6 +70,13 @@
         results.append(('%s      ' % m.group(1), m.group(2)))
     return results
 
+  @staticmethod
+  def GetEmail(repo_root):
+    """Retrieves the user email address if known."""
+    # We could want to look at the svn cred when it has a svn remote but it
+    # should be fine for now, users should simply configure their git settings.
+    return GIT.Capture(['config', 'user.email'], repo_root).strip()
+
 
 class SVN(object):
   COMMAND = "svn"
@@ -413,3 +420,57 @@
       data += "@@ -0,0 +1,%d @@\n" % nb_lines
       data += ''.join(file_content)
     return data
+
+  @staticmethod
+  def GetEmail(repo_root):
+    """Retrieves the svn account which we assume is an email address."""
+    infos = SVN.CaptureInfo(repo_root)
+    uuid = infos.get('UUID')
+    root = infos.get('Repository Root')
+    if not root:
+      return None
+
+    # Should check for uuid but it is incorrectly saved for https creds.
+    realm = root.rsplit('/', 1)[0]
+    if root.startswith('https') or not uuid:
+      regexp = re.compile(r'<%s:\d+>.*' % realm)
+    else:
+      regexp = re.compile(r'<%s:\d+> %s' % (realm, uuid))
+    if regexp is None:
+      return None
+    if sys.platform.startswith('win'):
+      if not 'APPDATA' in os.environ:
+        return None
+      auth_dir = os.path.join(os.environ['APPDATA'], 'auth', 'svn.simple')
+    else:
+      if not 'HOME' in os.environ:
+        return None
+      auth_dir = os.path.join(os.environ['HOME'], '.subversion', 'auth',
+                              'svn.simple')
+    for credfile in os.listdir(auth_dir):
+      cred_info = SVN.ReadSimpleAuth(os.path.join(auth_dir, credfile))
+      if regexp.match(cred_info.get('svn:realmstring')):
+        return cred_info.get('username')
+
+  @staticmethod
+  def ReadSimpleAuth(filename):
+    f = open(filename, 'r')
+    values = {}
+    def ReadOneItem(type):
+      m = re.match(r'%s (\d+)' % type, f.readline())
+      if not m:
+        return None
+      data = f.read(int(m.group(1)))
+      if f.read(1) != '\n':
+        return None
+      return data
+
+    while True:
+      key = ReadOneItem('K')
+      if not key:
+        break
+      value = ReadOneItem('V')
+      if not value:
+        break
+      values[key] = value
+    return values