-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpisimerge
executable file
·209 lines (165 loc) · 6.67 KB
/
pisimerge
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version. Please read the COPYING file.
#
# A tool for merging packages easily between Pardus repositories.
#
# Authors:
# Ozan Çağlayan <ozan_at_pardus.org.tr>
# Gökçen Eraslan <gokcen_at_pardus.org.tr>
"""
A tool for merging packages easily between Pardus repositories.
"""
import os
import sys
import locale
import piksemel
import tempfile
import subprocess
from optparse import OptionParser
def handle_user_choice(info):
"""Asks the user what to do right now."""
editor = os.environ.get("EDITOR", "vi")
while True:
print "\nSelect one of the following:\n\t"\
"[D]iff, [M]erge, [E]dit, [A]bort?"
choice = raw_input().lower()
if choice.startswith("d"):
# Show diff
subprocess.call([editor, info["diff"]])
if choice.startswith("m"):
return True
elif choice.startswith("e"):
# Edit
subprocess.call([editor, info["merge"]])
elif choice.startswith("a"):
# Abort
return False
def get_latest_change(ppath):
"""Gets the latest change from the path."""
os.system("svn up %s" % ppath)
ccmd = "svn info %s | grep 'Last Changed Rev'" % ppath
return int(os.popen(ccmd).read().strip().split(": ")[1])
def get_merge_log(ppath, rev):
"""Generates a merge log for the path@rev."""
os.system("svn up %s" % ppath)
ccmd = "svn log --xml -r %d:HEAD %s" % (rev, ppath)
print ccmd
log_xml = piksemel.parseString(os.popen(ccmd).read())
merge_log = """\
Merge from %s:
""" % ppath
for log in log_xml.root().tags():
rev = log.getAttribute('revision')
msg = log.getTagData('msg') or \
"<Empty log message, please warn the author.>"
date = log.getTagData('date')
author = log.getTagData('author')
merge_log += "rev. %s, by %s on %s\n%s\n\n" % \
(rev, author, date[:10], msg.rstrip("\n"))
return merge_log.rstrip("\n")
if __name__ == "__main__":
locale.setlocale(locale.LC_ALL, "C")
tmp_files = {}
note = """\
You have to run this in pardus/{corporate2,20xx} folder which contains the
(stable|testing) and devel sub-folders.
\nExample: %s devel/kernel/default/kernel\n\ncommand will merge
the differences of that package from the devel to the stable (for 2009 repo)
repository with a nice commit log.
""" % sys.argv[0]
usage = "%%prog [options] path1 [path2...] \n\nWarning! %s" % note
parser = OptionParser(usage)
parser.add_option("-y", "--yes-all", dest="yesAll",
default=False,
action="store_true",
help="Answer 'Merge' to all questions")
parser.add_option("-m", "--message", dest="message",
help="Commit message for merge operation")
parser.add_option("-t", "--target", dest="target",
help="Target repository to which changes will be applied")
(options, paths) = parser.parse_args()
# No path given
if len(paths) == 0:
parser.print_help()
sys.exit()
for p in paths:
if len(paths) > 1:
print "* Processing %s..." % p
# path becomes system/base/pam if it was devel/system/base/pam
target = options.target
src = p
dest = ""
if target:
# Merge to the specified folder
dest = "%s/%s" % (target, "/".join(p.split("/")[1:]))
path = p
print "***** %s %s" % (src, dest)
else:
path = p.split("devel/", 1)[1] if p.startswith("devel/") else p
src = "devel/%s" % path
# dest can change according to distro release
cmd = ["svn", "info", "--xml"]
devel_dir = os.getcwd() + "/devel"
output = subprocess.Popen(cmd,
cwd=devel_dir,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT).communicate()[0]
svn_info_xml = piksemel.parseString(output)
svn_url = svn_info_xml.getTag("entry").getTag("url").firstChild().data()
if "2009" in svn_url:
dest = "stable/%s" % path
else:
#fallback for 2011 and C2
dest = "testing/%s" % path
os.system("svn up %s %s" % (src, dest))
if not os.path.exists(dest):
# New package
print "\nCopying from %s.." % src
os.system("svn cp %s %s" % (src, dest))
if not options.message:
os.system("svn ci %s -m 'Package is ready.'" % dest)
else:
os.system("svn ci %s -m '%s'" % (dest, options.message))
else:
latest = get_latest_change(dest)
cmd = "svn diff -r %d:HEAD %s" % (latest, src)
diff_result = os.popen(cmd).read().strip()
if not diff_result:
print "WARNING: Skipping '%s' is it's probably merged." % src
else:
if not options.message:
merge_msg = get_merge_log(src, latest)
else:
merge_msg = options.message
tmp_files.clear()
# Generate safe temporary files
m_fd, tmp_files["merge"] = tempfile.mkstemp(prefix='pisimerge')
d_fd, tmp_files["diff"] = tempfile.mkstemp(prefix='pisimerge')
# Save merge message
open(tmp_files["merge"], "w").write(merge_msg)
# Save diff
open(tmp_files["diff"], "w").write(diff_result)
# Dump details
cmd = "diffstat -q %s" % tmp_files["diff"]
merge_msg += "\n\n---\n%s" % os.popen(cmd).read()
print "\n%s" % merge_msg
# Handle user choice
if options.yesAll or handle_user_choice(tmp_files):
print "\nMerging from %s.." % src
mr_cmd = "svn merge -r %d:HEAD %s %s" % (latest, src, dest)
ci_cmd = "svn ci %s -F %s" % (dest, tmp_files["merge"])
os.system(mr_cmd)
os.system(ci_cmd)
# Clean temporary files
for tmp in tmp_files.values():
try:
os.unlink(tmp)
except OSError:
pass
if p != paths[-1]:
print "\n***"