Semi-automatically importing directories with callthru: the gis.py script
From Cellbe
Importing files with callthru can be boring, especially when the files to import are many. This python script (gis.py) makes it easy to transfer files using the callthru facility provided with the simulator: Let's say you have a directory source which contains the file you want to transfer from the host to the guest, and you want its contents being placed in a directory named destination. Using the gis.py script you can do that as follows:
host /path/to/pwd/$ gis.py source/directory/path destination/relative/path/ > generated_import_script.sh
simulator /some/path/$ callthru source /path/to/pwd/generated_import_script.sh > gis.sh simulator /some/path/$ chmod a+x gis.sh simulator /some/path/$ ./gis.sh
Here's the script:
#!/usr/bin/python # # Author: Alessandro Piras (laynor@gmail.com) # Description: generates a script that copies the contents of a directory # in the host environment to a directory in the simulated # environment. # import os, sys from stat import * # function that walks the directory tree def walktree(top, fcallback, dcallback, dest_dir): '''recursively descend the directory tree rooted at top, calling the callback function for each regular file''' if dest_dir != '': pmkdir(dest_dir) ## ## The function is called on a file, executing the callback ## mode = os.stat(top)[ST_MODE] if S_ISREG(mode): # It's a file, call the callback function fcallback(top, '', mode, dest_dir) return 0 ## ## Walking the tree... ## for f in os.listdir(top): pathname = os.path.join(top, f) mode = os.stat(pathname)[ST_MODE] if S_ISDIR(mode): # It's a directory, recurse into it print "" walktree(pathname, fcallback, dcallback, dest_dir + f + '/') elif S_ISREG(mode): # It's a file, call the callback function fcallback(f, top, mode, dest_dir) else: # Unknown file type, print a message print 'Skipping %s' % pathname #function called when walking on a file (callthru) def pcallthru(file, base_dir, mode, dest_dir): #command='cat' command='callthru source' command = command + ' ' if base_dir == '': base_dir = os.path.dirname(file) file = os.path.basename(file) print "" print command + base_dir +'/'+ file + ' > ' + dest_dir + file x = '%o' %mode x = '0'+x[-3:] print 'chmod '+x+' ' + dest_dir + file #function called when walkin on a dir: def pmkdir(dir): print 'mkdir -p %s' %dir def main(argv): ## program arguments handling dest_dir = '' if len(argv) < 2: print "USAGE: %s /host/dir/ [/simulator/dir]" % argv[0] return 0 if len(argv) > 2: dest_dir=argv[2] if dest_dir[-1] != '/': dest_dir = dest_dir + '/' if not os.path.exists(argv[1]): print "File or directory not found: "+argv[1] return 0 print "#\ # " + argv[0] +""" generated file. Don't edit by hand!! # It will be replaced by the next """ + argv[0] + " call.\ #\ " base_dir = os.path.abspath(argv[1]) if base_dir[-1] == '/': base_dir = base_dir[:-1] walktree(base_dir, pcallthru, pmkdir, dest_dir) if __name__ == '__main__': main(sys.argv)