blob: ab0ada1f4581f49cdb899092ab3a75d3748fa125 [file] [log] [blame]
Greg Warda82122b2000-02-17 23:56:15 +00001"""distutils.command.sdist
2
3Implements the Distutils 'sdist' command (create a source distribution)."""
4
Greg Ward3ce77fd2000-03-02 01:49:45 +00005__revision__ = "$Id$"
Greg Warda82122b2000-02-17 23:56:15 +00006
Christian Heimesc5f05e42008-02-23 17:40:11 +00007import os, string
Tarek Ziadé85d6fb52009-01-04 00:04:49 +00008import sys
Greg Warda82122b2000-02-17 23:56:15 +00009from types import *
10from glob import glob
Greg Warda82122b2000-02-17 23:56:15 +000011from distutils.core import Command
Greg Wardab3a0f32000-08-05 01:31:54 +000012from distutils import dir_util, dep_util, file_util, archive_util
Greg Warda82122b2000-02-17 23:56:15 +000013from distutils.text_file import TextFile
Greg Ward6b24dff2000-07-30 01:47:16 +000014from distutils.errors import *
Greg Ward4571ac12000-07-30 01:05:02 +000015from distutils.filelist import FileList
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000016from distutils import log
Tarek Ziadéf68b5b82009-02-17 09:42:44 +000017from distutils.util import convert_path
Greg Warda82122b2000-02-17 23:56:15 +000018
Tarek Ziadé064a7682009-05-14 12:40:59 +000019def show_formats():
Greg Ward34593812000-06-24 01:23:37 +000020 """Print all possible values for the 'formats' option (used by
21 the "--help-formats" command-line option).
22 """
23 from distutils.fancy_getopt import FancyGetopt
24 from distutils.archive_util import ARCHIVE_FORMATS
Tarek Ziadé064a7682009-05-14 12:40:59 +000025 formats = []
Greg Ward34593812000-06-24 01:23:37 +000026 for format in ARCHIVE_FORMATS.keys():
27 formats.append(("formats=" + format, None,
28 ARCHIVE_FORMATS[format][2]))
29 formats.sort()
Tarek Ziadé064a7682009-05-14 12:40:59 +000030 FancyGetopt(formats).print_help(
Greg Ward34593812000-06-24 01:23:37 +000031 "List of available source distribution formats:")
32
Tarek Ziadé89539132009-05-14 14:56:14 +000033class sdist(Command):
Greg Warda82122b2000-02-17 23:56:15 +000034
35 description = "create a source distribution (tarball, zip file, etc.)"
36
Greg Wardbbeceea2000-02-18 00:25:39 +000037 user_options = [
38 ('template=', 't',
39 "name of manifest template file [default: MANIFEST.in]"),
40 ('manifest=', 'm',
41 "name of manifest file [default: MANIFEST]"),
42 ('use-defaults', None,
43 "include the default file set in the manifest "
44 "[default; disable with --no-defaults]"),
Greg Ward499822d2000-06-29 02:06:29 +000045 ('no-defaults', None,
46 "don't include the default file set"),
47 ('prune', None,
48 "specifically exclude files/directories that should not be "
49 "distributed (build tree, RCS/CVS dirs, etc.) "
50 "[default; disable with --no-prune]"),
51 ('no-prune', None,
52 "don't automatically exclude anything"),
Greg Ward839d5322000-04-26 01:14:33 +000053 ('manifest-only', 'o',
Greg Wardc3c8c6e2000-06-08 00:46:45 +000054 "just regenerate the manifest and then stop "
55 "(implies --force-manifest)"),
Greg Ward839d5322000-04-26 01:14:33 +000056 ('force-manifest', 'f',
Greg Wardbbeceea2000-02-18 00:25:39 +000057 "forcibly regenerate the manifest and carry on as usual"),
Greg Wardbbeceea2000-02-18 00:25:39 +000058 ('formats=', None,
Greg Ward2ff78872000-06-24 00:23:20 +000059 "formats for source distribution (comma-separated list)"),
Greg Wardaf64aed2000-09-25 01:51:01 +000060 ('keep-temp', 'k',
Greg Wardbbeceea2000-02-18 00:25:39 +000061 "keep the distribution tree around after creating " +
62 "archive file(s)"),
Greg Wardc0614102000-07-05 03:06:46 +000063 ('dist-dir=', 'd',
64 "directory to put the source distribution archive(s) in "
65 "[default: dist]"),
Greg Wardbbeceea2000-02-18 00:25:39 +000066 ]
Greg Wardf1fe1032000-06-08 00:14:18 +000067
Greg Ward99b032e2000-09-25 01:41:15 +000068 boolean_options = ['use-defaults', 'prune',
69 'manifest-only', 'force-manifest',
Greg Wardaf64aed2000-09-25 01:51:01 +000070 'keep-temp']
Greg Wardf1fe1032000-06-08 00:14:18 +000071
Greg Ward9d17a7a2000-06-07 03:00:06 +000072 help_options = [
73 ('help-formats', None,
Greg Ward2ff78872000-06-24 00:23:20 +000074 "list available distribution formats", show_formats),
Greg Wardfa9ff762000-10-14 04:06:40 +000075 ]
Greg Ward9d17a7a2000-06-07 03:00:06 +000076
Greg Ward499822d2000-06-29 02:06:29 +000077 negative_opt = {'no-defaults': 'use-defaults',
78 'no-prune': 'prune' }
Greg Warda82122b2000-02-17 23:56:15 +000079
Tarek Ziadé89539132009-05-14 14:56:14 +000080 default_format = {'posix': 'gztar',
81 'nt': 'zip' }
Greg Warda82122b2000-02-17 23:56:15 +000082
Tarek Ziadé89539132009-05-14 14:56:14 +000083 def initialize_options(self):
Greg Warda82122b2000-02-17 23:56:15 +000084 # 'template' and 'manifest' are, respectively, the names of
85 # the manifest template and manifest file.
86 self.template = None
87 self.manifest = None
88
89 # 'use_defaults': if true, we will include the default file set
90 # in the manifest
91 self.use_defaults = 1
Greg Ward499822d2000-06-29 02:06:29 +000092 self.prune = 1
Greg Warda82122b2000-02-17 23:56:15 +000093
94 self.manifest_only = 0
95 self.force_manifest = 0
96
97 self.formats = None
Greg Wardaf64aed2000-09-25 01:51:01 +000098 self.keep_temp = 0
Greg Wardc0614102000-07-05 03:06:46 +000099 self.dist_dir = None
Greg Warda82122b2000-02-17 23:56:15 +0000100
Greg Wardd87eb732000-06-01 01:10:56 +0000101 self.archive_files = None
102
Tarek Ziadé89539132009-05-14 14:56:14 +0000103 def finalize_options(self):
Greg Warda82122b2000-02-17 23:56:15 +0000104 if self.manifest is None:
105 self.manifest = "MANIFEST"
106 if self.template is None:
107 self.template = "MANIFEST.in"
108
Greg Ward62d5a572000-06-04 15:12:51 +0000109 self.ensure_string_list('formats')
Greg Warda82122b2000-02-17 23:56:15 +0000110 if self.formats is None:
111 try:
112 self.formats = [self.default_format[os.name]]
113 except KeyError:
114 raise DistutilsPlatformError, \
Greg Ward578c10d2000-03-31 02:50:04 +0000115 "don't know how to create source distributions " + \
116 "on platform %s" % os.name
Greg Warda82122b2000-02-17 23:56:15 +0000117
Greg Wardcb1f4c42000-09-30 18:27:54 +0000118 bad_format = archive_util.check_archive_formats(self.formats)
Greg Ward6a9a5452000-04-22 03:11:55 +0000119 if bad_format:
120 raise DistutilsOptionError, \
121 "unknown archive format '%s'" % bad_format
122
Greg Wardc0614102000-07-05 03:06:46 +0000123 if self.dist_dir is None:
124 self.dist_dir = "dist"
125
Tarek Ziadé89539132009-05-14 14:56:14 +0000126 def run(self):
Greg Ward23266fe2000-07-30 01:30:31 +0000127 # 'filelist' contains the list of files that will make up the
128 # manifest
129 self.filelist = FileList()
Fred Drake21d45352001-12-06 21:01:19 +0000130
Greg Warda82122b2000-02-17 23:56:15 +0000131 # Ensure that all required meta-data is given; warn if not (but
132 # don't die, it's not *that* serious!)
Greg Wardcb1f4c42000-09-30 18:27:54 +0000133 self.check_metadata()
Greg Warda82122b2000-02-17 23:56:15 +0000134
135 # Do whatever it takes to get the list of files to process
136 # (process the manifest template, read an existing manifest,
Greg Ward23266fe2000-07-30 01:30:31 +0000137 # whatever). File list is accumulated in 'self.filelist'.
Greg Wardcb1f4c42000-09-30 18:27:54 +0000138 self.get_file_list()
Greg Warda82122b2000-02-17 23:56:15 +0000139
140 # If user just wanted us to regenerate the manifest, stop now.
141 if self.manifest_only:
142 return
143
144 # Otherwise, go ahead and create the source distribution tarball,
145 # or zipfile, or whatever.
Greg Wardcb1f4c42000-09-30 18:27:54 +0000146 self.make_distribution()
Greg Warda82122b2000-02-17 23:56:15 +0000147
Tarek Ziadé89539132009-05-14 14:56:14 +0000148 def check_metadata(self):
Greg Wardc3c8c6e2000-06-08 00:46:45 +0000149 """Ensure that all required elements of meta-data (name, version,
150 URL, (author and author_email) or (maintainer and
151 maintainer_email)) are supplied by the Distribution object; warn if
152 any are missing.
153 """
Greg Ward535f2d92000-04-21 04:37:12 +0000154 metadata = self.distribution.metadata
Greg Warda82122b2000-02-17 23:56:15 +0000155
156 missing = []
157 for attr in ('name', 'version', 'url'):
Greg Wardcb1f4c42000-09-30 18:27:54 +0000158 if not (hasattr(metadata, attr) and getattr(metadata, attr)):
159 missing.append(attr)
Greg Warda82122b2000-02-17 23:56:15 +0000160
161 if missing:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000162 self.warn("missing required meta-data: " +
163 string.join(missing, ", "))
Greg Warda82122b2000-02-17 23:56:15 +0000164
Greg Ward535f2d92000-04-21 04:37:12 +0000165 if metadata.author:
166 if not metadata.author_email:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000167 self.warn("missing meta-data: if 'author' supplied, " +
168 "'author_email' must be supplied too")
Greg Ward535f2d92000-04-21 04:37:12 +0000169 elif metadata.maintainer:
170 if not metadata.maintainer_email:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000171 self.warn("missing meta-data: if 'maintainer' supplied, " +
172 "'maintainer_email' must be supplied too")
Greg Warda82122b2000-02-17 23:56:15 +0000173 else:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000174 self.warn("missing meta-data: either (author and author_email) " +
175 "or (maintainer and maintainer_email) " +
176 "must be supplied")
Greg Warda82122b2000-02-17 23:56:15 +0000177
Tarek Ziadé89539132009-05-14 14:56:14 +0000178 def get_file_list(self):
Greg Warda82122b2000-02-17 23:56:15 +0000179 """Figure out the list of files to include in the source
Greg Ward23266fe2000-07-30 01:30:31 +0000180 distribution, and put it in 'self.filelist'. This might involve
Greg Warde0c8c2f2000-06-08 00:24:01 +0000181 reading the manifest template (and writing the manifest), or just
182 reading the manifest, or just using the default file set -- it all
183 depends on the user's options and the state of the filesystem.
184 """
Greg Wardb2db0eb2000-06-21 03:29:57 +0000185 # If we have a manifest template, see if it's newer than the
186 # manifest; if so, we'll regenerate the manifest.
Greg Ward23266fe2000-07-30 01:30:31 +0000187 template_exists = os.path.isfile(self.template)
Greg Warda82122b2000-02-17 23:56:15 +0000188 if template_exists:
Greg Wardab3a0f32000-08-05 01:31:54 +0000189 template_newer = dep_util.newer(self.template, self.manifest)
Greg Warda82122b2000-02-17 23:56:15 +0000190
Greg Wardb2db0eb2000-06-21 03:29:57 +0000191 # The contents of the manifest file almost certainly depend on the
192 # setup script as well as the manifest template -- so if the setup
193 # script is newer than the manifest, we'll regenerate the manifest
194 # from the template. (Well, not quite: if we already have a
195 # manifest, but there's no template -- which will happen if the
196 # developer elects to generate a manifest some other way -- then we
197 # can't regenerate the manifest, so we don't.)
Greg Ward9821bf42000-08-29 01:15:18 +0000198 self.debug_print("checking if %s newer than %s" %
199 (self.distribution.script_name, self.manifest))
200 setup_newer = dep_util.newer(self.distribution.script_name,
201 self.manifest)
Greg Wardb2db0eb2000-06-21 03:29:57 +0000202
203 # cases:
204 # 1) no manifest, template exists: generate manifest
205 # (covered by 2a: no manifest == template newer)
206 # 2) manifest & template exist:
207 # 2a) template or setup script newer than manifest:
208 # regenerate manifest
209 # 2b) manifest newer than both:
210 # do nothing (unless --force or --manifest-only)
211 # 3) manifest exists, no template:
212 # do nothing (unless --force or --manifest-only)
213 # 4) no manifest, no template: generate w/ warning ("defaults only")
214
Greg Wardd3b76a82000-09-06 02:08:24 +0000215 manifest_outofdate = (template_exists and
216 (template_newer or setup_newer))
217 force_regen = self.force_manifest or self.manifest_only
218 manifest_exists = os.path.isfile(self.manifest)
219 neither_exists = (not template_exists and not manifest_exists)
Greg Warda82122b2000-02-17 23:56:15 +0000220
Greg Wardd3b76a82000-09-06 02:08:24 +0000221 # Regenerate the manifest if necessary (or if explicitly told to)
222 if manifest_outofdate or neither_exists or force_regen:
Greg Warda82122b2000-02-17 23:56:15 +0000223 if not template_exists:
Greg Ward23266fe2000-07-30 01:30:31 +0000224 self.warn(("manifest template '%s' does not exist " +
225 "(using default file list)") %
226 self.template)
Greg Ward6b24dff2000-07-30 01:47:16 +0000227 self.filelist.findall()
228
Greg Warda82122b2000-02-17 23:56:15 +0000229 if self.use_defaults:
Greg Ward23266fe2000-07-30 01:30:31 +0000230 self.add_defaults()
Greg Warda82122b2000-02-17 23:56:15 +0000231 if template_exists:
Greg Ward23266fe2000-07-30 01:30:31 +0000232 self.read_template()
Greg Ward499822d2000-06-29 02:06:29 +0000233 if self.prune:
234 self.prune_file_list()
Greg Wardce15c6c2000-06-08 01:06:02 +0000235
Greg Ward23266fe2000-07-30 01:30:31 +0000236 self.filelist.sort()
Greg Ward23266fe2000-07-30 01:30:31 +0000237 self.filelist.remove_duplicates()
Greg Ward23266fe2000-07-30 01:30:31 +0000238 self.write_manifest()
Greg Warda82122b2000-02-17 23:56:15 +0000239
240 # Don't regenerate the manifest, just read it in.
241 else:
Greg Ward23266fe2000-07-30 01:30:31 +0000242 self.read_manifest()
Greg Warda82122b2000-02-17 23:56:15 +0000243
Tarek Ziadé89539132009-05-14 14:56:14 +0000244 def add_defaults(self):
Greg Ward23266fe2000-07-30 01:30:31 +0000245 """Add all the default files to self.filelist:
Greg Wardc3c8c6e2000-06-08 00:46:45 +0000246 - README or README.txt
247 - setup.py
248 - test/test*.py
249 - all pure Python modules mentioned in setup script
Tarek Ziadé7dd53392009-02-16 21:38:01 +0000250 - all files pointed by package_data (build_py)
251 - all files defined in data_files.
252 - all files defined as scripts.
Greg Wardc3c8c6e2000-06-08 00:46:45 +0000253 - all C sources listed as part of extensions or C libraries
254 in the setup script (doesn't catch C headers!)
255 Warns if (README or README.txt) or setup.py are missing; everything
256 else is optional.
257 """
Greg Ward14c8d052000-06-08 01:22:48 +0000258
Greg Wardd3b76a82000-09-06 02:08:24 +0000259 standards = [('README', 'README.txt'), self.distribution.script_name]
Greg Warda82122b2000-02-17 23:56:15 +0000260 for fn in standards:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000261 if type(fn) is TupleType:
Greg Warda82122b2000-02-17 23:56:15 +0000262 alts = fn
Greg Ward48401122000-02-24 03:17:43 +0000263 got_it = 0
Greg Warda82122b2000-02-17 23:56:15 +0000264 for fn in alts:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000265 if os.path.exists(fn):
Greg Warda82122b2000-02-17 23:56:15 +0000266 got_it = 1
Greg Wardcb1f4c42000-09-30 18:27:54 +0000267 self.filelist.append(fn)
Greg Warda82122b2000-02-17 23:56:15 +0000268 break
269
270 if not got_it:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000271 self.warn("standard file not found: should have one of " +
272 string.join(alts, ', '))
Greg Warda82122b2000-02-17 23:56:15 +0000273 else:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000274 if os.path.exists(fn):
275 self.filelist.append(fn)
Greg Warda82122b2000-02-17 23:56:15 +0000276 else:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000277 self.warn("standard file '%s' not found" % fn)
Greg Warda82122b2000-02-17 23:56:15 +0000278
Greg Ward14c8d052000-06-08 01:22:48 +0000279 optional = ['test/test*.py', 'setup.cfg']
Greg Warda82122b2000-02-17 23:56:15 +0000280 for pattern in optional:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000281 files = filter(os.path.isfile, glob(pattern))
Greg Warda82122b2000-02-17 23:56:15 +0000282 if files:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000283 self.filelist.extend(files)
Greg Warda82122b2000-02-17 23:56:15 +0000284
Tarek Ziadé7dd53392009-02-16 21:38:01 +0000285 # build_py is used to get:
286 # - python modules
287 # - files defined in package_data
288 build_py = self.get_finalized_command('build_py')
289
290 # getting python files
Greg Ward578c10d2000-03-31 02:50:04 +0000291 if self.distribution.has_pure_modules():
Greg Wardcb1f4c42000-09-30 18:27:54 +0000292 self.filelist.extend(build_py.get_source_files())
Greg Warda82122b2000-02-17 23:56:15 +0000293
Tarek Ziadé7dd53392009-02-16 21:38:01 +0000294 # getting package_data files
295 # (computed in build_py.data_files by build_py.finalize_options)
296 for pkg, src_dir, build_dir, filenames in build_py.data_files:
297 for filename in filenames:
298 self.filelist.append(os.path.join(src_dir, filename))
299
300 # getting distribution.data_files
301 if self.distribution.has_data_files():
Tarek Ziadéf68b5b82009-02-17 09:42:44 +0000302 for item in self.distribution.data_files:
303 if isinstance(item, str): # plain file
304 item = convert_path(item)
305 if os.path.isfile(item):
306 self.filelist.append(item)
307 else: # a (dirname, filenames) tuple
308 dirname, filenames = item
309 for f in filenames:
Tarek Ziadé0e5001e2009-02-17 23:06:51 +0000310 f = convert_path(f)
Tarek Ziadéf68b5b82009-02-17 09:42:44 +0000311 if os.path.isfile(f):
312 self.filelist.append(f)
Tarek Ziadé7dd53392009-02-16 21:38:01 +0000313
Greg Ward578c10d2000-03-31 02:50:04 +0000314 if self.distribution.has_ext_modules():
Greg Wardcb1f4c42000-09-30 18:27:54 +0000315 build_ext = self.get_finalized_command('build_ext')
316 self.filelist.extend(build_ext.get_source_files())
Greg Warda82122b2000-02-17 23:56:15 +0000317
Greg Ward60908f12000-04-09 03:51:40 +0000318 if self.distribution.has_c_libraries():
Greg Wardcb1f4c42000-09-30 18:27:54 +0000319 build_clib = self.get_finalized_command('build_clib')
320 self.filelist.extend(build_clib.get_source_files())
Greg Ward60908f12000-04-09 03:51:40 +0000321
Fred Drake4b498232004-03-25 22:04:52 +0000322 if self.distribution.has_scripts():
323 build_scripts = self.get_finalized_command('build_scripts')
324 self.filelist.extend(build_scripts.get_source_files())
325
Tarek Ziadé89539132009-05-14 14:56:14 +0000326 def read_template(self):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000327 """Read and parse manifest template file named by self.template.
Greg Warda82122b2000-02-17 23:56:15 +0000328
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000329 (usually "MANIFEST.in") The parsing and processing is done by
330 'self.filelist', which updates itself accordingly.
Greg Ward23266fe2000-07-30 01:30:31 +0000331 """
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000332 log.info("reading manifest template '%s'", self.template)
Greg Wardcb1f4c42000-09-30 18:27:54 +0000333 template = TextFile(self.template,
334 strip_comments=1,
335 skip_blanks=1,
336 join_lines=1,
337 lstrip_ws=1,
338 rstrip_ws=1,
339 collapse_join=1)
Greg Warda82122b2000-02-17 23:56:15 +0000340
Greg Warda82122b2000-02-17 23:56:15 +0000341 while 1:
Greg Warda82122b2000-02-17 23:56:15 +0000342 line = template.readline()
343 if line is None: # end of file
344 break
345
Greg Ward6b24dff2000-07-30 01:47:16 +0000346 try:
347 self.filelist.process_template_line(line)
348 except DistutilsTemplateError, msg:
349 self.warn("%s, line %d: %s" % (template.filename,
350 template.current_line,
351 msg))
Greg Warda82122b2000-02-17 23:56:15 +0000352
Tarek Ziadé89539132009-05-14 14:56:14 +0000353 def prune_file_list(self):
Greg Wardce15c6c2000-06-08 01:06:02 +0000354 """Prune off branches that might slip into the file list as created
Greg Ward499822d2000-06-29 02:06:29 +0000355 by 'read_template()', but really don't belong there:
356 * the build tree (typically "build")
357 * the release tree itself (only an issue if we ran "sdist"
Greg Wardaf64aed2000-09-25 01:51:01 +0000358 previously with --keep-temp, or it aborted)
Georg Brandl1df03402008-03-06 06:47:18 +0000359 * any RCS, CVS, .svn, .hg, .git, .bzr, _darcs directories
Greg Wardce15c6c2000-06-08 01:06:02 +0000360 """
361 build = self.get_finalized_command('build')
362 base_dir = self.distribution.get_fullname()
Greg Wardce15c6c2000-06-08 01:06:02 +0000363
Greg Ward23266fe2000-07-30 01:30:31 +0000364 self.filelist.exclude_pattern(None, prefix=build.build_base)
365 self.filelist.exclude_pattern(None, prefix=base_dir)
Greg Wardf8b9e202000-06-08 00:08:14 +0000366
Tarek Ziadé85d6fb52009-01-04 00:04:49 +0000367 # pruning out vcs directories
368 # both separators are used under win32
Tarek Ziadéd81780b2009-01-04 10:37:52 +0000369 if sys.platform == 'win32':
370 seps = r'/|\\'
371 else:
372 seps = '/'
373
374 vcs_dirs = ['RCS', 'CVS', r'\.svn', r'\.hg', r'\.git', r'\.bzr',
375 '_darcs']
Tarek Ziadé85d6fb52009-01-04 00:04:49 +0000376 vcs_ptrn = r'(^|%s)(%s)(%s).*' % (seps, '|'.join(vcs_dirs), seps)
377 self.filelist.exclude_pattern(vcs_ptrn, is_regex=1)
Greg Wardf8b9e202000-06-08 00:08:14 +0000378
Tarek Ziadé89539132009-05-14 14:56:14 +0000379 def write_manifest(self):
Greg Ward23266fe2000-07-30 01:30:31 +0000380 """Write the file list in 'self.filelist' (presumably as filled in
381 by 'add_defaults()' and 'read_template()') to the manifest file
382 named by 'self.manifest'.
Greg Warde0c8c2f2000-06-08 00:24:01 +0000383 """
Greg Wardab3a0f32000-08-05 01:31:54 +0000384 self.execute(file_util.write_file,
Greg Ward23266fe2000-07-30 01:30:31 +0000385 (self.manifest, self.filelist.files),
Greg Wardf8b9e202000-06-08 00:08:14 +0000386 "writing manifest file '%s'" % self.manifest)
Greg Warda82122b2000-02-17 23:56:15 +0000387
Tarek Ziadé89539132009-05-14 14:56:14 +0000388 def read_manifest(self):
Greg Warde0c8c2f2000-06-08 00:24:01 +0000389 """Read the manifest file (named by 'self.manifest') and use it to
Greg Ward23266fe2000-07-30 01:30:31 +0000390 fill in 'self.filelist', the list of files to include in the source
Greg Warde0c8c2f2000-06-08 00:24:01 +0000391 distribution.
392 """
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000393 log.info("reading manifest file '%s'", self.manifest)
Greg Wardcb1f4c42000-09-30 18:27:54 +0000394 manifest = open(self.manifest)
Greg Warda82122b2000-02-17 23:56:15 +0000395 while 1:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000396 line = manifest.readline()
Greg Warda82122b2000-02-17 23:56:15 +0000397 if line == '': # end of file
398 break
399 if line[-1] == '\n':
400 line = line[0:-1]
Greg Wardcb1f4c42000-09-30 18:27:54 +0000401 self.filelist.append(line)
Andrew M. Kuchling2d6c13e2008-02-21 14:23:38 +0000402 manifest.close()
Greg Warda82122b2000-02-17 23:56:15 +0000403
Tarek Ziadé89539132009-05-14 14:56:14 +0000404 def make_release_tree(self, base_dir, files):
Greg Wardc3c8c6e2000-06-08 00:46:45 +0000405 """Create the directory tree that will become the source
406 distribution archive. All directories implied by the filenames in
407 'files' are created under 'base_dir', and then we hard link or copy
408 (if hard linking is unavailable) those files into place.
409 Essentially, this duplicates the developer's source tree, but in a
410 directory named after the distribution, containing only the files
411 to be distributed.
412 """
Greg Ward578c10d2000-03-31 02:50:04 +0000413 # Create all the directories under 'base_dir' necessary to
Greg Ward5fad2682000-09-06 02:18:59 +0000414 # put 'files' there; the 'mkpath()' is just so we don't die
415 # if the manifest happens to be empty.
416 self.mkpath(base_dir)
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000417 dir_util.create_tree(base_dir, files, dry_run=self.dry_run)
Greg Warda82122b2000-02-17 23:56:15 +0000418
419 # And walk over the list of files, either making a hard link (if
420 # os.link exists) to each one that doesn't already exist in its
421 # corresponding location under 'base_dir', or copying each file
422 # that's out-of-date in 'base_dir'. (Usually, all files will be
423 # out-of-date, because by default we blow away 'base_dir' when
424 # we're done making the distribution archives.)
Fred Drake21d45352001-12-06 21:01:19 +0000425
Greg Wardcb1f4c42000-09-30 18:27:54 +0000426 if hasattr(os, 'link'): # can make hard links on this system
Greg Ward578c10d2000-03-31 02:50:04 +0000427 link = 'hard'
Greg Warda82122b2000-02-17 23:56:15 +0000428 msg = "making hard links in %s..." % base_dir
Greg Ward578c10d2000-03-31 02:50:04 +0000429 else: # nope, have to copy
430 link = None
Greg Warda82122b2000-02-17 23:56:15 +0000431 msg = "copying files to %s..." % base_dir
432
Greg Ward5fad2682000-09-06 02:18:59 +0000433 if not files:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000434 log.warn("no files to distribute -- empty manifest?")
Greg Ward5fad2682000-09-06 02:18:59 +0000435 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000436 log.info(msg)
Greg Warda82122b2000-02-17 23:56:15 +0000437 for file in files:
Greg Ward5fad2682000-09-06 02:18:59 +0000438 if not os.path.isfile(file):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000439 log.warn("'%s' not a regular file -- skipping" % file)
Greg Ward5fad2682000-09-06 02:18:59 +0000440 else:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000441 dest = os.path.join(base_dir, file)
442 self.copy_file(file, dest, link=link)
Greg Warda82122b2000-02-17 23:56:15 +0000443
Andrew M. Kuchlinga7f225d2001-03-22 03:10:05 +0000444 self.distribution.metadata.write_pkg_info(base_dir)
Fred Drake21d45352001-12-06 21:01:19 +0000445
Tarek Ziadé89539132009-05-14 14:56:14 +0000446 def make_distribution(self):
Greg Wardc3c8c6e2000-06-08 00:46:45 +0000447 """Create the source distribution(s). First, we create the release
448 tree with 'make_release_tree()'; then, we create all required
449 archive files (according to 'self.formats') from the release tree.
450 Finally, we clean up by blowing away the release tree (unless
Greg Wardaf64aed2000-09-25 01:51:01 +0000451 'self.keep_temp' is true). The list of archive files created is
Greg Wardc3c8c6e2000-06-08 00:46:45 +0000452 stored so it can be retrieved later by 'get_archive_files()'.
453 """
Greg Ward578c10d2000-03-31 02:50:04 +0000454 # Don't warn about missing meta-data here -- should be (and is!)
455 # done elsewhere.
Greg Ward0ae7f762000-04-22 02:51:25 +0000456 base_dir = self.distribution.get_fullname()
Greg Wardc0614102000-07-05 03:06:46 +0000457 base_name = os.path.join(self.dist_dir, base_dir)
Greg Warda82122b2000-02-17 23:56:15 +0000458
Greg Wardcb1f4c42000-09-30 18:27:54 +0000459 self.make_release_tree(base_dir, self.filelist.files)
Greg Wardd87eb732000-06-01 01:10:56 +0000460 archive_files = [] # remember names of files we create
Tarek Ziadéaaedcef2009-01-25 23:34:00 +0000461 # tar archive must be created last to avoid overwrite and remove
462 if 'tar' in self.formats:
463 self.formats.append(self.formats.pop(self.formats.index('tar')))
464
Greg Warda82122b2000-02-17 23:56:15 +0000465 for fmt in self.formats:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000466 file = self.make_archive(base_name, fmt, base_dir=base_dir)
Greg Wardd87eb732000-06-01 01:10:56 +0000467 archive_files.append(file)
Martin v. Löwis98da5622005-03-23 18:54:36 +0000468 self.distribution.dist_files.append(('sdist', '', file))
Greg Wardd87eb732000-06-01 01:10:56 +0000469
470 self.archive_files = archive_files
Greg Warda82122b2000-02-17 23:56:15 +0000471
Greg Wardaf64aed2000-09-25 01:51:01 +0000472 if not self.keep_temp:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000473 dir_util.remove_tree(base_dir, dry_run=self.dry_run)
Greg Warda82122b2000-02-17 23:56:15 +0000474
Tarek Ziadé89539132009-05-14 14:56:14 +0000475 def get_archive_files(self):
Greg Wardd87eb732000-06-01 01:10:56 +0000476 """Return the list of archive files created when the command
477 was run, or None if the command hasn't run yet.
478 """
479 return self.archive_files