Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/util.py
blob: 7e03ccb1cd9592e5297f92e2607912ee67600a52 (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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python
# Copyright (C) 2009, George Hunt <georgejhunt@gmail.com>
# Copyright (C) 2009, One Laptop Per Child
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

from __future__ import with_statement
import os
from subprocess import Popen, PIPE

from gettext import gettext as _

#major packages
import gtk
import hashlib
import time
import logging

#from jarabe.model import shell
#home_model = shell.get_model()

#sugar stuff
from sugar.graphics.alert import *

logging.basicConfig()
_logger = logging.getLogger('vimtutor')
_logger.setLevel(logging.DEBUG)

__pdb = None

class Utilities():
    def __init__(self,activity):
        self._activity = __pdb = activity
        self.home_model = None
        
    #####################            ALERT ROUTINES   ##################################
    
    def alert(self,msg,title=None):
        alert = NotifyAlert(0)
        if title != None:
            alert.props.title=_('There is no Activity file')
        alert.props.msg = msg
        alert.connect('response',self.no_file_cb)
        self._activity.add_alert(alert)
        return alert
        
    def no_file_cb(self,alert,response_id):
        self._activity.remove_alert(alert)

    from sugar.graphics.alert import ConfirmationAlert
  
    def confirmation_alert(self,msg,title=None,confirmation_cb = None):
        alert = ConfirmationAlert()
        alert.props.title=title
        alert.props.msg = msg
        alert.pydebug_cb = confirmation_cb
        alert.connect('response', self._alert_response_cb)
        self._activity.add_alert(alert)
        return alert

    #### Method: _alert_response_cb, called when an alert object throws a
                 #response event.
    def _alert_response_cb(self, alert, response_id):
        #remove the alert from the screen, since either a response button
        #was clicked or there was a timeout
        this_alert = alert  #keep a reference to it
        self._activity.remove_alert(alert)
        #Do any work that is specific to the type of button clicked.
        if response_id is gtk.RESPONSE_OK and this_alert.pydebug_cb != None:
            this_alert.pydebug_cb (this_alert, response_id)
            
        
    def set_dirty(self, dirty):
        self.dirty = dirty

    def md5sum_buffer(self, buffer, hash = None):
        if hash == None:
            hash = hashlib.md5()
        hash.update(buffer)
        return hash.hexdigest()

    def md5sum(self, filename, hash = None):
        """return hexdigest of single file"""
        h = self._md5sum(filename,hash)
        return h.hexdigest()
       
    def _md5sum(self, filename, hash = None):
        """return hash of single file"""
        if hash == None:
            hash = hashlib.md5()
        try:
            fd = None
            fd =  open(filename, 'rb')
            while True:
                block = fd.read(128)
                if not block: break
                hash.update(block)
        finally:
            if fd != None:
                fd.close()
        return hash
    
    def md5sum_tree(self, root):
        """return hexdigest of file tree under root"""
        if not (root and os.path.isdir(root)):
            return None
        h = hashlib.md5()
        for dirpath, dirnames, filenames in os.walk(root):
            for filename in filenames:
                abs_path = os.path.join(dirpath, filename)
                h = self._md5sum(abs_path, h)
                #print abs_path
        return h.hexdigest()
    
    def set_permissions(self,root, perms='664'):
        """walk a directory setting permissions"""
        if not os.path.isdir(root):
            return None
        for dirpath, dirnames, filenames in os.walk(root):
            for filename in filenames:
                abs_path = os.path.join(dirpath, filename)
                old_perms = os.stat(abs_path).st_mode
                if os.path.isdir(abs_path):
                    new_perms = int(perms,8) | int('771',8)
                else:
                    new_perms = old_perms | int(perms,8)
                os.chmod(abs_path,new_perms)
    
    def sugar_version(self):
        """return list with four elements (error=0) (major,minor,micro,release)"""
        cmd = 'rpm -q sugar'
        reply,err = self.command_line(cmd)
        if reply and reply.find('sugar') > -1:
            version = reply.split('-')[1]
            version_chunks = version.split('.')
            release_holder = reply.split('-')[2]
            release = release_holder.split('.')[0]
            return (int(version_chunks[0]),int(version_chunks[1]),\
                    int(version_chunks[2]),int(release),)
        return (0, 0, 0, 0)

    def command_line(self, cmd, alert_error=True):
        """send cmd line to shell, rtn (text,error code)"""
        _logger.debug('command_line cmd:%s'%cmd)
        p1 = Popen(cmd,stdout=PIPE, shell=True)
        output = p1.communicate()
        if p1.returncode != 0 :
            _logger.debug('error returned from shell command: %s was %s'%(cmd,output[0]))
            if alert_error: self.alert(_('%s Command returned non zero\n'%cmd+output[0]))
        return output[0],p1.returncode
        
    def non_conflicting(self,root,basename):
        """
        create a non-conflicting filename by adding '-<number>' to a filename before extension
        """
        ext = ''
        basename = basename.split('.')
        word = basename[0]
        if len(basename) > 1:
            ext = '.' + basename[1]
        adder = ''
        index = 0
        while (os.path.isfile(os.path.join(root, word + adder + ext)) or 
                                os.path.isdir(os.path.join(root, word + adder + ext))):
            index += 1
            adder = '-%s'%index
        _logger.debug('non conflicting:%s'%os.path.join(root, word + adder +  ext))
        return os.path.join(root, word + adder + ext)
    
    def get_home_model(self):
        """Use shell model to return home_model
           --the home_model code changed between .82 and .84 sugar
           --so do the lookup differently depending on sugar version
        """
        global home_model
        (major, minor, micro, release) = self.sugar_version() 
        _logger.debug('sugar version %s'%minor)
        
        if minor and minor >= 84:
            _logger.debug('using jarabe')
            from jarabe.model import shell
            home_model = shell.get_model()
        else:
            if not '/usr/share/sugar/shell/' in sys.path:
                sys.path.append('/usr/share/sugar/shell/')
            import view.Shell
            instance = view.Shell.get_instance()
            home_model = instance.get_model().get_home()
        if home_model:
            return home_model
        else:
            _logger.error('failed to retrieve home model')
            return None
        
        return home_model
        
    def get_activity_from_activity_id(self, activity_id):
        if not self.home_model:
            self.home_model = self.get_home_model()
        if self.home_model:
            return self.home_model._get_activity_by_id(activity_id)
        return None
    
    def get_wnck_window_from_activity_id(self, activity_id):
        """use sugar home_model to get X11 window specified by activity_id"""
        _logger.debug('entered get_wnck_window_from_activity_id. id:%s'%activity_id)
        activity = self.get_activity_from_activity_id()
        if activity:
            return activity.get_window()
        else:
            _logger.debug('wnck_window was none')
            return None
        
    def get_wnck_window_from_bundle_id(self, bundle_id):
        """get the window associated with bundle_id or None"""
        if not self.home_model:
            self.home_model = self.get_home_model()
        if self.home_model:
            activity_list = self.home_model._get_activities_with_window()
            _logger.debug('length of activity_list:%s'%(len(activity_list,)))
            if activity_list:
                for activity in activity_list:
                    if activity.get_type == bundle_id:
                        return activity.get_window()
            _logger.debug("failed to find %s"%(bundle_id,))
            return None
        
    def get_wnck_window_from_xid(self, xid):
        """select shell activity, get window, via xid"""
        if not self.home_model:
            self.home_model = self.get_home_model()
        if self.home_model:
            return self.home_model._get_activity_by_xid(xid).get_window()
        else:
            _logger.debug('failed to get window associated with xid:%s'%(xid,))
            return None
        
    def get_xid(self):
        """return the X11 window id of the main window"""
        screen = gtk.gdk.screen_get_default()
        root = screen.get_root_window()
        if root:
            return root.xid
        else:
            _logger.debug('failed to get xid')
            return None
        
    def activate_xid(self,xid):
        """ use the shell model to look up the window from xid, activat window"""
        window = self.get_wnck_window_from_xid(xid)
        if window:
            window.activate(gtk.get_current_event_time())
        else:
            _logger.debug('failed to get window from xid:%s'%(xid,))
            
    def activate_bundle_id(self, bundle_id):
        """get shell activity, look up window, and activate"""
        window = self.get_wnck_window_from_bundle_id(bundle_id)
        if window:
            window.activate(gtk.get_current_event_time())
        
        
    def get_current_activity(self):
        """returns the shell activity object for current window"""
        if not self.home_model:
            self.home_model = self.get_home_model()
        if self.home_model:
            return self.home_model.get_activity_by_xid(self.get_xid())
        else:
            return None