annotate portops.py @ 12:1730494d9ecc

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