comparison portops.py @ 12:1730494d9ecc

add create
author taiki
date Mon, 02 Feb 2015 16:38:32 +0900
parents
children 8e1f57c91210
comparison
equal deleted inserted replaced
11:5e2df576a42d 12:1730494d9ecc
1 #!/usr/bin/python
2
3 portlist_file = "iedockerport.list"
4 testuser = "testuser"
5 delimiter = ","
6
7 def remove_port_list(user, projectname):
8 portlist = read_port_list()
9 delete_line = ""
10 release_port = ""
11 for port in portlist:
12 portline = port.split(delimiter)
13 if (len(portline) < 3):
14 continue
15 if (portline[1] == user and portline[2] == projectname):
16 delete_line = port
17 release_port = portline[0]
18 break
19
20 if release_port == "":
21 print("[!] No remove port.")
22 return
23 portlist.remove(delete_line)
24 portlist.append(release_port)
25 write_port_list(portlist)
26
27 def read_port_list():
28 f = open(portlist_file, "r")
29 portlist = []
30 for port in f:
31 portlist.append(port.rstrip())
32 f.close()
33 return portlist
34
35 def write_port_list(portlist):
36 portlist_tmp = []
37 for port in portlist:
38 portlist_tmp.append(port + "\n")
39 f = open(portlist_file, "w")
40 f.writelines(portlist_tmp)
41 f.close()
42
43 def is_limit(portlist, user):
44 count = 0
45 for port in portlist:
46 portline = port.split(delimiter)
47 if len(portline) < 2:
48 continue
49 if portline[1] == user:
50 count = count + 1
51 if count < 4:
52 return True
53 else:
54 return False
55
56 def mark_use_port(user, projectname):
57 portlist = read_port_list()
58 port_num = ""
59 for port in portlist:
60 portline = port.split(delimiter)
61 if len(portline) == 1:
62 port_num = portline[0]
63 break
64 portlist.remove(port_num)
65 portlist.append(port_num + delimiter + user + delimiter + projectname)
66 write_port_list(portlist)
67
68 def get_marked_port(user):
69 ports = read_port_list()
70 for port in ports:
71 portline = port.split(delimiter)
72 if len(portline) < 2:
73 continue
74 if portline[1] == user:
75 return portline[0]
76
77 def reserve_port(user, projectname):
78 portlist = read_port_list()
79 if not is_limit(portlist, user):
80 return False
81 mark_use_port(user,projectname)
82 return True
83
84 if __name__ == "__main__":
85 print("put test source.")
86 remove_port_list("taira", "sample")