pack.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 """
00003     roswww_pack pack.py
00004     it crawls all www directory in rospackage path and copy them into destination directory
00005 
00006     adopted some code from roswww
00007 """
00008 
00009 import rospy
00010 import subprocess
00011 import os
00012 import argparse
00013 
00014 def run(*args) :
00015     ''' run the provided command and return its stdout'''
00016     args = sum([(arg if type(arg) == list else [arg]) for arg in args], [])
00017     return subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0].strip()
00018 
00019 def split_words(text):
00020     ''' return a list of lines where each line is a list of words '''
00021     return [line.strip().split() for line in text.split('\n')]
00022 
00023 def get_packages():
00024     """Find the names and locations of all packages """
00025     line = split_words(run('rospack','list'))
00026     packages = [{'name': name, 'path':path} for name, path in line]
00027     return packages
00028 
00029 def parse_args():
00030     parser = argparse.ArgumentParser(description='Crawls all www directory in ros package path')
00031     parser.add_argument('-d','--destination',type=str,required=True,nargs=1,help='/home/yourdir/www')
00032     parser.add_argument('-l','--link',type=str,required=True,nargs=1,help='symbolic link or hard copy("link"/"copy")')
00033 
00034     args = parser.parse_args()
00035     return args
00036 
00037 # www only for now. We can copy other directories later.
00038 def copy_directories(packs,subdir,dest):
00039     print "Start Copying..."
00040     for p in packs:
00041         path = p['path'] + '/' + subdir
00042         name = p['name']
00043         destination = dest +'/'+name
00044         print "\tCopying " + name
00045         run('cp','-r',path,destination)
00046     print "Done"
00047 
00048 # www only for now. We can copy other directories later.
00049 def link_directories(packs,subdir,dest):
00050     print "Start Linking..."
00051     for p in packs:
00052         path = p['path'] + '/' + subdir
00053         name = p['name']
00054         destination = dest +'/'+name
00055         print "\tLinking " + name
00056         run('ln','-sf',path,destination)
00057     print "Done"
00058 
00059 if __name__=='__main__':
00060     subdir = 'www'
00061     args = parse_args()
00062     packages = get_packages()
00063     packages = [ p for p in packages if os.path.exists(p['path']+'/'+subdir)]
00064     
00065     try:         
00066         os.mkdir(args.destination[0])
00067     except OSError as e:
00068         print str(args.destination[0]) + " directory already exists"
00069 
00070     if args.link[0].lower() == "link":
00071         link_directories(packages,'www',args.destination[0])
00072     elif args.link[0].lower() == "copy":
00073         copy_directories(packages,'www',args.destination[0])
00074     else:
00075         print "Arugument Error " + args.link[0] + ". should use -l (copy/link)"
00076 


roswww_pack
Author(s): Jonathan Mace, Jihoon Lee , Isaac Isao Saito
autogenerated on Mon Nov 25 2013 00:24:35