~mvo/software-center/fix-index-update-terms-bug

« back to all changes in this revision

Viewing changes to utils/piston-helpers/piston_ubuntu_sso.py

add piston_generic_helper and use it to get rid of piston_get_review_stats_helper and piston_ubuntu_sso

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
# Copyright (C) 2011 Canonical
3
 
#
4
 
# Authors:
5
 
#  Michael Vogt
6
 
#
7
 
# This program is free software; you can redistribute it and/or modify it under
8
 
# the terms of the GNU General Public License as published by the Free Software
9
 
# Foundation; version 3.
10
 
#
11
 
# This program is distributed in the hope that it will be useful, but WITHOUT
12
 
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14
 
# details.
15
 
#
16
 
# You should have received a copy of the GNU General Public License along with
17
 
# this program; if not, write to the Free Software Foundation, Inc.,
18
 
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 
 
20
 
from gi.repository import GObject
21
 
 
22
 
import argparse
23
 
import logging
24
 
import os
25
 
import pickle
26
 
import sys
27
 
 
28
 
# useful for debugging
29
 
if "SOFTWARE_CENTER_DEBUG_HTTP" in os.environ:
30
 
    import httplib2
31
 
    httplib2.debuglevel = 1
32
 
 
33
 
import piston_mini_client.auth
34
 
 
35
 
from softwarecenter.paths import SOFTWARE_CENTER_CACHE_DIR
36
 
from softwarecenter.backend.piston.ubuntusso_pristine import (
37
 
    UbuntuSsoAPI)
38
 
from piston_get_scagent_available_apps import SSOLoginHelper
39
 
 
40
 
# patch default_service_root to the one we use
41
 
from softwarecenter.enums import SSO_LOGIN_HOST
42
 
UbuntuSsoAPI.default_service_root = SSO_LOGIN_HOST+"/api/1.0"
43
 
 
44
 
from gettext import gettext as _
45
 
 
46
 
LOG = logging.getLogger(__name__)
47
 
 
48
 
if __name__ == "__main__":
49
 
    logging.basicConfig()
50
 
 
51
 
    # command line parser
52
 
    parser = argparse.ArgumentParser(description="Helper for recommender-api")
53
 
    parser.add_argument("--debug", action="store_true", default=False,
54
 
                        help="enable debug output")
55
 
    parser.add_argument("--ignore-cache", action="store_true", default=False,
56
 
                        help="force ignore cache")
57
 
    parser.add_argument("--parent-xid", default=0,
58
 
                        help="xid of the parent window")
59
 
 
60
 
    subparser = parser.add_subparsers(title="Commands")
61
 
    # recommend_top
62
 
    command = subparser.add_parser("whoami")
63
 
    command.set_defaults(command="whoami")
64
 
 
65
 
    args = parser.parse_args()
66
 
 
67
 
    if args.debug:
68
 
        LOG.setLevel(logging.DEBUG)
69
 
 
70
 
    if args.ignore_cache:
71
 
        cachedir = None
72
 
    else:
73
 
        cachedir = os.path.join(SOFTWARE_CENTER_CACHE_DIR, "uraclient")
74
 
 
75
 
    # all calls are authenticated
76
 
    helper = SSOLoginHelper(args.parent_xid)
77
 
    token = helper.get_oauth_token_sync()
78
 
 
79
 
    auth = piston_mini_client.auth.OAuthAuthorizer(token["token"],
80
 
                                                   token["token_secret"],
81
 
                                                   token["consumer_key"],
82
 
                                                   token["consumer_secret"])
83
 
    urclient = UbuntuSsoAPI(cachedir=cachedir, auth=auth)
84
 
        
85
 
    piston_reply = None
86
 
    # handle the args
87
 
    f = getattr(urclient, args.command)
88
 
    try:
89
 
        piston_reply = f()
90
 
    except:
91
 
        LOG.exception("urclient_apps")
92
 
        sys.exit(1)
93
 
 
94
 
    # print to stdout where its consumed by the parent
95
 
    if piston_reply is not None:
96
 
        try:
97
 
            print pickle.dumps(piston_reply)
98
 
        except IOError:
99
 
            # this can happen if the parent gets killed, no need to trigger
100
 
            # apport for this
101
 
            pass