Avoid creating an empty netrc file in $TEMP
The code to load .netrc file copies it to a temporary file
named $TEMP/XXXXXXXXXgerrit_util/netrc before loading it
to workaround crbug.com/664664.
However, using a netrc file is deprecated (in favor of a
gitcookies file instead) so most of the time, the file is
empty. Instead of creating an empty file, load data from
/dev/null (named os.devnull).
This avoid leaving an empty file lying around (since the
temporary file is not deleted), and avoid race-conditions
on Windows when multiple processes/threads try to create
the same file.
Bug: none
Change-Id: I873b72402038ca3f0d32387353dc89b731aaa927
Reviewed-on: https://chromium-review.googlesource.com/1132998
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: Ryan Tseng <hinoka@chromium.org>
diff --git a/gerrit_util.py b/gerrit_util.py
index 81b28e7..d5780ad 100644
--- a/gerrit_util.py
+++ b/gerrit_util.py
@@ -134,16 +134,17 @@
def _get_netrc(cls):
# Buffer the '.netrc' path. Use an empty file if it doesn't exist.
path = cls.get_netrc_path()
- content = ''
- if os.path.exists(path):
- st = os.stat(path)
- if st.st_mode & (stat.S_IRWXG | stat.S_IRWXO):
- print >> sys.stderr, (
- 'WARNING: netrc file %s cannot be used because its file '
- 'permissions are insecure. netrc file permissions should be '
- '600.' % path)
- with open(path) as fd:
- content = fd.read()
+ if not os.path.exists(path):
+ return netrc.netrc(os.devnull)
+
+ st = os.stat(path)
+ if st.st_mode & (stat.S_IRWXG | stat.S_IRWXO):
+ print >> sys.stderr, (
+ 'WARNING: netrc file %s cannot be used because its file '
+ 'permissions are insecure. netrc file permissions should be '
+ '600.' % path)
+ with open(path) as fd:
+ content = fd.read()
# Load the '.netrc' file. We strip comments from it because processing them
# can trigger a bug in Windows. See crbug.com/664664.