1 from distutils.command.sdist import sdist as _sdist
2 from distutils.util import convert_path
3 from distutils import log
5 import os, re, sys, pkg_resources
8 ("<","<"), (">", ">"), (""", '"'), ("'", "'"),
13 for old,new in entities:
14 data = data.replace(old,new)
17 def re_finder(pattern, postproc=None):
18 def find(dirname, filename):
19 f = open(filename,'rU')
22 for match in pattern.finditer(data):
26 yield joinpath(dirname,path)
29 def joinpath(prefix,suffix):
32 return os.path.join(prefix,suffix)
42 def walk_revctrl(dirname=''):
43 """Find all files under revision control"""
44 for ep in pkg_resources.iter_entry_points('setuptools.file_finders'):
45 for item in ep.load()(dirname):
48 def _default_revctrl(dirname=''):
49 for path, finder in finders:
50 path = joinpath(dirname,path)
51 if os.path.isfile(path):
52 for path in finder(dirname,path):
53 if os.path.isfile(path):
55 elif os.path.isdir(path):
56 for item in _default_revctrl(path):
59 def externals_finder(dirname, filename):
60 """Find any 'svn:externals' directories"""
62 f = open(filename,'rb')
63 for line in iter(f.readline, ''): # can't use direct iter!
67 data = f.read(int(length))
68 if kind=='K' and data=='svn:externals':
70 elif kind=='V' and found:
77 for line in data.splitlines():
80 yield joinpath(dirname, parts[0])
83 entries_pattern = re.compile(r'name="([^"]+)"(?![^>]+deleted="true")', re.I)
85 def entries_finder(dirname, filename):
86 f = open(filename,'rU')
89 if data.startswith('<?xml'):
90 for match in entries_pattern.finditer(data):
91 yield joinpath(dirname,unescape(match.group(1)))
94 try: svnver = int(data.splitlines()[0])
97 log.warn("unrecognized .svn/entries format in %s", dirname)
99 for record in map(str.splitlines, data.split('\n\x0c\n')[1:]):
100 if not record or len(record)>=6 and record[5]=="delete":
101 continue # skip deleted
102 yield joinpath(dirname, record[0])
106 (convert_path('CVS/Entries'),
107 re_finder(re.compile(r"^\w?/([^/]+)/", re.M))),
108 (convert_path('.svn/entries'), entries_finder),
109 (convert_path('.svn/dir-props'), externals_finder),
110 (convert_path('.svn/dir-prop-base'), externals_finder), # svn 1.4
125 """Smart sdist that finds anything supported by revision control"""
129 "formats for source distribution (comma-separated list)"),
131 "keep the distribution tree around after creating " +
134 "directory to put the source distribution archive(s) in "
141 self.run_command('egg_info')
142 ei_cmd = self.get_finalized_command('egg_info')
143 self.filelist = ei_cmd.filelist
144 self.filelist.append(os.path.join(ei_cmd.egg_info,'SOURCES.txt'))
146 self.check_metadata()
147 self.make_distribution()
149 dist_files = getattr(self.distribution,'dist_files',[])
150 for file in self.archive_files:
151 data = ('sdist', '', file)
152 if data not in dist_files:
153 dist_files.append(data)
155 def read_template(self):
157 _sdist.read_template(self)
159 # grody hack to close the template file (MANIFEST.in)
160 # this prevents easy_install's attempt at deleting the file from
161 # dying and thus masking the real error
162 sys.exc_info()[2].tb_next.tb_frame.f_locals['template'].close()
165 # Cribbed from old distutils code, to work around new distutils code
166 # that tries to do some of the same stuff as we do, in a way that makes
169 def add_defaults (self):
170 standards = [('README', 'README.txt'), self.distribution.script_name]
173 if type(fn) is tuple:
177 if os.path.exists(fn):
179 self.filelist.append(fn)
183 self.warn("standard file not found: should have one of " +
186 if os.path.exists(fn):
187 self.filelist.append(fn)
189 self.warn("standard file '%s' not found" % fn)
191 optional = ['test/test*.py', 'setup.cfg']
193 for pattern in optional:
194 files = filter(os.path.isfile, glob(pattern))
196 self.filelist.extend(files)
198 if self.distribution.has_pure_modules():
199 build_py = self.get_finalized_command('build_py')
200 self.filelist.extend(build_py.get_source_files())
202 if self.distribution.has_ext_modules():
203 build_ext = self.get_finalized_command('build_ext')
204 self.filelist.extend(build_ext.get_source_files())
206 if self.distribution.has_c_libraries():
207 build_clib = self.get_finalized_command('build_clib')
208 self.filelist.extend(build_clib.get_source_files())
210 if self.distribution.has_scripts():
211 build_scripts = self.get_finalized_command('build_scripts')
212 self.filelist.extend(build_scripts.get_source_files())
215 def check_readme(self):
216 alts = ("README", "README.txt")
218 if os.path.exists(f):
222 "standard file not found: should have one of " +', '.join(alts)
226 def make_release_tree(self, base_dir, files):
227 _sdist.make_release_tree(self, base_dir, files)
229 # Save any egg_info command line options used to create this sdist
230 dest = os.path.join(base_dir, 'setup.cfg')
231 if hasattr(os,'link') and os.path.exists(dest):
232 # unlink and re-copy, since it might be hard-linked, and
233 # we don't want to change the source version
235 self.copy_file('setup.cfg', dest)
237 self.get_finalized_command('egg_info').save_version_info(dest)