~alessandrofac93/bumblebee-config-gui/0.6

« back to all changes in this revision

Viewing changes to bumblebee_config/AptHelper.py

  • Committer: Alessandro Facciorusso
  • Date: 2013-04-19 18:46:47 UTC
  • Revision ID: alessandrofac93@gmail.com-20130419184647-moiaphfw56412ja0

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
import sys
9
9
import threading
10
10
 
 
11
from gettext import gettext as _
 
12
 
11
13
try:
12
14
    import apt
13
 
    import platform
14
15
    from softwareproperties.SoftwareProperties import SoftwareProperties
15
16
except:
16
17
    imported = False
23
24
    def __init__(self, updateCallback):
24
25
        self.updateCallback = updateCallback
25
26
 
26
 
    def start(self):
27
 
        pass
28
 
 
29
 
    def stop(self):
30
 
        pass
31
 
 
32
 
    def updateStatus(self, uri, descr, shortDescr, status):
33
 
        pass
34
 
 
35
 
    def update_status_full(self, uri, descr, shortDescr, status, fileSize,
36
 
                           partialSize):
37
 
        pass
38
 
 
39
27
    def pulse(self):
40
28
        self.updateCallback(self.percent)
41
29
        return True
43
31
 
44
32
class TextInstallProgress(apt.progress.DumbInstallProgress):
45
33
 
46
 
    def __init__(self, updateCallback):
 
34
    def __init__(self, updateCallback, doNotFinish=False):
47
35
        super(TextInstallProgress, self).__init__()
48
36
 
 
37
        self.doNotFinish = doNotFinish
49
38
        self.updateCallback = updateCallback
50
39
        self.last = 0.0
51
40
 
54
43
        if self.last >= self.percent:
55
44
            return
56
45
 
57
 
        if self.last == 100:
58
 
            self.updateCallback(self.status, None)
59
 
        else:
60
 
            self.updateCallback(self.status, self.percent)
 
46
        self.updateCallback(self.status, self.percent)
61
47
        self.last = self.percent
62
48
 
63
49
    def finishUpdate(self):
64
 
        self.updateCallback("__FINISHED__", self.percent)
 
50
        if not self.doNotFinish:
 
51
            self.updateCallback("__FINISHED__", self.percent)
65
52
 
66
53
 
67
54
class InstallBumblebeeThread(threading.Thread):
68
55
 
69
 
    def __init__(self, downloadCb, installCb):
 
56
    def __init__(self, downloadCb, installCb, removeFirst=False):
70
57
        super(InstallBumblebeeThread, self).__init__()
 
58
        self.removeFirst = removeFirst
71
59
        self.downloadCb = downloadCb
72
60
        self.installCb = installCb
 
61
        self.softProp = SoftwareProperties()
73
62
 
74
63
    def run(self):
75
64
        cache = apt.Cache()
76
 
        # self.softProp = SoftwareProperties()
77
 
        # ppa = 'ppa:bumblebee/stable'
78
 
        # self.softProp.add_source_from_line(ppa)
79
 
        # self.softProp.sourceslist.save()
80
 
 
81
 
        # self.aptCache.update(apt.progress.TextFetchProgress())
 
65
 
 
66
        if self.removeFirst:
 
67
            cache.open(None)
 
68
            cache['bumblebee'].markDelete()
 
69
            cache.commit(UpdateFetchProgress(self.downloadCb), TextInstallProgress(self.installCb, True))
 
70
            cache.close()
 
71
 
 
72
        ppa = 'ppa:bumblebee/stable'
 
73
        self.softProp.remove_source(ppa)
 
74
        self.softProp.add_source_from_line(ppa)
 
75
        self.softProp.sourceslist.save()
 
76
 
 
77
        cache.update(UpdateFetchProgress(self.downloadCb))
82
78
 
83
79
        cache.open(None)
84
 
        cache['bumblebee'].mark_install()
85
 
        cache['bumblebee-nvidia'].mark_install()
86
 
        cache['linux-headers-generic'].mark_install()
87
 
        cache['primus'].mark_install()
88
 
        cache.commit(UpdateFetchProgress(self.downloadCb), TextInstallProgress(self.installCb))
 
80
        cache['bumblebee'].markInstall()
 
81
        cache['bumblebee-nvidia'].markInstall()
 
82
        cache['linux-headers-generic'].markInstall()
 
83
        cache['primus'].markInstall()
 
84
        cache.commit(UpdateFetchProgress(self.downloadCb), TextInstallProgress(self.installCb, False))
89
85
        cache.close()
90
86
 
91
87
 
92
88
class AptHelper(object):
93
89
 
94
 
    def __init__(self):
95
 
        if imported:
96
 
            self.softProp = SoftwareProperties()
97
 
 
98
90
    def checkBumblebeeInstalled(self):
99
91
        if imported:
100
92
            cache = apt.Cache()
101
 
            if platform.linux_distribution()[0].lower() == "ubuntu":
102
 
                res = None
103
 
                if cache['bumblebee'].is_installed:
104
 
                    res = False
105
 
                else:
106
 
                    res = True
107
 
                cache.close()
108
 
                return res
109
 
        else:
110
 
            return None
 
93
 
 
94
            res = False
 
95
            if cache['bumblebee'].isInstalled:
 
96
                res = True
 
97
            else:
 
98
                res = False
 
99
            cache.close()
 
100
            return res
111
101
 
112
102
    """
113
103
        downloadCb: (percent)
114
104
        installCb: (msg, percent)
115
105
    """
116
 
    def installBumblebee(self, downloadCb, installCb):
 
106
    def installBumblebee(self, downloadCb, installCb, removeFirst=False):
117
107
        if imported:
118
 
            thread = InstallBumblebeeThread(downloadCb, installCb)
 
108
            thread = InstallBumblebeeThread(removeFirst, downloadCb, installCb)
119
109
            thread.start()