remove newline characters when reading password hashes from a file
sort the entries by name during "save"
This commit is contained in:
parent
988fa15962
commit
ac804b286c
1 changed files with 8 additions and 2 deletions
10
htpasswd.py
10
htpasswd.py
|
@ -57,6 +57,10 @@ class HtpasswdFile:
|
||||||
for line in open(self.filename, 'r').readlines():
|
for line in open(self.filename, 'r').readlines():
|
||||||
if ":" in line:
|
if ":" in line:
|
||||||
username, pwhash = line.split(':', 1)
|
username, pwhash = line.split(':', 1)
|
||||||
|
# remove spaces
|
||||||
|
username = username.strip()
|
||||||
|
# remove newline characters and spaces
|
||||||
|
pwhash = pwhash.strip()
|
||||||
self.entries[username] = pwhash
|
self.entries[username] = pwhash
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
|
@ -73,8 +77,10 @@ class HtpasswdFile:
|
||||||
if os.path.isfile(self.filename):
|
if os.path.isfile(self.filename):
|
||||||
# copy the original file mode (mod, timestamps)
|
# copy the original file mode (mod, timestamps)
|
||||||
shutil.copystat(self.filename, temp_filename)
|
shutil.copystat(self.filename, temp_filename)
|
||||||
for name, pwhash in self.entries.items():
|
sorted_names = self.entries.keys()
|
||||||
os.write(temp_file, "%s:%s%s" % (name, pwhash, os.linesep))
|
sorted_names.sort()
|
||||||
|
for name in sorted_names:
|
||||||
|
os.write(temp_file, "%s:%s%s" % (name, self.entries[name], os.linesep))
|
||||||
os.close(temp_file)
|
os.close(temp_file)
|
||||||
except IOError:
|
except IOError:
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in a new issue