Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 62 lines (51 sloc) 1.85 KB
#!/usr/bin/env python
Create a source tarball of a Python project from a (local) source tree.
All this command doing is packing up the source tree (ignoring .git,
.svn, .hg, *~, .pyc, etc... files), and giving the directory and the
tarball a good name (by calling --fullname). This may be
overwritten using the --dst option.
import os
import sys
from os.path import isfile
from optparse import OptionParser
from subprocess import Popen, PIPE
import tarfile
def exclude_func(path):
if path.endswith(('~', '.pyc', '.pyo', '.tar.gz')):
return True
parts = path.split(os.sep)
assert parts[0] == '.', path
if len(parts) >= 2:
if parts[1].startswith(('.git', '.hg')):
return True
if parts[1] in ('build', 'dist'):
return True
if '.svn' in parts or '.git' in parts:
return True
return False
def get_name_version(path='.'):
cmd = [sys.executable, '', '--fullname']
p = Popen(cmd, cwd=path, stdout=PIPE, stderr=PIPE)
fullname = p.communicate()[0].split()[-1]
name, version = fullname.rsplit('-', 1)
return dict(name=name, version=version.rstrip('.dev'))
def main():
p = OptionParser(usage="usage: %prog [options]", description=__doc__)
p.add_option("--dst", action="store")
opts, args = p.parse_args()
if opts.dst and not opts.dst.endswith('.tar.gz'):
p.error('not a good name for a tarball: %s' % opts.dst)
if args:
p.error('no arguments expected')
if not opts.dst and not isfile(''):
sys.exit("Error: no such file:")
dst = opts.dst or '%(name)s-%(version)s.tar.gz' % get_name_version()
assert dst.endswith('.tar.gz')
print 'Creating:', dst
t =, 'w:gz')
t.add('.', arcname=dst[:-7], exclude=exclude_func)
if __name__ == '__main__':