forked from MDSplus/mdsplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_debs_mdsplus
executable file
·187 lines (181 loc) · 7.34 KB
/
build_debs_mdsplus
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
#!/usr/bin/env python
import subprocess,os,sys,xml.etree.ElementTree as ET,fnmatch,tempfile
def getPackageFiles(buildroot,includes,excludes):
files=list()
for f in includes:
f = buildroot+f
if os.path.isdir(f):
for root, dirs, filenams in os.walk(f):
for filenam in filenams:
files.append(os.path.join(root,filenam))
elif ('?' in f) or ('*' in f):
dirnam=f
while ('?' in dirnam) or ('*' in dirnam):
dirnam=os.path.dirname(dirnam)
for root, dirs, filenams in os.walk(dirnam):
if fnmatch.fnmatch(root,f):
files=files+getPackageFiles(buildroot,[root[len(buildroot):],],excludes)
else:
for filenam in filenams:
filenam=os.path.join(root,filenam)
if fnmatch.fnmatch(filenam,f):
files.append(filenam)
else:
try:
os.stat(f)
files.append(f)
except:
pass
if len(excludes) > 0:
hasuid=False
for exclude in excludes:
if '/xuid' in exclude:
print "excluding: %s" % exclude
hasuid=True
excludefiles=getPackageFiles(buildroot,excludes,[])
if hasuid:
print "Found %d" % len(excludefiles)
for exclude in excludefiles:
print "excluding: %s" % exclude
for exclude in excludefiles:
if exclude in files:
files.remove(exclude)
return files
def externalPackage(info, root, package):
matchlen=0
ans = None
for extpackages in root.getiterator('external_packages'):
platforms=extpackages.attrib['platforms']
for platform in platforms.split(','):
if info['dist'].lower().startswith(platform):
if len(platform) > matchlen:
matchlen = len(platform)
pkg = extpackages.find(package)
if pkg is not None:
if 'package' in pkg.attrib:
ans = pkg.attrib['package']
else:
ans = package
return ans
def doRequire(info, out, root, require):
if 'external' in require.attrib:
pkg=externalPackage(info,root,require.attrib['package'])
if pkg is not None:
os.write(out,"Requires: %s\n" % pkg)
else:
info['reqpkg']=require.attrib['package']
os.write(out,"Requires: mdsplus%(BNAME)s-%(reqpkg)s = %(major)d.%(minor)d-%(release)d.%(dist)s\n" % info)
def buildDebs():
info=dict()
if "INSTALLER_DIR" in os.environ:
info['installer_dir']=os.environ['INSTALLER_DIR']
else:
info['installer_dir']='/installer'
if 'BUILDROOT' in os.environ:
info['buildroot']=os.environ['BUILDROOT']
else:
info['buildroot']='/buildroot'
if 'SRCDIR' in os.environ:
info['srcdir']=os.environ['SRCDIR']
else:
info['srcdir']='.'
info['BRANCH']=os.environ['BRANCH']
info['flavor']=info['BRANCH']
info['major']=int(os.environ['MAJOR'])
info['minor']=int(os.environ['MINOR'])
info['release']=int(os.environ['RELEASE'])
info['dist']=os.environ['DIST']
info['arch']=os.environ['ARCH']
if info['BRANCH']=="stable":
info['BNAME']=""
else:
info['BNAME']="-%s" % info['BRANCH']
info['rflavor']=info['BNAME']
tree=ET.parse('%(srcdir)s/deploy/packaging.xml' % info)
root=tree.getroot()
debs=list()
for package in root.getiterator('package'):
pkg = package.attrib['name']
if pkg=='MDSplus':
info['packagename']=""
else:
info['packagename']="-%s" % pkg
info['description']=package.attrib['description']
info['tmpdir']=tempfile.mkdtemp()
try:
os.mkdir("%(tmpdir)s/DEBIAN" % info)
includes=list()
for inc in package.getiterator('include'):
for inctype in inc.attrib:
include=inc.attrib[inctype]
if inctype != "dironly":
includes.append(include)
excludes=list()
for exc in package.getiterator('exclude'):
for exctype in exc.attrib:
excludes.append(exc.attrib[exctype])
if package.find("exclude_staticlibs") is not None:
excludes.append("/usr/local/mdsplus/lib/*.a")
if package.find("include_staticlibs") is not None:
includes.append("/usr/local/mdsplus/lib/*.a")
files=getPackageFiles(info['buildroot'],includes,excludes)
for f in files:
info['file']=f[len("%(buildroot)s/"%info)-1:].replace(' ','\\ ').replace('$','\\$')\
.replace('(','\\(').replace(')','\\)')
if subprocess.Popen("""
set -o verbose
set -e
dn=$(dirname %(file)s)
mkdir -p %(tmpdir)s/DEBIAN
mkdir -p "%(tmpdir)s/${dn}"
cp -av %(buildroot)s/%(file)s "%(tmpdir)s/${dn}/"
""" % info,shell=True).wait() != 0:
raise Exception("Error building deb")
sys.stdout.flush()
depends=list()
for require in package.getiterator("requires"):
if 'external' in require.attrib:
pkg=externalPackage(info,root,require.attrib['package'])
if pkg is not None:
depends.append(pkg)
else:
depends.append("mdsplus%s-%s" % (info['rflavor'],require.attrib['package'].replace('_','-')))
if len(depends)==0:
info['depends']=''
else:
info['depends']="\nDepends: %s" % ','.join(depends)
info['name']=info['packagename'].replace('_','-')
f=open("%(tmpdir)s/DEBIAN/control" % info,"w")
f.write("""Package: mdsplus%(rflavor)s%(name)s
Version: %(major)d.%(minor)d.%(release)d
Section: admin
Priority: optional
Architecture: %(arch)s%(depends)s
Maintainer: Tom Fredian <[email protected]>
Description: %(description)s
""" % info)
f.close()
for s in ("preinst","postinst","prerm","postrm"):
script=package.find(s)
if script is not None and ("type" not in script.attrib or script.attrib["type"]!="rpm"):
info['script']=s
f=open("%(tmpdir)s/DEBIAN/%(script)s" % info,"w")
f.write("#!/bin/bash\n")
f.write("%s" % (script.text.replace("__INSTALL_PREFIX__","/usr/local")))
f.close()
os.chmod("%(tmpdir)s/DEBIAN/%(script)s" % info,0775)
info['debfile']="%(installer_dir)s/%(flavor)s/DEBS/%(arch)s/mdsplus%(rflavor)s%(packagename)s_%(major)d.%(minor)d.%(release)d_%(arch)s.deb" % info
if subprocess.Popen("""
set -o verbose
set -e
dpkg-deb --build %(tmpdir)s %(debfile)s
""" % info,shell=True).wait() != 0:
raise Exception("Problem building package")
sys.stdout.flush()
subprocess.Popen("rm -Rf %(tmpdir)s" % info,shell=True).wait()
debs.append({"deb":info["debfile"],"arch":info["arch"]})
except:
subprocess.Popen("rm -Rf %(tmpdir)s" % info,shell=True).wait()
raise
sys.stdout.flush()
buildDebs()