1
# -*- coding: utf-8 -*-
3
# Copyright 2012 Canonical Ltd.
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.
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.
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/>.
17
"""Platform - independent support code for utils."""
23
from configparser import (SafeConfigParser, NoSectionError, NoOptionError)
25
from ConfigParser import (SafeConfigParser, NoSectionError, NoOptionError)
27
from twisted.internet import defer
28
from twisted.internet.utils import getProcessValue
29
from twisted.python import procutils
31
from dirspec.basedir import load_config_paths
32
from dirspec.utils import get_program_path
34
from ubuntuone.controlpanel.utils import logger
36
UPDATE_BIN_NAME = 'ubuntuone-updater'
37
UPDATE_CONFIG_NAME = 'update.conf'
38
DARWIN_APP_NAMES = {UPDATE_BIN_NAME: 'UbuntuOne Updater.app'}
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,
47
cmd_args = [get_program_path(exe_name,
48
fallback_dirs=[fallback_dir],
49
app_names=DARWIN_APP_NAMES)]
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])
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
64
files = [os.path.join(dir, UPDATE_CONFIG_NAME)
65
for dir in load_config_paths("ubuntuone")]
67
conf = SafeConfigParser()
68
read_files = conf.read(files)
72
"Unable to find an update.conf file - not checking for updates")
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)
85
return url, version, channel, check
88
@defer.inlineCallbacks
89
def call_updater(custom_call=None, install=False):
90
"""Call the Ubuntu One updater."""
94
updater_cmd = get_bin_cmd(UPDATE_BIN_NAME)
96
logger.debug("Could not find updater command: %r" % e)
97
defer.returnValue(result)
99
url, local_version, channel, should_check = get_update_config()
102
logger.debug("ignoring update check")
103
defer.returnValue(result)
105
args = ["--url", url,
106
"--current", str(local_version),
107
"--release", channel]
109
args.append("--install")
112
args = updater_cmd[1:] + args
114
logger.debug("calling %s update process: %r args %r",
115
"custom" if custom_call else "regular",
119
custom_call(exe, args)
121
# updater returns 1 if available, zero if not
122
retcode = yield getProcessValue(exe, args)
123
result = retcode == 1
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)