aboutsummaryrefslogtreecommitdiff
path: root/addons/service.xbmc.versioncheck/lib/shellhandlerapt.py
blob: f539feeb5a9deba4877250ec7a3830a7ad32366d (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- 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:
    from subprocess import check_output
    from subprocess import call
except:
    log('subprocess import error')


class ShellHandlerApt:

    _pwd = ""

    def __init__(self, usesudo=False):
        self.sudo = usesudo
        installed, candidate = self._check_versions("xbmc", False)
        if not installed:
            # there is no package installed via repo, so we exit here
            log("No installed package found, exiting")
            import sys
            sys.exit(0)

    def _check_versions(self, package, update=True):
        _cmd = "apt-cache policy " + package

        if update and not self._update_cache():
            return False, False

        try:
            result = check_output([_cmd], shell=True).split("\n")
        except Exception as error:
            log("ShellHandlerApt: exception while executing shell command %s: %s" %(_cmd, error))
            return False, False

        if result[0].replace(":", "") == package:
            installed = result[1].split()[1]
            candidate = result[2].split()[1]
            if installed == "(none)":
                installed = False
            if candidate == "(none)":
                candiate = False
            return installed, candidate
        else:
            log("ShellHandlerApt: error during version check")
            return False, False

    def _update_cache(self):
        _cmd = 'apt-get update'
        try:
            if self.sudo:
                x = check_output('echo \'%s\' | sudo -S %s' %(self._getpassword(), _cmd), shell=True)
            else:
                x = check_output(_cmd.split())
        except Exception as error:
            log("Exception while executing shell command %s: %s" %(_cmd, error))
            return False

        return True

    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):
        _cmd = "apt-get install -y " + package
        try:
            if self.sudo:
                x = check_output('echo \'%s\' | sudo -S %s' %(self._getpassword(), _cmd), shell=True)
            else:
                x = check_output(_cmd.split())
            log("Upgrade successful")
        except Exception as error:
            log("Exception while executing shell command %s: %s" %(_cmd, error))
            return False

        return True

    def upgrade_system(self):
        _cmd = "apt-get upgrade -y"
        try:
            log("Upgrading system")
            if self.sudo:
                x = check_output('echo \'%s\' | sudo -S %s' %(self._getpassword(), _cmd), shell=True)
            else:
                x = check_output(_cmd.split())
        except Exception as error:
            log("Exception while executing shell command %s: %s" %(_cmd, error))
            return False

        return True

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