6488.1.1
by Jonathan Lange
Initial commit of utility to make a user on development environment. |
1 |
#!/usr/bin/python2.4
|
6488.1.3
by Jonathan Lange
Docstrings ahoy! |
2 |
"""Create a user for testing the local Launchpad.
|
3 |
||
4 |
Usage: make-lp-user <username> [<team1> <team2> ...]
|
|
5 |
||
6 |
This script will create a usable Launchpad user in the development database to
|
|
7 |
help you test a locally running copy of Launchpad.
|
|
8 |
||
9 |
You can add this user to teams by specifying them on the command-line. For
|
|
10 |
example:
|
|
11 |
||
12 |
make-lp-user fred vcs-imports bazaar-experts
|
|
13 |
||
14 |
will create a user 'fred' and add them to the 'vcs-imports' and
|
|
15 |
'bazaar-experts' teams.
|
|
16 |
||
17 |
In addition, this script will look in your ~/.ssh directory for public keys
|
|
18 |
and register them for the created user.
|
|
19 |
||
20 |
The login details will be printed to stdout.
|
|
21 |
||
22 |
Please note that this script is for testing purposes only. Do NOT use it in
|
|
23 |
production environments.
|
|
24 |
"""
|
|
25 |
||
6488.1.1
by Jonathan Lange
Initial commit of utility to make a user on development environment. |
26 |
import _pythonpath |
27 |
||
28 |
import os |
|
29 |
import sys |
|
30 |
||
31 |
import transaction |
|
32 |
||
33 |
from zope.component import getUtility |
|
34 |
||
35 |
from canonical.launchpad.interfaces import ( |
|
36 |
IPersonSet, |
|
37 |
ISSHKeySet, |
|
38 |
SSHKeyType, |
|
39 |
TeamMembershipStatus, |
|
40 |
)
|
|
41 |
from canonical.launchpad.scripts import execute_zcml_for_scripts |
|
42 |
from canonical.launchpad.testing.factory import LaunchpadObjectFactory |
|
43 |
||
44 |
# Shut up, pyflakes.
|
|
45 |
_pythonpath = _pythonpath |
|
46 |
||
47 |
||
48 |
DEFAULT_PASSWORD = 'test' |
|
49 |
factory = LaunchpadObjectFactory() |
|
50 |
||
51 |
||
52 |
def make_person(username): |
|
6488.1.3
by Jonathan Lange
Docstrings ahoy! |
53 |
"""Create and return a person with the given username.
|
54 |
||
55 |
The email address for the user will be <username>@example.com. The
|
|
56 |
password will be the value of `DEFAULT_PASSWORD`.
|
|
57 |
||
58 |
These details will be printed to stdout.
|
|
59 |
"""
|
|
6488.1.1
by Jonathan Lange
Initial commit of utility to make a user on development environment. |
60 |
email = '%s@example.com' % username |
61 |
person = factory.makePerson( |
|
62 |
name=username, password=DEFAULT_PASSWORD, email=email) |
|
63 |
print "username: %s" % (username,) |
|
64 |
print "email: %s" % (email,) |
|
65 |
print "password: %s" % (DEFAULT_PASSWORD,) |
|
66 |
return person |
|
67 |
||
68 |
||
69 |
def add_person_to_teams(person, team_names): |
|
6488.1.3
by Jonathan Lange
Docstrings ahoy! |
70 |
"""Add `person` to the teams named in `team_names`.
|
71 |
||
72 |
`person` is provided as its own review, team membership status is always
|
|
73 |
`APPROVED`. This function will add users even to restricted teams.
|
|
74 |
||
75 |
A list of teams joined will be printed to stdout.
|
|
76 |
"""
|
|
6488.1.1
by Jonathan Lange
Initial commit of utility to make a user on development environment. |
77 |
person_set = getUtility(IPersonSet) |
6488.1.4
by Jonathan Lange
Slightly more error handling. |
78 |
teams_joined = [] |
6488.1.1
by Jonathan Lange
Initial commit of utility to make a user on development environment. |
79 |
for team_name in team_names: |
80 |
team = person_set.getByName(team_name) |
|
6488.1.4
by Jonathan Lange
Slightly more error handling. |
81 |
if team is None: |
82 |
print "ERROR: %s not found." % (team_name,) |
|
83 |
continue
|
|
84 |
if not team.is_team: |
|
85 |
print "ERROR: %s is not a team." % (team_name,) |
|
86 |
continue
|
|
6488.1.1
by Jonathan Lange
Initial commit of utility to make a user on development environment. |
87 |
team.addMember( |
88 |
person, person, status=TeamMembershipStatus.APPROVED) |
|
6488.1.4
by Jonathan Lange
Slightly more error handling. |
89 |
teams_joined.append(team_name) |
90 |
print "teams: %s" % ' '.join(teams_joined) |
|
6488.1.1
by Jonathan Lange
Initial commit of utility to make a user on development environment. |
91 |
|
92 |
||
93 |
def add_ssh_public_keys(person): |
|
6488.1.3
by Jonathan Lange
Docstrings ahoy! |
94 |
"""Look for public keys and register them for `person`.
|
95 |
||
96 |
This function looks in ~/.ssh/id_rsa.pub and ~/.ssh/id_dsa.pub for SSH
|
|
97 |
public keys and registers them as SSH keys for `person`.
|
|
98 |
"""
|
|
6488.1.1
by Jonathan Lange
Initial commit of utility to make a user on development environment. |
99 |
ssh_dir = os.path.expanduser('~/.ssh') |
100 |
key_set = getUtility(ISSHKeySet) |
|
101 |
key_guesses = [ |
|
102 |
(SSHKeyType.RSA, 'id_rsa.pub'), |
|
103 |
(SSHKeyType.DSA, 'id_dsa.pub'), |
|
104 |
]
|
|
105 |
for key_type, guessed_filename in key_guesses: |
|
106 |
guessed_filename = os.path.join(ssh_dir, guessed_filename) |
|
107 |
try: |
|
108 |
public_key_file = open(guessed_filename, 'r') |
|
109 |
try: |
|
110 |
public_key = public_key_file.read() |
|
111 |
finally: |
|
112 |
public_key_file.close() |
|
113 |
except (OSError, IOError): |
|
114 |
continue
|
|
115 |
public_key = public_key.split()[1] |
|
116 |
key_set.new(person, key_type, public_key, 'Added by utility script.') |
|
117 |
print 'Registered SSH key: %s' % (guessed_filename,) |
|
118 |
||
119 |
||
120 |
def main(arguments): |
|
6488.1.3
by Jonathan Lange
Docstrings ahoy! |
121 |
"""Run the script."""
|
6488.1.4
by Jonathan Lange
Slightly more error handling. |
122 |
if len(arguments) == 0: |
123 |
print __doc__ |
|
124 |
return 2 |
|
6488.1.1
by Jonathan Lange
Initial commit of utility to make a user on development environment. |
125 |
execute_zcml_for_scripts() |
126 |
username, teams = arguments[0], arguments[1:] |
|
127 |
transaction.begin() |
|
128 |
person = make_person(username) |
|
129 |
add_person_to_teams(person, teams) |
|
130 |
add_ssh_public_keys(person) |
|
131 |
transaction.commit() |
|
132 |
return 0 |
|
133 |
||
134 |
||
135 |
if __name__ == '__main__': |
|
136 |
sys.exit(main(sys.argv[1:])) |