~ubuntu-branches/ubuntu/precise/python-keyring/precise-updates

« back to all changes in this revision

Viewing changes to keyring/cli.py

  • Committer: Package Import Robot
  • Author(s): Carl Chenet
  • Date: 2012-02-14 12:07:30 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20120214120730-ekuysup9a2s5yl9d
Tags: 0.7.1-1
* New upstream version (Closes: #656680, #624690)
* debian/control
  - Add X-Python-Version for Python 3.2
  - Add B-D for Python 3.2
  - Add unzip for B-D to repack upstream sources
  - Add python3-keyring description
  - Recommends python3-crypto for Python3 binary package
* Add python-keyring.install and python3-keyring.install files
* debian/rules
  - Execute unit tests if available
  - repack upstream sources
* debian/watch
  - New URL to catch upstream sources

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
"""Simple command line interface to get/set password from a keyring"""
3
3
 
4
4
import getpass
5
 
import optparse
 
5
from optparse import OptionParser
6
6
import sys
7
7
 
8
8
import keyring
9
9
import keyring.core
10
10
 
11
11
 
12
 
def input_password(prompt):
13
 
    """Ask for a password to the user.
14
 
 
15
 
    This mostly exists to ease the testing process.
16
 
    """
17
 
 
18
 
    return getpass.getpass(prompt)
19
 
 
20
 
 
21
 
def output_password(password):
22
 
    """Output the password to the user.
23
 
 
24
 
    This mostly exists to ease the testing process.
25
 
    """
26
 
 
27
 
    print password
 
12
class CommandLineTool(object):
 
13
    def __init__(self):
 
14
        self.parser = OptionParser(usage="%prog [get|set] SERVICE USERNAME")
 
15
        self.parser.add_option("-p", "--keyring-path",
 
16
                               dest="keyring_path", default=None,
 
17
                               help="Path to the keyring backend")
 
18
        self.parser.add_option("-b", "--keyring-backend",
 
19
                               dest="keyring_backend", default=None,
 
20
                               help="Name of the keyring backend")
 
21
 
 
22
    def run(self, argv):
 
23
        opts, args = self.parser.parse_args(argv)
 
24
 
 
25
        try:
 
26
            kind, service, username = args
 
27
        except ValueError:
 
28
            if len(args) == 0:
 
29
                # Be nice with the user if he just tries to launch the tool
 
30
                self.parser.print_help()
 
31
                return 1
 
32
            else:
 
33
                self.parser.error("Wrong number of arguments")
 
34
 
 
35
        if opts.keyring_backend is not None:
 
36
            try:
 
37
                backend = keyring.core.load_keyring(opts.keyring_path,
 
38
                                                    opts.keyring_backend)
 
39
                keyring.set_keyring(backend)
 
40
            except Exception, e:
 
41
                # Tons of things can go wrong here:
 
42
                #   ImportError when using "fjkljfljkl"
 
43
                #   AttributeError when using "os.path.bar"
 
44
                #   TypeError when using "__builtins__.str"
 
45
                # So, we play on the safe side, and catch everything.
 
46
                self.parser.error("Unable to load specified keyring: %s" % e)
 
47
 
 
48
 
 
49
        if kind == 'get':
 
50
            password = keyring.get_password(service, username)
 
51
            if password is None:
 
52
                return 1
 
53
 
 
54
            self.output_password(password)
 
55
            return 0
 
56
 
 
57
        elif kind == 'set':
 
58
            password = self.input_password("Password for '%s' in '%s': " %
 
59
                                           (username, service))
 
60
            keyring.set_password(service, username, password)
 
61
            return 0
 
62
 
 
63
        else:
 
64
            self.parser.error("You can only 'get' or 'set' a password.")
 
65
            pass
 
66
 
 
67
    def input_password(self, prompt):
 
68
        """Ask for a password to the user.
 
69
 
 
70
        This mostly exists to ease the testing process.
 
71
        """
 
72
 
 
73
        return getpass.getpass(prompt)
 
74
 
 
75
 
 
76
    def output_password(self, password):
 
77
        """Output the password to the user.
 
78
 
 
79
        This mostly exists to ease the testing process.
 
80
        """
 
81
 
 
82
        print password
28
83
 
29
84
 
30
85
def main(argv=None):
31
86
    """Main command line interface."""
32
87
 
33
 
    parser = optparse.OptionParser(usage="%prog [get|set] SERVICE USERNAME")
34
 
    parser.add_option("-p", "--keyring-path", dest="keyring_path", default=None,
35
 
                      help="Path to the keyring backend")
36
 
    parser.add_option("-b", "--keyring-backend", dest="keyring_backend", default=None,
37
 
                      help="Name of the keyring backend")
38
88
 
39
89
    if argv is None:
40
90
        argv = sys.argv[1:]
41
91
 
42
 
    opts, args = parser.parse_args(argv)
43
 
 
44
 
    try:
45
 
        kind, service, username = args
46
 
    except ValueError:
47
 
        if len(args) == 0:
48
 
            # Be nice with the user if he just tries to launch the tool
49
 
            parser.print_help()
50
 
            return 1
51
 
        else:
52
 
            parser.error("Wrong number of arguments")
53
 
 
54
 
    if opts.keyring_backend is not None:
55
 
        try:
56
 
            backend = keyring.core.load_keyring(opts.keyring_path, opts.keyring_backend)
57
 
            keyring.set_keyring(backend)
58
 
        except Exception, e:
59
 
            # Tons of things can go wrong here:
60
 
            #   ImportError when using "fjkljfljkl"
61
 
            #   AttributeError when using "os.path.bar"
62
 
            #   TypeError when using "__builtins__.str"
63
 
            # So, we play on the safe side, and catch everything.
64
 
            parser.error("Unable to load specified keyring: %s" % e)
65
 
 
66
 
 
67
 
    if kind == 'get':
68
 
        password = keyring.get_password(service, username)
69
 
        if password is None:
70
 
            return 1
71
 
 
72
 
        output_password(password)
73
 
        return 0
74
 
 
75
 
    elif kind == 'set':
76
 
        password = input_password("Password for '%s' in '%s': " %
77
 
                                  (username, service))
78
 
        keyring.set_password(service, username, password)
79
 
        return 0
80
 
 
81
 
    else:
82
 
        parser.error("You can only 'get' or 'set' a password.")
 
92
    cli = CommandLineTool()
 
93
    return cli.run(argv)
83
94
 
84
95
 
85
96
if __name__ == '__main__':