summaryrefslogtreecommitdiff
path: root/cpp/RPMTools.py
blob: 804d67aa98c1c840da48bcb841a2347bf00e3014 (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# **********************************************************************
#
# Copyright (c) 2003-2005 ZeroC, Inc. All rights reserved.
#
# This copy of Ice is licensed to you under the terms described in the
# ICE_LICENSE file included in this distribution.
#
# **********************************************************************
import os, sys, shutil, string, logging

iceDescription = """Ice is a modern alternative to object middleware such as CORBA or COM/DCOM/COM+.
It is easy to learn, yet provides a powerful network infrastructure for
demanding technical applications. It features an object-oriented specification
language, easy to use C++, Java, Python, PHP, C#, and Visual Basic mappings, a
highly efficient protocol, asynchronous method invocation and dispatch, dynamic
transport plug-ins, TCP/IP and UDP/IP support, SSL-based security, a firewall
solution, and much more."""

#
# Represents the 'main' package of an RPM spec file.
# TODO: The package should really write the RPM header has well.
#
class Package:
    """Encapsulates RPM spec file information to be used to generate a spec file on Linux
       and create RPMs for Ice"""
    def __init__(self, name, requires, summary, group, description, other, filelist):
        self.name = name
        self.requires = requires
        self.summary = summary
        self.group = group
        self.description = description
        self.filelist = filelist
	self.other = other
	self.prepTextGen = []
	self.buildTextGen = []
	self.installTextGen = []
        
    def writeHdr(self, ofile, version, release, installDir):
	ofile.write("%define _unpackaged_files_terminate_build 0\n")
	ofile.write("Summary: " + self.summary + "\n")
	ofile.write("Name: " + self.name + "\n")
	ofile.write("Version: " + version + "\n")
	ofile.write("Release: " + release + "\n")
	if self.requires <> "":
            if self.requires.find("%version%"):
                self.requires = self.requires.replace("%version%", version)
	    ofile.write("Requires: " + self.requires + "\n")
	ofile.write("License: GPL\n")
	ofile.write("Group:" + self.group + "\n")
	ofile.write("Vendor: ZeroC, Inc\n")
	ofile.write("URL: http://www.zeroc.com/\n")
	ofile.write("Source0: http://www.zeroc.com/download/Ice/2.1/Ice-%{version}.tar.gz\n")
	ofile.write("Source1: http://www.zeroc.com/download/Ice/2.1/IceJ-%{version}.tar.gz\n")
	ofile.write("Source2: http://www.zeroc.com/download/Ice/2.1/IcePy-%{version}.tar.gz\n")
	ofile.write("Source3: http://www.zeroc.com/download/Ice/2.1/IceCS-%{version}.tar.gz\n")
	ofile.write("Source4: http://www.zeroc.com/download/Ice/2.1/Ice-%{version}-demos.tar.gz\n")
	ofile.write("Source5: http://www.zeroc.com/download/Ice/2.1/README.Linux-RPM\n")
	ofile.write("\n")
	if installDir <> "":
	    ofile.write("BuildRoot: " + installDir + "\n")
	else:
	    ofile.write('BuildRoot: /var/tmp/Ice-' + version + '-' + release + '-buildroot\n')
	ofile.write("\n")
	ofile.write("%description\n")
	ofile.write(self.description)
	ofile.write("\n")
	ofile.write("%prep\n")
	for g in self.prepTextGen:
	    g(ofile, version)
	ofile.write("\n")
	ofile.write("%build\n")
	for g in self.buildTextGen:
	    g(ofile, version)
	ofile.write("\n")
	ofile.write("%install\n")
	for g in self.installTextGen:
	    g(ofile, version)
	ofile.write("\n")
	ofile.write("%clean\n")
	ofile.write("\n")
	ofile.write("%changelog\n")
	ofile.write("* Tue Mar 8 2005 ZeroC Staff\n")
	ofile.write("- See source distributions or the ZeroC website for more information\n")
	ofile.write("  about the changes in this release\n") 
	ofile.write("\n")

    def writeFileList(self, ofile, version, intVersion, installDir):
        ofile.write("%defattr(644, root, root, 755)\n\n")
        for perm, f in self.filelist:
            prefix = ""
	    
	    #
	    # Select an RPM spec file attribute depending on the type of
	    # file or directory we've specified.
	    #
            if perm == "exe" or perm == "lib":
                prefix = "%attr(755, root, root) "
	    elif perm == "xdir":
		prefix = "%dir "

            if f.find("%version%"):
                f = f.replace("%version%", version)

            if perm == "lib" and f.endswith(".VERSION"):
		fname = os.path.splitext(f)[0]
                ofile.write(prefix + "/usr/" + fname + "." + version + "\n")
                ofile.write(prefix + "/usr/" + fname + "." + str(intVersion) + "\n")
	    else:
		ofile.write(prefix + "/usr/" + f + "\n")

        ofile.write("\n")    

    def writePostInstall(self, ofile, version, intVersion, installDir):
	ofile.write('cd /usr\n')
	for perm, f in self.filelist:
	    if perm == "dll":
		ofile.write('gacutil -i ' + f + ' > /dev/null\n')
	ofile.write('\n')

    def writePostUninstall(self, ofile, version, intVersion, installDir):
	for perm, f in self.filelist:
	    if perm == 'dll':
		if os.path.exists(installDir + '/usr/' + f):
		    #
		    # We need to trap the assembly name from the DLL. We can use the monodis command to do this.
		    #
		    pipe_stdin, pipe_stdout = os.popen2('monodis --assembly ' + installDir + '/usr/' + f);
		    lines = pipe_stdout.readlines()
		    pipe_stdin.close()
		    pipe_stdout.close()
		    assemblyName = ''
		    assemblyVersion = ''
		    for l in lines:
			if l.startswith('Name:'):
			    assemblyName = l.split(':')[1].strip()
			elif l.startswith('Version:'):
			    assemblyVersion = l.split(':')[1].strip()

		    ofile.write('gacutil -u ' + assemblyName + ', version=' + assemblyVersion + ' > /dev/null\n')
		else:
		    ofile.write('# Unable to determine assembly information for ' + f)
		    ofile.write(', please adjust the file manually\n')

	ofile.write('\n')

    def writeFiles(self, ofile, version, intVersion, installDir):
        ofile.write("%files\n")
        self.writeFileList(ofile, version, intVersion, installDir)
	ofile.write('\n')

	ofile.write("%post\n")
	self.writePostInstall(ofile, version, intVersion, installDir)

	ofile.write("%postun\n")
	self.writePostUninstall(ofile, version, intVersion, installDir)
	ofile.write('\n')

    def addPrepGenerator(self, gen):
	self.prepTextGen.append(gen)

    def addBuildGenerator(self, gen):
	self.buildTextGen.append(gen)

    def addInstallGenerator(self, gen):
	self.installTextGen.append(gen)

#
# Represents subpackages in an RPM spec file.
#
class Subpackage(Package):
    def writeHdr(self, ofile, version, release, installDir):
        ofile.write("%package " + self.name + "\n")
        ofile.write("Summary: " + self.summary + "\n")
        ofile.write("Group: " + self.group + "\n")
	if self.requires <> "":
            if self.requires.find("%version%"):
                self.requires = self.requires.replace("%version%", version)
	    ofile.write("Requires: " + self.requires + "\n")
	if not self.other == "":
	    ofile.write(self.other + "\n")
        ofile.write("%description " + self.name + "\n")
        ofile.write(self.description)

    def writeFiles(self, ofile, version, intVersion, installDir):
        ofile.write("%files " + self.name + "\n")
        self.writeFileList(ofile, version, intVersion, installDir)

	ofile.write("%post " + self.name + "\n")
	self.writePostInstall(ofile, version, intVersion, installDir)

	ofile.write("%postun " + self.name + "\n")
	self.writePostUninstall(ofile, version, intVersion, installDir)

#
# NOTE: File transforms should be listed before directory transforms.
#
transforms = [ ("file", "lib/Ice.jar", "lib/Ice-%version%/Ice.jar" ),
	       ("dir", "slice", "share/slice"),
	       ("dir", "ant", "lib/Ice-%version%/ant"),
	       ("dir", "python", "lib/Ice-%version%/python"),
               ("dir", "doc", "share/doc/Ice-%version%/doc"),
               ("file", "README", "share/doc/Ice-%version%/README"),
               ("file", "ICE_LICENSE", "share/doc/Ice-%version%/ICE_LICENSE"),
               ("file", "LICENSE", "share/doc/Ice-%version%/LICENSE")
               ]

#
# fileLists is an in-memory representation of the package contents of
# the Ice spec file.
# 
fileLists = [
    Package("ice",
            "",
	    "The Ice base runtime and services",
            "System Environment/Libraries",
	    iceDescription,
	    "",
            [("xdir", "share/doc/Ice-%version%"),
             ("doc", "share/doc/Ice-%version%/ICE_LICENSE"),
             ("doc", "share/doc/Ice-%version%/LICENSE"),
             ("doc", "share/doc/Ice-%version%/README"),
             ("exe", "bin/dumpdb"),
             ("exe", "bin/transformdb"),
             ("exe", "bin/glacier2router"),
             ("exe", "bin/icebox"),
             ("exe", "bin/iceboxadmin"),
             ("exe", "bin/icepackadmin"),
             ("exe", "bin/icecpp"),
             ("exe", "bin/icepacknode"),
             ("exe", "bin/icepackregistry"),
             ("exe", "bin/icepatch2calc"),
             ("exe", "bin/icepatch2client"),
             ("exe", "bin/icepatch2server"),
             ("exe", "bin/icestormadmin"),
             ("exe", "bin/slice2docbook"), 
             ("lib", "lib/libFreeze.so.VERSION"),
             ("lib", "lib/libGlacier2.so.VERSION"),
             ("lib", "lib/libIceBox.so.VERSION"),
             ("lib", "lib/libIcePack.so.VERSION"),
             ("lib", "lib/libIcePatch2.so.VERSION"),
             ("lib", "lib/libIce.so.VERSION"),
             ("lib", "lib/libIceSSL.so.VERSION"),
             ("lib", "lib/libIceStormService.so.VERSION"),
             ("lib", "lib/libIceStorm.so.VERSION"),
             ("lib", "lib/libIceUtil.so.VERSION"),
             ("lib", "lib/libIceXML.so.VERSION"),
             ("lib", "lib/libSlice.so.VERSION"),
             ("dir", "share/slice"),
             ("dir", "share/doc/Ice-%version%/doc"),
             ("xdir", "share/doc/Ice-%version%/certs"),
	     ("file", "share/doc/Ice-%version%/certs/cacert.pem"),
	     ("file", "share/doc/Ice-%version%/certs/c_dh1024.pem"),
	     ("file", "share/doc/Ice-%version%/certs/client_sslconfig.xml"),
	     ("file", "share/doc/Ice-%version%/certs/server_sslconfig.xml"),
	     ("file", "share/doc/Ice-%version%/certs/c_rsa1024_priv.pem"),
	     ("file", "share/doc/Ice-%version%/certs/c_rsa1024_pub.pem"),
	     ("file", "share/doc/Ice-%version%/certs/s_dh1024.pem"),
	     ("file", "share/doc/Ice-%version%/certs/s_rsa1024_priv.pem"),
	     ("file", "share/doc/Ice-%version%/certs/s_rsa1024_pub.pem"),
	     ("file", "share/doc/Ice-%version%/certs/sslconfig.dtd"),
	     ("file", "share/doc/Ice-%version%/certs/sslconfig.xml"),
	     ("file", "share/doc/Ice-%version%/README.DEMOS")]),
    Subpackage("c++-devel",
               "",
               "Tools and demos for developing Ice applications in C++",
               "Development/Tools",
	       iceDescription,
	       "",
               [("exe", "bin/slice2cpp"),
                ("exe", "bin/slice2freeze"),
                ("dir", "include"),
		("lib", "lib/libFreeze.so"),
		("lib", "lib/libGlacier2.so"),
		("lib", "lib/libIceBox.so"),
		("lib", "lib/libIcePack.so"),
		("lib", "lib/libIcePatch2.so"),
		("lib", "lib/libIce.so"),
		("lib", "lib/libIceSSL.so"),
		("lib", "lib/libIceStormService.so"),
		("lib", "lib/libIceStorm.so"),
		("lib", "lib/libIceUtil.so"),
		("lib", "lib/libIceXML.so"),
		("lib", "lib/libSlice.so"),
		("xdir", "share/doc/Ice-%version%"),
		("dir", "share/doc/Ice-%version%/demo"),
		("xdir", "share/doc/Ice-%version%/config"),
		("file", "share/doc/Ice-%version%/config/Make.rules"),
		("file", "share/doc/Ice-%version%/config/Make.rules.Linux"),
		]),
    Subpackage("dotnet",
               "ice = %version%, mono-core >= 1.1.7",
               "The Ice runtime for C# applications",
               "System Environment/Libraries",
	       iceDescription,
	       "",
               [("dll", "bin/glacier2cs.dll"), 
	        ("dll", "bin/icecs.dll"), 
		("dll", "bin/icepackcs.dll"),
                ("dll", "bin/icepatch2cs.dll"), 
		("dll", "bin/icestormcs.dll")]),
    Subpackage("csharp-devel",
               "ice-dotnet = %version%",
               "Tools and demos for developing Ice applications in C#",
               "Development/Tools",
	       iceDescription,
	       "",
               [("exe", "bin/slice2cs"),
		("xdir", "share/doc/Ice-%version%"),
		("xdir", "share/doc/Ice-%version%/config"),
		("file", "share/doc/Ice-%version%/config/Make.rules.cs"),
	        ("dir", "share/doc/Ice-%version%/democs")]),
    Subpackage("java-devel",
               "ice-java = %version%",
               "Tools and demos for developing Ice applications in Java",
               "Development/Tools",
	       iceDescription,
	       "",
               [("exe", "bin/slice2java"),
                ("exe", "bin/slice2freezej"),
		("xdir", "lib/Ice-%version%"),
		("dir", "lib/Ice-%version%/ant"),
		("xdir", "share/doc/Ice-%version%"),
		("xdir", "share/doc/Ice-%version%/certs"),
	        ("file", "share/doc/Ice-%version%/certs/certs.jks"),
	        ("file", "share/doc/Ice-%version%/certs/client.jks"),
	        ("file", "share/doc/Ice-%version%/certs/server.jks"),
		("xdir", "share/doc/Ice-%version%/config"),
	        ("file", "share/doc/Ice-%version%/config/build.properties"),
	        ("file", "share/doc/Ice-%version%/config/common.xml"),
		("dir", "share/doc/Ice-%version%/demoj")]),
    Subpackage("python",
               "ice = %version%, python >= 2.3.4",
               "The Ice runtime for Python applications",
               "System Environment/Libraries",
	       iceDescription,
	       "",
               [("lib", "lib/IcePy.so.VERSION"), ("lib", "lib/IcePy.so"), ("dir", "lib/Ice-%version%/python")]),
    Subpackage("python-devel",
               "ice-python = %version%",
               "Tools and demos for developing Ice applications in Python",
               "Development/Tools",
	       iceDescription,
	       "",
               [("exe", "bin/slice2py"),
		("xdir", "share/doc/Ice-%version%"),
	        ("dir", "share/doc/Ice-%version%/demopy")])
    ]

noarchFileList = [
    Package("ice-java",
	    "ice = %version%, db4-java = 4.2.52",
	    "The Ice runtime for Java",
	    "System Environment/Libraries",
	    iceDescription,
	    "BuildArch: noarch",
	    [ ("xdir", "lib/Ice-%version%"),
              ("dir", "lib/Ice-%version%/Ice.jar")
	    ])
    ]

#
# XXX - not used anywhere?
#
def _missingPathParts(source, dest):
    print "Calculating :  " + source + " and " + dest
        
    startPath = dest.find(source)

    result = dest[0:startPath]
    #
    # There is no common element, we'll need to create the whole destination tree.
    #
    if startPath == -1:
        result = dest
    #
    # The common element is at the head, but we already know the path doesn't exists
    # so we need to remaining path elements
    #
    elif startPath == 0:
        result = dest
    #
    # If the common element is not at the tail of destination, then we probably
    # need to create the whole path
    #
    elif startPath + len(source) + 1 < len(dest):
        result = dest
        
    print "Making " + result
    return result

def _transformDirectories(transforms, version, installDir):
    """Transforms a directory tree that was created with 'make installs' to an RPM friendly
       directory tree.  NOTE, this will not work on all transforms, there are certain
       types of transforms in certain orders that will break it."""
    cwd = os.getcwd()
    os.chdir(installDir + "/Ice-" + version)
    for type, source, dest in transforms:
        if dest.find("%version%"):
            dest = dest.replace("%version%", version)

        sourcedir = source
        destdir = dest

	if os.path.exists("./tmp"):
	    shutil.rmtree("./tmp")
	try:
	    if not os.path.isdir(sourcedir):
		os.renames(source, dest)
	    else:
		# 
		# This is a special problem.  What this implies is that
		# we are trying to move the contents of a directory into
		# a subdirectory of itself.  The regular shutil.move()
		# won't cut it.
		# 
		if os.path.isdir(sourcedir) and sourcedir.split("/")[0] == destdir.split("/")[0]:
		    os.renames(sourcedir, "./tmp/" + sourcedir)
		    os.renames("./tmp/" + sourcedir, destdir)	
		else:
		    os.renames(source, dest)

	except OSError:
	    print "Exception occurred while trying to transform " + source + " to " + dest
	    raise

    os.chdir(cwd)

def createArchSpecFile(ofile, installDir, version, soVersion):
    for v in fileLists:
	v.writeHdr(ofile, version, "1", installDir)
	ofile.write("\n\n\n")
    for v in fileLists:
	v.writeFiles(ofile, version, soVersion, installDir)
	ofile.write("\n")

def createNoArchSpecFile(ofile, installDir, version, soVersion):
    for v in noarchFileList:
	v.writeHdr(ofile, version, "1", installDir)
	ofile.write("\n\n\n")
    for v in noarchFileList:
	v.writeFiles(ofile, version, soVersion, installDir)
	ofile.write("\n")

def createFullSpecFile(ofile, installDir, version, soVersion):
    fullFileList = []
    fullFileList.extend(fileLists)
    fullFileList.append(
	    Subpackage("java",
		"ice = %version%, db4-java >= 4.2.52",
		"Ice runtime for Ice Java applciations",
		"Development/Libraries",
		"",
		"",
		[("dir", "lib/Ice-%version%/Ice.jar")
		]))
    fullFileList[0].addPrepGenerator(writeUnpackingCommands)
    fullFileList[0].addBuildGenerator(writeBuildCommands)
    fullFileList[0].addInstallGenerator(writeInstallCommands)
    fullFileList[0].addInstallGenerator(writeTransformCommands)
    fullFileList[0].addInstallGenerator(writeDemoPkgCommands)

    for v in fullFileList:
	v.writeHdr(ofile, version, "1", installDir)
	ofile.write("\n\n\n")
    for v in fullFileList:
	v.writeFiles(ofile, version, soVersion, installDir)
	ofile.write("\n")

def createRPMSFromBinaries(buildDir, installDir, version, soVersion):
    _transformDirectories(transforms, version, installDir)
    os.system("tar xfz " + installDir + "/Ice-" + version + "-demos.tar.gz -C " + installDir)
    shutil.move(installDir + "/Ice-" + version, installDir + "/usr")

    ofile = open(buildDir + "/Ice-" + version + ".spec", "w")
    createArchSpecFile(ofile, installDir, version, soVersion)
    ofile.flush()
    ofile.close()
    #
    # Copy demo files so the RPM spec file can pick them up.
    #
    os.system("cp -pR " + installDir + "/Ice-" + version + "-demos/* " + installDir + "/usr/share/doc/Ice-" + version)
    if os.path.exists(installDir + "/Ice-" + version + "-demos"):
	shutil.rmtree(installDir + "/Ice-" + version + "-demos")
    cwd = os.getcwd()
    os.chdir(buildDir)
    os.system("rpmbuild -bb Ice-" + version + ".spec")

    # 
    # Build noarch RPMs
    #
    ofile = open(buildDir + "/IceJ-" + version + ".spec", "w")
    createNoArchSpecFile(ofile, installDir, version, soVersion)
    ofile.flush()
    ofile.close()
    os.system("rpmbuild --target noarch -bb IceJ-" + version + ".spec")

    ofile = open(installDir + '/ice-' + version + '.spec', 'w')
    createFullSpecFile(ofile, installDir, version, soVersion)
    ofile.flush()
    ofile.close()
    os.system('rpmbuild -bs ' + installDir + '/ice-' + version + '.spec')


def writeUnpackingCommands(ofile, version):
    ofile.write('%setup -n Ice-%{version} -q -T -D -b 0\n')
    ofile.write("""#
# The Ice make system does not allow the prefix directory to be specified
# through an environment variable or a command line option.  So we edit some
# files in place with sed.
#
sed -i -e 's/^prefix.*$/prefix = $\(RPM_BUILD_ROOT\)/' $RPM_BUILD_DIR/Ice-%{version}/config/Make.rules
%setup -q -n IceJ-%{version} -T -D -b 1
%setup -q -n IcePy-%{version} -T -D -b 2
sed -i -e 's/^prefix.*$/prefix = $\(RPM_BUILD_ROOT\)/' $RPM_BUILD_DIR/IcePy-%{version}/config/Make.rules
%setup -q -n IceCS-%{version} -T -D -b 3 
sed -i -e 's/^prefix.*$/prefix = $\(RPM_BUILD_ROOT\)/' $RPM_BUILD_DIR/IceCS-%{version}/config/Make.rules.cs
""")

def writeBuildCommands(ofile, version):
    ofile.write("""
cd $RPM_BUILD_DIR/Ice-%{version}
gmake OPTIMIZE=yes RPM_BUILD_ROOT=$RPM_BUILD_ROOT/usr
cd $RPM_BUILD_DIR/IcePy-%{version}
gmake  OPTIMIZE=yes ICE_HOME=$RPM_BUILD_DIR/Ice-%{version} RPM_BUILD_ROOT=$RPM_BUILD_ROOT/usr
cd $RPM_BUILD_DIR/IceCS-%{version}
export PATH=$RPM_BUILD_DIR/Ice-%{version}/bin:$PATH
export LD_LIBRARY_PATH=$RPM_BUILD_DIR/Ice-%{version}/lib:$LD_LIBRARY_PATH
gmake OPTIMIZE=yes ICE_HOME=$RPM_BUILD_DIR/Ice-%{version} RPM_BUILD_ROOT=$RPM_BUILD_ROOT/usr
""")

def writeInstallCommands(ofile, version):
    ofile.write("""
rm -rf $RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT/usr
mkdir -p $RPM_BUILD_ROOT/usr/bin
mkdir -p $RPM_BUILD_ROOT/usr/lib
mkdir -p $RPM_BUILD_ROOT/usr/include
mkdir -p $RPM_BUILD_ROOT/usr/doc
cd $RPM_BUILD_DIR/Ice-%{version}
gmake RPM_BUILD_ROOT=$RPM_BUILD_ROOT/usr install
cp -p $RPM_BUILD_DIR/IceJ-%{version}/lib/Ice.jar $RPM_BUILD_ROOT/usr/lib/Ice.jar
cp -pR $RPM_BUILD_DIR/IceJ-%{version}/ant $RPM_BUILD_ROOT/usr
cd $RPM_BUILD_DIR/IcePy-%{version}
gmake ICE_HOME=$RPM_BUILD_DIR/Ice-%{version} RPM_BUILD_ROOT=$RPM_BUILD_ROOT/usr install
cd $RPM_BUILD_DIR/IceCS-%{version}
export PATH=$RPM_BUILD_DIR/Ice-%{version}/bin:$PATH
export LD_LIBRARY_PATH=$RPM_BUILD_DIR/Ice-%{version}/lib:$LD_LIBRARY_PATH
gmake NOGAC=yes ICE_HOME=$RPM_BUILD_DIR/Ice-%{version} RPM_BUILD_ROOT=$RPM_BUILD_ROOT/usr install
cp $RPM_SOURCE_DIR/README.Linux-RPM $RPM_BUILD_ROOT/usr/README
""")

def writeTransformCommands(ofile, version):
    ofile.write('#\n')
    ofile.write('# The following commands transform a standard Ice installation directory\n')
    ofile.write('# structure to a directory structure more suited to integrating into a\n')
    ofile.write('# Linux system.\n')
    ofile.write('#\n\n')
    for type, source, dest in transforms:
	dest = dest.replace('%version%', version)
	if type == 'file':
	    ofile.write('mkdir -p $RPM_BUILD_ROOT/usr/' + os.path.dirname(dest) + '\n')
	    ofile.write('mv $RPM_BUILD_ROOT/usr/' + source + ' $RPM_BUILD_ROOT/usr/' + dest + '\n')
	elif type == 'dir':
	    if os.path.dirname(dest) <> '' and source.split('/')[0] == dest.split('/')[0]:
		ofile.write('mkdir -p $RPM_BUILD_ROOT/arraftmp\n')
		ofile.write('mv $RPM_BUILD_ROOT/usr/' + source + ' $RPM_BUILD_ROOT/arraftmp/' + source + '\n')
		ofile.write('mkdir -p $RPM_BUILD_ROOT/usr/' + os.path.dirname(dest) + '\n')
		ofile.write('mv $RPM_BUILD_ROOT/arraftmp/' + source + ' $RPM_BUILD_ROOT/usr/' + dest + '\n')
		ofile.write('rm -rf $RPM_BUILD_ROOT/arraftmp\n')
	    elif os.path.dirname(dest) <> '':
		ofile.write('mkdir -p $RPM_BUILD_ROOT/usr/' + os.path.dirname(dest) + '\n')
		ofile.write('mv $RPM_BUILD_ROOT/usr/' + source + ' $RPM_BUILD_ROOT/usr/' + dest + '\n')
	    else:
		ofile.write('mv $RPM_BUILD_ROOT/usr/' + source + ' $RPM_BUILD_ROOT/usr/' + dest + '\n')

def writeDemoPkgCommands(ofile, version):
    ofile.write('#\n')
    ofile.write('# Extract the contents of the demo packaged into the installed location.\n')
    ofile.write('#\n')
    ofile.write('mkdir -p $RPM_BUILD_ROOT/usr/share/doc/Ice-%{version}\n')
    ofile.write('tar xfz $RPM_SOURCE_DIR/Ice-%{version}-demos.tar.gz -C $RPM_BUILD_ROOT/usr/share/doc\n')
    ofile.write('cp -pR $RPM_BUILD_ROOT/usr/share/doc/Ice-%{version}-demos/* $RPM_BUILD_ROOT/usr/share/doc/Ice-%{version}\n')
    ofile.write('rm -rf $RPM_BUILD_ROOT/usr/share/doc/Ice-%{version}-demos\n')
	
if __name__ == "main":
    print 'Ice RPM Tools module'