# -*- coding: utf-8 -*- import os.path import base64 import os import pwd import multiprocessing import socket import ssl import shutil from xmlrpclib import * NET_FOLDER="/net/.lliurex-harvester/" class TeacherShare: def send_to_teacher(self,from_user,to_user,file_name,file_in_b64): file_name=file_name.encode("utf-8") server = ServerProxy ("https://server:9779") try: self.paths=server.get_paths("","TeacherShareManager") except Exception as e: print e return False if to_user in self.paths: try: path,ip,name=self.paths[to_user] fname=self.generate_file_name(from_user,file_name,path) f=open(fname,'w') f.write(base64.b64decode(file_in_b64)) f.close() os.chown(fname,pwd.getpwnam(to_user)[2],pwd.getpwnam(to_user)[3]) return True except Exception as e: print e return False else: return False #def send_to_users def send_to_teacher_socket_p(self,from_user,to_user,file_path,queue): ret_value=False file_path=file_path.encode("utf-8") server = ServerProxy ("https://server:9779") try: paths=server.get_paths("","TeacherShareManager") except Exception as e: print e if to_user in paths: try: path,name,ip,port=paths[to_user] student_uid=pwd.getpwnam(from_user)[2] student_gid=pwd.getpwnam(from_user)[3] os.setgid(student_gid) os.setuid(student_uid) f=open(file_path,'rb') tmp=file_path.split("/") file_name=tmp[len(tmp)-1] s=socket.socket() ssl_socket=ssl.wrap_socket(s) ssl_socket.connect((ip,port)) ssl_socket.sendall("[N4D-HEADER-BEGIN]user="+from_user+";file="+file_name+"[N4D-HEADER-END]"+f.read()) ret_value=True ssl_socket.close() except Exception as e: print e queue.put(ret_value) #def send_to_teacher_socket_mp def send_to_teacher_net(self,from_user,to_user,file_path): student_uid=pwd.getpwnam(from_user)[2] student_gid=pwd.getpwnam(from_user)[3] teacher_uid=pwd.getpwnam(to_user)[2] teacher_gid=pwd.getpwnam(to_user)[3] server = ServerProxy ("https://server:9779") try: paths=server.get_paths("","TeacherShareManager") except Exception as e: print e return False if to_user in paths: path,name,ip,port=paths[to_user] try: #STEP 1 --- TO /NET src=file_path tmp=file_path.split("/") file_name=tmp[len(tmp)-1] dst=NET_FOLDER+"."+to_user+"_"+name+"/["+from_user+"]_"+file_name queue=multiprocessing.Queue() p=multiprocessing.Process(target=self.copy_file_as_user,args=(src,dst,student_uid,student_gid,False,queue)) p.start() p.join() if not queue.get(): return False #STEP 2 -- TEACHER GRABS IT server=ServerProxy("https://"+ip+":9779") return server.grab_file("","TeacherShare",to_user,"["+from_user+"]_"+file_name) ''' src=dst tmp=dst.split("/") file_name=tmp[len(tmp)-1] dst=path+file_name queue=multiprocessing.Queue() p=multiprocessing.Process(target=self.copy_file_as_user,args=(src,dst,teacher_uid,teacher_gid,True,queue)) p.start() p.join() return queue.get() ''' except Exception as e: print e return False #def send_to_teacher_net def grab_file(self,teacher,file_name): try: server=ServerProxy("https://server:9779") paths=server.get_paths("","TeacherShareManager") if teacher in paths: path,name,ip,port=paths[teacher] src=NET_FOLDER+"."+teacher+"_"+name+"/"+file_name dst=path+file_name teacher_uid=pwd.getpwnam(teacher)[2] teacher_gid=pwd.getpwnam(teacher)[3] queue=multiprocessing.Queue() p=multiprocessing.Process(target=self.copy_file_as_user,args=(src,dst,teacher_uid,teacher_gid,True,queue)) p.start() p.join() return queue.get() else: print "[TeacherShare] Teacher " + teacher + " not sharing [!]" return False except Exception as e: print e return False #def grab_file def copy_file_as_user(self,src,dst,uid,gid,delete,queue): try: os.setgid(gid) os.setuid(uid) shutil.copyfile(src,dst) if delete: os.remove(src) queue.put(True) except Exception as e: print e queue.put(False) #def copy_file_as_user def send_to_teacher_socket(self,from_user,to_user,file_name): queue=multiprocessing.Queue() p=multiprocessing.Process(target=self.send_to_teacher_socket_p,args=(from_user,to_user, file_name,queue,)) p.start() p.join() return queue.get() #def send_to_teacher_socket def generate_file_name(self,from_user,file_name,path): done=False counter=1 original_path=path original_file_name=file_name file_name="["+from_user+"]_"+file_name while not done: if os.path.exists(path+file_name): tmp_list=original_file_name.split(".") tmp_list[0]=tmp_list[0]+"_("+str(counter)+")" file_name=".".join(tmp_list) file_name="["+from_user+"]_"+file_name counter+=1 else: done=True return path+file_name #def generate_file_name #class TeacherFileManager if __name__=="__main__": tfm=TeacherShare() tfm.send_to_teacher_socket(1,2,3) #print tfm.generate_file_name("hector","hola.txt","/tmp/test/")