blob: 5c74e5329fc5df7fac0245c594ad9c8ba3e50423 [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]"),
Tarek Ziadé1b486712009-10-02 23:49:48 +000077 ('owner=', 'u',
78 "Owner name used when creating a tar file [default: current user]"),
79 ('group=', 'g',
80 "Group name used when creating a tar file [default: current group]"),
Greg Wardbbeceea2000-02-18 00:25:39 +000081 ]
Greg Wardf1fe1032000-06-08 00:14:18 +000082
Greg Ward99b032e2000-09-25 01:41:15 +000083 boolean_options = ['use-defaults', 'prune',
84 'manifest-only', 'force-manifest',
Tarek Ziadécb768042009-05-16 16:37:06 +000085 'keep-temp', 'metadata-check']
Greg Wardf1fe1032000-06-08 00:14:18 +000086
Greg Ward9d17a7a2000-06-07 03:00:06 +000087 help_options = [
88 ('help-formats', None,
Greg Ward2ff78872000-06-24 00:23:20 +000089 "list available distribution formats", show_formats),
Greg Wardfa9ff762000-10-14 04:06:40 +000090 ]
Greg Ward9d17a7a2000-06-07 03:00:06 +000091
Greg Ward499822d2000-06-29 02:06:29 +000092 negative_opt = {'no-defaults': 'use-defaults',
93 'no-prune': 'prune' }
Greg Warda82122b2000-02-17 23:56:15 +000094
Tarek Ziadé89539132009-05-14 14:56:14 +000095 default_format = {'posix': 'gztar',
96 'nt': 'zip' }
Greg Warda82122b2000-02-17 23:56:15 +000097
Tarek Ziadécb768042009-05-16 16:37:06 +000098 sub_commands = [('check', checking_metadata)]
99
Tarek Ziadé89539132009-05-14 14:56:14 +0000100 def initialize_options(self):
Greg Warda82122b2000-02-17 23:56:15 +0000101 # 'template' and 'manifest' are, respectively, the names of
102 # the manifest template and manifest file.
103 self.template = None
104 self.manifest = None
105
106 # 'use_defaults': if true, we will include the default file set
107 # in the manifest
108 self.use_defaults = 1
Greg Ward499822d2000-06-29 02:06:29 +0000109 self.prune = 1
Greg Warda82122b2000-02-17 23:56:15 +0000110
111 self.manifest_only = 0
112 self.force_manifest = 0
113
114 self.formats = None
Greg Wardaf64aed2000-09-25 01:51:01 +0000115 self.keep_temp = 0
Greg Wardc0614102000-07-05 03:06:46 +0000116 self.dist_dir = None
Greg Warda82122b2000-02-17 23:56:15 +0000117
Greg Wardd87eb732000-06-01 01:10:56 +0000118 self.archive_files = None
Tarek Ziadécb768042009-05-16 16:37:06 +0000119 self.metadata_check = 1
Tarek Ziadé1b486712009-10-02 23:49:48 +0000120 self.owner = None
121 self.group = None
Greg Wardd87eb732000-06-01 01:10:56 +0000122
Tarek Ziadé89539132009-05-14 14:56:14 +0000123 def finalize_options(self):
Greg Warda82122b2000-02-17 23:56:15 +0000124 if self.manifest is None:
125 self.manifest = "MANIFEST"
126 if self.template is None:
127 self.template = "MANIFEST.in"
128
Greg Ward62d5a572000-06-04 15:12:51 +0000129 self.ensure_string_list('formats')
Greg Warda82122b2000-02-17 23:56:15 +0000130 if self.formats is None:
131 try:
132 self.formats = [self.default_format[os.name]]
133 except KeyError:
134 raise DistutilsPlatformError, \
Greg Ward578c10d2000-03-31 02:50:04 +0000135 "don't know how to create source distributions " + \
136 "on platform %s" % os.name
Greg Warda82122b2000-02-17 23:56:15 +0000137
Greg Wardcb1f4c42000-09-30 18:27:54 +0000138 bad_format = archive_util.check_archive_formats(self.formats)
Greg Ward6a9a5452000-04-22 03:11:55 +0000139 if bad_format:
140 raise DistutilsOptionError, \
141 "unknown archive format '%s'" % bad_format
142
Greg Wardc0614102000-07-05 03:06:46 +0000143 if self.dist_dir is None:
144 self.dist_dir = "dist"
145
Tarek Ziadé89539132009-05-14 14:56:14 +0000146 def run(self):
Greg Ward23266fe2000-07-30 01:30:31 +0000147 # 'filelist' contains the list of files that will make up the
148 # manifest
149 self.filelist = FileList()
Fred Drake21d45352001-12-06 21:01:19 +0000150
Tarek Ziadécb768042009-05-16 16:37:06 +0000151 # Run sub commands
152 for cmd_name in self.get_sub_commands():
153 self.run_command(cmd_name)
Greg Warda82122b2000-02-17 23:56:15 +0000154
155 # Do whatever it takes to get the list of files to process
156 # (process the manifest template, read an existing manifest,
Greg Ward23266fe2000-07-30 01:30:31 +0000157 # whatever). File list is accumulated in 'self.filelist'.
Greg Wardcb1f4c42000-09-30 18:27:54 +0000158 self.get_file_list()
Greg Warda82122b2000-02-17 23:56:15 +0000159
160 # If user just wanted us to regenerate the manifest, stop now.
161 if self.manifest_only:
162 return
163
164 # Otherwise, go ahead and create the source distribution tarball,
165 # or zipfile, or whatever.
Greg Wardcb1f4c42000-09-30 18:27:54 +0000166 self.make_distribution()
Greg Warda82122b2000-02-17 23:56:15 +0000167
Tarek Ziadé89539132009-05-14 14:56:14 +0000168 def check_metadata(self):
Tarek Ziadécb768042009-05-16 16:37:06 +0000169 """Deprecated API."""
170 warn("distutils.command.sdist.check_metadata is deprecated, \
171 use the check command instead", PendingDeprecationWarning)
172 check = self.distribution.get_command_obj('check')
173 check.ensure_finalized()
174 check.run()
Greg Warda82122b2000-02-17 23:56:15 +0000175
Tarek Ziadé89539132009-05-14 14:56:14 +0000176 def get_file_list(self):
Greg Warda82122b2000-02-17 23:56:15 +0000177 """Figure out the list of files to include in the source
Greg Ward23266fe2000-07-30 01:30:31 +0000178 distribution, and put it in 'self.filelist'. This might involve
Greg Warde0c8c2f2000-06-08 00:24:01 +0000179 reading the manifest template (and writing the manifest), or just
180 reading the manifest, or just using the default file set -- it all
181 depends on the user's options and the state of the filesystem.
182 """
Greg Wardb2db0eb2000-06-21 03:29:57 +0000183 # If we have a manifest template, see if it's newer than the
184 # manifest; if so, we'll regenerate the manifest.
Greg Ward23266fe2000-07-30 01:30:31 +0000185 template_exists = os.path.isfile(self.template)
Greg Warda82122b2000-02-17 23:56:15 +0000186 if template_exists:
Greg Wardab3a0f32000-08-05 01:31:54 +0000187 template_newer = dep_util.newer(self.template, self.manifest)
Greg Warda82122b2000-02-17 23:56:15 +0000188
Greg Wardb2db0eb2000-06-21 03:29:57 +0000189 # The contents of the manifest file almost certainly depend on the
190 # setup script as well as the manifest template -- so if the setup
191 # script is newer than the manifest, we'll regenerate the manifest
192 # from the template. (Well, not quite: if we already have a
193 # manifest, but there's no template -- which will happen if the
194 # developer elects to generate a manifest some other way -- then we
195 # can't regenerate the manifest, so we don't.)
Greg Ward9821bf42000-08-29 01:15:18 +0000196 self.debug_print("checking if %s newer than %s" %
197 (self.distribution.script_name, self.manifest))
198 setup_newer = dep_util.newer(self.distribution.script_name,
199 self.manifest)
Greg Wardb2db0eb2000-06-21 03:29:57 +0000200
201 # cases:
202 # 1) no manifest, template exists: generate manifest
203 # (covered by 2a: no manifest == template newer)
204 # 2) manifest & template exist:
205 # 2a) template or setup script newer than manifest:
206 # regenerate manifest
207 # 2b) manifest newer than both:
208 # do nothing (unless --force or --manifest-only)
209 # 3) manifest exists, no template:
210 # do nothing (unless --force or --manifest-only)
211 # 4) no manifest, no template: generate w/ warning ("defaults only")
212
Greg Wardd3b76a82000-09-06 02:08:24 +0000213 manifest_outofdate = (template_exists and
214 (template_newer or setup_newer))
215 force_regen = self.force_manifest or self.manifest_only
216 manifest_exists = os.path.isfile(self.manifest)
217 neither_exists = (not template_exists and not manifest_exists)
Greg Warda82122b2000-02-17 23:56:15 +0000218
Greg Wardd3b76a82000-09-06 02:08:24 +0000219 # Regenerate the manifest if necessary (or if explicitly told to)
220 if manifest_outofdate or neither_exists or force_regen:
Greg Warda82122b2000-02-17 23:56:15 +0000221 if not template_exists:
Greg Ward23266fe2000-07-30 01:30:31 +0000222 self.warn(("manifest template '%s' does not exist " +
223 "(using default file list)") %
224 self.template)
Greg Ward6b24dff2000-07-30 01:47:16 +0000225 self.filelist.findall()
226
Greg Warda82122b2000-02-17 23:56:15 +0000227 if self.use_defaults:
Greg Ward23266fe2000-07-30 01:30:31 +0000228 self.add_defaults()
Greg Warda82122b2000-02-17 23:56:15 +0000229 if template_exists:
Greg Ward23266fe2000-07-30 01:30:31 +0000230 self.read_template()
Greg Ward499822d2000-06-29 02:06:29 +0000231 if self.prune:
232 self.prune_file_list()
Greg Wardce15c6c2000-06-08 01:06:02 +0000233
Greg Ward23266fe2000-07-30 01:30:31 +0000234 self.filelist.sort()
Greg Ward23266fe2000-07-30 01:30:31 +0000235 self.filelist.remove_duplicates()
Greg Ward23266fe2000-07-30 01:30:31 +0000236 self.write_manifest()
Greg Warda82122b2000-02-17 23:56:15 +0000237
238 # Don't regenerate the manifest, just read it in.
239 else:
Greg Ward23266fe2000-07-30 01:30:31 +0000240 self.read_manifest()
Greg Warda82122b2000-02-17 23:56:15 +0000241
Tarek Ziadé89539132009-05-14 14:56:14 +0000242 def add_defaults(self):
Greg Ward23266fe2000-07-30 01:30:31 +0000243 """Add all the default files to self.filelist:
Greg Wardc3c8c6e2000-06-08 00:46:45 +0000244 - README or README.txt
245 - setup.py
246 - test/test*.py
247 - all pure Python modules mentioned in setup script
Tarek Ziadé7dd53392009-02-16 21:38:01 +0000248 - all files pointed by package_data (build_py)
249 - all files defined in data_files.
250 - all files defined as scripts.
Greg Wardc3c8c6e2000-06-08 00:46:45 +0000251 - all C sources listed as part of extensions or C libraries
252 in the setup script (doesn't catch C headers!)
253 Warns if (README or README.txt) or setup.py are missing; everything
254 else is optional.
255 """
Greg Ward14c8d052000-06-08 01:22:48 +0000256
Greg Wardd3b76a82000-09-06 02:08:24 +0000257 standards = [('README', 'README.txt'), self.distribution.script_name]
Greg Warda82122b2000-02-17 23:56:15 +0000258 for fn in standards:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000259 if type(fn) is TupleType:
Greg Warda82122b2000-02-17 23:56:15 +0000260 alts = fn
Greg Ward48401122000-02-24 03:17:43 +0000261 got_it = 0
Greg Warda82122b2000-02-17 23:56:15 +0000262 for fn in alts:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000263 if os.path.exists(fn):
Greg Warda82122b2000-02-17 23:56:15 +0000264 got_it = 1
Greg Wardcb1f4c42000-09-30 18:27:54 +0000265 self.filelist.append(fn)
Greg Warda82122b2000-02-17 23:56:15 +0000266 break
267
268 if not got_it:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000269 self.warn("standard file not found: should have one of " +
270 string.join(alts, ', '))
Greg Warda82122b2000-02-17 23:56:15 +0000271 else:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000272 if os.path.exists(fn):
273 self.filelist.append(fn)
Greg Warda82122b2000-02-17 23:56:15 +0000274 else:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000275 self.warn("standard file '%s' not found" % fn)
Greg Warda82122b2000-02-17 23:56:15 +0000276
Greg Ward14c8d052000-06-08 01:22:48 +0000277 optional = ['test/test*.py', 'setup.cfg']
Greg Warda82122b2000-02-17 23:56:15 +0000278 for pattern in optional:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000279 files = filter(os.path.isfile, glob(pattern))
Greg Warda82122b2000-02-17 23:56:15 +0000280 if files:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000281 self.filelist.extend(files)
Greg Warda82122b2000-02-17 23:56:15 +0000282
Tarek Ziadé7dd53392009-02-16 21:38:01 +0000283 # build_py is used to get:
284 # - python modules
285 # - files defined in package_data
286 build_py = self.get_finalized_command('build_py')
287
288 # getting python files
Greg Ward578c10d2000-03-31 02:50:04 +0000289 if self.distribution.has_pure_modules():
Greg Wardcb1f4c42000-09-30 18:27:54 +0000290 self.filelist.extend(build_py.get_source_files())
Greg Warda82122b2000-02-17 23:56:15 +0000291
Tarek Ziadé7dd53392009-02-16 21:38:01 +0000292 # getting package_data files
293 # (computed in build_py.data_files by build_py.finalize_options)
294 for pkg, src_dir, build_dir, filenames in build_py.data_files:
295 for filename in filenames:
296 self.filelist.append(os.path.join(src_dir, filename))
297
298 # getting distribution.data_files
299 if self.distribution.has_data_files():
Tarek Ziadéf68b5b82009-02-17 09:42:44 +0000300 for item in self.distribution.data_files:
301 if isinstance(item, str): # plain file
302 item = convert_path(item)
303 if os.path.isfile(item):
304 self.filelist.append(item)
305 else: # a (dirname, filenames) tuple
306 dirname, filenames = item
307 for f in filenames:
Tarek Ziadé0e5001e2009-02-17 23:06:51 +0000308 f = convert_path(f)
Tarek Ziadéf68b5b82009-02-17 09:42:44 +0000309 if os.path.isfile(f):
310 self.filelist.append(f)
Tarek Ziadé7dd53392009-02-16 21:38:01 +0000311
Greg Ward578c10d2000-03-31 02:50:04 +0000312 if self.distribution.has_ext_modules():
Greg Wardcb1f4c42000-09-30 18:27:54 +0000313 build_ext = self.get_finalized_command('build_ext')
314 self.filelist.extend(build_ext.get_source_files())
Greg Warda82122b2000-02-17 23:56:15 +0000315
Greg Ward60908f12000-04-09 03:51:40 +0000316 if self.distribution.has_c_libraries():
Greg Wardcb1f4c42000-09-30 18:27:54 +0000317 build_clib = self.get_finalized_command('build_clib')
318 self.filelist.extend(build_clib.get_source_files())
Greg Ward60908f12000-04-09 03:51:40 +0000319
Fred Drake4b498232004-03-25 22:04:52 +0000320 if self.distribution.has_scripts():
321 build_scripts = self.get_finalized_command('build_scripts')
322 self.filelist.extend(build_scripts.get_source_files())
323
Tarek Ziadé89539132009-05-14 14:56:14 +0000324 def read_template(self):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000325 """Read and parse manifest template file named by self.template.
Greg Warda82122b2000-02-17 23:56:15 +0000326
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000327 (usually "MANIFEST.in") The parsing and processing is done by
328 'self.filelist', which updates itself accordingly.
Greg Ward23266fe2000-07-30 01:30:31 +0000329 """
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000330 log.info("reading manifest template '%s'", self.template)
Greg Wardcb1f4c42000-09-30 18:27:54 +0000331 template = TextFile(self.template,
332 strip_comments=1,
333 skip_blanks=1,
334 join_lines=1,
335 lstrip_ws=1,
336 rstrip_ws=1,
337 collapse_join=1)
Greg Warda82122b2000-02-17 23:56:15 +0000338
Greg Warda82122b2000-02-17 23:56:15 +0000339 while 1:
Greg Warda82122b2000-02-17 23:56:15 +0000340 line = template.readline()
341 if line is None: # end of file
342 break
343
Greg Ward6b24dff2000-07-30 01:47:16 +0000344 try:
345 self.filelist.process_template_line(line)
346 except DistutilsTemplateError, msg:
347 self.warn("%s, line %d: %s" % (template.filename,
348 template.current_line,
349 msg))
Greg Warda82122b2000-02-17 23:56:15 +0000350
Tarek Ziadé89539132009-05-14 14:56:14 +0000351 def prune_file_list(self):
Greg Wardce15c6c2000-06-08 01:06:02 +0000352 """Prune off branches that might slip into the file list as created
Greg Ward499822d2000-06-29 02:06:29 +0000353 by 'read_template()', but really don't belong there:
354 * the build tree (typically "build")
355 * the release tree itself (only an issue if we ran "sdist"
Greg Wardaf64aed2000-09-25 01:51:01 +0000356 previously with --keep-temp, or it aborted)
Georg Brandl1df03402008-03-06 06:47:18 +0000357 * any RCS, CVS, .svn, .hg, .git, .bzr, _darcs directories
Greg Wardce15c6c2000-06-08 01:06:02 +0000358 """
359 build = self.get_finalized_command('build')
360 base_dir = self.distribution.get_fullname()
Greg Wardce15c6c2000-06-08 01:06:02 +0000361
Greg Ward23266fe2000-07-30 01:30:31 +0000362 self.filelist.exclude_pattern(None, prefix=build.build_base)
363 self.filelist.exclude_pattern(None, prefix=base_dir)
Greg Wardf8b9e202000-06-08 00:08:14 +0000364
Tarek Ziadé85d6fb52009-01-04 00:04:49 +0000365 # pruning out vcs directories
366 # both separators are used under win32
Tarek Ziadéd81780b2009-01-04 10:37:52 +0000367 if sys.platform == 'win32':
368 seps = r'/|\\'
369 else:
370 seps = '/'
371
372 vcs_dirs = ['RCS', 'CVS', r'\.svn', r'\.hg', r'\.git', r'\.bzr',
373 '_darcs']
Tarek Ziadé85d6fb52009-01-04 00:04:49 +0000374 vcs_ptrn = r'(^|%s)(%s)(%s).*' % (seps, '|'.join(vcs_dirs), seps)
375 self.filelist.exclude_pattern(vcs_ptrn, is_regex=1)
Greg Wardf8b9e202000-06-08 00:08:14 +0000376
Tarek Ziadé89539132009-05-14 14:56:14 +0000377 def write_manifest(self):
Greg Ward23266fe2000-07-30 01:30:31 +0000378 """Write the file list in 'self.filelist' (presumably as filled in
379 by 'add_defaults()' and 'read_template()') to the manifest file
380 named by 'self.manifest'.
Greg Warde0c8c2f2000-06-08 00:24:01 +0000381 """
Greg Wardab3a0f32000-08-05 01:31:54 +0000382 self.execute(file_util.write_file,
Greg Ward23266fe2000-07-30 01:30:31 +0000383 (self.manifest, self.filelist.files),
Greg Wardf8b9e202000-06-08 00:08:14 +0000384 "writing manifest file '%s'" % self.manifest)
Greg Warda82122b2000-02-17 23:56:15 +0000385
Tarek Ziadé89539132009-05-14 14:56:14 +0000386 def read_manifest(self):
Greg Warde0c8c2f2000-06-08 00:24:01 +0000387 """Read the manifest file (named by 'self.manifest') and use it to
Greg Ward23266fe2000-07-30 01:30:31 +0000388 fill in 'self.filelist', the list of files to include in the source
Greg Warde0c8c2f2000-06-08 00:24:01 +0000389 distribution.
390 """
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000391 log.info("reading manifest file '%s'", self.manifest)
Greg Wardcb1f4c42000-09-30 18:27:54 +0000392 manifest = open(self.manifest)
Greg Warda82122b2000-02-17 23:56:15 +0000393 while 1:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000394 line = manifest.readline()
Greg Warda82122b2000-02-17 23:56:15 +0000395 if line == '': # end of file
396 break
397 if line[-1] == '\n':
398 line = line[0:-1]
Greg Wardcb1f4c42000-09-30 18:27:54 +0000399 self.filelist.append(line)
Andrew M. Kuchling2d6c13e2008-02-21 14:23:38 +0000400 manifest.close()
Greg Warda82122b2000-02-17 23:56:15 +0000401
Tarek Ziadé89539132009-05-14 14:56:14 +0000402 def make_release_tree(self, base_dir, files):
Greg Wardc3c8c6e2000-06-08 00:46:45 +0000403 """Create the directory tree that will become the source
404 distribution archive. All directories implied by the filenames in
405 'files' are created under 'base_dir', and then we hard link or copy
406 (if hard linking is unavailable) those files into place.
407 Essentially, this duplicates the developer's source tree, but in a
408 directory named after the distribution, containing only the files
409 to be distributed.
410 """
Greg Ward578c10d2000-03-31 02:50:04 +0000411 # Create all the directories under 'base_dir' necessary to
Greg Ward5fad2682000-09-06 02:18:59 +0000412 # put 'files' there; the 'mkpath()' is just so we don't die
413 # if the manifest happens to be empty.
414 self.mkpath(base_dir)
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000415 dir_util.create_tree(base_dir, files, dry_run=self.dry_run)
Greg Warda82122b2000-02-17 23:56:15 +0000416
417 # And walk over the list of files, either making a hard link (if
418 # os.link exists) to each one that doesn't already exist in its
419 # corresponding location under 'base_dir', or copying each file
420 # that's out-of-date in 'base_dir'. (Usually, all files will be
421 # out-of-date, because by default we blow away 'base_dir' when
422 # we're done making the distribution archives.)
Fred Drake21d45352001-12-06 21:01:19 +0000423
Greg Wardcb1f4c42000-09-30 18:27:54 +0000424 if hasattr(os, 'link'): # can make hard links on this system
Greg Ward578c10d2000-03-31 02:50:04 +0000425 link = 'hard'
Greg Warda82122b2000-02-17 23:56:15 +0000426 msg = "making hard links in %s..." % base_dir
Greg Ward578c10d2000-03-31 02:50:04 +0000427 else: # nope, have to copy
428 link = None
Greg Warda82122b2000-02-17 23:56:15 +0000429 msg = "copying files to %s..." % base_dir
430
Greg Ward5fad2682000-09-06 02:18:59 +0000431 if not files:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000432 log.warn("no files to distribute -- empty manifest?")
Greg Ward5fad2682000-09-06 02:18:59 +0000433 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000434 log.info(msg)
Greg Warda82122b2000-02-17 23:56:15 +0000435 for file in files:
Greg Ward5fad2682000-09-06 02:18:59 +0000436 if not os.path.isfile(file):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000437 log.warn("'%s' not a regular file -- skipping" % file)
Greg Ward5fad2682000-09-06 02:18:59 +0000438 else:
Greg Wardcb1f4c42000-09-30 18:27:54 +0000439 dest = os.path.join(base_dir, file)
440 self.copy_file(file, dest, link=link)
Greg Warda82122b2000-02-17 23:56:15 +0000441
Andrew M. Kuchlinga7f225d2001-03-22 03:10:05 +0000442 self.distribution.metadata.write_pkg_info(base_dir)
Fred Drake21d45352001-12-06 21:01:19 +0000443
Tarek Ziadé89539132009-05-14 14:56:14 +0000444 def make_distribution(self):
Greg Wardc3c8c6e2000-06-08 00:46:45 +0000445 """Create the source distribution(s). First, we create the release
446 tree with 'make_release_tree()'; then, we create all required
447 archive files (according to 'self.formats') from the release tree.
448 Finally, we clean up by blowing away the release tree (unless
Greg Wardaf64aed2000-09-25 01:51:01 +0000449 'self.keep_temp' is true). The list of archive files created is
Greg Wardc3c8c6e2000-06-08 00:46:45 +0000450 stored so it can be retrieved later by 'get_archive_files()'.
451 """
Greg Ward578c10d2000-03-31 02:50:04 +0000452 # Don't warn about missing meta-data here -- should be (and is!)
453 # done elsewhere.
Greg Ward0ae7f762000-04-22 02:51:25 +0000454 base_dir = self.distribution.get_fullname()
Greg Wardc0614102000-07-05 03:06:46 +0000455 base_name = os.path.join(self.dist_dir, base_dir)
Greg Warda82122b2000-02-17 23:56:15 +0000456
Greg Wardcb1f4c42000-09-30 18:27:54 +0000457 self.make_release_tree(base_dir, self.filelist.files)
Greg Wardd87eb732000-06-01 01:10:56 +0000458 archive_files = [] # remember names of files we create
Tarek Ziadéaaedcef2009-01-25 23:34:00 +0000459 # tar archive must be created last to avoid overwrite and remove
460 if 'tar' in self.formats:
461 self.formats.append(self.formats.pop(self.formats.index('tar')))
462
Greg Warda82122b2000-02-17 23:56:15 +0000463 for fmt in self.formats:
Tarek Ziadé1b486712009-10-02 23:49:48 +0000464 file = self.make_archive(base_name, fmt, base_dir=base_dir,
465 owner=self.owner, group=self.group)
Greg Wardd87eb732000-06-01 01:10:56 +0000466 archive_files.append(file)
Martin v. Löwis98da5622005-03-23 18:54:36 +0000467 self.distribution.dist_files.append(('sdist', '', file))
Greg Wardd87eb732000-06-01 01:10:56 +0000468
469 self.archive_files = archive_files
Greg Warda82122b2000-02-17 23:56:15 +0000470
Greg Wardaf64aed2000-09-25 01:51:01 +0000471 if not self.keep_temp:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000472 dir_util.remove_tree(base_dir, dry_run=self.dry_run)
Greg Warda82122b2000-02-17 23:56:15 +0000473
Tarek Ziadé89539132009-05-14 14:56:14 +0000474 def get_archive_files(self):
Greg Wardd87eb732000-06-01 01:10:56 +0000475 """Return the list of archive files created when the command
476 was run, or None if the command hasn't run yet.
477 """
478 return self.archive_files