~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/python2.4
#
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Create a user for testing the local Launchpad.

Usage: make-lp-user <username> [<team1> <team2> ...]

This script will create a usable Launchpad user in the development database to
help you test a locally running copy of Launchpad.

You can add this user to teams by specifying them on the command-line. For
example:

  make-lp-user fred vcs-imports bazaar-experts

will create a user 'fred' and add them to the 'vcs-imports' and
'bazaar-experts' teams.

In addition, this script will look in your ~/.ssh directory for public keys
and register them for the created user.

The login details will be printed to stdout.

Please note that this script is for testing purposes only. Do NOT use it in
production environments.
"""

import _pythonpath

import os
import sys

import transaction

from zope.component import getUtility

from canonical.launchpad.interfaces import (
    IPersonSet,
    ISSHKeySet,
    SSHKeyType,
    TeamMembershipStatus,
    )
from canonical.launchpad.scripts import execute_zcml_for_scripts
from lp.testing.factory import LaunchpadObjectFactory

# Shut up, pyflakes.
_pythonpath = _pythonpath


DEFAULT_PASSWORD = 'test'
factory = LaunchpadObjectFactory()


def make_person(username):
    """Create and return a person with the given username.

    The email address for the user will be <username>@example.com. The
    password will be the value of `DEFAULT_PASSWORD`.

    These details will be printed to stdout.
    """
    email = '%s@example.com' % username
    person = factory.makePerson(
        name=username, password=DEFAULT_PASSWORD, email=email)
    print "username: %s" % (username,)
    print "email:    %s" % (email,)
    print "password: %s" % (DEFAULT_PASSWORD,)
    return person


def add_person_to_teams(person, team_names):
    """Add `person` to the teams named in `team_names`.

    `person` is provided as its own review, team membership status is always
    `APPROVED`. This function will add users even to restricted teams.

    A list of teams joined will be printed to stdout.
    """
    person_set = getUtility(IPersonSet)
    teams_joined = []
    for team_name in team_names:
        team = person_set.getByName(team_name)
        if team is None:
            print "ERROR: %s not found." % (team_name,)
            continue
        if not team.is_team:
            print "ERROR: %s is not a team." % (team_name,)
            continue
        team.addMember(
            person, person, status=TeamMembershipStatus.APPROVED)
        teams_joined.append(team_name)
    print "teams:    %s" % ' '.join(teams_joined)


def add_ssh_public_keys(person):
    """Look for public keys and register them for `person`.

    This function looks in ~/.ssh/id_rsa.pub and ~/.ssh/id_dsa.pub for SSH
    public keys and registers them as SSH keys for `person`.
    """
    ssh_dir = os.path.expanduser('~/.ssh')
    key_set = getUtility(ISSHKeySet)
    key_guesses = [
        (SSHKeyType.RSA, 'id_rsa.pub'),
        (SSHKeyType.DSA, 'id_dsa.pub'),
        ]
    for key_type, guessed_filename in key_guesses:
        guessed_filename = os.path.join(ssh_dir, guessed_filename)
        try:
            public_key_file = open(guessed_filename, 'r')
            try:
                public_key = public_key_file.read()
            finally:
                public_key_file.close()
        except (OSError, IOError):
            continue
        public_key = public_key.split()[1]
        key_set.new(person, key_type, public_key, 'Added by utility script.')
        print 'Registered SSH key: %s' % (guessed_filename,)


def main(arguments):
    """Run the script."""
    if len(arguments) == 0:
        print __doc__
        return 2
    execute_zcml_for_scripts()
    username, teams = arguments[0], arguments[1:]
    transaction.begin()
    person = make_person(username)
    add_person_to_teams(person, teams)
    add_ssh_public_keys(person)
    transaction.commit()
    return 0


if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))