~ubuntu-branches/ubuntu/natty/mago/natty

« back to all changes in this revision

Viewing changes to mago/application/update_manager.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2011-02-08 13:32:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110208133213-m1og7ey0m990chg6
Tags: 0.3+bzr20-0ubuntu1
* debian/rules:
  - updated to debhelper 7
  - use dh_python2 instead of python-central
* debian/pycompat:
  - removed, no longer needed
* debian/control:
  - dropped cdbs and python-central dependencies
* bzr snapshot of the current trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
PACKAGE = "mago"
2
 
 
3
 
"""
4
 
This is the "update_manager" module.
5
 
 
6
 
This module provides a wrapper for LDTP to make the writing of Update Manager
7
 
tests easier.
8
 
"""
9
 
import ldtp , ooldtp
10
 
import re
11
 
from .main import Application
12
 
from .policykit import PolicyKit
13
 
from ..cmd import globals
14
 
import gettext
15
 
 
16
 
gettext.install (True)
17
 
gettext.bindtextdomain (PACKAGE, globals.LOCALE_SHARE)
18
 
gettext.textdomain (PACKAGE)
19
 
t = gettext.translation(PACKAGE, globals.LOCALE_SHARE, fallback = True)
20
 
_ = t.gettext
21
 
 
22
 
 
23
 
class UpdateManager(Application):
24
 
    """
25
 
    UpdateManager class manages the Ubuntu Update Manager application.
26
 
    
27
 
    If the test is going to need admin permissions, you need to provide the su password
28
 
    when creating an instance of the class.
29
 
    
30
 
    i.e. C{updateManager = UpdateManager("my_password")}
31
 
    """
32
 
    MNU_ITEM        = _("mnuUpdateManager")
33
 
    WINDOW          = _("frmUpdateManager")
34
 
    LAUNCHER        = "update-manager"
35
 
    BTN_CLOSE       = _("btnClose")
36
 
    BTN_CHECK       = _("btnCheck")
37
 
    BTN_INSTALL     = _("btnInstallUpdates")
38
 
    TBL_UPDATES     = _("updates")
39
 
    BAN_LIST        = _(" updates")
40
 
    TAB_CHANGES     = _("Changes")
41
 
    TXT_DESCRIPTION = _("Description")
42
 
    LBL_WAIT        = _("lblKeepyoursystemup-to-date")
43
 
    LBL_UPTODATE    = _("lblYoursystemisup-to-date")
44
 
    LBL_N_UPDATES   = _(r'lbl(\d+)updates?hasbeenselected')
45
 
    LBL_DOWNLOADSIZE = _(r'lblDownloadsize((\d+)((\.)(\d+))?)(.*)') 
46
 
 
47
 
 
48
 
    
49
 
    def __init__(self, password = ""):
50
 
        """
51
 
        UpdateManager class init method
52
 
        
53
 
        If the test is going to need admin permissions, you need to provide the su password
54
 
        when creating an instance of the class.
55
 
        
56
 
        i.e. C{updateManager = UpdateManager("my_password")}
57
 
        
58
 
        @type password: string
59
 
        @param password: User's password for administrative tasks.
60
 
 
61
 
        """
62
 
        Application.__init__(self)
63
 
        self.password = password
64
 
 
65
 
    def set_password(self, password):
66
 
        self.password = password
67
 
 
68
 
    def open(self, dist_upgrade=False):
69
 
        """
70
 
        It opens the update-manager application and raises an error if the application
71
 
        didn't start properly.
72
 
 
73
 
        """
74
 
 
75
 
        if dist_upgrade:
76
 
            ldtp.launchapp(self.LAUNCHER, ['-d'], 0)
77
 
            response = ldtp.waittillguiexist(self.name, '', 20)
78
 
    
79
 
            if response == 0:
80
 
                raise ldtp.LdtpExecutionError, "The " + self.name + " window was not found."    
81
 
 
82
 
        else:
83
 
            Application.open(self)
84
 
 
85
 
        # Wait the population of the list
86
 
        updateManager = ooldtp.context(self.name)
87
 
 
88
 
        populating = True
89
 
 
90
 
        while populating:
91
 
            populating = False
92
 
            self.remap()
93
 
 
94
 
            label = updateManager.getchild(role = 'label')
95
 
            for i in label:
96
 
                label_name = i.getName()
97
 
                if label_name == self.LBL_WAIT:
98
 
                    populating = True
99
 
        
100
 
    def close(self):
101
 
        """
102
 
        It closes the update-manager window using the close button.
103
 
        """
104
 
 
105
 
        updateManager = ooldtp.context(self.name)
106
 
    
107
 
        closeButton = updateManager.getchild(self.BTN_CLOSE)
108
 
        closeButton.click()
109
 
        
110
 
        ldtp.waittillguinotexist (self.name)
111
 
           
112
 
    def number_updates(self):
113
 
        """
114
 
        With the available repositories, it returns the number of available
115
 
        updates for the system. 
116
 
 
117
 
        @return: An integer with the number of available updates.
118
 
        
119
 
        """
120
 
        updateManager = ooldtp.context(self.name)
121
 
 
122
 
        label = updateManager.getchild(role = 'label')
123
 
        for i in label:
124
 
            label_name = i.getName()
125
 
            if label_name == self.LBL_UPTODATE:
126
 
                return 0
127
 
            else:
128
 
                groups = re.match(self.LBL_N_UPDATES, label_name)
129
 
                if groups:
130
 
                    number = groups.group(1)
131
 
                    return int(number)
132
 
        return 0
133
 
 
134
 
    def download_size(self):
135
 
        """
136
 
        It returns the download size of the selected updates.
137
 
 
138
 
        @return: A float with the download size in bytes 
139
 
        """
140
 
        updateManager = ooldtp.context(self.name) 
141
 
 
142
 
        label = updateManager.getchild(role = 'label')
143
 
        for i in label:
144
 
            label_name = i.getName()
145
 
            groups = re.match(self.LBL_DOWNLOADSIZE, label_name)
146
 
                
147
 
            if groups:
148
 
                # Calculate size based on the tag after the number
149
 
                tag_size = groups.group(6)
150
 
                if tag_size == 'B':
151
 
                    size = 1
152
 
                elif tag_size == 'KB':
153
 
                    size = 1024
154
 
                elif tag_size == 'MB':
155
 
                    size = 1048576
156
 
                elif tag_size == 'GB':
157
 
                    size = 1073741824
158
 
                else:
159
 
                    size = 0
160
 
 
161
 
                total_size = float(groups.group(1)) * size
162
 
                return total_size
163
 
 
164
 
        return 0.0
165
 
 
166
 
    def select_all(self):
167
 
        """
168
 
        It selects all the available updates 
169
 
        """
170
 
        updateManager = ooldtp.context(self.name) 
171
 
 
172
 
        table  = updateManager.getchild(self.TBL_UPDATES, role = 'table')
173
 
        updates_table = table[0]
174
 
 
175
 
        for i in range(0, updates_table.getrowcount(), 1):
176
 
            updates_table.checkrow(i)
177
 
            ldtp.wait(1)
178
 
 
179
 
        return 0
180
 
 
181
 
    def unselect_all(self):
182
 
        """
183
 
        It unselects all the available updates 
184
 
        """
185
 
        updateManager = ooldtp.context(self.name) 
186
 
 
187
 
        table  = updateManager.getchild(self.TBL_UPDATES, role = 'table')
188
 
        updates_table = table[0]
189
 
 
190
 
        # TODO: When table admits right click, use the context menu
191
 
        self.remap()
192
 
        size_updates = self.download_size()
193
 
        while size_updates > 0:
194
 
            for i in range(0, updates_table.getrowcount(), 1):
195
 
                updates_table.uncheckrow(i)
196
 
                ldtp.wait(1)
197
 
            self.remap()
198
 
            size_updates = self.download_size()
199
 
 
200
 
        return 0
201
 
 
202
 
    def get_available_updates(self):
203
 
        """
204
 
        It gets the name of the packages of the available updates 
205
 
 
206
 
        @return: A list with the available updates
207
 
        """
208
 
 
209
 
        updateManager = ooldtp.context(self.name)
210
 
 
211
 
        available_updates = []
212
 
 
213
 
        table  = updateManager.getchild(self.TBL_UPDATES, role = 'table')
214
 
        updates_table = table[0]
215
 
 
216
 
        for i in range(0, updates_table.getrowcount(), 1):
217
 
            text = updates_table.getcellvalue(i, 1)
218
 
            candidate = text.split('\n')[0]
219
 
            if candidate.find(self.BAN_LIST) == -1:
220
 
                available_updates.append(candidate)
221
 
            ldtp.wait(1)
222
 
 
223
 
        return available_updates
224
 
 
225
 
    def select_update(self, name):
226
 
        """
227
 
        It selects a particular package in the list (not for update, just the list).
228
 
 
229
 
        @type name: string
230
 
        @param name: The name of the package to select
231
 
        """
232
 
 
233
 
        updateManager = ooldtp.context(self.name)
234
 
 
235
 
        table  = updateManager.getchild(self.TBL_UPDATES, role = 'table')
236
 
        updates_table = table[0]
237
 
 
238
 
        for i in range(0, updates_table.getrowcount(), 1):
239
 
            text = updates_table.getcellvalue(i, 1)
240
 
            candidate = text.split('\n')[0]
241
 
            if candidate == name:
242
 
                updates_table.selectrowindex(i)
243
 
                break
244
 
            ldtp.wait(1)
245
 
 
246
 
    def tick_update(self, name):
247
 
        """
248
 
        It selects a particular package for update.
249
 
 
250
 
        @type name: string
251
 
        @param name: The name of the package to select
252
 
        """
253
 
 
254
 
        updateManager = ooldtp.context(self.name)
255
 
 
256
 
        table  = updateManager.getchild(self.TBL_UPDATES, role = 'table')
257
 
        updates_table = table[0]
258
 
 
259
 
        for i in range(0, updates_table.getrowcount(), 1):
260
 
            text = updates_table.getcellvalue(i, 1)
261
 
            candidate = text.split('\n')[0]
262
 
            if candidate == name:
263
 
                updates_table.checkrow(i)
264
 
                break
265
 
            ldtp.wait(1)
266
 
 
267
 
    def check_updates(self):
268
 
        """
269
 
        It checks the repositories for new updates in the update-manager application.
270
 
        
271
 
        This action requires administrative permissions, therefore this method will
272
 
        raise an error if the UpdateManager instance was created without password.
273
 
        """
274
 
 
275
 
        updateManager = ooldtp.context(self.name)
276
 
        
277
 
        if self.password == "":
278
 
            raise ldtp.LdtpExecutionError, "Checking for updates requires administrative permissions."
279
 
 
280
 
        
281
 
        # We will need administrative permission
282
 
        polKit = PolicyKit(self.password)
283
 
 
284
 
        checkButton = updateManager.getchild(self.BTN_CHECK)
285
 
        checkButton.click()
286
 
 
287
 
        # Administrative permissions
288
 
        if polKit.wait():
289
 
            # HACK
290
 
            ldtp.wait(2)
291
 
            polKit.set_password()
292
 
        
293
 
        # HACK to wait for repositories
294
 
        ldtp.wait(20)
295
 
 
296
 
    def install_updates(self):
297
 
        """
298
 
        It installs the selected updates. 
299
 
        
300
 
        If no updates are available, it won't do anything.
301
 
        """
302
 
 
303
 
        updateManager = ooldtp.context(self.name)
304
 
 
305
 
        # If there is any update available, install it
306
 
        if self.number_updates() > 0:
307
 
            btnInstall = updateManager.getchild(self.BTN_INSTALL)
308
 
            
309
 
            if btnInstall.stateenabled():
310
 
                btnInstall.click()
311
 
 
312
 
                # We will need administrative permission
313
 
                polKit = PolicyKit(self.password)
314
 
 
315
 
               # Administrative permissions
316
 
                if polKit.wait():
317
 
                    # HACK
318
 
                    ldtp.wait(2)
319
 
                    polKit.set_password()
320
 
        
321
 
        # Wait for the the close button to be ready
322
 
        btnClose = updateManager.getchild(self.BTN_CLOSE)
323
 
        while not btnClose.stateenabled():
324
 
            ldtp.wait(5)
325
 
            
326
 
    def test_install_state(self):
327
 
        """
328
 
        It checks if the install button is enabled or not
329
 
 
330
 
        @return: True if the install button is enabled. False, otherwise.
331
 
        """
332
 
 
333
 
        updateManager = ooldtp.context(self.name)
334
 
        
335
 
        btnTest = updateManager.getchild(self.BTN_INSTALL)
336
 
        state = btnTest.stateenabled()
337
 
        
338
 
        return state
339
 
 
340
 
    def show_changes(self):
341
 
        """
342
 
        It shows the Description of the updates 
343
 
        """
344
 
        self.toggle_changes(True)
345
 
 
346
 
    def hide_changes(self):
347
 
        """
348
 
        It hides the Description of the updates 
349
 
        """
350
 
        self.toggle_changes(False)
351
 
 
352
 
    def get_description(self, name=''):
353
 
        """
354
 
        It returns the description of a package for a given update.
355
 
        If no update is mentioned, then the description for the
356
 
        selected application is returned.
357
 
 
358
 
        @type name: string
359
 
        @param name: The package to show the description. If left blank, the current
360
 
        selection will be chosen.
361
 
 
362
 
        @return: The decription of the packages, as shown in the application
363
 
        """
364
 
        updateManager = ooldtp.context(self.name)
365
 
        
366
 
        if name != '':
367
 
            self.select_update(name)
368
 
 
369
 
        # Get the description text field 
370
 
        text_field = updateManager.getchild(self.TXT_DESCRIPTION, role='text')
371
 
         # Get the text 
372
 
        text = ldtp.gettextvalue(self.name, text_field[0].getName())
373
 
        return text
374
 
        
375
 
        return '' 
376
 
 
377
 
    def get_changes(self, name=''):
378
 
        """
379
 
        It returns the changes description for a given update.
380
 
        If no update is mentioned, then the changes description for the
381
 
        selected application is returned.
382
 
 
383
 
        @type name: string
384
 
        @param name: The package to show changes. If left blank, the current
385
 
        selection will be chosen.
386
 
 
387
 
        @return: The decription of the changes
388
 
        """
389
 
        ooldtp.context(self.name)
390
 
        
391
 
        if name != '':
392
 
            self.select_update(name)
393
 
 
394
 
        # Get the filler tab
395
 
        filler = ldtp.getobjectproperty(self.name , self.TAB_CHANGES, 'children')
396
 
        # Get the scroll pane
397
 
        scroll_pane = ldtp.getobjectproperty(self.name , filler, 'children')
398
 
        # Get the text field
399
 
        text_field = ldtp.getobjectproperty(self.name , scroll_pane, 'children')
400
 
        text_field = text_field.split(' ')[0]
401
 
        # Get the text
402
 
        text = ldtp.gettextvalue(self.name, text_field)
403
 
        return text
404
 
        
405
 
        return '' 
406
 
 
407
 
    def toggle_changes(self, show):
408
 
        """
409
 
        It shows or hides the Description of the updates 
410
 
 
411
 
        @type show: boolean
412
 
        @param show: True, to show the description; False, to hide the description.
413
 
        """
414
 
        updateManager = ooldtp.context(self.name)
415
 
        
416
 
        toggle_button = updateManager.getchild(role='toggle button')
417
 
        state = toggle_button[0].verifytoggled()
418
 
 
419
 
        if state != show:
420
 
            toggle_button[0].click()
421
 
        
422
 
        return state