import os import fnmatch import json import os.path import hashlib import tarfile import base64 import StringIO import types class PlankSync: def __init__(self): pass #def init def get_profiles(self): file_names=[] for root,dirnames,filenames in os.walk('/etc/syncer-plank'): for filename in fnmatch.filter(filenames,'*.sync'): file_names.append(filename.split('.')[0]) if len(file_names) < 1: return [] profiles=[] for profile in file_names: with open('/etc/syncer-plank/'+profile+'.sync','r') as f: try: data = json.load(f) grp=self._get_group(data) profiles.append((profile, grp)) except: pass return profiles #def get_profiles def _get_group(self,data): try: if isinstance(data,(types.ListType)): if isinstance(data[0],(types.ListType)): # First iteration, list of lists retgrp=None for item in data: retgrp = self._get_group(item) if retgrp != None: return retgrp if isinstance(data[1],(types.ListType)) and isinstance(data[0],(types.StringType,types.UnicodeType)): if isinstance(data[1][0],(types.StringType,types.UnicodeType)): if data[0].lower() == 'users' and data[1][0].lower() == 'assert_grp': if isinstance(data[1][1],(types.StringType,types.UnicodeType)): return data[1][1] else: return None else: return None else: return None except Exception as e: raise Exception(e) def _get_planks(self,data): try: if isinstance(data,types.ListType): if isinstance(data[0][0],(types.StringType,types.UnicodeType)) and isinstance(data[0][1][0],(types.StringType,types.UnicodeType)): plugin_name=data[0][0] operation=data[0][1][0] if plugin_name.lower() == 'plank' and operation.lower() == 'add': app=data[0][1][1] if (len(data) > 1): p=self._get_planks(data[1:]) p.append(app) return p else: return [app] except Exception as e: return str(e) def read_profile_old(self,profile_name): try: with open('/etc/syncer-plank/'+profile_name+'.sync','r') as f: data=json.load(f) return self._get_planks(data[3:]) except: return False def read_profile(self,profile_name): try: with open('/etc/syncer-plank/'+profile_name+'.sync','r') as f: data=json.load(f) return data except: return False def remove_profile(self,profile_name): try: os.remove('/etc/syncer-plank/'+profile_name+'.sync') return True except: return False def rename_profile(self,oldname,newname): try: oldfile='/etc/syncer-plank/'+oldname+'.sync' newfile='/etc/syncer-plank/'+newname+'.sync' if os.path.exists(oldfile) and os.path.isfile(oldfile): if os.path.exists(newfile): return False else: os.rename(oldfile,newfile) return True else: return False except Exception as e: raise Exception(e) def update_profile(self,name,newname,grouplist,applist): import traceback try: file='/etc/syncer-plank/'+newname+'.sync' content=[] #add group element content.append(['users',['disavow_grp','teachers']]) content.append(['users',['assert_grp',','.join(grouplist)]]) #remove previous elements content.append(['plank', ['remove','all']]) #add elements for i in applist['plank']: content.append(['file',['encoded',i[1],i[2]]]) content.append(['comm',['one']]) content.append(['plank',['addcontent','param']]) for i in applist['desktop']: content.append(['file',['encoded',i[1],i[2]]]) content.append(['comm',['one']]) content.append(['desktop',['addcontent','param']]) if 'background' in applist and len(applist['background']) > 1: content.append(['file',['encoded',applist['background'][0],applist['background'][1]]]) content.append(['comm',['one']]) content.append(['dconf',['/org/mate/desktop/background/picture-filename','param']]) except Exception as e: raise Exception(traceback.format_exc()+str(e)) with open(file,'w') as f: try: json.dump(content,f,separators=(',', ':')) except Exception as e: raise Exception(str(e)) if name[0] != '_': if name != newname: try: os.remove('/etc/syncer-plank/'+name+'.sync') except Exception as e: raise Exception(str(e)) return True def update_profile_groups(self,name,grouplist): try: file = '/etc/syncer-plank/'+name+'.sync' content = [] if not os.path.exists(file): return False with open(file,'r') as f: temp = json.load(f) content.append(['users',['disavow_grp','teachers']]) content.append(['users',['assert_grp',','.join(grouplist)]]) #remove previous elements content.append(['plank', ['remove','all']]) for item in temp: if item[0] == 'users': continue content.append(item) with open(file,'w') as f: json.dump(content,f,separators=(',',':')) except Exception as e: raise Exception(str(e)) return True def get_profile_pack(self,userinfo): import grp if 'u' not in userinfo or 'g' not in userinfo: raise Exception('invalid user info, {}'.format(userinfo)) list_groups = userinfo['g'] lfiles = [] try: named_list_groups = [grp.getgrgid(g)[0] for g in list_groups] for pfile in os.listdir('/etc/syncer-plank'): fpath = '/etc/syncer-plank/'+pfile with open(fpath,'r') as fp: content = json.load(fp) for item in content: if item[0] != 'users': continue if item[1][0] != 'assert_grp': continue lgrps = item[1][1].split(',') add = False for g in lgrps: if g in named_list_groups: add = True break if add: lfiles.append(fpath) msg = '{}'.format(lfiles) except: import traceback msg = '{}'.format(traceback.format_exc()) with open('/tmp/testeo.txt','w') as fp: fp.write(msg) tmpfile=StringIO.StringIO() try: with tarfile.open(fileobj=tmpfile,mode='w:gz') as tgz: #loc='/etc/syncer-plank/' #tgz.add(loc,arcname=os.path.basename(loc)) for f in lfiles: tgz.add(f,arcname=os.path.basename(f)) md5=hashlib.md5(tmpfile.getvalue()).hexdigest() content=base64.b64encode(tmpfile.getvalue()) return [md5,content] except Exception as e: return str(e) #if __name__ == '__main__': # sync=PlankSync() # ret=sync.read_profile('nuevo') # print('{}'.format(ret))