diff --git a/gcl.py b/gcl.py index 90ad65a1d..5d1479378 100755 --- a/gcl.py +++ b/gcl.py @@ -141,7 +141,7 @@ def GetCachedFile(filename, max_age=60*60*24*3, use_root=False): # fetch it each time. gclient_utils.FileWrite(cached_file, content) else: - content = ReadFile(cached_file) + content = gclient_utils.FileRead(cached_file, 'r') # Keep the content cached in memory. FILES_CACHE[filename] = content return FILES_CACHE[filename] @@ -205,14 +205,6 @@ def RunShell(command, print_output=False): return RunShellWithReturnCode(command, print_output)[0] -def ReadFile(filename, flags='r'): - """Returns the contents of a file.""" - f = open(filename, flags) - result = f.read() - f.close() - return result - - def FilterFlag(args, flag): """Returns True if the flag is present in args list. @@ -416,7 +408,8 @@ class ChangeInfo(object): ErrorExit("Changelist " + changename + " not found.") return ChangeInfo(changename, 0, 0, '', None, local_root, needs_upload=False) - split_data = ReadFile(info_file).split(ChangeInfo._SEPARATOR, 2) + split_data = gclient_utils.FileRead(info_file, 'r').split( + ChangeInfo._SEPARATOR, 2) if len(split_data) != 3: ErrorExit("Changelist file %s is corrupt" % info_file) items = split_data[0].split(', ') @@ -1012,7 +1005,7 @@ def Change(change_info, args): if not silent: os.system(GetEditor() + " " + filename) - result = ReadFile(filename) + result = gclient_utils.FileRead(filename, 'r') os.remove(filename) if not result: diff --git a/scm.py b/scm.py index 58f26c6a9..757935d75 100644 --- a/scm.py +++ b/scm.py @@ -416,11 +416,7 @@ class SVN(object): # We know the diff will be incorrectly formatted. Fix it. if SVN.IsMoved(filename): - # The file is "new" in the patch sense. Generate a homebrew diff. - # We can't use ReadFile() since it's not using binary mode. - file_handle = open(filename, 'rb') - file_content = file_handle.read() - file_handle.close() + file_content = gclient_utils.FileRead(filename, 'rb') # Prepend '+' to every lines. file_content = ['+' + i for i in file_content.splitlines(True)] nb_lines = len(file_content) diff --git a/tests/gcl_unittest.py b/tests/gcl_unittest.py index 6c37bd472..e59831206 100755 --- a/tests/gcl_unittest.py +++ b/tests/gcl_unittest.py @@ -19,7 +19,7 @@ class GclTestsBase(SuperMoxTestBase): self.mox.StubOutWithMock(gcl.SVN, 'CaptureInfo') self.mox.StubOutWithMock(gcl, 'tempfile') self.mox.StubOutWithMock(gcl.upload, 'RealMain') - self.mox.StubOutWithMock(gcl, 'ReadFile') + self.mox.StubOutWithMock(gcl.gclient_utils, 'FileRead') self.mox.StubOutWithMock(gcl.gclient_utils, 'FileWrite') @@ -38,7 +38,7 @@ class GclUnittest(GclTestsBase): 'GetModifiedFiles', 'GetRepositoryRoot', 'Help', 'Lint', 'LoadChangelistInfoForMultiple', 'MISSING_TEST_MSG', 'Opened', 'OptionallyDoPresubmitChecks', 'PresubmitCL', 'REPOSITORY_ROOT', - 'ReadFile', 'RunShell', 'RunShellWithReturnCode', 'SVN', + 'RunShell', 'RunShellWithReturnCode', 'SVN', 'SendToRietveld', 'TryChange', 'UnknownFiles', 'UploadCL', 'Warn', 'breakpad', 'gclient_utils', 'getpass', 'main', 'os', 'random', 're', 'shutil', 'string', 'subprocess', 'sys', 'tempfile', 'upload', @@ -185,7 +185,7 @@ class ChangeInfoUnittest(GclTestsBase): description = ["This is some description.", "force an extra separator."] gcl.GetChangelistInfoFile('bleh').AndReturn('bleeeh') gcl.os.path.exists('bleeeh').AndReturn(True) - gcl.ReadFile('bleeeh').AndReturn( + gcl.gclient_utils.FileRead('bleeeh', 'r').AndReturn( gcl.ChangeInfo._SEPARATOR.join(["42, 53", "G b.cc"] + description)) self.mox.ReplayAll() @@ -200,7 +200,7 @@ class ChangeInfoUnittest(GclTestsBase): def testLoadEmpty(self): gcl.GetChangelistInfoFile('bleh').AndReturn('bleeeh') gcl.os.path.exists('bleeeh').AndReturn(True) - gcl.ReadFile('bleeeh').AndReturn( + gcl.gclient_utils.FileRead('bleeeh', 'r').AndReturn( gcl.ChangeInfo._SEPARATOR.join(["", "", ""])) self.mox.ReplayAll() diff --git a/tests/presubmit_unittest.py b/tests/presubmit_unittest.py index f7ecbdb34..c11283e37 100755 --- a/tests/presubmit_unittest.py +++ b/tests/presubmit_unittest.py @@ -49,9 +49,8 @@ def GetPreferredTrySlaves(): presubmit.os.chdir = MockChdir self.mox.StubOutWithMock(presubmit.scm.SVN, 'CaptureInfo') self.mox.StubOutWithMock(presubmit.scm.SVN, 'GetFileProperty') - # TODO(maruel): Err, small duplication of code here. - self.mox.StubOutWithMock(presubmit.gcl, 'ReadFile') self.mox.StubOutWithMock(presubmit.gclient_utils, 'FileRead') + self.mox.StubOutWithMock(presubmit.gclient_utils, 'FileWrite') class PresubmitUnittest(PresubmitTestsBase): diff --git a/trychange.py b/trychange.py index 51f176a67..3febb1273 100755 --- a/trychange.py +++ b/trychange.py @@ -520,7 +520,7 @@ def TryChange(argv, if options.url: options.diff = urllib.urlopen(options.url).read() elif options.diff: - options.diff = gcl.ReadFile(options.diff) + options.diff = gcl.gclient_utils.FileRead(options.diff, 'rb') # Process the VCS in any case at least to retrieve the email address. try: options.scm = GuessVCS(options)