summaryrefslogtreecommitdiffstats
path: root/Lib/distutils/command/bdist_dumb.py
blob: 5456e57ac32ae4e1c9876550e1dfade6bd129517 (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
"""distutils.command.bdist_dumb

Implements the Distutils 'bdist_dumb' command (create a "dumb" built
distribution -- i.e., just an archive to be unpacked under $prefix or
$exec_prefix)."""

# created 2000/03/29, Greg Ward

__revision__ = "$Id$"

import os
from distutils.core import Command
from distutils.util import get_platform, create_tree, remove_tree


class bdist_dumb (Command):

    description = "create a \"dumb\" built distribution"

    user_options = [('format=', 'f',
                     "archive format to create (tar, ztar, gztar, zip)"),
                    ('keep-tree', 'k',
                     "keep the pseudo-installation tree around after " +
                     "creating the distribution archive"),
                   ]

    default_format = { 'posix': 'gztar',
                       'nt': 'zip', }


    def initialize_options (self):
        self.format = None
        self.keep_tree = 0

    # initialize_options()


    def finalize_options (self):
        if self.format is None:
            try:
                self.format = self.default_format[os.name]
            except KeyError:
                raise DistutilsPlatformError, \
                      ("don't know how to create dumb built distributions " +
                       "on platform %s") % os.name

    # finalize_options()


    def run (self):

        self.run_peer ('build')
        install = self.find_peer ('install')
        inputs = install.get_inputs ()
        outputs = install.get_outputs ()
        assert (len (inputs) == len (outputs))

        # First, strip the installation base directory (prefix or
        # exec-prefix) from all the output filenames.
        self.strip_base_dirs (outputs, install)

        # Figure out where to copy them to: "build/bdist" by default; this
        # directory masquerades as prefix/exec-prefix (ie.  we'll make the
        # archive from 'output_dir').
        build_base = self.get_peer_option ('build', 'build_base')
        output_dir = os.path.join (build_base, "bdist")

        # Copy the built files to the pseudo-installation tree.
        self.make_install_tree (output_dir, inputs, outputs)

        # And make an archive relative to the root of the
        # pseudo-installation tree.
        archive_basename = "%s.%s" % (self.distribution.get_full_name(),
                                      get_platform())
        print "output_dir = %s" % output_dir
        print "self.format = %s" % self.format
        self.make_archive (archive_basename, self.format,
                           root_dir=output_dir)

        if not self.keep_tree:
            remove_tree (output_dir, self.verbose, self.dry_run)

    # run()


    def strip_base_dirs (self, outputs, install_cmd):
        # XXX this throws away the prefix/exec-prefix distinction, and
        # means we can only correctly install the resulting archive on a
        # system where prefix == exec-prefix (but at least we can *create*
        # it on one where they differ).  I don't see a way to fix this
        # without either 1) generating two archives, one for prefix and one
        # for exec-prefix, or 2) putting absolute paths in the archive
        # rather than making them relative to one of the prefixes.

        base = install_cmd.install_base + os.sep
        platbase = install_cmd.install_platbase + os.sep
        b_len = len (base)
        pb_len = len (platbase)
        for i in range (len (outputs)):
            if outputs[i][0:b_len] == base:
                outputs[i] = outputs[i][b_len:]
            elif outputs[i][0:pb_len] == platbase:
                outputs[i] = outputs[i][pb_len:]
            else:
                raise DistutilsInternalError, \
                      ("installation output filename '%s' doesn't start " + 
                       "with either install_base ('%s') or " +
                       "install_platbase ('%s')") % \
                      (outputs[i], base, platbase)

    # strip_base_dirs()


    def make_install_tree (self, output_dir, inputs, outputs):

        assert (len(inputs) == len(outputs))

        # Create all the directories under 'output_dir' necessary to
        # put 'outputs' there.
        create_tree (output_dir, outputs,
                     verbose=self.verbose, dry_run=self.dry_run)


        # XXX this bit of logic is duplicated in sdist.make_release_tree():
        # would be nice to factor it out...
        if hasattr (os, 'link'):        # can make hard links on this system
            link = 'hard'
            msg = "making hard links in %s..." % output_dir
        else:                           # nope, have to copy
            link = None
            msg = "copying files to %s..." % output_dir

        for i in range (len(inputs)):
            output = os.path.join (output_dir, outputs[i])
            self.copy_file (inputs[i], output, link=link)

    # make_install_tree ()


# class bdist_dumb