Guest User

Untitled

a guest
May 1st, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  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.     # File list for each sources
  16.     files_list = [set(os.listdir(x)) for x in src_path_list]
  17.  
  18.     # Find all entries and count hits
  19.     files_all = set()
  20.     for fl in files_list:
  21.         files_all.update(fl)
  22.  
  23.     # Exclude .git
  24.     files_all.discard('.git')
  25.  
  26.     # Process all entries
  27.     for n in files_all:
  28.         # Destination
  29.         dp = os.path.join(dst_path, n)
  30.  
  31.         # Find matchings path
  32.         mp = find_match(src_path_list, files_list, n)
  33.  
  34.         # Single hit -> Direct link
  35.         if len(mp) == 1:
  36.             print "Direct single : %s" % n
  37.             sp = os.path.join(mp[0],n)
  38.             os.symlink(sp, dp)
  39.  
  40.         # Multiple hit, but normal file -> Direct link
  41.         elif not os.path.isdir(os.path.join(mp[0], n)):
  42.             print "Direct file   : %s" % n
  43.             sp = os.path.join(mp[0],n)
  44.             os.symlink(sp, dp)
  45.  
  46.         # Multiple hits and directory -> Recurse
  47.         else:
  48.             print "Recurse       : %s" % n
  49.             if not os.path.exists(dp):
  50.                 os.mkdir(dp)
  51.             process(dp, [os.path.join(x,n) for x in mp])
  52.  
  53.  
  54. def main(argv0, dst, *srcs):
  55.     process(
  56.         os.path.abspath(dst),
  57.         [os.path.abspath(x) for x in srcs]
  58.     )
  59.  
  60. if __name__ == '__main__':
  61.     main(*sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment