Merge pull request #4364 from stoned/pre-1.6-passlib

Fix compatibility issue with passlib version < 1.6 (as encountered on eg...
This commit is contained in:
Michael DeHaan 2013-10-04 14:47:18 -07:00
commit d1bb1220d5

View file

@ -76,9 +76,11 @@ EXAMPLES = """
import os import os
from distutils.version import StrictVersion
try: try:
from passlib.apache import HtpasswdFile from passlib.apache import HtpasswdFile
import passlib
except ImportError: except ImportError:
passlib_installed = False passlib_installed = False
else: else:
@ -101,14 +103,10 @@ def present(dest, username, password, crypt_scheme, create, check_mode):
if check_mode: if check_mode:
return ("Create %s" % dest, True) return ("Create %s" % dest, True)
create_missing_directories(dest) create_missing_directories(dest)
try: if StrictVersion(passlib.__version__) >= StrictVersion('1.6'):
ht = HtpasswdFile(dest, new=True, default_scheme=crypt_scheme) ht = HtpasswdFile(dest, new=True, default_scheme=crypt_scheme)
except: else:
# library version doesn't take 'new', deal with it. ht = HtpasswdFile(dest, autoload=False, default=crypt_scheme)
fh = open(dest, 'w')
fh.write('')
fh.close()
ht = HtpasswdFile(dest, default_scheme=crypt_scheme)
if getattr(ht, 'set_password', None): if getattr(ht, 'set_password', None):
ht.set_password(username, password) ht.set_password(username, password)
else: else:
@ -116,10 +114,10 @@ def present(dest, username, password, crypt_scheme, create, check_mode):
ht.save() ht.save()
return ("Created %s and added %s" % (dest, username), True) return ("Created %s and added %s" % (dest, username), True)
else: else:
try: if StrictVersion(passlib.__version__) >= StrictVersion('1.6'):
ht = HtpasswdFile(dest, new=False, default_scheme=crypt_scheme) ht = HtpasswdFile(dest, new=False, default_scheme=crypt_scheme)
except: else:
ht = HtpasswdFile(dest, default_scheme=crypt_scheme) ht = HtpasswdFile(dest, default=crypt_scheme)
found = None found = None
if getattr(ht, 'check_password', None): if getattr(ht, 'check_password', None):
@ -146,9 +144,9 @@ def absent(dest, username, check_mode):
if not os.path.exists(dest): if not os.path.exists(dest):
raise ValueError("%s does not exists" % dest) raise ValueError("%s does not exists" % dest)
try: if StrictVersion(passlib.__version__) >= StrictVersion('1.6'):
ht = HtpasswdFile(dest, new=False) ht = HtpasswdFile(dest, new=False)
except: else:
ht = HtpasswdFile(dest) ht = HtpasswdFile(dest)
if username not in ht.users(): if username not in ht.users():