File: find2dirs

package info (click to toggle)
rdiff-backup 2.2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,640 kB
  • sloc: python: 24,129; javascript: 9,512; sh: 1,230; ansic: 580; makefile: 36
file content (40 lines) | stat: -rwxr-xr-x 948 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3

from __future__ import generators
import sys
import os
import stat


def usage():
    print "Usage: find2dirs dir1 dir2"
    print
    print "Given the name of two directories, list all the files in both, one"
    print "per line, but don't repeat a file even if it is in both directories"
    sys.exit(1)


def getlist(base, ext=""):
    """Return iterator yielding filenames from directory"""
    if ext: yield ext
    else: yield "."

    fullname = os.path.join(base, ext)
    if stat.S_ISDIR(stat.S_IFMT(os.lstat(fullname)[stat.ST_MODE])):
        for subfile in os.listdir(fullname):
            for fn in getlist(base, os.path.join(ext, subfile)):
                yield fn


def main(dir1, dir2):
    d = {}
    for fn in getlist(dir1):
        d[fn] = 1
    for fn in getlist(dir2):
        d[fn] = 1
    for fn in d.keys():
        print fn


if not len(sys.argv) == 3: usage()
else: main(sys.argv[1], sys.argv[2])