1 | import os |
---|
2 | import os.path |
---|
3 | import shutil |
---|
4 | import ldap |
---|
5 | import subprocess |
---|
6 | import pwd |
---|
7 | import multiprocessing |
---|
8 | |
---|
9 | |
---|
10 | class NetShareOperations: |
---|
11 | |
---|
12 | USER_DIRS=["Music","Videos","Pictures","Documents","Share","Groups"] |
---|
13 | LOG_FILE="/var/log/n4d/fileoperations" |
---|
14 | STUDENTS_FOLDER="/net/server-sync/home/students/" |
---|
15 | NET_TMP="/net/.tmp/" |
---|
16 | |
---|
17 | def __init__(self): |
---|
18 | |
---|
19 | if not os.path.exists(NetShareOperations.NET_TMP): |
---|
20 | os.mkdir(NetShareOperations.NET_TMP) |
---|
21 | |
---|
22 | #def init |
---|
23 | |
---|
24 | |
---|
25 | def send_file_to_group(self,group,orig_path,dest_folder="Desktop"): |
---|
26 | |
---|
27 | p = subprocess.Popen(["llxcfg-showvars","LDAP_BASE_DN"],stdout=subprocess.PIPE) |
---|
28 | output = p.communicate()[0] |
---|
29 | if output != "": |
---|
30 | basedn = output.split("'")[1] |
---|
31 | |
---|
32 | connect_ldap = ldap.initialize('ldap://server') |
---|
33 | connect_ldap.PORT = 636 |
---|
34 | ldap_result = connect_ldap.search_s("ou=Managed,ou=Group,"+basedn,ldap.SCOPE_SUBTREE,'cn='+group) |
---|
35 | try: |
---|
36 | ret=ldap_result[0][1] |
---|
37 | if ret.has_key("memberUid"): |
---|
38 | tmp=orig_path.split("/") |
---|
39 | file_name=tmp[len(tmp)-1] |
---|
40 | shutil.copy(orig_path,NetShareOperations.NET_TMP+file_name) |
---|
41 | for item in ret["memberUid"]: |
---|
42 | try: |
---|
43 | student_folder=NetShareOperations.STUDENTS_FOLDER+item+"/"+dest_folder+"/" |
---|
44 | src=NetShareOperations.NET_TMP+file_name |
---|
45 | dst=student_folder+file_name |
---|
46 | user_uid=pwd.getpwnam(item)[2] |
---|
47 | user_gid=pwd.getpwnam(item)[3] |
---|
48 | queue=multiprocessing.Queue() |
---|
49 | p=multiprocessing.Process(target=self.copy_file_as_user,args=(src,dst,user_uid,user_gid,True,queue)) |
---|
50 | p.start() |
---|
51 | p.join() |
---|
52 | ret=queue.get() |
---|
53 | if ret!=True: |
---|
54 | self.log("send_file_to_group",e,"copying from net to student folder multiprocess" + item) |
---|
55 | return False |
---|
56 | |
---|
57 | except Exception as e: |
---|
58 | self.log("send_file_to_group",e,"copying from net to student folder " + item) |
---|
59 | return False |
---|
60 | |
---|
61 | os.remove(NetShareOperations.NET_TMP+file_name) |
---|
62 | return True |
---|
63 | except Exception as e: |
---|
64 | self.log("send_file_to_group",e,group + " " + orig_path + " " + dest_folder) |
---|
65 | try: |
---|
66 | os.remove(NetShareOperations.NET_TMP+file_name) |
---|
67 | except: |
---|
68 | pass |
---|
69 | return False |
---|
70 | else: |
---|
71 | return False |
---|
72 | |
---|
73 | |
---|
74 | #def send_file_to_group |
---|
75 | |
---|
76 | def copy_file_as_user(self,src,dst,uid,gid,delete,queue): |
---|
77 | |
---|
78 | try: |
---|
79 | os.setgid(gid) |
---|
80 | os.setuid(uid) |
---|
81 | shutil.copy(src,dst) |
---|
82 | queue.put(True) |
---|
83 | except Exception as e: |
---|
84 | queue.put(e) |
---|
85 | |
---|
86 | #def copy_file_as_user |
---|
87 | |
---|
88 | def delete_dir_content(self,dir,net_share_safe=False): |
---|
89 | try: |
---|
90 | |
---|
91 | for item in os.listdir(dir): |
---|
92 | if os.path.isfile(dir+item): |
---|
93 | os.remove(dir+item) |
---|
94 | elif os.path.isdir(dir+item): |
---|
95 | |
---|
96 | if net_share_safe: |
---|
97 | if item not in NetShareOperations.USER_DIRS: |
---|
98 | shutil.rmtree(dir+item) |
---|
99 | else: |
---|
100 | self.delete_dir_content(dir+item+"/",net_share_safe) |
---|
101 | else: |
---|
102 | shutil.rmtree(dir+item) |
---|
103 | except Exception as e: |
---|
104 | print(e) |
---|
105 | self.log("delete_dir_content",e,dir + item) |
---|
106 | |
---|
107 | def log(self,function_name,exception,comment=None): |
---|
108 | |
---|
109 | try: |
---|
110 | f=open(NetShareOperations.LOG_FILE,"a") |
---|
111 | f.write("* When calling " + function_name + " something happened...\n\t") |
---|
112 | f.write(str(exception)) |
---|
113 | f.write("\n") |
---|
114 | if comment!=None: |
---|
115 | f.write("\tCoder Hint: " + str(comment)) |
---|
116 | f.write("\n") |
---|
117 | f.close() |
---|
118 | except: |
---|
119 | pass |
---|
120 | |
---|
121 | #def log |
---|
122 | |
---|
123 | |
---|
124 | |
---|
125 | if __name__=="__main__": |
---|
126 | |
---|
127 | nso=NetShareOperations() |
---|
128 | nso.send_file_to_group("4ESOA","asd") |
---|
129 | nso.send_file_to_group("asdddd","asd") |
---|