Guest User

Untitled

a guest
May 1st, 2013
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. import os
  4. import sys
  5.  
  6.  
  7. def find_match(src_path_list, files_list, name):
  8.     rv = []
  9.     for p,f in zip(src_path_list, files_list):
  10.         if name in f:
  11.             rv.append(p)
  12.     return rv
  13.  
  14. def process(dst_path, src_path_list):
  15.     # Check destination
  16.     if not os.path.exists(dst_path):
  17.         os.mkdir(dst_path)
  18.  
  19.     # File list for each sources
  20.     files_list = [set(os.listdir(x)) for x in src_path_list]
  21.  
  22.     # Find all entries and count hits
  23.     files_all = set()
  24.     for fl in files_list:
  25.         files_all.update(fl)
  26.  
  27.     # Exclude .git
  28.     files_all.discard('.git')
  29.  
  30.     # Process all entries
  31.     for n in files_all:
  32.         # Destination
  33.         dp = os.path.join(dst_path, n)
  34.  
  35.         # Find matchings path
  36.         mp = find_match(src_path_list, files_list, n)
  37.  
  38.         # Single hit -> Direct link
  39.         if len(mp) == 1:
  40.             print "Direct single : %s" % n
  41.             sp = os.path.join(mp[0],n)
  42.             os.symlink(sp, dp)
  43.  
  44.         # Multiple hit, but normal file -> Direct link
  45.         elif not os.path.isdir(os.path.join(mp[0], n)):
  46.             print "Direct file   : %s" % n
  47.             sp = os.path.join(mp[0],n)
  48.             os.symlink(sp, dp)
  49.  
  50.         # Multiple hits and directory -> Recurse
  51.         else:
  52.             print "Recurse       : %s" % n
  53.             process(dp, [os.path.join(x,n) for x in mp])
  54.  
  55.  
  56. def main(argv0, dst, *srcs):
  57.     process(
  58.         os.path.abspath(dst),
  59.         [os.path.abspath(x) for x in srcs]
  60.     )
  61.  
  62. if __name__ == '__main__':
  63.     main(*sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment