~gladex/gladex/0.4

« back to all changes in this revision

Viewing changes to gladex_callbacks.py

  • Committer: Christopher Pax
  • Date: 2007-09-16 04:02:19 UTC
  • Revision ID: chris@iron-20070916040219-zgj8h68epcd9jzfk
Implemented permissions checking on output directory. Took some examples from the internet and applied it to my code. Python should have a simpler way to do this task.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import time
22
22
from threading import Timer
23
23
from time import sleep
24
 
import sys
25
 
import os
 
24
import sys, os, stat
 
25
import pwd, grp, locale
26
26
import pygtk
27
27
pygtk.require("2.0")
28
28
import gtk
190
190
                return None
191
191
        else:
192
192
                return global_data["plugins"][activate_plugin_index]
193
 
        
 
193
 
 
194
#function to get info from mode
 
195
def get_mode_info(mode):
 
196
    aperms = {"USR":[], "GRP":[], "OTH":[]}
 
197
    mode=stat.S_IMODE(mode)
 
198
    for who in "USR", "GRP", "OTH":
 
199
        for what in "R", "W", "X":
 
200
            if mode & getattr(stat,"S_I"+what+who):
 
201
                aperms[who].append(what.lower())
 
202
    return aperms
 
203
#
 
204
 
 
205
def get_permisson_info(path):
 
206
        if not os.path.exists(path):
 
207
                return False
 
208
        try:
 
209
                stat_info=os.lstat(path)
 
210
        except:
 
211
                return False
 
212
        perms, colour, link = get_mode_info(stat_info.st_mode)
 
213
 
 
214
        try:
 
215
                name = pwd.getpwuid(stat_info.st_uid)[0]
 
216
        except KeyError:
 
217
                name = stat_info.st_uid
 
218
        try:
 
219
                group = grp.getgrgid(stat_info.st_gid)[0]
 
220
        except KeyError:
 
221
                group = stat_info.st_gid
 
222
        info = get_mode_info(stat_info.st_mode)
 
223
        return [name, group, info]
 
224
#
 
225
 
 
226
 
 
227
def has_permissions(path):
 
228
        info = get_permisson_info(path)
 
229
        currentName = os.getlogin()
 
230
        if currentName == info[0] and ["r","w","x"] == info[2]["USR"]:
 
231
                return True
 
232
        elif currentName == info[2] and ["r","w", "x"] == info[2]["GRP"]:
 
233
                return True
 
234
        elif ["r","w", "x"] == info[2]["OTH"]:
 
235
                return True
 
236
        else:
 
237
                return False
 
238
#
 
239
 
194
240
def on_execute_clicked(widget, data, wtree):
195
241
        global global_data
196
242
        ## TODO:
197
243
        # check if a glade file and a plugin is chosen
198
244
        c1 = wtree.get_widget("output_directory_chooser").get_filename()
 
245
        
199
246
        c2 = wtree.get_widget("glade_file_chooser").get_filename()
200
247
        if None in [c1, c2]:
201
 
                show_message("please choose a glade file",True)
 
248
                show_message("Please choose a glade file",True)
202
249
                return
203
250
        
 
251
        #check permissions
 
252
        if not has_permissions(c1):
 
253
                show_message("ERROR: Insufficient permission.\n Please change output directory.",False)
 
254
                return
204
255
        
205
256
        # calls execute function from plugin
206
257
        plug = get_selected_plugin()