jenkins-bot merged this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
http.py: Make sure the cookie file is created with the right permissions

pywikibot.tools:
- Add `create` as new argument for file_mode_checker. It'll create an
empty file with appropriate permissions if True.
pywikibot.comms.http:
- There is no need to check and fix file permissions on every load and
save. Do it only once before creating the cookie_jar object.
Use the new capability of file_mode_checker to do so.
- Deprecate PywikibotCookieJar class.
- Now that we are directly using LWPCookieJar, `cookie_jar.load()` won't
raise IOError anymore (IOError used to be raised by file_mode_checker).
Remove IOError from the except clause of `cookie_jar.load()`.

Bug: T206387
Change-Id: I769c85be9523bfcc9912954b6afc8bdfb7e09f22
---
M pywikibot/comms/http.py
M pywikibot/tools/__init__.py
2 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py
index a497ac6..893942e 100644
--- a/pywikibot/comms/http.py
+++ b/pywikibot/comms/http.py
@@ -68,6 +68,7 @@
_logger = 'comm.http'


+# Should be marked as deprecated after PywikibotCookieJar is removed.
def mode_check_decorator(func):
"""Decorate load()/save() CookieJar methods."""
def wrapper(cls, **kwargs):
@@ -84,7 +85,12 @@
# in PY2 cookielib.LWPCookieJar is not a new-style class.
class PywikibotCookieJar(cookielib.LWPCookieJar, object):

- """CookieJar which checks file permissions."""
+ """DEPRECATED. CookieJar which checks file permissions."""
+
+ @deprecated(since='20181007')
+ def __init__(self, *args, **kwargs):
+ """Initialize the class."""
+ super(PywikibotCookieJar, self).__init__(*args, **kwargs)

@mode_check_decorator
def load(self, **kwargs):
@@ -97,10 +103,12 @@
super(PywikibotCookieJar, self).save()


-cookie_jar = PywikibotCookieJar(config.datafilepath('pywikibot.lwp'))
+cookie_file_path = config.datafilepath('pywikibot.lwp')
+file_mode_checker(cookie_file_path, create=True)
+cookie_jar = cookielib.LWPCookieJar(cookie_file_path)
try:
cookie_jar.load()
-except (IOError, cookielib.LoadError):
+except cookielib.LoadError:
debug('Loading cookies failed.', _logger)
else:
debug('Loaded cookies from file.', _logger)
diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py
index af948fc..73a0baf 100644
--- a/pywikibot/tools/__init__.py
+++ b/pywikibot/tools/__init__.py
@@ -1997,17 +1997,27 @@
return open_archive(filename, use_extension=use_extension)


-def file_mode_checker(filename, mode=0o600, quiet=False):
+def file_mode_checker(filename, mode=0o600, quiet=False, create=False):
"""Check file mode and update it, if needed.

@param filename: filename path
@type filename: basestring
@param mode: requested file mode
@type mode: int
-
+ @param quiet: warn about file mode change if False.
+ @type quiet: bool
+ @param create: create the file if it does not exist already
+ @type create: bool
+ @raise IOError: The file does not exist and `create` is False.
"""
+ try:
+ st_mode = os.stat(filename).st_mode
+ except OSError: # file does not exist
+ if not create:
+ raise
+ os.close(os.open(filename, os.O_CREAT | os.O_EXCL, mode))
+ return
warn_str = 'File {0} had {1:o} mode; converted to {2:o} mode.'
- st_mode = os.stat(filename).st_mode
if stat.S_ISREG(st_mode) and (st_mode - stat.S_IFREG != mode):
os.chmod(filename, mode)
# re-read and check changes

To view, visit change 465021. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I769c85be9523bfcc9912954b6afc8bdfb7e09f22
Gerrit-Change-Number: 465021
Gerrit-PatchSet: 3
Gerrit-Owner: Dalba <dalba.wiki@gmail.com>
Gerrit-Reviewer: Dalba <dalba.wiki@gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot (75)