~system76-dev/system76-driver/2.1

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
#
## System76, Inc.
## Copyright System76, Inc.
## Released under the GNU General Public License (See LICENSE)
##
## Restores machine with base_system.py module

import os
import sys
import time
import model
import base_system
import driverscontrol
import gobject
import threading

try:
     import pygtk
     pygtk.require("2.0")
except:
      pass
try:
    import gtk
    import gtk.glade
except:
    sys.exit(1)

#Initializing the gtk's thread engine
gtk.gdk.threads_init()

#Keep track of thread PID's
restoret1, restoret2 = 0, 0

#Setup Source Directory
datadir = os.path.join(os.path.dirname(__file__),'.')
IMAGEDIR = os.path.join(os.path.dirname(__file__), 'images')
WINDOW_ICON = os.path.join(IMAGEDIR, '76icon.png')

class pulseSetter(threading.Thread):
    """This class sets the fraction of the progressbar"""
    
    # Set restoret2 to the PID for this thread
    global restoret2
    restoret2 = os.getpid()
    
    #Thread event, stops the thread if it is set.
    stopthread = threading.Event()
    
    def run(self):
        """Run method, this is the code that runs while thread is alive."""
        
        #Importing the progressbar widget from the global scope
        global progress_bar 
        
        #While the stopthread event isn't setted, the thread keeps going on
        while not self.stopthread.isSet() :
            # Acquiring the gtk global mutex
            gtk.gdk.threads_enter()
            #Setting pulse
            progress_bar.pulse()
            # Releasing the gtk global mutex
            gtk.gdk.threads_leave()
            #Delaying 100ms until the next iteration
            time.sleep(0.1)
            
    def stop(self):
        """Stop method, sets the event to terminate the thread's main loop"""
        self.stopthread.set()

class progressWindow:
    def __init__(self):
        #setup the glade file
        self.datadir = datadir
        self.wTree = gtk.glade.XML(os.path.join(self.datadir, 'system76driver.glade'), 'restoreDialog')
    
    def run(self):
        
        #Get the actual dialog widget
        self.dlg = self.wTree.get_widget("restoreDialog")
        #Get the progress bar widget
        self.progress_bar = self.wTree.get_widget("restoreProgress")
        #Set window logo
        self.icon = gtk.gdk.pixbuf_new_from_file(os.path.join(WINDOW_ICON))
        self.dlg.set_icon(self.icon)

        #Set Global Variables
        global restoreDialog
        restoreDialog = self.dlg
        
        global progress_bar
        progress_bar = self.progress_bar
        
        #run the dialog      
        self.dlg.run()
        
        #we are done with the dialog, destroy it
        self.dlg.destroy()

class restore(threading.Thread):
    """This class restores the system"""
    
    global restoreDialog

    # Set restoret1 to the PID for this thread
    global restoret1
    restoret1 = os.getpid()
    
    #ps starts and stops the progress bar thread
    global ps
    #Thread event, stops the thread if it is set.
    stopthread = threading.Event()
    
    def run(self):
        """Thread install base system."""
        
        #While the stopthread event isn't set, the thread keeps going
        while not self.stopthread.isSet():
            base_system.app_install()
            driverscontrol.installDrivers()
            ps.stop()
            self.stop()
            
    def stop(self):
        """Stop method, sets the event to terminate the thread's main loop"""
        self.stopthread.set()
        ps.stop()
        restoreDialog.destroy()
        complete = restoreComplete()
        complete.run()
        
class restoreComplete:
    def __init__(self):
        #setup the glade file
        self.datadir = datadir
        self.wTree = gtk.glade.XML(os.path.join(self.datadir, 'system76driver.glade'), 'restoreComplete')
    
    def run(self):
        
        #Get the actual dialog widget
        self.dlg = self.wTree.get_widget("restoreComplete")
        #Set window logo
        self.icon = gtk.gdk.pixbuf_new_from_file(os.path.join(WINDOW_ICON))
        self.dlg.set_icon(self.icon)

        #run the dialog
        self.dlg.run()
        
        #we are done with the dialog, destroy it
        self.dlg.destroy()
        os.popen("kill -9 "+str(restoret1))
        os.popen("kill -9 "+str(restoret2))
        sys.exit(0)
       
def start():
    global ps
    ps = pulseSetter()
    ps.start()
    global r
    r = restore()
    r.start()
    startRestore = progressWindow()
    startRestore.run()