From ac804b286cc68268afe9f2752e1792f134ebd8f2 Mon Sep 17 00:00:00 2001 From: lars Date: Thu, 10 Jun 2010 14:02:47 +0000 Subject: [PATCH] remove newline characters when reading password hashes from a file sort the entries by name during "save" --- htpasswd.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/htpasswd.py b/htpasswd.py index 6c1611c..6ffd7ab 100644 --- a/htpasswd.py +++ b/htpasswd.py @@ -57,6 +57,10 @@ class HtpasswdFile: for line in open(self.filename, 'r').readlines(): if ":" in line: username, pwhash = line.split(':', 1) + # remove spaces + username = username.strip() + # remove newline characters and spaces + pwhash = pwhash.strip() self.entries[username] = pwhash def save(self): @@ -73,8 +77,10 @@ class HtpasswdFile: if os.path.isfile(self.filename): # copy the original file mode (mod, timestamps) shutil.copystat(self.filename, temp_filename) - for name, pwhash in self.entries.items(): - os.write(temp_file, "%s:%s%s" % (name, pwhash, os.linesep)) + sorted_names = self.entries.keys() + 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) except IOError: try: