-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmkspec.py
executable file
·160 lines (133 loc) · 5.41 KB
/
mkspec.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/python
#############################################################################
# Copyright (c) 2010 Taobao.com, Inc.
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, contact Taobao.com, Inc.
#
# To contact Taobao.com about this file by physical or electronic mail,
# you may find current contact information at www.taobao.com
#
#############################################################################
import os, subprocess, sys
import commands, getopt, shutil
import config
patch_start_no = 200
config_start_no = 50
def print_usage ():
print >>sys.stderr, "Usage: mkspec.py --patches <patches' names, splited with spaces>\n" \
"--release-string <release string>\n" \
"--configs <config files' names>\n" \
"--changelog <Changelog file's name>\n" \
"[--release <mark for a kernel release>]\n"
return
def parse_opts ():
import getopt
release_string = None
configs = []
changelog = None
released = False
buildid = ""
try:
opts, args = getopt.getopt(sys.argv[1:], "", \
["buildid=", "patches=", "release-string=", "configs=", "changelog=", "release", "help"])
for o, a in opts:
if o in ("--help"):
print_usage()
sys.exit(2)
if o in ("--patches"):
patches = a.strip().split(" ")
if o in ("--buildid"):
buildid = a.strip()
if o in ("--release-string"):
if " " in a.strip():
print >>sys.stderr, "--release-string option argument must not contain spaces\n"
sys.exit(1)
release_string = a.strip()
if o in ("--configs"):
configs = a.strip().split(" ")
if o in ("--changelog"):
changelog = a.strip()
if o in ("--release"):
released = True
except:
print_usage()
sys.exit(2)
return patches, release_string, configs, changelog, released, buildid
def get_script_loc ():
path = sys.path[0]
if os.path.isdir(path):
return path
elif os.path.isfile(path):
return os.path.dirname(path)
if __name__ == "__main__":
patches, release_string, config_files, changelog, released, buildid = parse_opts()
script_dir = get_script_loc()
os.chdir(script_dir)
os.chdir("..")
WORKING_DIR = os.getcwd()
rpm_dir = os.path.join(WORKING_DIR, "rpm")
build_dir = os.path.join(WORKING_DIR, config.BUILD_DIR)
config_dir = os.path.join(WORKING_DIR, "config")
spec_temple = "".join(open(os.path.join(rpm_dir, "kernel.spec.in")).readlines())
# Generate version number from tags and base kernel version number.
# tb_base_var is something like 2.6.32
tb_base_ver = config.get_srcversion().split("-")[0]
rh_release_id = config.get_srcversion().split("-")[1][:-4]
tb_sublevel = tb_base_ver.split(".")[2]
try:
tb_short_commit = commands.getoutput("git log --pretty=format:%%h -1")
tb_long_commit = commands.getoutput("git log --pretty=format:%H -1")
except:
tr_short_commit = tb_long_commit = "UnknownCommitId"
# the lst string is distribution id, for,
# branch master, it's ".master"
# branch 5u4, it's ".el5"
# branch 6u, it's ".el6"
pkg_release = rh_release_id + "." + "tb" + buildid + ".master"
dynamic_values = {"RPMVERSION" : tb_base_ver,
"PKG_RELEASE" : pkg_release,
"KVERSION" : config.get_srcversion(),
"SUBLEVEL" : tb_sublevel,
"COMMITID" : tb_long_commit,
"RHELBASE" : "kernel-" + config.get_srcversion()
}
for key in dynamic_values.keys():
spec_temple = spec_temple.replace("%%" + key + "%%", str(dynamic_values[key]))
for key in config.MACROS.keys():
spec_temple = spec_temple.replace("%%" + key + "%%", str(config.MACROS[key]))
configs = ""
index = config_start_no
for cn in config_files:
configs += "Source%d: %s\n" % (index, cn)
index +=1
spec_temple = spec_temple.replace("%%CONFIGS%%", configs)
text = ""
applypatch = ""
index = patch_start_no
for cn in patches:
text += "Source%d: %s\n" % (index, cn)
applypatch +="tar xjf %%{SOURCE%d}\n" % (index,)
index +=1
applypatch += "%_sourcedir/apply-patches.sh %_sourcedir/series.conf .\n"
for cn in patches:
applypatch += "rm -rf `echo %s|sed 's/.tar.bz2//'`\n" % (cn,)
spec_temple = spec_temple.replace("%%PATCH_LIST%%", text)
spec_temple = spec_temple.replace("%%PATCH_APPLICATION%%", applypatch)
changes = ""
if changelog:
changes = "".join(open(changelog).readlines())
spec_temple = spec_temple.replace("%%CHANGELOG%%", changes)
spec = open(os.path.join(build_dir, "kernel.spec"), "w")
spec.write(spec_temple)
spec.close()