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: Dirk Pranke <dpranke@chromium.org>
Commit-Queue: Raul Tambre <raul@tambre.ee>
changes/60/1608560/2
Raul Tambre 7 years ago committed by Commit Bot
parent 1d49bad9f0
commit 7d1300b89a

@ -156,7 +156,7 @@ class FakeReposBase(object):
# It is 1-based too. # It is 1-based too.
self.git_hashes = {} self.git_hashes = {}
self.gitdaemon = None self.gitdaemon = None
self.git_pid_file = None self.git_pid_file_name = None
self.git_root = None self.git_root = None
self.git_dirty = False self.git_dirty = False
self.git_port = None self.git_port = None
@ -196,16 +196,16 @@ class FakeReposBase(object):
logging.debug('Killing git-daemon pid %s' % self.gitdaemon.pid) logging.debug('Killing git-daemon pid %s' % self.gitdaemon.pid)
self.gitdaemon.kill() self.gitdaemon.kill()
self.gitdaemon = None self.gitdaemon = None
if self.git_pid_file: if self.git_pid_file_name:
pid = int(self.git_pid_file.read()) pid = int(open(self.git_pid_file_name).read())
self.git_pid_file.close()
logging.debug('Killing git daemon pid %s' % pid) logging.debug('Killing git daemon pid %s' % pid)
try: try:
subprocess2.kill_pid(pid) subprocess2.kill_pid(pid)
except OSError as e: except OSError as e:
if e.errno != errno.ESRCH: # no such process if e.errno != errno.ESRCH: # no such process
raise 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) wait_for_port_to_free(self.host, self.git_port)
self.git_port = None self.git_port = None
self.git_base = None self.git_base = None
@ -238,7 +238,7 @@ class FakeReposBase(object):
self.set_up() self.set_up()
if self.gitdaemon: if self.gitdaemon:
return True return True
assert self.git_pid_file == None assert self.git_pid_file_name == None
try: try:
subprocess2.check_output(['git', '--version']) subprocess2.check_output(['git', '--version'])
except (OSError, subprocess2.CalledProcessError): except (OSError, subprocess2.CalledProcessError):
@ -249,12 +249,14 @@ class FakeReposBase(object):
self.git_port = find_free_port(self.host, 20000) self.git_port = find_free_port(self.host, 20000)
self.git_base = 'git://%s:%d/git/' % (self.host, self.git_port) self.git_base = 'git://%s:%d/git/' % (self.host, self.git_port)
# Start the daemon. # 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', cmd = ['git', 'daemon',
'--export-all', '--export-all',
'--reuseaddr', '--reuseaddr',
'--base-path=' + self.root_dir, '--base-path=' + self.root_dir,
'--pid-file=' + self.git_pid_file.name, '--pid-file=' + self.git_pid_file_name,
'--port=%d' % self.git_port] '--port=%d' % self.git_port]
if self.host == '127.0.0.1': if self.host == '127.0.0.1':
cmd.append('--listen=' + self.host) cmd.append('--listen=' + self.host)

Loading…
Cancel
Save