~ubuntu-branches/ubuntu/wily/update-manager/wily

« back to all changes in this revision

Viewing changes to UpdateManager/Core/utils.py

  • Committer: Package Import Robot
  • Author(s): Michael Terry
  • Date: 2013-01-24 14:20:22 UTC
  • Revision ID: package-import@ubuntu.com-20130124142022-cm69mix6n06ezorb
Tags: 1:0.178
* Implement the "available updates" details pane from the SoftwareUpdates
  spec.  Specifically, this adds grouping of related updates, adds an
  "Ubuntu base" group for core packages, and shows only the description
  summary in the main view.
* Show a restart icon next to packages that declare they will need a
  system restart via XB-Restart-Required: system

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# utils.py
2
2
# -*- Mode: Python; indent-tabs-mode: nil; tab-width: 4; coding: utf-8 -*-
3
3
#
4
 
#  Copyright (c) 2004-2008 Canonical
 
4
#  Copyright (c) 2004-2013 Canonical
5
5
#
6
 
#  Author: Michael Vogt <mvo@debian.org>
 
6
#  Authors: Michael Vogt <mvo@debian.org>
 
7
#           Michael Terry <michael.terry@canonical.com>
7
8
#
8
9
#  This program is free software; you can redistribute it and/or
9
10
#  modify it under the terms of the GNU General Public License as
27
28
from stat import (S_IMODE, ST_MODE, S_IXUSR)
28
29
from math import ceil
29
30
 
 
31
import apt
30
32
import apt_pkg
31
33
apt_pkg.init_config()
32
34
 
58
60
    from urlparse import urlsplit
59
61
 
60
62
from copy import copy
 
63
from gi.repository import GLib
61
64
 
62
65
 
63
66
class ExecutionTime(object):
402
405
        return None
403
406
 
404
407
 
405
 
def get_ubuntu_flavor():
 
408
def get_ubuntu_flavor(cache=None):
406
409
    """ try to guess the flavor based on the running desktop """
407
410
    # this will (of course) not work in a server environment,
408
411
    # but the main use case for this is to show the right
409
 
    # release notes
410
 
    # TODO: actually examine which meta packages are installed, like
411
 
    # DistUpgrade/DistUpgradeCache.py does and use that to choose a flavor.
412
 
    denv = os.environ.get("DESKTOP_SESSION", "")
413
 
    if "gnome" in denv:
414
 
        return "ubuntu"
415
 
    elif "kde" in denv:
416
 
        return "kubuntu"
417
 
    elif "xfce" in denv or "xubuntu" in denv:
418
 
        return "xubuntu"
419
 
    elif "LXDE" in denv or "Lubuntu" in denv:
420
 
        return "lubuntu"
421
 
    # default to ubuntu if nothing more specific is found
422
 
    return "ubuntu"
423
 
 
424
 
 
425
 
def get_ubuntu_flavor_name():
426
 
    flavor = get_ubuntu_flavor()
427
 
    if flavor == "kubuntu":
428
 
        return "Kubuntu"
429
 
    elif flavor == "xubuntu":
430
 
        return "Xubuntu"
431
 
    elif flavor == "lubuntu":
432
 
        return "Lubuntu"
 
412
    # release notes.
 
413
    pkg = get_ubuntu_flavor_package(cache=cache)
 
414
    return pkg.split('-', 1)[0]
 
415
 
 
416
 
 
417
def _load_meta_pkg_list():
 
418
    # This could potentially introduce a circular dependency, but the config
 
419
    # parser logic is simple, and doesn't rely on any UpdateManager code.
 
420
    from DistUpgrade.DistUpgradeConfigParser import DistUpgradeConfig
 
421
    parser = DistUpgradeConfig('/usr/share/ubuntu-release-upgrader')
 
422
    return parser.getlist('Distro', 'MetaPkgs')
 
423
 
 
424
 
 
425
def get_ubuntu_flavor_package(cache=None):
 
426
    """ try to guess the flavor metapackage based on the running desktop """
 
427
    # From spec, first if ubuntu-desktop is installed, use that.
 
428
    # Second, grab first installed one from DistUpgrade.cfg.
 
429
    # Lastly, fallback to ubuntu-desktop again.
 
430
    meta_pkgs = ['ubuntu-desktop']
 
431
 
 
432
    try:
 
433
        meta_pkgs.extend(sorted(_load_meta_pkg_list()))
 
434
    except Exception as e:
 
435
        print('Could not load list of meta packages:', e)
 
436
 
 
437
    if cache is None:
 
438
        cache = apt.Cache()
 
439
    for meta_pkg in meta_pkgs:
 
440
        cache_pkg = cache[meta_pkg] if meta_pkg in cache else None
 
441
        if cache_pkg and cache_pkg.is_installed:
 
442
            return meta_pkg
 
443
    return 'ubuntu-desktop'
 
444
 
 
445
 
 
446
def get_ubuntu_flavor_name(cache=None):
 
447
    """ try to guess the flavor name based on the running desktop """
 
448
    pkg = get_ubuntu_flavor_package(cache=cache)
 
449
    lookup = {'ubuntustudio-desktop': 'Ubuntu Studio'}
 
450
    if pkg in lookup:
 
451
        return lookup[pkg]
 
452
    elif pkg.endswith('-desktop'):
 
453
        return capitalize_first_word(pkg.rsplit('-desktop', 1)[0])
 
454
    elif pkg.endswith('-netbook'):
 
455
        return capitalize_first_word(pkg.rsplit('-netbook', 1)[0])
433
456
    else:
434
 
        return "Ubuntu"
 
457
        return 'Ubuntu'
435
458
 
436
459
 
437
460
# Unused by update-manager, but still used by ubuntu-release-upgrader
523
546
    return True
524
547
 
525
548
 
 
549
def capitalize_first_word(string):
 
550
    """ this uppercases the first word's first letter
 
551
    """
 
552
    if len(string) > 1 and string[0].isalpha() and not string[0].isupper():
 
553
        return string[0].capitalize() + string[1:]
 
554
    return string
 
555
 
 
556
 
 
557
def get_package_label(pkg):
 
558
    """ this takes a package synopsis and uppercases the first word's
 
559
        first letter
 
560
    """
 
561
    name = GLib.markup_escape_text(getattr(pkg.candidate, "summary", ""))
 
562
    return capitalize_first_word(name)
 
563
 
 
564
 
526
565
if __name__ == "__main__":
527
566
    #print(mirror_from_sources_list())
528
567
    #print(on_battery())