Skip to content

Commit c10a673

Browse files
committed
authfiles: Only create root directories at file save time
Signed-off-by: Cole Robinson <crobinso@redhat.com>
1 parent d6fd480 commit c10a673

1 file changed

Lines changed: 21 additions & 15 deletions

File tree

bugzilla/_authfiles.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ def _parse_hostname(url):
1919
return parsedbits.netloc or parsedbits.path
2020

2121

22+
def _makedirs(path):
23+
if os.path.exists(os.path.dirname(path)):
24+
return
25+
os.makedirs(os.path.dirname(path), 0o700)
26+
27+
2228
def _default_location(filename, kind):
2329
"""
2430
Determine default location for passed filename and xdg kind,
2531
example: ~/.cache/python-bugzilla/bugzillacookies
2632
"""
2733
xdgpath = os.path.expanduser("~/.%s/python-bugzilla/%s" % (kind, filename))
28-
if not os.path.exists(os.path.dirname(xdgpath)):
29-
os.makedirs(os.path.dirname(xdgpath), 0o700)
3034
return xdgpath
3135

3236

@@ -141,6 +145,7 @@ def set_value(self, url, value):
141145
self._cfg.set(domain, 'token', value)
142146

143147
if self._filename:
148+
_makedirs(self._filename)
144149
with open(self._filename, 'w') as _cfg:
145150
log.debug("Saving to _cfg")
146151
self._cfg.write(_cfg)
@@ -180,6 +185,7 @@ def _save_api_key(url, api_key, configpaths):
180185

181186
cfg.set(section, 'api_key', api_key.strip())
182187

188+
_makedirs(config_filename)
183189
with open(config_filename, 'w') as configfile:
184190
cfg.write(configfile)
185191

@@ -197,13 +203,8 @@ def __init__(self):
197203

198204
def _build_cookiejar(self, cookiefile):
199205
cj = MozillaCookieJar(cookiefile)
200-
if cookiefile is None:
201-
return cj
202-
if not os.path.exists(cookiefile):
203-
# Make sure a new file has correct permissions
204-
open(cookiefile, 'a').close()
205-
os.chmod(cookiefile, 0o600)
206-
cj.save()
206+
if (cookiefile is None or
207+
not os.path.exists(cookiefile)):
207208
return cj
208209

209210
try:
@@ -224,12 +225,17 @@ def get_cookiejar(self):
224225
return self._cookiejar
225226

226227
def set_cookies(self, cookies):
227-
if self._cookiejar is None:
228-
return
229-
230228
for cookie in cookies:
231229
self._cookiejar.set_cookie(cookie)
232230

233-
if self._cookiejar.filename is not None:
234-
# Save is required only if we have a filename
235-
self._cookiejar.save()
231+
cookiefile = self._cookiejar.filename
232+
if not cookiefile:
233+
return
234+
235+
if not os.path.exists(cookiefile):
236+
_makedirs(cookiefile)
237+
# Make sure a new file has correct permissions
238+
open(cookiefile, 'a').close()
239+
os.chmod(cookiefile, 0o600)
240+
241+
self._cookiejar.save()

0 commit comments

Comments
 (0)