| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Generic utils.""" | 5 """Generic utils.""" |
| 6 | 6 |
| 7 import codecs | 7 import codecs |
| 8 import logging | 8 import logging |
| 9 import os | 9 import os |
| 10 import pipes | 10 import pipes |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 | 27 |
| 28 class Error(Exception): | 28 class Error(Exception): |
| 29 """gclient exception class.""" | 29 """gclient exception class.""" |
| 30 def __init__(self, msg, *args, **kwargs): | 30 def __init__(self, msg, *args, **kwargs): |
| 31 index = getattr(threading.currentThread(), 'index', 0) | 31 index = getattr(threading.currentThread(), 'index', 0) |
| 32 if index: | 32 if index: |
| 33 msg = '\n'.join('%d> %s' % (index, l) for l in msg.splitlines()) | 33 msg = '\n'.join('%d> %s' % (index, l) for l in msg.splitlines()) |
| 34 super(Error, self).__init__(msg, *args, **kwargs) | 34 super(Error, self).__init__(msg, *args, **kwargs) |
| 35 | 35 |
| 36 |
| 36 def SplitUrlRevision(url): | 37 def SplitUrlRevision(url): |
| 37 """Splits url and returns a two-tuple: url, rev""" | 38 """Splits url and returns a two-tuple: url, rev""" |
| 38 if url.startswith('ssh:'): | 39 if url.startswith('ssh:'): |
| 39 # Make sure ssh://user-name@example.com/~/test.git@stable works | 40 # Make sure ssh://user-name@example.com/~/test.git@stable works |
| 40 regex = r'(ssh://(?:[-.\w]+@)?[-\w:\.]+/[-~\w\./]+)(?:@(.+))?' | 41 regex = r'(ssh://(?:[-.\w]+@)?[-\w:\.]+/[-~\w\./]+)(?:@(.+))?' |
| 41 components = re.search(regex, url).groups() | 42 components = re.search(regex, url).groups() |
| 42 else: | 43 else: |
| 43 components = url.split('@', 1) | 44 components = url.split('@', 1) |
| 44 if len(components) == 1: | 45 if len(components) == 1: |
| 45 components += [None] | 46 components += [None] |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 | 97 |
| 97 | 98 |
| 98 def FileWrite(filename, content, mode='w'): | 99 def FileWrite(filename, content, mode='w'): |
| 99 with codecs.open(filename, mode=mode, encoding='utf-8') as f: | 100 with codecs.open(filename, mode=mode, encoding='utf-8') as f: |
| 100 f.write(content) | 101 f.write(content) |
| 101 | 102 |
| 102 | 103 |
| 103 def safe_rename(old, new): | 104 def safe_rename(old, new): |
| 104 """Renames a file reliably. | 105 """Renames a file reliably. |
| 105 | 106 |
| 106 Sometimes os.rename does not work because a dying git process keeps a handle | 107 Sometimes os.rename does not work because a dying git process keeps a handle |
| 107 on it for a few seconds. An exception is then thrown, which make the program | 108 on it for a few seconds. An exception is then thrown, which make the program |
| 108 give up what it was doing and remove what was deleted. | 109 give up what it was doing and remove what was deleted. |
| 109 The only solution is to catch the exception and try again until it works. | 110 The only solution is to catch the exception and try again until it works. |
| 110 """ | 111 """ |
| 111 # roughly 10s | 112 # roughly 10s |
| 112 retries = 100 | 113 retries = 100 |
| 113 for i in range(retries): | 114 for i in range(retries): |
| 114 try: | 115 try: |
| 115 os.rename(old, new) | 116 os.rename(old, new) |
| 116 break | 117 break |
| 117 except OSError: | 118 except OSError: |
| 118 if i == (retries - 1): | 119 if i == (retries - 1): |
| 119 # Give up. | 120 # Give up. |
| (...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 while True: | 554 while True: |
| 554 file_path = os.path.join(path, filename) | 555 file_path = os.path.join(path, filename) |
| 555 if os.path.exists(file_path): | 556 if os.path.exists(file_path): |
| 556 return path | 557 return path |
| 557 (new_path, _) = os.path.split(path) | 558 (new_path, _) = os.path.split(path) |
| 558 if new_path == path: | 559 if new_path == path: |
| 559 return None | 560 return None |
| 560 path = new_path | 561 path = new_path |
| 561 | 562 |
| 562 | 563 |
| 564 def GetMacWinOrLinux(): |
| 565 """Returns 'mac', 'win', or 'linux', matching the current platform.""" |
| 566 if sys.platform.startswith(('cygwin', 'win')): |
| 567 return 'win' |
| 568 elif sys.platform.startswith('linux'): |
| 569 return 'linux' |
| 570 elif sys.platform == 'darwin': |
| 571 return 'mac' |
| 572 raise Error('Unknown platform: ' + sys.platform) |
| 573 |
| 574 |
| 575 def GetExeSuffix(): |
| 576 """Returns '' or '.exe' depending on how executables work on this platform.""" |
| 577 if sys.platform.startswith(('cygwin', 'win')): |
| 578 return '.exe' |
| 579 return '' |
| 580 |
| 581 |
| 563 def GetGClientRootAndEntries(path=None): | 582 def GetGClientRootAndEntries(path=None): |
| 564 """Returns the gclient root and the dict of entries.""" | 583 """Returns the gclient root and the dict of entries.""" |
| 565 config_file = '.gclient_entries' | 584 config_file = '.gclient_entries' |
| 566 root = FindFileUpwards(config_file, path) | 585 root = FindFileUpwards(config_file, path) |
| 567 if not root: | 586 if not root: |
| 568 print "Can't find %s" % config_file | 587 print "Can't find %s" % config_file |
| 569 return None | 588 return None |
| 570 config_path = os.path.join(root, config_file) | 589 config_path = os.path.join(root, config_file) |
| 571 env = {} | 590 env = {} |
| 572 execfile(config_path, env) | 591 execfile(config_path, env) |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 894 | 913 |
| 895 Python on OSX 10.6 raises a NotImplementedError exception. | 914 Python on OSX 10.6 raises a NotImplementedError exception. |
| 896 """ | 915 """ |
| 897 try: | 916 try: |
| 898 import multiprocessing | 917 import multiprocessing |
| 899 return multiprocessing.cpu_count() | 918 return multiprocessing.cpu_count() |
| 900 except: # pylint: disable=W0702 | 919 except: # pylint: disable=W0702 |
| 901 # Mac OS 10.6 only | 920 # Mac OS 10.6 only |
| 902 # pylint: disable=E1101 | 921 # pylint: disable=E1101 |
| 903 return int(os.sysconf('SC_NPROCESSORS_ONLN')) | 922 return int(os.sysconf('SC_NPROCESSORS_ONLN')) |
| OLD | NEW |