| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import httplib |
| 7 import optparse |
| 8 import os |
| 9 import re |
| 10 import sys |
| 11 import socket |
| 12 import subprocess |
| 13 |
| 14 __author__ = 'laforge/nkang' |
| 15 |
| 16 class ChromeCheckout: |
| 17 """A class to check out Chrome source files based on revision number.""" |
| 18 |
| 19 def __init__(self, version, dest=None): |
| 20 # Verify that he version has the correct format. |
| 21 self._version = ((lambda l: str(l[0]) if len(l) > 0 else '') |
| 22 ((lambda v: re.findall('\S+\.\S+\.\S+\.\S+', v) |
| 23 if(type(v) == str) else [])(version)))
|
| 24 self._dest = dest |
| 25 self._base_svn_url = 'svn://svn.chromium.org/chrome' |
| 26 self._selenium_url = 'http://selenium.googlecode.com/svn/trunk/py' |
| 27 self._pyftpdlib_url = 'http://pyftpdlib.googlecode.com/svn/trunk' |
| 28 self._deps_cache = {} |
| 29 self._GetPath = (lambda par, ch: os.path.join(par, ch) if par else ch) |
| 30 # By default the destination is the CWD, but if a different directory was |
| 31 # specified, make sure it exists. If not, create it. |
| 32 if self._dest: |
| 33 if not os.path.isdir(self._dest): |
| 34 try: |
| 35 os.mkdir(self._dest) |
| 36 # Failed to create test directory, CWD will be used for checkout. |
| 37 except(OSError, IOError), err: |
| 38 print 'ChromeCheckout.__init__: Failed to create %s. Checkout '\ |
| 39 'will be done in the current working directory.' % self._dest |
| 40 self._dest = None |
| 41 |
| 42 def _GetContentAndReturnResponse(self, server, path, content): |
| 43 """Gets contents of DEPS, which contains info about source files needed. |
| 44 |
| 45 Args: |
| 46 server: Host address, in this case the SVN server. |
| 47 path: The URL where the file is located. |
| 48 content: A string of data to send after the headers are finished. By |
| 49 default its an empty string and should be left that way. |
| 50 |
| 51 Returns: |
| 52 A string containing the file's contents if successful, otherwise None. |
| 53 """ |
| 54 try: |
| 55 conn = httplib.HTTPConnection(server) |
| 56 headers = {"Content-type": "text/html"} |
| 57 conn.request('GET', path, content, headers) |
| 58 response = conn.getresponse() |
| 59 except socket.gaierror, err: |
| 60 print 'ChromeCheckout._GetContentAndReturnResponse: %s' % err |
| 61 conn.close() |
| 62 return None |
| 63 if response.status != 200: |
| 64 return None |
| 65 data = response.read() |
| 66 buff = '' |
| 67 for line in data: |
| 68 buff += line |
| 69 conn.close() |
| 70 return buff |
| 71 |
| 72 def _GetDeps(self, version): |
| 73 """Returns cached version of DEPS if possible, otherwise gets its contents.
|
| 74 |
| 75 Args: |
| 76 version: Chrome version number (e.g. - 21.0.1136.0). |
| 77 |
| 78 Returns: |
| 79 string: A string buffer containing the contents of the DEPS file. |
| 80 """ |
| 81 if not version: |
| 82 return None |
| 83 if version in self._deps_cache: |
| 84 return self._deps_cache[version] |
| 85 deps = self._GetContentAndReturnResponse('src.chromium.org', |
| 86 '/viewvc/chrome/releases/%s/DEPS' |
| 87 % version, '') |
| 88 self._deps_cache[version] = deps |
| 89 return deps |
| 90 |
| 91 def _ParseVersion(self, version): |
| 92 """Parses the version number to get the different identifiers.""" |
| 93 match = re.search(r'((\d+)\.(\d+)\.(\d+)\.(\d+))', version) |
| 94 if match: |
| 95 version = {'version': match.group(1), |
| 96 'major': int(match.group(2)), |
| 97 'minor': int(match.group(3)), |
| 98 'build': int(match.group(4)), |
| 99 'patch': int(match.group(5))} |
| 100 return version |
| 101 return {} |
| 102 |
| 103 def _GetRevisionInfo(self, version_str): |
| 104 """Gets the revision info by parsing the contents of the DEPS file. |
| 105 |
| 106 Args: |
| 107 version_str: A string representing the Chrome version number. |
| 108 |
| 109 Returns: |
| 110 A string that contains pertinent information about the Chrome version. |
| 111 """ |
| 112 version = self._ParseVersion(version_str) |
| 113 if version: |
| 114 body = self._GetDeps(version['version']) |
| 115 if body: |
| 116 match = re.search("'src':[\n\r ]+'(.*?)'", body) |
| 117 if match: |
| 118 match = re.search(r"@(\d+)", match.group(1)) |
| 119 if match: |
| 120 version['revision'] = int(match.group(1)) |
| 121 |
| 122 match = re.search("""['"]src['"].*?:.*?['"]/branches/(.*?)/.*?,""", |
| 123 body, re.DOTALL | re.IGNORECASE) |
| 124 if match: |
| 125 version['branch'] = match.group(1) |
| 126 else: |
| 127 version['branch'] = 'trunk' |
| 128 return version |
| 129 |
| 130 def _GetRevision(self, version_str, rev_type='selenium'): |
| 131 """Gets selenium/pyftpdlib rev. number by parsing contents of DEPS file. |
| 132 |
| 133 Args: |
| 134 version_str: A string representing the Chrome build number. |
| 135 rev_type: Type of revision number to look up - Selenium or Pyftpdlib. |
| 136 |
| 137 Returns: |
| 138 A string representing the revision no. if successful, otherwise None. |
| 139 """ |
| 140 version = self._ParseVersion(version_str) |
| 141 if version: |
| 142 body = self._GetDeps(version_str) |
| 143 if body: |
| 144 if rev_type == 'selenium': |
| 145 m = re.search(r"http://selenium\.googlecode\.com/svn/trunk/py@(\d+)", |
| 146 body, re.DOTALL | re.IGNORECASE | re.MULTILINE) |
| 147 elif rev_type == 'pyftpdlib': |
| 148 m = re.search(r"http://pyftpdlib\.googlecode\.com/svn/trunk@(\d+)", |
| 149 body, re.DOTALL | re.IGNORECASE | re.MULTILINE) |
| 150 if m: |
| 151 return int(m.group(1)) |
| 152 return None |
| 153 |
| 154 def _SvnCo(self, path, revision=None, dest=None): |
| 155 """Does a SVN checkout on specified source files. |
| 156 |
| 157 Args: |
| 158 path: URL that is to be checked out. |
| 159 revision: Revision number. |
| 160 dest: Destination where the data will be downloaded. |
| 161 """ |
| 162 cmd = "svn co" |
| 163 if revision: |
| 164 cmd += " --revision %d" % revision |
| 165 cmd += " %s" % path |
| 166 if dest: |
| 167 cmd += " %s" % dest |
| 168 print cmd |
| 169 return subprocess.Popen.wait(subprocess.Popen(cmd, shell=True)) |
| 170 |
| 171 def CheckOut(self): |
| 172 """Checks out all necessary source files.""" |
| 173 # No version was specified. |
| 174 if not self._version: |
| 175 print 'Please specify a Chrome version to checkout.' |
| 176 return -1 |
| 177 rev_info = self._GetRevisionInfo(self._version) |
| 178 print rev_info |
| 179 # If its a patch, check out the branch. |
| 180 if rev_info['patch']: |
| 181 svn_url_base = self._base_svn_url + '/branches/%s' % rev_info['branch'] |
| 182 # If not, check out the trunk. |
| 183 else: |
| 184 svn_url_base = self._base_svn_url + '/trunk' |
| 185 self._SvnCo('%s/src/chrome/test/functional' % svn_url_base, |
| 186 rev_info['revision'], self._GetPath(self._dest, 'functional')) |
| 187 self._SvnCo('%s/src/chrome/test/pyautolib' % svn_url_base, |
| 188 rev_info['revision'], self._GetPath(self._dest, 'pyautolib')) |
| 189 self._SvnCo('%s/src/third_party/simplejson' % svn_url_base, |
| 190 rev_info['revision'], self._GetPath(self._dest, 'simplejson')) |
| 191 self._SvnCo('%s/src/third_party/tlslite' % svn_url_base, |
| 192 rev_info['revision'], self._GetPath(self._dest, 'tlslite')) |
| 193 self._SvnCo('%s/src/net/tools/testserver' % svn_url_base, |
| 194 rev_info['revision'], self._GetPath(self._dest, 'testserver'))
|
| 195 self._SvnCo(self._selenium_url, self._GetRevision( |
| 196 self._version, 'selenium'), self._GetPath(self._dest, 'selenium')) |
| 197 self._SvnCo(self._pyftpdlib_url, self._GetRevision( |
| 198 self._version, 'pyftpdlib'), self._GetPath(self._dest, 'pyftpdlib')) |
| 199 return 0 |
| 200 |
| OLD | NEW |