comparison create.py @ 0:61bc8410d480

add files
author taiki
date Wed, 11 Feb 2015 20:23:09 +0900
parents
children 6ebce132ee89
comparison
equal deleted inserted replaced
-1:000000000000 0:61bc8410d480
1 #!/usr/bin/python
2
3 import os, re
4 import argparse
5 import portops
6
7 base_path = "/home/k138582/docker-hg/"
8
9 def ie_mkdir(projectname):
10 username = os.getlogin()
11 m = re.match('^([ek]\d\d[58]\d\d\d)$', username)
12 if m is not None:
13 dir = base_path + "students/" + username[:3] + os.sep + username
14
15 mkdir1(dir)
16 os.system("/bin/chown "+os.getlogin()+" "+ dir)
17
18 if (len(os.listdir(dir)) > 4):
19 print("[!] Too many project.")
20 exit()
21
22 dir = dir + os.sep + projectname
23 mkdir1(dir)
24 os.system("/bin/chown " + os.getlogin() + " " + dir)
25 return dir
26 print("[!] Permission denied. You are not permitted user.")
27 exit()
28
29 # make necessary sub directory
30 # /etc/libvirt/qemu/teachers
31 # /var/log/libvirt/qemu/teachers
32 # /var/run/libvirt/qemu/teachers
33
34 def mkdir1(name):
35 if not os.path.exists(name):
36 os.makedirs(name);
37
38 def change_own(base_path):
39 if not os.path.isdir(base_path):
40 os.system("/bin/chown " + os.getlogin() + " " + base_path)
41 return
42
43 for f in os.listdir(base_path):
44 path = os.path.join(base_path, f)
45 os.system("/bin/chown " + os.getlogin() + " " + path)
46 if os.path.isdir(path):
47 change_own(path)
48
49
50 def make_hgrepo(projpath):
51 os.chdir(projpath)
52 os.system("hg init")
53
54 dockerfile = "Dockerfile"
55 fd = open(dockerfile, "w")
56 fd.write("# Using ie-docker, you sould edit this file\n")
57 fd.write("FROM fedora\n")
58 fd.close()
59
60 os.system("hg add " + dockerfile)
61 os.system("hg commit -u " + os.getlogin() + " -m systemcommit")
62
63 change_own(os.getcwd())
64
65 #
66 # main method
67 #
68
69 if __name__ == "__main__":
70
71 parser = argparse.ArgumentParser(description='Create new project for ie-docker.')
72 parser.add_argument('projectname', metavar='project name', help='ie-docker project name')
73 args = parser.parse_args()
74
75 if not portops.reserve_port(os.getlogin(), args.projectname):
76 print("[!] Can't get port to use for project.")
77 print("[!] Check your number of projects.")
78 sys.exit()
79
80 projpath = ie_mkdir(args.projectname)
81
82 make_hgrepo(projpath)
83
84 print("Create your project on " + projpath)
85 print("Exec on your client : hg clone ssh://your_account@" + os.uname()[1] + os.sep + projpath)
86
87
88 # end