~boucft/boucft/hardy

« back to all changes in this revision

Viewing changes to boucft/scripts/symlink-dupes

  • Committer: duanedesign
  • Date: 2009-04-21 03:02:36 UTC
  • Revision ID: duanedesign@gmail.com-20090421030236-pgr05gic2j7xviml
initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
import os, subprocess
 
4
 
 
5
def pathsplit(p, rest=[]):
 
6
    (h,t) = os.path.split(p)
 
7
    if len(h) < 1: return [t]+rest
 
8
    if len(t) < 1: return [h]+rest
 
9
    return pathsplit(h,[t]+rest)
 
10
 
 
11
def commonpath(l1, l2, common=[]):
 
12
    if len(l1) < 1: return (common, l1, l2)
 
13
    if len(l2) < 1: return (common, l1, l2)
 
14
    if l1[0] != l2[0]: return (common, l1, l2)
 
15
    return commonpath(l1[1:], l2[1:], common+[l1[0]])
 
16
 
 
17
def relpath(p1, p2):
 
18
    (common,l1,l2) = commonpath(pathsplit(p1), pathsplit(p2))
 
19
    p = []
 
20
    if len(l1) > 0:
 
21
        p = [ '../' * len(l1) ]
 
22
    p = p + l2
 
23
    return os.path.join( *p )
 
24
 
 
25
fdupes = subprocess.Popen(['fdupes', '-r', '--sameline', '.'], stdout=subprocess.PIPE)
 
26
for l in fdupes.stdout:
 
27
    files = l.split()
 
28
    master = files.pop(0)
 
29
    for dup in files:
 
30
        target = relpath(os.path.dirname(dup), master)
 
31
        os.unlink(dup)
 
32
        os.symlink(target, dup)
 
33
        print dup, '->', target