Commit 7d1300b8 authored by Raul Tambre's avatar Raul Tambre Committed by Commit Bot

fake_repos: Fix use of NamedTemporaryFile on Windows

Python doesn't guarantee that the file created by tempfile.NamedTemporaryFile can be opened by multiple programs at once.
This is the case on Windows, so we need to close the temporary PID file and only then pass it to Git.

Bug: 962263
Change-Id: Idf4a36b33ce06d7ab06453c20a690622179aef58
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1608560
Auto-Submit: Raul Tambre <raul@tambre.ee>
Reviewed-by: 's avatarDirk Pranke <dpranke@chromium.org>
Commit-Queue: Raul Tambre <raul@tambre.ee>
parent 1d49bad9
......@@ -156,7 +156,7 @@ class FakeReposBase(object):
# It is 1-based too.
self.git_hashes = {}
self.gitdaemon = None
self.git_pid_file = None
self.git_pid_file_name = None
self.git_root = None
self.git_dirty = False
self.git_port = None
......@@ -196,16 +196,16 @@ class FakeReposBase(object):
logging.debug('Killing git-daemon pid %s' % self.gitdaemon.pid)
self.gitdaemon.kill()
self.gitdaemon = None
if self.git_pid_file:
pid = int(self.git_pid_file.read())
self.git_pid_file.close()
if self.git_pid_file_name:
pid = int(open(self.git_pid_file_name).read())
logging.debug('Killing git daemon pid %s' % pid)
try:
subprocess2.kill_pid(pid)
except OSError as e:
if e.errno != errno.ESRCH: # no such process
raise
self.git_pid_file = None
os.remove(self.git_pid_file_name)
self.git_pid_file_name = None
wait_for_port_to_free(self.host, self.git_port)
self.git_port = None
self.git_base = None
......@@ -238,7 +238,7 @@ class FakeReposBase(object):
self.set_up()
if self.gitdaemon:
return True
assert self.git_pid_file == None
assert self.git_pid_file_name == None
try:
subprocess2.check_output(['git', '--version'])
except (OSError, subprocess2.CalledProcessError):
......@@ -249,12 +249,14 @@ class FakeReposBase(object):
self.git_port = find_free_port(self.host, 20000)
self.git_base = 'git://%s:%d/git/' % (self.host, self.git_port)
# Start the daemon.
self.git_pid_file = tempfile.NamedTemporaryFile()
git_pid_file = tempfile.NamedTemporaryFile(delete=False)
self.git_pid_file_name = git_pid_file.name
git_pid_file.close()
cmd = ['git', 'daemon',
'--export-all',
'--reuseaddr',
'--base-path=' + self.root_dir,
'--pid-file=' + self.git_pid_file.name,
'--pid-file=' + self.git_pid_file_name,
'--port=%d' % self.git_port]
if self.host == '127.0.0.1':
cmd.append('--listen=' + self.host)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment