-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare.py
executable file
·143 lines (116 loc) · 4.83 KB
/
prepare.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import sys
import os
class Magic:
'''magic for back dircetory'''
magic_orign = ".ababc234back_origin"
magic_deploy = ".ababc234back_deploy"
def paser_gitmodule(gitmodule_file):
f = open(gitmodule_file)
modules = []
#used to check 0, 1, 0, 1 sequence
pair_open = False
project_name = None
project_path = None
for line in f:
line = line.strip()
#check very carefuly because this is a basic tools
if line[0] == "[" and line[-1] == "]":
line = line[1 : -1]
tokens = line.split()
if len(tokens) == 2:
first = tokens[0].strip()
if first == "submodule":
if pair_open != False:
return False, modules
#maybe put submodule with diff name and some other path
#so split tokes[1] again, and take the last part as the name of
#the submodule
tokens = tokens[1].split("/")
project_name = tokens[-1].strip()
project_name = project_name.strip('"')
pair_open = True
else:
tokens = line.split("=")
if len(tokens) == 2:
first = tokens[0].strip()
if first == "path":
if pair_open != True:
return False, modules
project_path = tokens[1].strip()
pair_open = False
#a pair has beed check correct put is into modules
modules.append((project_name, project_path))
return True, modules
def save_origin_git_recursive(workspace_base, project_root, project_name):
#check the .gitmodules file is exist
gitmodule_file = ("%s/%s") % (project_root, ".gitmodules")
ret = os.path.isfile(gitmodule_file)
if ret:
success, modules = paser_gitmodule(gitmodule_file)
if not success:
print ("%s has something wrong!") % (project_root)
return False
for module in modules:
#module --- [project_name, project_path]
next_project_root = ("%s/%s") % (project_root, module[1])
success = save_origin_git_recursive(workspace_base, next_project_root, module[0])
if not success:
return False
#copy all the current level .git* to .ababc234back_origin
current_level_git = ("%s/%s") % (project_root, ".git")
back_current_origin = ("%s/%s/%s") % (workspace_base, Magic.magic_orign, project_name)
#store project_root and corrsponding back directory here
store_file = ("%s/%s/store_info") % (workspace_base, Magic.magic_orign)
f = open(store_file, "a")
f.write(("%s %s\n") % (project_root, back_current_origin))
f.flush()
f.close()
#if current is a git project back all .git to the back_current_origin
ret = os.path.exists(current_level_git)
if not ret:
print "project don't have .git ignore"
else:
current_level_git = ("%s/%s") % (project_root, ".git*")
if not os.path.exists(back_current_origin):
os.mkdir(back_current_origin)
cmd = ("mv %s %s") % (current_level_git, back_current_origin)
os.system(cmd)
return True
if __name__ == '__main__':
if len(sys.argv) != 3:
print "wrong number of args, except 3"
exit(-1)
print "prepare ..."
workspace_base = sys.argv[1]
project_name = sys.argv[2]
ret = os.path.exists(workspace_base)
if not ret:
print "your workspace is not exists", workspace_base
exit(-1)
project_path = ("%s/%s") % (workspace_base, project_name)
ret = os.path.exists(project_path)
if not ret:
print "your porject is not exists", project_path
exit(-1)
back_origin = ("%s/%s") % (workspace_base, Magic.magic_orign)
ret = os.path.exists(back_origin)
if not ret:
os.mkdir(back_origin)
#back_origin_root = ("%s/%s") % (workspace_base, Magic.magic)
project_root = ("%s/%s") % (workspace_base, project_name)
success = save_origin_git_recursive(workspace_base, project_root, project_name)
if not success:
exit(-1)
#switch deploy .git into work space
depoly_current = ("%s/%s_%s") % (workspace_base, Magic.magic_deploy, project_name)
#first time depoly need create direactory.
ret = os.path.exists(depoly_current)
if not ret:
print "first time to depoly ", project_name, "may spent a little time"
os.mkdir(depoly_current)
cmd = ("cd %s; git init .; git add .; git commit -m 'for deploy';") % (project_root)
os.system(cmd);
else:
cmd = ("mv %s/.git* %s; cd %s; git add .; git commit -m'for deploy'") % (depoly_current, project_root, project_root)
#cmd = ("mv %s/.git* %s;") % (depoly_current, project_root)
os.system(cmd);