~ubuntu-branches/ubuntu/saucy/ubuntuone-control-panel/saucy

« back to all changes in this revision

Viewing changes to ubuntuone/controlpanel/utils/common.py

  • Committer: Package Import Robot
  • Author(s): Rodney Dawes
  • Date: 2013-01-10 15:01:04 UTC
  • mfrom: (1.1.42)
  • Revision ID: package-import@ubuntu.com-20130110150104-n9pxjr0ql2iowhkn
Tags: 4.1.2-0ubuntu1
* New upstream release.
  - Support translations on other platforms.
* debian/control:
  - Update dependencies on ubuntu-sso-client packages for new API.
  - Update standards version to 3.9.4.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright 2012 Canonical Ltd.
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify it
 
6
# under the terms of the GNU General Public License version 3, as published
 
7
# by the Free Software Foundation.
 
8
#
 
9
# This program is distributed in the hope that it will be useful, but
 
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
12
# PURPOSE.  See the GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License along
 
15
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
"""Platform - independent support code for utils."""
 
18
 
 
19
import os
 
20
import sys
 
21
 
 
22
try:
 
23
    from configparser import (SafeConfigParser, NoSectionError, NoOptionError)
 
24
except ImportError:
 
25
    from ConfigParser import (SafeConfigParser, NoSectionError, NoOptionError)
 
26
 
 
27
from twisted.internet import defer
 
28
from twisted.internet.utils import getProcessValue
 
29
from twisted.python import procutils
 
30
 
 
31
from dirspec.basedir import load_config_paths
 
32
from dirspec.utils import get_program_path
 
33
 
 
34
from ubuntuone.controlpanel.utils import logger
 
35
 
 
36
UPDATE_BIN_NAME = 'ubuntuone-updater'
 
37
UPDATE_CONFIG_NAME = 'update.conf'
 
38
DARWIN_APP_NAMES = {UPDATE_BIN_NAME: 'UbuntuOne Updater.app'}
 
39
 
 
40
 
 
41
def get_bin_cmd(exe_name):
 
42
    """Return list of cmds for an exe from the control panel project."""
 
43
    fallback_dir = os.path.join(os.path.dirname(__file__),
 
44
                                 os.pardir, os.pardir, os.pardir,
 
45
                                 "bin")
 
46
 
 
47
    cmd_args = [get_program_path(exe_name,
 
48
                                 fallback_dirs=[fallback_dir],
 
49
                                 app_names=DARWIN_APP_NAMES)]
 
50
 
 
51
    # procutils.which('python') returns python.exe on windows:
 
52
    if getattr(sys, 'frozen', None) is None:
 
53
        cmd_args.insert(0, procutils.which('python')[0])
 
54
 
 
55
    return cmd_args
 
56
 
 
57
 
 
58
def get_update_config():
 
59
    """Read the update.conf file and return a triple of
 
60
    the URL, version, and whether updates should be checked."""
 
61
    no_config = (None, None, None, False)
 
62
    url, version, channel, check = no_config
 
63
 
 
64
    files = [os.path.join(dir, UPDATE_CONFIG_NAME)
 
65
             for dir in load_config_paths("ubuntuone")]
 
66
 
 
67
    conf = SafeConfigParser()
 
68
    read_files = conf.read(files)
 
69
 
 
70
    if not read_files:
 
71
        logger.debug(
 
72
            "Unable to find an update.conf file - not checking for updates")
 
73
        return no_config
 
74
 
 
75
    try:
 
76
        url = conf.get("Update", "url")
 
77
        version = conf.get("Update", "version_id")
 
78
        channel = conf.get("Update", "channel")
 
79
        check = conf.getint("Update", "check_for_updates")
 
80
    except (NoSectionError, NoOptionError) as e:
 
81
        logger.debug("could not read update.conf - not checking for updates")
 
82
        logger.debug("   (exception was %r)" % e)
 
83
        return no_config
 
84
 
 
85
    return url, version, channel, check
 
86
 
 
87
 
 
88
@defer.inlineCallbacks
 
89
def call_updater(custom_call=None, install=False):
 
90
    """Call the Ubuntu One updater."""
 
91
    result = False
 
92
    retcode = None
 
93
    try:
 
94
        updater_cmd = get_bin_cmd(UPDATE_BIN_NAME)
 
95
    except OSError, e:
 
96
        logger.debug("Could not find updater command: %r" % e)
 
97
        defer.returnValue(result)
 
98
 
 
99
    url, local_version, channel, should_check = get_update_config()
 
100
 
 
101
    if not should_check:
 
102
        logger.debug("ignoring update check")
 
103
        defer.returnValue(result)
 
104
 
 
105
    args = ["--url", url,
 
106
            "--current", str(local_version),
 
107
            "--release", channel]
 
108
    if install:
 
109
        args.append("--install")
 
110
 
 
111
    exe = updater_cmd[0]
 
112
    args = updater_cmd[1:] + args
 
113
 
 
114
    logger.debug("calling %s update process: %r args %r",
 
115
                 "custom" if custom_call else "regular",
 
116
                 exe, args)
 
117
 
 
118
    if custom_call:
 
119
        custom_call(exe, args)
 
120
    else:
 
121
        # updater returns 1 if available, zero if not
 
122
        retcode = yield getProcessValue(exe, args)
 
123
        result = retcode == 1
 
124
 
 
125
    logger.debug('call_updater: %s, updater cmd: %r, return code: %r,'
 
126
                 ' result: %r', "installing" if install else "checking",
 
127
                 updater_cmd, retcode, result)
 
128
    defer.returnValue(result)