Initialize the new branch.
[scons.git] / SConstruct
1 #
2 # SConstruct file to build scons packages during development.
3 #
4
5 #
6 # Copyright (c) 2001, 2002 Steven Knight
7 #
8 # Permission is hereby granted, free of charge, to any person obtaining
9 # a copy of this software and associated documentation files (the
10 # "Software"), to deal in the Software without restriction, including
11 # without limitation the rights to use, copy, modify, merge, publish,
12 # distribute, sublicense, and/or sell copies of the Software, and to
13 # permit persons to whom the Software is furnished to do so, subject to
14 # the following conditions:
15 #
16 # The above copyright notice and this permission notice shall be included
17 # in all copies or substantial portions of the Software.
18 #
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
20 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
21 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #
27
28 import distutils.util
29 import os
30 import os.path
31 import stat
32 import string
33 import sys
34 import time
35
36 project = 'scons'
37 default_version = '0.07'
38
39 Default('.')
40
41 #
42 # An internal "whereis" routine to figure out if we have a
43 # given program available.  Put it in the "cons::" package
44 # so subsidiary Conscript files can get at it easily, too.
45 #
46
47 def whereis(file):
48     for dir in string.split(os.environ['PATH'], os.pathsep):
49         f = os.path.join(dir, file)
50         if os.path.isfile(f):
51             try:
52                 st = os.stat(f)
53             except:
54                 continue
55             if stat.S_IMODE(st[stat.ST_MODE]) & 0111:
56                 return f
57     return None
58
59 #
60 # We let the presence or absence of various utilities determine
61 # whether or not we bother to build certain pieces of things.
62 # This will allow people to still do SCons work even if they
63 # don't have Aegis or RPM installed, for example.
64 #
65 aegis = whereis('aegis')
66 aesub = whereis('aesub')
67 dh_builddeb = whereis('dh_builddeb')
68 fakeroot = whereis('fakeroot')
69 gzip = whereis('gzip')
70 rpm = whereis('rpm')
71 unzip = whereis('unzip')
72 zip = whereis('zip')
73
74 # My installation on Red Hat doesn't like any debhelper version
75 # beyond 2, so let's use 2 as the default on any non-Debian build.
76 if os.path.isfile('/etc/debian_version'):
77     dh_compat = 3
78 else:
79     dh_compat = 2
80
81 #
82 # Now grab the information that we "build" into the files (using sed).
83 #
84 try:
85     date = ARGUMENTS['date']
86 except:
87     date = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(time.time()))
88     
89 if ARGUMENTS.has_key('developer'):
90     developer = ARGUMENTS['developer']
91 elif os.environ.has_key('USERNAME'):
92     developer = os.environ['USERNAME']
93 elif os.environ.has_key('LOGNAME'):
94     developer = os.environ['LOGNAME']
95 elif os.environ.has_key('USER'):
96     developer = os.environ['USER']
97
98 try:
99     revision = ARGUMENTS['version']
100 except:
101     if aesub:
102         revision = os.popen(aesub + " \\$version", "r").read()[:-1]
103     else:
104         revision = default_version
105
106 a = string.split(revision, '.')
107 arr = [a[0]]
108 for s in a[1:]:
109     if len(s) == 1:
110         s = '0' + s
111     arr.append(s)
112 revision = string.join(arr, '.')
113
114 # Here's how we'd turn the calculated $revision into our package $version.
115 # This makes it difficult to coordinate with other files (debian/changelog
116 # and rpm/scons.spec) that hard-code the version number, so just go with
117 # the flow for now and hard code it here, too.
118 #if len(arr) >= 2:
119 #    arr = arr[:-1]
120 #def xxx(str):
121 #    if str[0] == 'C' or str[0] == 'D':
122 #        str = str[1:]
123 #    while len(str) > 2 and str[0] == '0':
124 #        str = str[1:]
125 #    return str
126 #arr = map(lambda x, xxx=xxx: xxx(x), arr)
127 #version = string.join(arr, '.')
128 version = default_version
129
130 try:
131     change = ARGUMENTS['change']
132 except:
133     if aesub:
134         change = os.popen(aesub + " \\$change", "r").read()[:-1]
135     else:
136         change = default_version
137
138 python_ver = sys.version[0:3]
139
140 platform = distutils.util.get_platform()
141
142 ENV = { 'PATH' : os.environ['PATH'] }
143 if os.environ.has_key('AEGIS_PROJECT'):
144     ENV['AEGIS_PROJECT'] = os.environ['AEGIS_PROJECT']
145
146 lib_project = os.path.join("lib", project)
147
148 unpack_tar_gz_dir = os.path.join(os.getcwd(), "build", "unpack-tar-gz")
149
150 unpack_zip_dir = os.path.join(os.getcwd(), "build", "unpack-zip")
151
152 test_tar_gz_dir = os.path.join(os.getcwd(), "build", "test-tar-gz")
153 test_src_tar_gz_dir = os.path.join(os.getcwd(), "build", "test-src-tar-gz")
154
155 test_zip_dir = os.path.join(os.getcwd(), "build", "test-zip")
156 test_src_zip_dir = os.path.join(os.getcwd(), "build", "test-src-zip")
157
158 test_rpm_dir = os.path.join(os.getcwd(), "build", "test-rpm")
159
160 test_deb_dir = os.path.join(os.getcwd(), "build", "test-deb")
161
162 if platform == "win32":
163     tar_hflag = ''
164     python_project_subinst_dir = None
165     project_script_subinst_dir = 'Scripts'
166 else:
167     tar_hflag = 'h'
168     python_project_subinst_dir = lib_project
169     project_script_subinst_dir = 'bin'
170
171 #
172 # Figure out if we can handle .zip files.
173 #
174 try:
175     import zipfile
176
177     def zipit(env, target, source):
178         print "Zipping %s:" % str(target[0])
179         def visit(arg, dirname, names):
180             for name in names:
181                 path = os.path.join(dirname, name)
182                 if os.path.isfile(path):
183                     arg.write(path)
184         zf = zipfile.ZipFile(str(target[0]), 'w')
185         os.chdir('build')
186         os.path.walk(env['PSV'], visit, zf)
187         os.chdir('..')
188         zf.close()
189
190     def unzipit(env, target, source):
191         print "Unzipping %s:" % str(source[0])
192         zf = zipfile.ZipFile(str(source[0]), 'r')
193         for name in zf.namelist():
194             dest = os.path.join(env['UNPACK_ZIP_DIR'], name)
195             dir = os.path.dirname(dest)
196             try:
197                 os.makedirs(dir)
198             except:
199                 pass
200             print dest,name
201             # if the file exists, then delete it before writing
202             # to it so that we don't end up trying to write to a symlink:
203             if os.path.exists(dest):
204                 os.unlink(dest)
205             open(dest, 'w').write(zf.read(name))
206
207 except:
208     if unzip and zip:
209         zipit = "cd build && $ZIP $ZIPFLAGS dist/${TARGET.file} $PSV"
210         unzipit = "$UNZIP $UNZIPFLAGS $SOURCES"
211
212 def SCons_revision(target, source, env):
213     """Interpolate specific values from the environment into a file.
214     
215     This is used to copy files into a tree that gets packaged up
216     into the source file package.
217     """
218     t = str(target[0])
219     s = str(source[0])
220     # Note:  We don't use $VERSION from the environment so that
221     # this routine will change when the version number changes
222     # and things will get rebuilt properly.
223     global version
224     print "SCons_revision() < %s > %s" % (s, t)
225     inf = open(s, 'rb')
226     outf = open(t, 'wb')
227     for line in inf.readlines():
228         # Note:  We construct the __*__ substitution strings here
229         # so that they don't get replaced when this file gets
230         # copied into the tree for packaging.
231         line = string.replace(line, '_' + '_DATE__', env['DATE'])
232         line = string.replace(line, '_' + '_DEVELOPER__', env['DEVELOPER'])
233         line = string.replace(line, '_' + '_FILE__', s)
234         line = string.replace(line, '_' + '_REVISION__', env['REVISION'])
235         line = string.replace(line, '_' + '_VERSION__', version)
236         outf.write(line)
237     inf.close()
238     outf.close()
239     os.chmod(t, os.stat(s)[0])
240
241 revbuilder = Builder(name = 'SCons_revision', action = SCons_revision)
242
243 env = Environment(
244                    ENV                 = ENV,
245  
246                    DATE                = date,
247                    DEVELOPER           = developer,
248                    REVISION            = revision,
249                    VERSION             = version,
250                    DH_COMPAT           = dh_compat,
251
252                    TAR_HFLAG           = tar_hflag,
253
254                    ZIP                 = zip,
255                    ZIPFLAGS            = '-r',
256                    UNZIP               = unzip,
257                    UNZIPFLAGS          = '-o -d $UNPACK_ZIP_DIR',
258
259                    TEST_DEB_DIR        = test_deb_dir,
260                    TEST_RPM_DIR        = test_rpm_dir,
261                    TEST_SRC_TAR_GZ_DIR = test_src_tar_gz_dir,
262                    TEST_SRC_ZIP_DIR    = test_src_zip_dir,
263                    TEST_TAR_GZ_DIR     = test_tar_gz_dir,
264                    TEST_ZIP_DIR        = test_zip_dir,
265
266                    UNPACK_TAR_GZ_DIR   = unpack_tar_gz_dir,
267                    UNPACK_ZIP_DIR      = unpack_zip_dir,
268
269                    BUILDERS            = [ revbuilder ],
270                  )
271
272 #
273 # Define SCons packages.
274 #
275 # In the original, more complicated packaging scheme, we were going
276 # to have separate packages for:
277 #
278 #       python-scons    only the build engine
279 #       scons-script    only the script
280 #       scons           the script plus the build engine
281 #
282 # We're now only delivering a single "scons" package, but this is still
283 # "built" as two sub-packages (the build engine and the script), so
284 # the definitions remain here, even though we're not using them for
285 # separate packages.
286 #
287
288 python_scons = {
289         'pkg'           : 'python-' + project,
290         'src_subdir'    : 'engine',
291         'inst_subdir'   : os.path.join('lib', 'python1.5', 'site-packages'),
292
293         'debian_deps'   : [
294                             'debian/rules',
295                             'debian/control',
296                             'debian/changelog',
297                             'debian/copyright',
298                             'debian/python-scons.postinst',
299                             'debian/python-scons.prerm',
300                           ],
301
302         'files'         : [ 'LICENSE.txt',
303                             'README.txt',
304                             'setup.cfg',
305                             'setup.py',
306                           ],
307
308         'filemap'       : {
309                             'LICENSE.txt' : '../LICENSE.txt'
310                           },
311 }
312
313 #
314 # The original packaging scheme would have have required us to push
315 # the Python version number into the package name (python1.5-scons,
316 # python2.0-scons, etc.), which would have required a definition
317 # like the following.  Leave this here in case we ever decide to do
318 # this in the future, but note that this would require some modification
319 # to src/engine/setup.py before it would really work.
320 #
321 #python2_scons = {
322 #        'pkg'          : 'python2-' + project,
323 #        'src_subdir'   : 'engine',
324 #        'inst_subdir'  : os.path.join('lib', 'python2.1', 'site-packages'),
325 #
326 #        'debian_deps'  : [
327 #                           'debian/rules',
328 #                           'debian/control',
329 #                           'debian/changelog',
330 #                           'debian/copyright',
331 #                           'debian/python2-scons.postinst',
332 #                           'debian/python2-scons.prerm',
333 #                          ],
334 #
335 #        'files'        : [
336 #                            'LICENSE.txt',
337 #                            'README.txt',
338 #                            'setup.cfg',
339 #                            'setup.py',
340 #                          ],
341 #        'filemap'      : {
342 #                            'LICENSE.txt' : '../LICENSE.txt',
343 #                          },
344 #}
345 #
346
347 scons_script = {
348         'pkg'           : project + '-script',
349         'src_subdir'    : 'script',
350         'inst_subdir'   : 'bin',
351
352         'debian_deps'   : [
353                             'debian/rules',
354                             'debian/control',
355                             'debian/changelog',
356                             'debian/copyright',
357                             'debian/python-scons.postinst',
358                             'debian/python-scons.prerm',
359                           ],
360
361         'files'         : [
362                             'LICENSE.txt',
363                             'README.txt',
364                             'setup.cfg',
365                             'setup.py',
366                           ],
367
368         'filemap'       : {
369                             'LICENSE.txt' : '../LICENSE.txt',
370                             'scons'       : 'scons.py',
371                            }
372 }
373
374 scons = {
375         'pkg'           : project,
376
377         'debian_deps'   : [ 
378                             'debian/rules',
379                             'debian/control',
380                             'debian/changelog',
381                             'debian/copyright',
382                             'debian/scons.postinst',
383                             'debian/scons.prerm',
384                           ],
385
386         'files'         : [ 
387                             'CHANGES.txt',
388                             'LICENSE.txt',
389                             'README.txt',
390                             'RELEASE.txt',
391                             'os_spawnv_fix.diff',
392                             'scons.1',
393                             'script/scons.bat',
394                             'setup.cfg',
395                             'setup.py',
396                           ],
397
398         'filemap'       : {
399                             'scons.1' : '../doc/man/scons.1',
400                           },
401
402         'subpkgs'       : [ python_scons, scons_script ],
403
404         'subinst_dirs'  : {
405                              'python-' + project : python_project_subinst_dir,
406                              project + '-script' : project_script_subinst_dir,
407                            },
408 }
409
410 src_deps = []
411 src_files = []
412
413 for p in [ scons ]:
414     #
415     # Initialize variables with the right directories for this package.
416     #
417     pkg = p['pkg']
418     pkg_version = "%s-%s" % (pkg, version)
419
420     src = 'src'
421     if p.has_key('src_subdir'):
422         src = os.path.join(src, p['src_subdir'])
423
424     build = os.path.join('build', pkg)
425
426     tar_gz = os.path.join(build, 'dist', "%s.tar.gz" % pkg_version)
427     platform_tar_gz = os.path.join(build,
428                                    'dist',
429                                    "%s.%s.tar.gz" % (pkg_version, platform))
430     zip = os.path.join(build, 'dist', "%s.zip" % pkg_version)
431     platform_zip = os.path.join(build,
432                                 'dist',
433                                 "%s.%s.zip" % (pkg_version, platform))
434     win32_exe = os.path.join(build, 'dist', "%s.win32.exe" % pkg_version)
435
436     #
437     # Update the environment with the relevant information
438     # for this package.
439     #
440     # We can get away with calling setup.py using a directory path
441     # like this because we put a preamble in it that will chdir()
442     # to the directory in which setup.py exists.
443     #
444     env.Update(PKG = pkg,
445                PKG_VERSION = pkg_version,
446                SETUP_PY = os.path.join(build, 'setup.py'))
447
448     #
449     # Read up the list of source files from our MANIFEST.in.
450     # This list should *not* include LICENSE.txt, MANIFEST,
451     # README.txt, or setup.py.  Make a copy of the list for the
452     # destination files.
453     #
454     src_files = map(lambda x: x[:-1],
455                     open(os.path.join(src, 'MANIFEST.in')).readlines())
456     dst_files = src_files[:]
457
458     MANIFEST_in_list = []
459
460     if p.has_key('subpkgs'):
461         #
462         # This package includes some sub-packages.  Read up their
463         # MANIFEST.in files, and add them to our source and destination
464         # file lists, modifying them as appropriate to add the
465         # specified subdirs.
466         #
467         for sp in p['subpkgs']:
468             ssubdir = sp['src_subdir']
469             isubdir = p['subinst_dirs'][sp['pkg']]
470             MANIFEST_in = os.path.join(src, ssubdir, 'MANIFEST.in')
471             MANIFEST_in_list.append(MANIFEST_in)
472             f = map(lambda x: x[:-1], open(MANIFEST_in).readlines())
473             src_files.extend(map(lambda x, s=ssubdir: os.path.join(s, x), f))
474             if isubdir:
475                 f = map(lambda x, i=isubdir: os.path.join(i, x), f)
476             dst_files.extend(f)
477             for k in sp['filemap'].keys():
478                 f = sp['filemap'][k]
479                 if f:
480                     k = os.path.join(sp['src_subdir'], k)
481                     p['filemap'][k] = os.path.join(sp['src_subdir'], f)
482
483     #
484     # Now that we have the "normal" source files, add those files
485     # that are standard for each distribution.  Note that we don't
486     # add these to dst_files, because they don't get installed.
487     # And we still have the MANIFEST to add.
488     #
489     src_files.extend(p['files'])
490
491     #
492     # Now run everything in src_file through the sed command we
493     # concocted to expand __FILE__, __VERSION__, etc.
494     #
495     for b in src_files:
496         s = p['filemap'].get(b, b)
497         env.SCons_revision(os.path.join(build, b), os.path.join(src, s))
498
499     #
500     # NOW, finally, we can create the MANIFEST, which we do
501     # by having Python spit out the contents of the src_files
502     # array we've carefully created.  After we've added
503     # MANIFEST itself to the array, of course.
504     #
505     src_files.append("MANIFEST")
506     MANIFEST_in_list.append(os.path.join(src, 'MANIFEST.in'))
507
508     def copy(target, source, **kw):
509         global src_files
510         src_files.sort()
511         f = open(str(target[0]), 'wb')
512         for file in src_files:
513             f.write(file + "\n")
514         f.close()
515         return 0
516     env.Command(os.path.join(build, 'MANIFEST'), MANIFEST_in_list, copy)
517
518     #
519     # Now go through and arrange to create whatever packages we can.
520     #
521     build_src_files = map(lambda x, b=build: os.path.join(b, x), src_files)
522
523     distutils_formats = []
524
525     distutils_targets = [ win32_exe ]
526
527     install_targets = distutils_targets[:]
528
529     if gzip:
530
531         distutils_formats.append('gztar')
532
533         src_deps.append(tar_gz)
534
535         distutils_targets.extend([ tar_gz, platform_tar_gz ])
536         install_targets.extend([ tar_gz, platform_tar_gz ])
537
538         #
539         # Unpack the tar.gz archive created by the distutils into
540         # build/unpack-tar-gz/scons-{version}.
541         #
542         # We'd like to replace the last three lines with the following:
543         #
544         #       tar zxf $SOURCES -C $UNPACK_TAR_GZ_DIR
545         #
546         # but that gives heartburn to Cygwin's tar, so work around it
547         # with separate zcat-tar-rm commands.
548         #
549         unpack_tar_gz_files = map(lambda x, u=unpack_tar_gz_dir, pv=pkg_version:
550                                          os.path.join(u, pv, x),
551                                   src_files)
552         env.Command(unpack_tar_gz_files, tar_gz, [
553                     "rm -rf %s" % os.path.join(unpack_tar_gz_dir, pkg_version),
554                     "zcat $SOURCES > .temp",
555                     "tar xf .temp -C $UNPACK_TAR_GZ_DIR",
556                     "rm -f .temp",
557         ])
558     
559         #
560         # Run setup.py in the unpacked subdirectory to "install" everything
561         # into our build/test subdirectory.  The runtest.py script will set
562         # PYTHONPATH so that the tests only look under build/test-{package},
563         # and under etc (for the testing modules TestCmd.py, TestSCons.py,
564         # and unittest.py).  This makes sure that our tests pass with what
565         # we really packaged, not because of something hanging around in
566         # the development directory.
567         #
568         # We can get away with calling setup.py using a directory path
569         # like this because we put a preamble in it that will chdir()
570         # to the directory in which setup.py exists.
571         #
572         dfiles = map(lambda x, d=test_tar_gz_dir: os.path.join(d, x), dst_files)
573         env.Command(dfiles, unpack_tar_gz_files, [
574             "rm -rf %s" % os.path.join(unpack_tar_gz_dir, pkg_version, 'build'),
575             "rm -rf $TEST_TAR_GZ_DIR",
576             "python %s install --prefix=$TEST_TAR_GZ_DIR" % \
577                 os.path.join(unpack_tar_gz_dir, pkg_version, 'setup.py'),
578         ])
579
580     if zipit:
581
582         distutils_formats.append('zip')
583
584         src_deps.append(zip)
585
586         distutils_targets.extend([ zip, platform_zip ])
587         install_targets.extend([ zip, platform_zip ])
588
589         #
590         # Unpack the zip archive created by the distutils into
591         # build/unpack-zip/scons-{version}.
592         #
593         unpack_zip_files = map(lambda x, u=unpack_zip_dir, pv=pkg_version:
594                                       os.path.join(u, pv, x),
595                                src_files)
596
597         env.Command(unpack_zip_files, zip, unzipit)
598
599         #
600         # Run setup.py in the unpacked subdirectory to "install" everything
601         # into our build/test subdirectory.  The runtest.py script will set
602         # PYTHONPATH so that the tests only look under build/test-{package},
603         # and under etc (for the testing modules TestCmd.py, TestSCons.py,
604         # and unittest.py).  This makes sure that our tests pass with what
605         # we really packaged, not because of something hanging around in
606         # the development directory.
607         #
608         # We can get away with calling setup.py using a directory path
609         # like this because we put a preamble in it that will chdir()
610         # to the directory in which setup.py exists.
611         #
612         dfiles = map(lambda x, d=test_zip_dir: os.path.join(d, x), dst_files)
613         env.Command(dfiles, unpack_zip_files, [
614             "rm -rf %s" % os.path.join(unpack_zip_dir, pkg_version, 'build'),
615             "rm -rf $TEST_ZIP_DIR",
616             "python %s install --prefix=$TEST_ZIP_DIR" % \
617                 os.path.join(unpack_zip_dir, pkg_version, 'setup.py'),
618         ])
619
620     if rpm:
621         topdir = os.path.join(os.getcwd(), build, 'build',
622                               'bdist.' + platform, 'rpm')
623
624         BUILDdir = os.path.join(topdir, 'BUILD', pkg + '-' + version)
625         RPMSdir = os.path.join(topdir, 'RPMS', 'noarch')
626         SOURCESdir = os.path.join(topdir, 'SOURCES')
627         SPECSdir = os.path.join(topdir, 'SPECS')
628         SRPMSdir = os.path.join(topdir, 'SRPMS')
629
630         specfile = os.path.join(SPECSdir, "%s-1.spec" % pkg_version)
631         sourcefile = os.path.join(SOURCESdir, "%s.tar.gz" % pkg_version);
632         rpm = os.path.join(RPMSdir, "%s-1.noarch.rpm" % pkg_version)
633         src_rpm = os.path.join(SRPMSdir, "%s-1.src.rpm" % pkg_version)
634
635         env.InstallAs(specfile, os.path.join('rpm', "%s.spec" % pkg))
636         env.InstallAs(sourcefile, tar_gz)
637
638         targets = [ rpm, src_rpm ]
639         cmd = "rpm --define '_topdir $(%s$)' -ba $SOURCES" % topdir
640         if not os.path.isdir(BUILDdir):
641             cmd = ("$( mkdir -p %s; $)" % BUILDdir) + cmd
642         env.Command(targets, specfile, cmd)
643         env.Depends(targets, sourcefile)
644
645         install_targets.extend(targets)
646
647         dfiles = map(lambda x, d=test_rpm_dir: os.path.join(d, 'usr', x),
648                      dst_files)
649         env.Command(dfiles,
650                     rpm,
651                     "rpm2cpio $SOURCES | (cd $TEST_RPM_DIR && cpio -id)")
652
653     if dh_builddeb and fakeroot:
654         # Our Debian packaging builds directly into build/dist,
655         # so we don't need to add the .debs to install_targets.
656         deb = os.path.join('build', 'dist', "%s_%s-1_all.deb" % (pkg, version))
657         env.Command(deb, build_src_files, [
658             "fakeroot make -f debian/rules VERSION=$VERSION DH_COMPAT=$DH_COMPAT ENVOKED_BY_CONSTRUCT=1 binary-$PKG",
659             "env DH_COMPAT=$DH_COMPAT dh_clean"
660                     ])
661         env.Depends(deb, p['debian_deps'])
662
663         dfiles = map(lambda x, d=test_deb_dir: os.path.join(d, 'usr', x),
664                      dst_files)
665         env.Command(dfiles,
666                     deb,
667                     "dpkg --fsys-tarfile $SOURCES | (cd $TEST_DEB_DIR && tar -xf -)")
668
669     #
670     # Use the Python distutils to generate the appropriate packages.
671     #
672     commands = [
673         "rm -rf %s" % os.path.join(build, 'build', 'lib'),
674         "rm -rf %s" % os.path.join(build, 'build', 'scripts'),
675     ]
676
677     if distutils_formats:
678         commands.append("rm -rf %s" % os.path.join(build,
679                                                    'build',
680                                                    'bdist.' + platform,
681                                                    'dumb'))
682         for format in distutils_formats:
683             commands.append("python $SETUP_PY bdist_dumb -f %s" % format)
684
685         commands.append("python $SETUP_PY sdist --formats=%s" %  \
686                             string.join(distutils_formats, ','))
687
688     commands.append("python $SETUP_PY bdist_wininst")
689
690     env.Command(distutils_targets, build_src_files, commands)
691
692     #
693     # And, lastly, install the appropriate packages in the
694     # appropriate subdirectory.
695     #
696     env.Install(os.path.join('build', 'dist'), install_targets)
697
698 #
699 #
700 #
701 Export('env')
702
703 SConscript('etc/SConscript')
704
705 #
706 # Documentation.
707 #
708 BuildDir('build/doc', 'doc')
709
710 Export('env', 'whereis')
711
712 SConscript('build/doc/SConscript')
713
714 #
715 # If we're running in the actual Aegis project, pack up a complete
716 # source archive from the project files and files in the change,
717 # so we can share it with helpful developers who don't use Aegis.
718 #
719
720 if change:
721     df = []
722     cmd = "aegis -list -unf -c %s cf 2>/dev/null" % change
723     for line in map(lambda x: x[:-1], os.popen(cmd, "r").readlines()):
724         a = string.split(line)
725         if a[1] == "remove":
726             if a[3][0] == '(':
727                 df.append(a[4])
728             else:
729                 df.append(a[3])
730
731     cmd = "aegis -list -terse pf 2>/dev/null"
732     pf = map(lambda x: x[:-1], os.popen(cmd, "r").readlines())
733     cmd = "aegis -list -terse cf 2>/dev/null"
734     cf = map(lambda x: x[:-1], os.popen(cmd, "r").readlines())
735     u = {}
736     for f in pf + cf:
737         u[f] = 1
738     for f in df:
739         del u[f]
740     sfiles = filter(lambda x: x[-9:] != '.aeignore' and x[-9:] != '.sconsign',
741                     u.keys())
742
743     if sfiles:
744         ps = "%s-src" % project
745         psv = "%s-%s" % (ps, version)
746         b_ps = os.path.join('build', ps)
747         b_psv = os.path.join('build', psv)
748         b_psv_stamp = b_psv + '-stamp'
749
750         src_tar_gz = os.path.join('build', 'dist', '%s.tar.gz' % psv)
751         src_zip = os.path.join('build', 'dist', '%s.zip' % psv)
752
753         for file in sfiles:
754             env.SCons_revision(os.path.join(b_ps, file), file)
755
756         b_ps_files = map(lambda x, d=b_ps: os.path.join(d, x), sfiles)
757         cmds = [
758             "rm -rf %s" % b_psv,
759             "cp -rp %s %s" % (b_ps, b_psv),
760             "find %s -name .sconsign -exec rm {} \\;" % b_psv,
761             "touch $TARGET",
762         ]
763
764         env.Command(b_psv_stamp, src_deps + b_ps_files, cmds)
765
766         if gzip:
767
768             env.Command(src_tar_gz, b_psv_stamp,
769                         "tar cz${TAR_HFLAG} -f $TARGET -C build %s" % psv)
770     
771             #
772             # Unpack the archive into build/unpack/scons-{version}.
773             #
774             unpack_tar_gz_files = map(lambda x, u=unpack_tar_gz_dir, psv=psv:
775                                              os.path.join(u, psv, x),
776                                       sfiles)
777     
778             #
779             # We'd like to replace the last three lines with the following:
780             #
781             #   tar zxf $SOURCES -C $UNPACK_TAR_GZ_DIR
782             #
783             # but that gives heartburn to Cygwin's tar, so work around it
784             # with separate zcat-tar-rm commands.
785             env.Command(unpack_tar_gz_files, src_tar_gz, [
786                 "rm -rf %s" % os.path.join(unpack_tar_gz_dir, psv),
787                 "zcat $SOURCES > .temp",
788                 "tar xf .temp -C $UNPACK_TAR_GZ_DIR",
789                 "rm -f .temp",
790             ])
791     
792             #
793             # Run setup.py in the unpacked subdirectory to "install" everything
794             # into our build/test subdirectory.  The runtest.py script will set
795             # PYTHONPATH so that the tests only look under build/test-{package},
796             # and under etc (for the testing modules TestCmd.py, TestSCons.py,
797             # and unittest.py).  This makes sure that our tests pass with what
798             # we really packaged, not because of something hanging around in
799             # the development directory.
800             #
801             # We can get away with calling setup.py using a directory path
802             # like this because we put a preamble in it that will chdir()
803             # to the directory in which setup.py exists.
804             #
805             dfiles = map(lambda x, d=test_src_tar_gz_dir: os.path.join(d, x),
806                             dst_files)
807             ENV = env.Dictionary('ENV')
808             ENV['SCONS_LIB_DIR'] = os.path.join(unpack_tar_gz_dir, psv, 'src', 'engine')
809             ENV['USERNAME'] = developer
810             env.Copy(ENV = ENV).Command(dfiles, unpack_tar_gz_files, [
811                 "rm -rf %s" % os.path.join(unpack_tar_gz_dir,
812                                            psv,
813                                            'build',
814                                            'scons',
815                                            'build'),
816                 "rm -rf $TEST_SRC_TAR_GZ_DIR",
817                 "cd %s && python %s %s" % \
818                     (os.path.join(unpack_tar_gz_dir, psv),
819                      os.path.join('src', 'script', 'scons.py'),
820                      os.path.join('build', 'scons')),
821                 "python %s install --prefix=$TEST_SRC_TAR_GZ_DIR" % \
822                     os.path.join(unpack_tar_gz_dir,
823                                  psv,
824                                  'build',
825                                  'scons',
826                                  'setup.py'),
827             ])
828
829         if zipit:
830
831             env.Copy(PSV = psv).Command(src_zip, b_psv_stamp, zipit)
832     
833             #
834             # Unpack the archive into build/unpack/scons-{version}.
835             #
836             unpack_zip_files = map(lambda x, u=unpack_zip_dir, psv=psv:
837                                              os.path.join(u, psv, x),
838                                       sfiles)
839     
840             env.Command(unpack_zip_files, src_zip, unzipit)
841     
842             #
843             # Run setup.py in the unpacked subdirectory to "install" everything
844             # into our build/test subdirectory.  The runtest.py script will set
845             # PYTHONPATH so that the tests only look under build/test-{package},
846             # and under etc (for the testing modules TestCmd.py, TestSCons.py,
847             # and unittest.py).  This makes sure that our tests pass with what
848             # we really packaged, not because of something hanging around in
849             # the development directory.
850             #
851             # We can get away with calling setup.py using a directory path
852             # like this because we put a preamble in it that will chdir()
853             # to the directory in which setup.py exists.
854             #
855             dfiles = map(lambda x, d=test_src_zip_dir: os.path.join(d, x),
856                             dst_files)
857             ENV = env.Dictionary('ENV')
858             ENV['SCONS_LIB_DIR'] = os.path.join(unpack_zip_dir, psv, 'src', 'engine')
859             ENV['USERNAME'] = developer
860             env.Copy(ENV = ENV).Command(dfiles, unpack_zip_files, [
861                 "rm -rf %s" % os.path.join(unpack_zip_dir,
862                                            psv,
863                                            'build',
864                                            'scons',
865                                            'build'),
866                 "rm -rf $TEST_SRC_ZIP_DIR",
867                 "cd %s && python %s %s" % \
868                     (os.path.join(unpack_zip_dir, psv),
869                      os.path.join('src', 'script', 'scons.py'),
870                      os.path.join('build', 'scons')),
871                 "python %s install --prefix=$TEST_SRC_ZIP_DIR" % \
872                     os.path.join(unpack_zip_dir,
873                                  psv,
874                                  'build',
875                                  'scons',
876                                  'setup.py'),
877             ])