~alessandrofac93/bumblebee-config-gui/0.6

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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@author: Alessandro Facciorusso
"""

import sys
import threading

from gettext import gettext as _

try:
    import apt
    from softwareproperties.SoftwareProperties import SoftwareProperties
except:
    imported = False
else:
    imported = True


class UpdateFetchProgress(apt.FetchProgress):

    def __init__(self, updateCallback):
        self.updateCallback = updateCallback

    def pulse(self):
        self.updateCallback(self.percent)
        return True


class TextInstallProgress(apt.progress.DumbInstallProgress):

    def __init__(self, updateCallback, doNotFinish=False):
        super(TextInstallProgress, self).__init__()

        self.doNotFinish = doNotFinish
        self.updateCallback = updateCallback
        self.last = 0.0

    def updateInterface(self):
        apt.progress.DumbInstallProgress.updateInterface(self)
        if self.last >= self.percent:
            return

        self.updateCallback(self.status, self.percent)
        self.last = self.percent

    def finishUpdate(self):
        if not self.doNotFinish:
            self.updateCallback("__FINISHED__", self.percent)


class InstallBumblebeeThread(threading.Thread):

    def __init__(self, downloadCb, installCb, removeFirst=False):
        super(InstallBumblebeeThread, self).__init__()
        self.removeFirst = removeFirst
        self.downloadCb = downloadCb
        self.installCb = installCb
        self.softProp = SoftwareProperties()

    def run(self):
        cache = apt.Cache()

        if self.removeFirst:
            cache.open(None)
            cache['bumblebee'].markDelete()
            cache.commit(UpdateFetchProgress(self.downloadCb), TextInstallProgress(self.installCb, True))
            cache.close()

        ppa = 'ppa:bumblebee/stable'
        self.softProp.remove_source(ppa)
        self.softProp.add_source_from_line(ppa)
        self.softProp.sourceslist.save()

        cache.update(UpdateFetchProgress(self.downloadCb))

        cache.open(None)
        cache['bumblebee'].markInstall()
        cache['bumblebee-nvidia'].markInstall()
        cache['linux-headers-generic'].markInstall()
        cache['primus'].markInstall()
        cache.commit(UpdateFetchProgress(self.downloadCb), TextInstallProgress(self.installCb, False))
        cache.close()


class AptHelper(object):

    def checkBumblebeeInstalled(self):
        if imported:
            cache = apt.Cache()

            res = False
            if cache['bumblebee'].isInstalled:
                res = True
            else:
                res = False
            cache.close()
            return res

    """
        downloadCb: (percent)
        installCb: (msg, percent)
    """
    def installBumblebee(self, downloadCb, installCb, removeFirst=False):
        if imported:
            thread = InstallBumblebeeThread(removeFirst, downloadCb, installCb)
            thread.start()