Merge lp:~stub/charm-helpers/bug-1185743-add-pwgen into lp:charm-helpers

Proposed by Stuart Bishop
Status: Merged
Merge reported by: Stuart Bishop
Merged at revision: not available
Proposed branch: lp:~stub/charm-helpers/bug-1185743-add-pwgen
Merge into: lp:charm-helpers
Prerequisite: lp:~stub/charm-helpers/bug-1195649-fix-write-file
Diff against target: 46 lines (+24/-0)
2 files modified
charmhelpers/core/host.py (+14/-0)
tests/core/test_host.py (+10/-0)
To merge this branch: bzr merge lp:~stub/charm-helpers/bug-1185743-add-pwgen
Reviewer Review Type Date Requested Status
Liam Young (community) Approve
Review via email: mp+173682@code.launchpad.net

Description of the change

Add a pwgen helper per Bug #1185743

To post a comment you must log in.
37. By Stuart Bishop

Merged bug-1195649-fix-write-file into bug-1185743-add-pwgen.

38. By Stuart Bishop

Merged bug-1195649-fix-write-file into bug-1185743-add-pwgen.

Revision history for this message
Liam Young (gnuoy) wrote :

LGTM once the prereq branch is merged. ( vowels are excluded to avoid accidental obscenities and l0QD1v are excluded because they look the same using some fonts )

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/core/host.py'
2--- charmhelpers/core/host.py 2013-07-09 10:50:35 +0000
3+++ charmhelpers/core/host.py 2013-07-09 10:50:35 +0000
4@@ -9,6 +9,8 @@
5 import os
6 import pwd
7 import grp
8+import random
9+import string
10 import subprocess
11 import hashlib
12
13@@ -258,3 +260,15 @@
14 k, v = l.split('=')
15 d[k.strip()] = v.strip()
16 return d
17+
18+
19+def pwgen(length=None):
20+ '''Generate a random pasword.'''
21+ if length is None:
22+ length = random.choice(range(35, 45))
23+ alphanumeric_chars = [
24+ l for l in (string.letters + string.digits)
25+ if l not in 'l0QD1vAEIOUaeiou']
26+ random_chars = [
27+ random.choice(alphanumeric_chars) for _ in range(length)]
28+ return(''.join(random_chars))
29
30=== modified file 'tests/core/test_host.py'
31--- tests/core/test_host.py 2013-07-09 10:50:35 +0000
32+++ tests/core/test_host.py 2013-07-09 10:50:35 +0000
33@@ -729,3 +729,13 @@
34 for key in result:
35 print lsb_release
36 self.assertEqual(result[key], lsb_release[key])
37+
38+ def test_pwgen(self):
39+ pw = host.pwgen()
40+ self.assert_(len(pw) >= 35, 'Password is too short')
41+
42+ pw = host.pwgen(10)
43+ self.assertEqual(len(pw), 10, 'Password incorrect length')
44+
45+ pw2 = host.pwgen(10)
46+ self.assertNotEqual(pw, pw2, 'Duplicated password')

Subscribers

People subscribed via source and target branches