File: utils.py

package info (click to toggle)
aptoncd 0.1-1.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,572 kB
  • ctags: 544
  • sloc: python: 3,125; xml: 1,216; makefile: 125; sh: 6
file content (354 lines) | stat: -rw-r--r-- 10,670 bytes parent folder | download
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/python
# -*- coding: utf-8 -*-
# utils.py
#  
#  Author: Laudeci Oliveira <laudeci@gmail.com>
#          Rafael Proença   <cypherbios@ubuntu.com>
# 
#  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA

import apt_inst
import apt_pkg
import gtk
import math
import fnmatch
import os
import pygtk
import shutil
import urllib
import gnomevfs
import re
import tarfile
import bz2

import config

def isValidFileName(filename):
    try:
        p=re.compile('[^0-9A-Za-z_\+\.,-]+')
        if p.search(filename):
            return False
        else:
            return True
    except:
        return False
    
def updateUI():
    while gtk.events_pending(): gtk.main_iteration() 

#  Do case insensitive wildcard search on column 1.
def TreeViewSearch( model, column, key, it, data):
    key, title = key.lower(), model.get_value(it, column).lower()
    return title.find(key) == -1
        
def getCDROM():
    vol_monitor =  gnomevfs.VolumeMonitor()
    volume = gnomevfs.Volume()
    drives = []
    for volume in vol_monitor.get_mounted_volumes():
        if volume.get_hal_udi() != None:
            vol_type = volume.get_device_type()
            if vol_type == gnomevfs.DEVICE_TYPE_CDROM or vol_type == gnomevfs.DEVICE_TYPE_VIDEO_DVD:
                drives.append(volume.get_drive())
    
    return drives
            
def packageTooltip(widget, e, tooltips, cell, emptyText="no information"):
     """ If emptyText is None, the cursor has to enter widget from a side that contains an item, otherwise no tooltip will be displayed. """
     try:
         (path, col, x, y) = widget.get_path_at_pos(int(e.x), int(e.y))
         it = widget.get_model().get_iter(path)
         value = widget.get_model().get_value(it, config.C_PKG)
         tooltips.set_tip(widget, value.get_pkg_ShortDescription())
         tooltips.enable()
     except:
         tooltips.set_tip(widget, emptyText)
            
def checkAccess(path):
    """Checks file/folder permissions and returns True if the file/folder could be accessed"""
    mRet = False
    if pathExists(path):
        if os.access(path, os.R_OK | os.W_OK | os.X_OK):
            mRet = True
        return mRet

def copyFile(source , destination):
    try:
        dest = os.path.dirname(destination)
        if not pathExists(dest):
            mkdir(dest)
        
        shutil.copyfile(source , destination)
    except Exception , e :
        print str(e)

def delFile( path):
    """ Deletes a file if it exists"""
    if fileExist(path):
        os.remove(path)
    
def removePath( path):
    """ Removes a directory path """
    try:
        shutil.rmtree(path, ignore_errors=True)
    except:
        pass
        
def pathExists( path):
    """Returns True if a path exists."""
    return os.path.isdir(path)
    
def fileExist( path):
    """Returns True if a file exist."""
    return os.path.isfile(path)
    
def mkdir( path, removeExisting = False):
    """ Create a full path, directory by directory"""
    #remove directiory if already exists
    if removeExisting:
        if pathExists(path):
            removePath(path)
                
    dirs = path.split("/")
    dir = ""
        
    for dirname in dirs:
        dir += dirname + "/"
        if not os.path.isdir(dir) and len(dir)>0:
            os.mkdir(dir)
            
def TimeFormat(value):
    #get days
    
    fDays = (value / 86400)
    tModDay = math.modf(fDays)
    Days = int(tModDay[1])
    decimalDays =tModDay[0]
    
    fTimeHour = decimalDays  * 24

     # Extract decimal from integer part
    tModHour = math.modf(fTimeHour)
    nHour = int(tModHour[1])
    fDecimalHour = tModHour[0]
    
    # Transform decimal in minutes
    fMinute = fDecimalHour*60
    # Again, extract the decimal and the integer part
    tModMinute = math.modf(fMinute)
    nMinute = int(tModMinute[1])
    fDecimalMinute = tModMinute[0]
    # Transform decimal in seconds
    fSecond = fDecimalMinute*60    
    # Again, extract the decimal and the integer part
    tModSecond = math.modf(fSecond)
    nSecond = int(tModSecond[1])
    
    
    output=''
    if Days >0:
        output = '%id' % Days

    if nHour >0:
        output+='%ih' % nHour
    
    if nMinute >0:
        output+='%im' % nMinute
    
    output+='%is' % nSecond
    
    return output


def fileSizeFormat( i):
    """
        Taken from jinja
        Format the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB,
        102 bytes, etc).
    """
    bytes = float(i)
    if bytes < 1024:
        return u"%d Byte%s" % (bytes, bytes != 1 and u's' or u'')
    elif bytes < 1024 * 1024:
        return u"%.1f KB" % (bytes / 1024)
    elif bytes < 1024 * 1024 * 1024:
        return u"%.1f MB" % (bytes / (1024 * 1024))
    else:
        return u"%.1f GB" % (bytes / (1024 * 1024 * 1024)) 

def openSelectDialog(title, filterName, pattern, selectMultiple = False):
        
    file_open = gtk.FileChooserDialog(title=title, 
                          action=gtk.FILE_CHOOSER_ACTION_OPEN, 
                          buttons=(gtk.STOCK_CANCEL, 
                          gtk.RESPONSE_CANCEL, 
                          gtk.STOCK_OPEN, gtk.RESPONSE_OK))
    filter = gtk.FileFilter()
    filter.set_name(filterName)
    filter.add_pattern(pattern)
    file_open.add_filter(filter)
    file_open.set_select_multiple(selectMultiple)
        
    result = ""
    path = ""
    filename = ""
    if file_open.run() == gtk.RESPONSE_OK:
        file_open.hide()
        results = file_open.get_filenames()
        file_open.destroy()
        return gtk.RESPONSE_OK, results
    else:
        file_open.destroy()
        return gtk.RESPONSE_CANCEL, []
        
def getFilePathFromUrl( uri):
     """ helper to get a useful path from a drop uri"""
     path = urllib.url2pathname(uri) # escape special chars
     path = path.strip('\r\n\x00') # remove \r\n and NULL
     # get the path to file
     if path.startswith('file:\\\\\\'): # windows
         path = path[8:] # 8 is len('file:///')
     elif path.startswith('file://'): # nautilus, rox
         path = path[7:] # 7 is len('file://')
     elif path.startswith('file:'): # xffm
         path = path[5:] # 5 is len('file:')
     return path          

class cursorManager:

	def __init__(self):
		self.window = None

	#procedure to change window cursor to ready cursor
	def setBusy(self, window_main = None, flag = True):
		""" Show a watch cursor if the app is busy for more than 0.3 sec.
		Furthermore provide a loop to handle user interface events """
		if window_main is None:
			return
		self.window = window_main.window
		if flag == True:
			try:
				self.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.WATCH))
			except:
				print "An error occured changing cursor"
		else:
			self.window.set_cursor(None)
			while gtk.events_pending():
				gtk.main_iteration()

		return
    
class ContextMenu(gtk.Menu):
    def __init__(self, *args):
        gtk.Menu.__init__(self)
        self.menuItem = None
         
    def addMenuItem(self,menuName,actionFunction= None):
        
        if menuName == "-":
            self.menuItem = gtk.SeparatorMenuItem()
        else: 
            self.menuItem = gtk.MenuItem(menuName)
            if actionFunction is not None :
                self.menuItem.connect("activate", actionFunction)
        self.menuItem.show()
        self.append(self.menuItem)
        return
    
class SystemInfo:

    def __init__(self):
        self.distro = ''
        self.architecture = ''
        self.codename = ''
        self.release = ''
        self.read_release_info()
        
    def read_release_info(self):

        self.get_infos()
        self.architecture = self.__architecture()
   
    def get_infos(self):
  
        try:
            p = open('/etc/lsb-release','r')
            for line in p:
                if 'DISTRIB_ID' in line:
                    self.distro = line.split('=')[-1].replace('\n','').lower()
                elif 'DISTRIB_CODENAME' in line:
                    self.codename = line.split('=')[-1].replace('\n','').lower()
                elif 'DISTRIB_RELEASE' in line:
                    self.release = line.split('=')[-1].replace('\n','').lower()
            p.close()
        except:
            pass

    def __architecture(self):
        """
        Detect whether the architecture is x86/ppc/amd64
        """
        arch = os.uname()[-1]
        if arch in ('ppc', 'ppc64'):
            arch = 'powerpc'
        elif arch in ('sparc32','sparc64','sparc'):
            arch = 'sparc'
        elif arch =='x86_64':
            if self.distro in ('debian'):
                arch = 'ia64'
            else:
                arch = 'amd64'
        elif arch in ('i386','i686','i586','k7'):
            arch = 'i386'

        return arch
        
class RecursiveSearch:
    def __init__(self, directory, pattern="*"):
        self.stack = [directory]
        self.pattern = pattern
        self.files = []
        self.index = 0

    def __getitem__(self, index):
        while 1:
            try:
                file = self.files[self.index]
                self.index = self.index + 1
            except IndexError:
                # pop next directory from stack
                self.directory = self.stack.pop()
                self.files = os.listdir(self.directory)
                self.index = 0
            else:
                # got a filename
                fullname = os.path.join(self.directory, file)
                if os.path.isdir(fullname) and not os.path.islink(fullname):
                    self.stack.append(fullname)
                if fnmatch.fnmatch(file, self.pattern):
                    return fullname
                    
def compress(source, dest):
    dest_ext = '.bz2'
    arcname = os.path.basename (source)
    dest_name = '%s%s' % (dest, dest_ext)
    dest_path = os.path.join(dest, dest_name)

    input = bz2.compress(file(source, 'r').read())
    out = file(dest_path,'w')
    out.write(input)
    out.close()
    return dest_path