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