-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·118 lines (104 loc) · 4.27 KB
/
main.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
#!/usr/bin/env python3
import os, sys
from pygitlib import *
def help():
print(
'''
pygitlib/main.py is a script to keep the MIMESIS's team SOFA repository up to date.
It is supposed to merge all compatible branches of the remote into our mimesis branch.
Important: be sure to allow the script to run with bash
>> chmod +x main.py
Usage:
>> mimesis_merger.py [optinonal-sofa-commit-hash]
By default, every branch is merged into sofa-framework latest commit.
If an optionnal sofa commit hash is given, the merge will be performed into this specific commit.
For any questions, be sure to ask:
- Omar Boukhris (@omarboukhris)
- Pedro Perrusi (@pedroperrusi)
'''
)
class MimesisMerger :
def __init__ (self, repo_dir=os.environ.get("SOFA_WORK_DIRECTORY") + "/sofa/") :
self.gitrepo = GitCmd (repo_dir)
origin = self.gitrepo.checkrepo ("origin", "[email protected]:mimesis-inria/sofa.git")
sofaframework = self.gitrepo.checkrepo("sofa-framework", "https://github.com/sofa-framework/sofa.git")
if not origin or not sofaframework:
print (">>> Expected remotes not found, add them to the git project? [y/n]")
answer = input('--> ').strip()
if answer[0] == 'y':
self.gitrepo.addRemote("origin", "[email protected]:mimesis-inria/sofa.git")
self.gitrepo.addRemote("sofa-framework", "https://github.com/sofa-framework/sofa.git")
else:
print('Exiting mimesis_merger...')
exit()
self.gitrepo.fetch()
self.gitrepo.fetch("--prune")
self.blacklist = []
self.mergedbranches = []
def load_blacklist (self, filepath) :
try :
fstream = open (filepath+"blacklistbranches.name", "r")
self.blacklist = [line.split("\n")[0] for line in fstream.readlines()]
fstream.close()
print ("[+] blacklist read")
except :
print ("[-] blacklist can't be read")
def load_mergedbranches (self, filepath) :
try :
fstream = open (filepath+"mergedbranches.hash", "r")
self.mergedbranches = fstream.readlines()
fstream.close()
print ("[+] merged branches read")
except :
print ("[-] merged branches can't be read")
# selects branches to merge
def compute_whitelist(self, path="") :
branches_to_cut = self.gitrepo.getBranches ("-r | grep sofa-framework | sed 's/sofa-framework/origin/'")
branches = self.gitrepo.getBranches ("-r | grep origin")
self.load_blacklist(path)
self.load_mergedbranches(path)
whitelist_branches = cut_branches(cut_branches(branches, branches_to_cut), self.blacklist)
printlist("whitelist branches :", whitelist_branches)
self.whitelist_branches = []
for branch in whitelist_branches:
if not self.gitrepo.get_hash(branch) in self.mergedbranches :
self.whitelist_branches.append (branch)
else :
print ("branch {} has already been merged".format(branch))
printlist("whitelist branches to merge :", self.whitelist_branches)
def merge_all (self, commitkw="master") :
self.gitrepo.checkout("mimesis")
self.gitrepo.reset_hard_to_commit(commitkw)
faulty_branches, successful_br = [], []
print ("=========================")
for branch in self.whitelist_branches :
headhash = self.gitrepo.get_hash ("HEAD")
print ("merging " + branch)
if not self.gitrepo.merge (branch) :
print ("\t(fatal) fix merge conflict before resuming")
self.gitrepo.reset_hard_to_commit(headhash)
faulty_branches.append(branch)
else :
successful_br.append(branch)
print ("=========================")
#self.gitrepo.checkout("master")
self.gitrepo.dump_log(
err_file=os.environ.get("SOFA_WORK_DIRECTORY") + "/mimesiscript/pygitlib/err_log.txt",
succ_file=os.environ.get("SOFA_WORK_DIRECTORY") + "/mimesiscript/pygitlib/succ_log.txt")
return faulty_branches, successful_br
def push(self) :
self.gitrepo.push("--force origin mimesis")
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == '-h':
help()
exit()
mimesis_merge = MimesisMerger()
mimesis_merge.compute_whitelist(path=os.environ.get("SOFA_WORK_DIRECTORY") + "/mimesiscript/pygitlib/")
faulty_branches, successful_br = [], []
if len(sys.argv) == 2 :
faulty_branches, successful_br = mimesis_merge.merge_all (readhash(sys.argv[1]))
else :
faulty_branches, successful_br = mimesis_merge.merge_all ()
mimesis_merge.push()
printlist("faulty branches :", faulty_branches)
printlist("successful merges :", successful_br)