blob: c21f3917b67104b7da82b57d6bf55fdc5cf421fa [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
Tarek Ziadécb768042009-05-16 16:37:06 +000011from warnings import warn
12
Greg Warda82122b2000-02-17 23:56:15 +000013from distutils.core import Command
Greg Wardab3a0f32000-08-05 01:31:54 +000014from distutils import dir_util, dep_util, file_util, archive_util
Greg Warda82122b2000-02-17 23:56:15 +000015from distutils.text_file import TextFile
Greg Ward6b24dff2000-07-30 01:47:16 +000016from distutils.errors import *
Greg Ward4571ac12000-07-30 01:05:02 +000017from distutils.filelist import FileList
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000018from distutils import log
Tarek Ziadéf68b5b82009-02-17 09:42:44 +000019from distutils.util import convert_path
Greg Warda82122b2000-02-17 23:56:15 +000020
Tarek Ziadé064a7682009-05-14 12:40:59 +000021def show_formats():
Greg Ward34593812000-06-24 01:23:37 +000022 """Print all possible values for the 'formats' option (used by
23 the "--help-formats" command-line option).
24 """
25 from distutils.fancy_getopt import FancyGetopt
26 from distutils.archive_util import ARCHIVE_FORMATS
Tarek Ziadé064a7682009-05-14 12:40:59 +000027 formats = []
Greg Ward34593812000-06-24 01:23:37 +000028 for format in ARCHIVE_FORMATS.keys():
29 formats.append(("formats=" + format, None,
30 ARCHIVE_FORMATS[format][2]))
31 formats.sort()
Tarek Ziadé064a7682009-05-14 12:40:59 +000032 FancyGetopt(formats).print_help(
Greg Ward34593812000-06-24 01:23:37 +000033 "List of available source distribution formats:")
34
Tarek Ziadé89539132009-05-14 14:56:14 +000035class sdist(Command):
Greg Warda82122b2000-02-17 23:56:15 +000036
37 description = "create a source distribution (tarball, zip file, etc.)"
38
Tarek Ziadécb768042009-05-16 16:37:06 +000039 def checking_metadata(self):
40 """Callable used for the check sub-command.
41
42 Placed here so user_options can view it"""
43 return self.metadata_check
44
Greg Wardbbeceea2000-02-18 00:25:39 +000045 user_options = [
46 ('template=', 't',
47 "name of manifest template file [default: MANIFEST.in]"),
48 ('manifest=', 'm',
49 "name of manifest file [default: MANIFEST]"),
50 ('use-defaults', None,
51 "include the default file set in the manifest "
52 "[default; disable with --no-defaults]"),
Greg Ward499822d2000-06-29 02:06:29 +000053 ('no-defaults', None,
54 "don't include the default file set"),
55 ('prune', None,
56 "specifically exclude files/directories that should not be "
57 "distributed (build tree, RCS/CVS dirs, etc.) "
58 "[default; disable with --no-prune]"),
59 ('no-prune', None,
60 "don't automatically exclude anything"),
Greg Ward839d5322000-04-26 01:14:33 +000061 ('manifest-only', 'o',
Greg Wardc3c8c6e2000-06-08 00:46:45 +000062 "just regenerate the manifest and then stop "
63 "(implies --force-manifest)"),
Greg Ward839d5322000-04-26 01:14:33 +000064 ('force-manifest', 'f',
Greg Wardbbeceea2000-02-18 00:25:39 +000065 "forcibly regenerate the manifest and carry on as usual"),
Greg Wardbbeceea2000-02-18 00:25:39 +000066 ('formats=', None,
Greg Ward2ff78872000-06-24 00:23:20 +000067 "formats for source distribution (comma-separated list)"),
Greg Wardaf64aed2000-09-25 01:51:01 +000068 ('keep-temp', 'k',
Greg Wardbbeceea2000-02-18 00:25:39 +000069 "keep the distribution tree around after creating " +
70 "archive file(s)"),
Greg Wardc0614102000-07-05 03:06:46 +000071 ('dist-dir=', 'd',
72 "directory to put the source distribution archive(s) in "
73 "[default: dist]"),
Tarek Ziadécb768042009-05-16 16:37:06 +000074 ('medata-check', None,
75 "Ensure that all required elements of meta-data "
76 "are supplied. Warn if any missing. [default]"),
Greg Wardbbeceea2000-02-18 00:25:39 +000077 ]
Greg Wardf1fe1032000-06-08 00:14:18 +000078
Greg Ward99b032e2000-09-25 01:41:15 +000079 boolean_options = ['use-defaults', 'prune',
80 'manifest-only', 'force-manifest',
Tarek Ziadécb768042009-05-16 16:37:06 +000081 'keep-temp', 'metadata-check']
Greg Wardf1fe1032000-06-08 00:14:18 +000082
Greg Ward9d17a7a2000-06-07 03:00:06 +000083 help_options = [
84 ('help-formats', None,
Greg Ward2ff78872000-06-24 00:23:20 +000085 "list available distribution formats", show_formats),
Greg Wardfa9ff762000-10-14 04:06:40 +000086 ]
Greg Ward9d17a7a2000-06-07 03:00:06 +000087
Greg Ward499822d2000-06-29 02:06:29 +000088 negative_opt = {'no-defaults': 'use-defaults',
89 'no-prune': 'prune' }
Greg Warda82122b2000-02-17 23:56:15 +000090
Tarek Ziadé89539132009-05-14 14:56:14 +000091 default_format = {'posix': 'gztar',
92 'nt': 'zip' }
Greg Warda82122b2000-02-17 23:56:15 +000093
Tarek Ziadécb768042009-05-16 16:37:06 +000094 sub_commands = [('check', checking_metadata)]
95
Tarek Ziadé89539132009-05-14 14:56:14 +000096 def initialize_options(self):
Greg Warda82122b2000-02-17 23:56:15 +000097 # 'template' and 'manifest' are, respectively, the names of
98 # the manifest template and manifest file.
99 self.template = None
100 self.manifest = None
101
102 # 'use_defaults': if true, we will include the default file set
103 # in the manifest
104 self.use_defaults = 1
Greg Ward499822d2000-06-29 02:06:29 +0000105 self.prune = 1
Greg Warda82122b2000-02-17 23:56:15 +0000106
107 self.manifest_only = 0
108 self.force_manifest = 0
109
110 self.formats = None
Greg Wardaf64aed2000-09-25 01:51:01 +0000111 self.keep_temp = 0
Greg Wardc0614102000-07-05 03:06:46 +0000112 self.dist_dir = None
Greg Warda82122b2000-02-17 23:56:15 +0000113
Greg Wardd87eb732000-06-01 01:10:56 +0000114 self.archive_files = None
Tarek Ziadécb768042009-05-16 16:37:06 +0000115 self.metadata_check = 1
Greg Wardd87eb732000-06-01 01:10:56 +0000116
Tarek Ziadé89539132009-05-14 14:56:14 +0000117 def finalize_options(self):
Greg Warda82122b2000-02-17 23:56:15 +0000118 if self.manifest is None:
119 self.manifest = "MANIFEST"
120 if self.template is None:
121 self.template = "MANIFEST.in"
122
Greg Ward62d5a572000-06-04 15:12:51 +0000123 self.ensure_string_list('formats')
Greg Warda82122b2000-02-17 23:56:15 +0000124 if self.formats is None:
125 try:
126 self.formats = [self.default_format[os.name]]
127 except KeyError:
128 raise DistutilsPlatformError, \
Greg Ward578c10d2000-03-31 02:50:04 +0000129 "don't know how to create source distributions " + \
130 "on platform %s" % os.name
Greg Warda82122b2000-02-17 23:56:15 +0000131
Greg Wardcb1f4c42000-09-30 18:27:54 +0000132 bad_format = archive_util.check_archive_formats(self.formats)
Greg Ward6a9a5452000-04-22 03:11:55 +0000133 if bad_format:
134 raise DistutilsOptionError, \
135 "unknown archive format '%s'" % bad_format
136
Greg Wardc0614102000-07-05 03:06:46 +0000137 if self.dist_dir is None:
138 self.dist_dir = "dist"
139
Tarek Ziadé89539132009-05-14 14:56:14 +0000140 def run(self):
Greg Ward23266fe2000-07-30 01:30:31 +0000141 # 'filelist' contains the list of files that will make up the
142 # manifest
143 self.filelist = FileList()
Fred Drake21d45352001-12-06 21:01:19 +0000144
Tarek Ziadécb768042009-05-16 16:37:06 +0000145 # Run sub commands
146 for cmd_name in self.get_sub_commands():
147 self.run_command(cmd_name)
Greg Warda82122b2000-02-17 23:56:15 +0000148
149 # Do whatever it takes to get the list of files to process
150 # (process the manifest template, read an existing manifest,
Greg Ward23266fe2000-07-30 01:30:31 +0000151 # whatever). File list is accumulated in 'self.filelist'.
Greg Wardcb1f4c42000-09-30 18:27:54 +0000152 self.get_file_list()
Greg Warda82122b2000-02-17 23:56:15 +0000153
154 # If user just wanted us to regenerate the manifest, stop now.
155 if self.manifest_only:
156 return
157
158 # Otherwise, go ahead and create the source distribution tarball,
159 # or zipfile, or whatever.
Greg Wardcb1f4c42000-09-30 18:27:54 +0000160 self.make_distribution()
Greg Warda82122b2000-02-17 23:56:15 +0000161
Tarek Ziadé89539132009-05-14 14:56:14 +0000162 def check_metadata(self):
Tarek Ziadécb768042009-05-16 16:37:06 +0000163 """Deprecated API."""
164 warn("distutils.command.sdist.check_metadata is deprecated, \
165 use the check command instead", PendingDeprecationWarning)
166 check = self.distribution.get_command_obj('check')
167 check.ensure_finalized()
168 check.run()
Greg Warda82122b2000-02-17 23:56:15 +0000169
Tarek Ziadé89539132009-05-14 14:56:14 +0000170 def get_file_list(self):
Greg Warda82122b2000-02-17 23:56:15 +0000171 """Figure out the list of files to include in the source
Greg Ward23266fe2000-07-30 01:30:31 +0000172 distribution, and put it in 'self.filelist'. This might involve
Greg Warde0c8c2f2000-06-08 00:24:01 +0000173 reading the manifest template (and writing the manifest), or just
174 reading the manifest, or just using the default file set -- it all
175 depends on the user's options and the state of the filesystem.
176 """
Greg Wardb2db0eb2000-06-21 03:29:57 +0000177 # If we have a manifest template, see if it's newer than the
178 # manifest; if so, we'll regenerate the manifest.
Greg Ward23266fe2000-07-30 01:30:31 +0000179 template_exists = os.path.isfile(self.template)
Greg Warda82122b2000-02-17 23:56:15 +0000180 if template_exists:
Greg Wardab3a0f32000-08-05 01:31:54 +0000181 template_newer = dep_util.newer(self.template, self.manifest)
Greg Warda82122b2000-02-17 23:56:15 +0000182
Greg Wardb2db0eb2000-06-21 03:29:57 +0000183 # The contents of the manifest file almost certainly depend on the
184 # setup script as well as the manifest template -- so if the setup
185 # script is newer than the manifest, we'll regenerate the manifest
186 # from the template. (Well, not quite: if we already have a
187 # manifest, but there's no template -- which will happen if the
188 # developer elects to generate a manifest some other way -- then we
189 # can't regenerate the manifest, so we don't.)
Greg Ward9821bf42000-08-29 01:15:18 +0000190 self.debug_print("checking if %s newer than %s" %
191 (self.distribution.script_name, self.manifest))
192 setup_newer = dep_util.newer(self.distribution.script_name,
193 self.manifest)
Greg Wardb2db0eb2000-06-21 03:29:57 +0000194
195 # cases:
196 # 1) no manifest, template exists: generate manifest
197 # (covered by 2a: no manifest == template newer)
198 # 2) manifest & template exist:
199 # 2a) template or setup script newer than manifest:
200 # regenerate manifest
201 # 2b) manifest newer than both:
202 # do nothing (unless --force or --manifest-only)
203 # 3) manifest exists, no template:
204 # do nothing (unless --force or --manifest-only)
205 # 4) no manifest, no template: generate w/ warning ("defaults only")
206
Greg Wardd3b76a82000-09-06 02:08:24 +0000207 manifest_outofdate = (template_exists and
208 (template_newer or setup_newer))
209 force_regen = self.force_manifest or self.manifest_only
210 manifest_exists = os.path.isfile(self.manifest)
211 neither_exists = (not template_exists and not manifest_exists)
Greg Warda82122b2000-02-17 23:56:15 +0000212
Greg Wardd3b76a82000-09-06 02:08:24 +0000213 # Regenerate the manifest if necessary (or if explicitly told to)
214 if manifest_outofdate or neither_exists or force_regen:
Greg Warda82122b2000-02-17 23:56:15 +0000215 if not template_exists:
Greg Ward23266fe2000-07-30 01:30:31 +0000216 self.warn(("manifest template '%s' does not exist " +
217 "(using default file list)") %
218 self.template)
Greg Ward6b24dff2000-07-30 01:47:16 +0000219 self.filelist.findall()
220
Greg Warda82122b2000-02-17 23:56:15 +0000221 if self.use_defaults:
Greg Ward23266fe2000-07-30 01:30:31 +0000222 self.add_defaults()
Greg Warda82122b2000-02-17 23:56:15 +0000223 if template_exists:
Greg Ward23266fe2000-07-30 01:30:31 +0000224 self.read_template()
Greg Ward499822d2000-06-29 02:06:29 +0000225 if self.prune:
226 self.prune_file_list()
Greg Wardce15c6c2000-06-08 01:06:02 +0000227
Greg Ward23266fe2000-07-30 01:30:31 +0000228 self.filelist.sort()
Greg Ward23266fe2000-07-30 01:30:31 +0000229 self.filelist.remove_duplicates()
Greg Ward23266fe2000-07-30 01:30:31 +0000230 self.write_manifest()
Greg Warda82122b2000-02-17 23:56:15 +0000231
232 # Don't regenerate the manifest, just read it in.
233 else:
Greg Ward23266fe2000-07-30 01:30:31 +0000234 self.read_manifest()
Greg Warda82122b2000-02-17 23:56:15 +0000235
Tarek Ziadé89539132009-05-14 14:56:14 +0000236 def add_defaults(self):
Greg Ward23266fe2000-07-30 01:30:31 +0000237 """Add all the default files to self.filelist:
Greg Wardc3c8c6e2000-06-08 00:46:45 +0000238 - README or README.txt
239 - setup.py
240 - test/test*.py
241 - all pure Python modules mentioned in setup script
Tarek Ziadé7dd53392009-02-16 21:38:01 +0000242 - all files pointed by package_data (build_py)
243 - all files defined in data_files.
244 - all files defined as scripts.
Greg Wardc3c8c6e2000-06-08 00:46:45 +0000245 - all C sources listed as part of extensions or C libraries
246 in the setup script (doesn't catch C headers!)
247 Warns if (README or README.txt) or setup.py are missing; everything
248 else is optional.
249 """
Greg Ward14c8d052000-06-08 01:22:48 +0000250
Greg Wardd3b76a82000-09-06 02:08:24 +0000251 standards = [('README', 'README.txt'), self.distribution.script_name]
Greg Warda82122b2000-02-17 23:56:15 +0000252 for fn in standards:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000253 if type(fn) is TupleType:
Greg Warda82122b2000-02-17 23:56:15 +0000254 alts = fn
Greg Ward48401122000-02-24 03:17:43 +0000255 got_it = 0
Greg Warda82122b2000-02-17 23:56:15 +0000256 for fn in alts:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000257 if os.path.exists(fn):
Greg Warda82122b2000-02-17 23:56:15 +0000258 got_it = 1
Greg Wardcb1f4c42000-09-30 18:27:54 +0000259 self.filelist.append(fn)
Greg Warda82122b2000-02-17 23:56:15 +0000260 break
261
262 if not got_it:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000263 self.warn("standard file not found: should have one of " +
264 string.join(alts, ', '))
Greg Warda82122b2000-02-17 23:56:15 +0000265 else:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000266 if os.path.exists(fn):
267 self.filelist.append(fn)
Greg Warda82122b2000-02-17 23:56:15 +0000268 else:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000269 self.warn("standard file '%s' not found" % fn)
Greg Warda82122b2000-02-17 23:56:15 +0000270
Greg Ward14c8d052000-06-08 01:22:48 +0000271 optional = ['test/test*.py', 'setup.cfg']
Greg Warda82122b2000-02-17 23:56:15 +0000272 for pattern in optional:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000273 files = filter(os.path.isfile, glob(pattern))
Greg Warda82122b2000-02-17 23:56:15 +0000274 if files:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000275 self.filelist.extend(files)
Greg Warda82122b2000-02-17 23:56:15 +0000276
Tarek Ziadé7dd53392009-02-16 21:38:01 +0000277 # build_py is used to get:
278 # - python modules
279 # - files defined in package_data
280 build_py = self.get_finalized_command('build_py')
281
282 # getting python files
Greg Ward578c10d2000-03-31 02:50:04 +0000283 if self.distribution.has_pure_modules():
Greg Wardcb1f4c42000-09-30 18:27:54 +0000284 self.filelist.extend(build_py.get_source_files())
Greg Warda82122b2000-02-17 23:56:15 +0000285
Tarek Ziadé7dd53392009-02-16 21:38:01 +0000286 # getting package_data files
287 # (computed in build_py.data_files by build_py.finalize_options)
288 for pkg, src_dir, build_dir, filenames in build_py.data_files:
289 for filename in filenames:
290 self.filelist.append(os.path.join(src_dir, filename))
291
292 # getting distribution.data_files
293 if self.distribution.has_data_files():
Tarek Ziadéf68b5b82009-02-17 09:42:44 +0000294 for item in self.distribution.data_files:
295 if isinstance(item, str): # plain file
296 item = convert_path(item)
297 if os.path.isfile(item):
298 self.filelist.append(item)
299 else: # a (dirname, filenames) tuple
300 dirname, filenames = item
301 for f in filenames:
Tarek Ziadé0e5001e2009-02-17 23:06:51 +0000302 f = convert_path(f)
Tarek Ziadéf68b5b82009-02-17 09:42:44 +0000303 if os.path.isfile(f):
304 self.filelist.append(f)
Tarek Ziadé7dd53392009-02-16 21:38:01 +0000305
Greg Ward578c10d2000-03-31 02:50:04 +0000306 if self.distribution.has_ext_modules():
Greg Wardcb1f4c42000-09-30 18:27:54 +0000307 build_ext = self.get_finalized_command('build_ext')
308 self.filelist.extend(build_ext.get_source_files())
Greg Warda82122b2000-02-17 23:56:15 +0000309
Greg Ward60908f12000-04-09 03:51:40 +0000310 if self.distribution.has_c_libraries():
Greg Wardcb1f4c42000-09-30 18:27:54 +0000311 build_clib = self.get_finalized_command('build_clib')
312 self.filelist.extend(build_clib.get_source_files())
Greg Ward60908f12000-04-09 03:51:40 +0000313
Fred Drake4b498232004-03-25 22:04:52 +0000314 if self.distribution.has_scripts():
315 build_scripts = self.get_finalized_command('build_scripts')
316 self.filelist.extend(build_scripts.get_source_files())
317
Tarek Ziadé89539132009-05-14 14:56:14 +0000318 def read_template(self):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000319 """Read and parse manifest template file named by self.template.
Greg Warda82122b2000-02-17 23:56:15 +0000320
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000321 (usually "MANIFEST.in") The parsing and processing is done by
322 'self.filelist', which updates itself accordingly.
Greg Ward23266fe2000-07-30 01:30:31 +0000323 """
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000324 log.info("reading manifest template '%s'", self.template)
Greg Wardcb1f4c42000-09-30 18:27:54 +0000325 template = TextFile(self.template,
326 strip_comments=1,
327 skip_blanks=1,
328 join_lines=1,
329 lstrip_ws=1,
330 rstrip_ws=1,
331 collapse_join=1)
Greg Warda82122b2000-02-17 23:56:15 +0000332
Greg Warda82122b2000-02-17 23:56:15 +0000333 while 1:
Greg Warda82122b2000-02-17 23:56:15 +0000334 line = template.readline()
335 if line is None: # end of file
336 break
337
Greg Ward6b24dff2000-07-30 01:47:16 +0000338 try:
339 self.filelist.process_template_line(line)
340 except DistutilsTemplateError, msg:
341 self.warn("%s, line %d: %s" % (template.filename,
342 template.current_line,
343 msg))
Greg Warda82122b2000-02-17 23:56:15 +0000344
Tarek Ziadé89539132009-05-14 14:56:14 +0000345 def prune_file_list(self):
Greg Wardce15c6c2000-06-08 01:06:02 +0000346 """Prune off branches that might slip into the file list as created
Greg Ward499822d2000-06-29 02:06:29 +0000347 by 'read_template()', but really don't belong there:
348 * the build tree (typically "build")
349 * the release tree itself (only an issue if we ran "sdist"
Greg Wardaf64aed2000-09-25 01:51:01 +0000350 previously with --keep-temp, or it aborted)
Georg Brandl1df03402008-03-06 06:47:18 +0000351 * any RCS, CVS, .svn, .hg, .git, .bzr, _darcs directories
Greg Wardce15c6c2000-06-08 01:06:02 +0000352 """
353 build = self.get_finalized_command('build')
354 base_dir = self.distribution.get_fullname()
Greg Wardce15c6c2000-06-08 01:06:02 +0000355
Greg Ward23266fe2000-07-30 01:30:31 +0000356 self.filelist.exclude_pattern(None, prefix=build.build_base)
357 self.filelist.exclude_pattern(None, prefix=base_dir)
Greg Wardf8b9e202000-06-08 00:08:14 +0000358
Tarek Ziadé85d6fb52009-01-04 00:04:49 +0000359 # pruning out vcs directories
360 # both separators are used under win32
Tarek Ziadéd81780b2009-01-04 10:37:52 +0000361 if sys.platform == 'win32':
362 seps = r'/|\\'
363 else:
364 seps = '/'
365
366 vcs_dirs = ['RCS', 'CVS', r'\.svn', r'\.hg', r'\.git', r'\.bzr',
367 '_darcs']
Tarek Ziadé85d6fb52009-01-04 00:04:49 +0000368 vcs_ptrn = r'(^|%s)(%s)(%s).*' % (seps, '|'.join(vcs_dirs), seps)
369 self.filelist.exclude_pattern(vcs_ptrn, is_regex=1)
Greg Wardf8b9e202000-06-08 00:08:14 +0000370
Tarek Ziadé89539132009-05-14 14:56:14 +0000371 def write_manifest(self):
Greg Ward23266fe2000-07-30 01:30:31 +0000372 """Write the file list in 'self.filelist' (presumably as filled in
373 by 'add_defaults()' and 'read_template()') to the manifest file
374 named by 'self.manifest'.
Greg Warde0c8c2f2000-06-08 00:24:01 +0000375 """
Greg Wardab3a0f32000-08-05 01:31:54 +0000376 self.execute(file_util.write_file,
Greg Ward23266fe2000-07-30 01:30:31 +0000377 (self.manifest, self.filelist.files),
Greg Wardf8b9e202000-06-08 00:08:14 +0000378 "writing manifest file '%s'" % self.manifest)
Greg Warda82122b2000-02-17 23:56:15 +0000379
Tarek Ziadé89539132009-05-14 14:56:14 +0000380 def read_manifest(self):
Greg Warde0c8c2f2000-06-08 00:24:01 +0000381 """Read the manifest file (named by 'self.manifest') and use it to
Greg Ward23266fe2000-07-30 01:30:31 +0000382 fill in 'self.filelist', the list of files to include in the source
Greg Warde0c8c2f2000-06-08 00:24:01 +0000383 distribution.
384 """
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000385 log.info("reading manifest file '%s'", self.manifest)
Greg Wardcb1f4c42000-09-30 18:27:54 +0000386 manifest = open(self.manifest)
Greg Warda82122b2000-02-17 23:56:15 +0000387 while 1:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000388 line = manifest.readline()
Greg Warda82122b2000-02-17 23:56:15 +0000389 if line == '': # end of file
390 break
391 if line[-1] == '\n':
392 line = line[0:-1]
Greg Wardcb1f4c42000-09-30 18:27:54 +0000393 self.filelist.append(line)
Andrew M. Kuchling2d6c13e2008-02-21 14:23:38 +0000394 manifest.close()
Greg Warda82122b2000-02-17 23:56:15 +0000395
Tarek Ziadé89539132009-05-14 14:56:14 +0000396 def make_release_tree(self, base_dir, files):
Greg Wardc3c8c6e2000-06-08 00:46:45 +0000397 """Create the directory tree that will become the source
398 distribution archive. All directories implied by the filenames in
399 'files' are created under 'base_dir', and then we hard link or copy
400 (if hard linking is unavailable) those files into place.
401 Essentially, this duplicates the developer's source tree, but in a
402 directory named after the distribution, containing only the files
403 to be distributed.
404 """
Greg Ward578c10d2000-03-31 02:50:04 +0000405 # Create all the directories under 'base_dir' necessary to
Greg Ward5fad2682000-09-06 02:18:59 +0000406 # put 'files' there; the 'mkpath()' is just so we don't die
407 # if the manifest happens to be empty.
408 self.mkpath(base_dir)
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000409 dir_util.create_tree(base_dir, files, dry_run=self.dry_run)
Greg Warda82122b2000-02-17 23:56:15 +0000410
411 # And walk over the list of files, either making a hard link (if
412 # os.link exists) to each one that doesn't already exist in its
413 # corresponding location under 'base_dir', or copying each file
414 # that's out-of-date in 'base_dir'. (Usually, all files will be
415 # out-of-date, because by default we blow away 'base_dir' when
416 # we're done making the distribution archives.)
Fred Drake21d45352001-12-06 21:01:19 +0000417
Greg Wardcb1f4c42000-09-30 18:27:54 +0000418 if hasattr(os, 'link'): # can make hard links on this system
Greg Ward578c10d2000-03-31 02:50:04 +0000419 link = 'hard'
Greg Warda82122b2000-02-17 23:56:15 +0000420 msg = "making hard links in %s..." % base_dir
Greg Ward578c10d2000-03-31 02:50:04 +0000421 else: # nope, have to copy
422 link = None
Greg Warda82122b2000-02-17 23:56:15 +0000423 msg = "copying files to %s..." % base_dir
424
Greg Ward5fad2682000-09-06 02:18:59 +0000425 if not files:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000426 log.warn("no files to distribute -- empty manifest?")
Greg Ward5fad2682000-09-06 02:18:59 +0000427 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000428 log.info(msg)
Greg Warda82122b2000-02-17 23:56:15 +0000429 for file in files:
Greg Ward5fad2682000-09-06 02:18:59 +0000430 if not os.path.isfile(file):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000431 log.warn("'%s' not a regular file -- skipping" % file)
Greg Ward5fad2682000-09-06 02:18:59 +0000432 else:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000433 dest = os.path.join(base_dir, file)
434 self.copy_file(file, dest, link=link)
Greg Warda82122b2000-02-17 23:56:15 +0000435
Andrew M. Kuchlinga7f225d2001-03-22 03:10:05 +0000436 self.distribution.metadata.write_pkg_info(base_dir)
Fred Drake21d45352001-12-06 21:01:19 +0000437
Tarek Ziadé89539132009-05-14 14:56:14 +0000438 def make_distribution(self):
Greg Wardc3c8c6e2000-06-08 00:46:45 +0000439 """Create the source distribution(s). First, we create the release
440 tree with 'make_release_tree()'; then, we create all required
441 archive files (according to 'self.formats') from the release tree.
442 Finally, we clean up by blowing away the release tree (unless
Greg Wardaf64aed2000-09-25 01:51:01 +0000443 'self.keep_temp' is true). The list of archive files created is
Greg Wardc3c8c6e2000-06-08 00:46:45 +0000444 stored so it can be retrieved later by 'get_archive_files()'.
445 """
Greg Ward578c10d2000-03-31 02:50:04 +0000446 # Don't warn about missing meta-data here -- should be (and is!)
447 # done elsewhere.
Greg Ward0ae7f762000-04-22 02:51:25 +0000448 base_dir = self.distribution.get_fullname()
Greg Wardc0614102000-07-05 03:06:46 +0000449 base_name = os.path.join(self.dist_dir, base_dir)
Greg Warda82122b2000-02-17 23:56:15 +0000450
Greg Wardcb1f4c42000-09-30 18:27:54 +0000451 self.make_release_tree(base_dir, self.filelist.files)
Greg Wardd87eb732000-06-01 01:10:56 +0000452 archive_files = [] # remember names of files we create
Tarek Ziadéaaedcef2009-01-25 23:34:00 +0000453 # tar archive must be created last to avoid overwrite and remove
454 if 'tar' in self.formats:
455 self.formats.append(self.formats.pop(self.formats.index('tar')))
456
Greg Warda82122b2000-02-17 23:56:15 +0000457 for fmt in self.formats:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000458 file = self.make_archive(base_name, fmt, base_dir=base_dir)
Greg Wardd87eb732000-06-01 01:10:56 +0000459 archive_files.append(file)
Martin v. Löwis98da5622005-03-23 18:54:36 +0000460 self.distribution.dist_files.append(('sdist', '', file))
Greg Wardd87eb732000-06-01 01:10:56 +0000461
462 self.archive_files = archive_files
Greg Warda82122b2000-02-17 23:56:15 +0000463
Greg Wardaf64aed2000-09-25 01:51:01 +0000464 if not self.keep_temp:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000465 dir_util.remove_tree(base_dir, dry_run=self.dry_run)
Greg Warda82122b2000-02-17 23:56:15 +0000466
Tarek Ziadé89539132009-05-14 14:56:14 +0000467 def get_archive_files(self):
Greg Wardd87eb732000-06-01 01:10:56 +0000468 """Return the list of archive files created when the command
469 was run, or None if the command hasn't run yet.
470 """
471 return self.archive_files