00001
00002
00003 """
00004 usage: %prog [args]
00005 """
00006
00007 from __future__ import with_statement
00008 import os, sys, string
00009 from optparse import OptionParser
00010
00011 import re
00012 import tarfile
00013
00014 TAR_IGNORE_TOP=['build']
00015 TAR_IGNORE_ALL=['.svn']
00016
00017 def tar_exclude(name):
00018 if name.split('/')[-1] in TAR_IGNORE_ALL:
00019 return True
00020 else:
00021 return False
00022
00023 def package_source(path):
00024 """
00025 Create a source tarball from a stack at a particular path.
00026
00027 @param path: the path of the stack to package up
00028 @return: the path of the resulting tarball, or else None
00029 """
00030
00031
00032 stack_xml_path = os.path.join(path,'stack.xml')
00033 cmake_lists_path = os.path.join(path, 'CMakeLists.txt')
00034
00035 if not os.path.exists(stack_xml_path):
00036 print >> sys.stderr, "Did not find: [%s]."%stack_xml_path
00037 return None
00038
00039 if not os.path.exists(cmake_lists_path):
00040 print >> sys.stderr, "Did not find: [%s]."%cmake_lists_path
00041 return None
00042
00043
00044 stack_name = os.path.split(os.path.abspath(path))[-1]
00045
00046
00047 with open(cmake_lists_path, 'r') as f:
00048 m = re.search('rosbuild_make_distribution\((.*)\)',f.read())
00049 if m is not None:
00050 stack_version = m.group(1)
00051 else:
00052 print >> sys.stderr, "Could not find version number in CMakeLists.txt for stack"
00053 return None
00054
00055
00056 build_dir = os.path.join(path, 'build')
00057 if not os.path.exists(build_dir):
00058 os.makedirs(build_dir)
00059
00060 tarfile_name = os.path.join('build','%s-%s.tar.bz2'%(stack_name, stack_version))
00061 archive_dir = '%s-%s'%(stack_name, stack_version)
00062
00063 tar = tarfile.open(tarfile_name, 'w:bz2')
00064
00065 for x in os.listdir(path):
00066 if x not in TAR_IGNORE_TOP + TAR_IGNORE_ALL:
00067
00068 p = os.path.join(path,x)
00069
00070 tp = os.path.join(archive_dir, x)
00071
00072 tar.add(p, tp, exclude=tar_exclude)
00073
00074 tar.close()
00075
00076 return tarfile_name
00077
00078 def main(argv, stdout, environ):
00079
00080 parser = OptionParser(__doc__.strip())
00081
00082 (options, args) = parser.parse_args()
00083
00084 if (args == 1):
00085 parser.error("Must provide a path")
00086
00087 tar_file = package_source(args[0])
00088
00089 if tar_file is None:
00090 print >> sys.stderr, "Failed to package source."
00091 sys.exit(1)
00092
00093 print "Created: %s"%tar_file
00094 sys.exit(0)
00095
00096 if __name__ == "__main__":
00097 main(sys.argv, sys.stdout, os.environ)