aboutsummaryrefslogtreecommitdiff
path: root/addons/service.xbmc.versioncheck/lib/aptdeamonhandler.py
blob: 012312758c7017aa0b91e9efa38090ca857c9777 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# -*- coding: utf-8 -*-
#
#     Copyright (C) 2013 Team-XBMC
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import xbmc
from common import *

try:
    #import apt
    import apt
    from aptdaemon import client
    from aptdaemon import errors
except:
    log('python apt import error')

class AptdeamonHandler:

    def __init__(self):
        self.aptclient = client.AptClient()

    def _check_versions(self, package):
        if not self._update_cache():
            return False, False
        try:
            trans = self.aptclient.upgrade_packages([package])
            #trans = self.aptclient.upgrade_packages("bla")
            trans.simulate(reply_handler=self._apttransstarted, error_handler=self._apterrorhandler)
            pkg = trans.packages[4][0]
            if pkg == package:
               cache=apt.Cache()
               cache.open(None)
               cache.upgrade()
               if cache[pkg].installed:
                   return cache[pkg].installed.version, cache[pkg].candidate.version

            return False, False

        except Exception as error:
            log("Exception while checking versions: %s" %error)
            return False, False

    def _update_cache(self):
        try:
            if self.aptclient.update_cache(wait=True) == "exit-success":
                return True
            else:
                return False
        except errors.NotAuthorizedError:
            log("You are not allowed to update the cache")
            return False

    def check_upgrade_available(self, package):
        '''returns True if newer package is available in the repositories'''
        installed, candidate = self._check_versions(package)
        if installed and candidate:
            if installed != candidate:
                log("Version installed  %s" %installed)
                log("Version available  %s" %candidate)
                return True
            else:
                log("Already on newest version")
        elif not installed:
                log("No installed package found")
                return False
        else:
            return False

    def upgrade_package(self, package):
        try:
            log("Installing new version")
            if self.aptclient.upgrade_packages([package], wait=True) == "exit-success":
                log("Upgrade successful")
                return True
        except Exception as error:
            log("Exception during upgrade: %s" %error)
        return False

    def upgrade_system(self):
        try:
            log("Upgrading system")
            if self.aptclient.upgrade_system(wait=True) == "exit-success":
                return True
        except Exception as error:
            log("Exception during system upgrade: %s" %error)
        return False

    def _getpassword(self):
        if len(self._pwd) == 0:
            self._pwd = get_password_from_user()
        return self._pwd

    def _apttransstarted(self):
        pass
    
    def _apterrorhandler(self, error):
        log("Apt Error %s" %error)