| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | import gzip |
|---|
| 4 | import os |
|---|
| 5 | from StringIO import StringIO |
|---|
| 6 | import subprocess |
|---|
| 7 | import tarfile |
|---|
| 8 | import tempfile |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | def build_documentation(project_dir): |
|---|
| 12 | doc_dir = os.path.join(project_dir, 'docs') |
|---|
| 13 | temp_file = tempfile.TemporaryFile() |
|---|
| 14 | # We don't need any output on stdout |
|---|
| 15 | subprocess.call(['make', 'html'], cwd=doc_dir, stdout=temp_file) |
|---|
| 16 | |
|---|
| 17 | def make_relative_filename(topdir, filename): |
|---|
| 18 | assert filename.startswith(topdir) |
|---|
| 19 | relative_filename = filename[len(topdir):] |
|---|
| 20 | if relative_filename.startswith(os.sep): |
|---|
| 21 | relative_filename = relative_filename[len(os.sep):] |
|---|
| 22 | return relative_filename |
|---|
| 23 | |
|---|
| 24 | def make_tarname(topdir, filename, path_prefix): |
|---|
| 25 | relative_name = make_relative_filename(topdir, filename) |
|---|
| 26 | tarname = '%s/%s' % (path_prefix, relative_name) |
|---|
| 27 | return tarname |
|---|
| 28 | |
|---|
| 29 | def add_file(tar, filename, arcname, project_dir, path_prefix): |
|---|
| 30 | tarname = make_tarname(project_dir, filename, path_prefix) |
|---|
| 31 | if arcname is not None: |
|---|
| 32 | tarname = make_tarname(project_dir, arcname, path_prefix) |
|---|
| 33 | tar.add(filename, arcname=tarname) |
|---|
| 34 | |
|---|
| 35 | def build_fname_with_changed_path_prefix(project_dir, root, arcname, basename): |
|---|
| 36 | nr_of_dirs = lambda path: len(path.split('/')) |
|---|
| 37 | relative_root = make_relative_filename(project_dir, root) |
|---|
| 38 | assert nr_of_dirs(arcname) <= nr_of_dirs(relative_root), 'Untested' |
|---|
| 39 | |
|---|
| 40 | offset_path_items = relative_root.split('/')[nr_of_dirs(arcname):] |
|---|
| 41 | offset_path = os.path.join(arcname, *offset_path_items) |
|---|
| 42 | tar_fname = os.path.join(project_dir, offset_path, basename) |
|---|
| 43 | return tar_fname |
|---|
| 44 | |
|---|
| 45 | def add_files_below_directory(tar, dirname, arcname, project_dir, path_prefix): |
|---|
| 46 | for (root, dirs, files) in os.walk(dirname): |
|---|
| 47 | for basename in files: |
|---|
| 48 | if basename.endswith('.pyc'): |
|---|
| 49 | continue |
|---|
| 50 | fname = os.path.join(root, basename) |
|---|
| 51 | tar_fname = fname |
|---|
| 52 | if arcname is not None: |
|---|
| 53 | tar_fname = build_fname_with_changed_path_prefix(project_dir, root, arcname, basename) |
|---|
| 54 | tarname = make_tarname(project_dir, tar_fname, path_prefix) |
|---|
| 55 | tar.add(fname, tarname) |
|---|
| 56 | |
|---|
| 57 | def create_tarball(project_dir, package_files, path_prefix): |
|---|
| 58 | tar_fp = StringIO() |
|---|
| 59 | tar = tarfile.open(fileobj=tar_fp, mode='w') |
|---|
| 60 | |
|---|
| 61 | for filename in package_files: |
|---|
| 62 | arcname = None |
|---|
| 63 | if not isinstance(filename, basestring): |
|---|
| 64 | filename, arcname = filename |
|---|
| 65 | filename = os.path.join(project_dir, filename) |
|---|
| 66 | if os.path.isfile(filename): |
|---|
| 67 | add_file(tar, filename, arcname, project_dir, path_prefix) |
|---|
| 68 | else: |
|---|
| 69 | add_files_below_directory(tar, filename, arcname, project_dir, path_prefix) |
|---|
| 70 | tar.close() |
|---|
| 71 | tar_fp.seek(0,0) |
|---|
| 72 | return tar_fp |
|---|
| 73 | |
|---|
| 74 | def get_name_and_version(): |
|---|
| 75 | release_info = {} |
|---|
| 76 | execfile(os.path.join("pymta", "release.py"), release_info) |
|---|
| 77 | return (release_info['name'], release_info['version']) |
|---|
| 78 | |
|---|
| 79 | def main(): |
|---|
| 80 | name, version = get_name_and_version() |
|---|
| 81 | this_dir = os.path.abspath(os.path.dirname(__file__)) |
|---|
| 82 | build_documentation(this_dir) |
|---|
| 83 | |
|---|
| 84 | package_files = ('docs', 'examples', 'pymta', 'tests', 'Changelog.txt', |
|---|
| 85 | 'COPYING.txt', 'setup.py', ('build/html', 'docs/html')) |
|---|
| 86 | tar_fp = create_tarball(this_dir, package_files, '%s-%s' % (name, version)) |
|---|
| 87 | |
|---|
| 88 | gz_filename = '%s-%s.tar.gz' % (name, version) |
|---|
| 89 | gzip.open(gz_filename, 'wb').write(tar_fp.read()) |
|---|
| 90 | |
|---|
| 91 | if __name__ == '__main__': |
|---|
| 92 | main() |
|---|
| 93 | |
|---|
| 94 | |
|---|