1#!/usr/bin/env python3
2
3from __future__ import generators
4import sys
5import os
6import stat
7
8
9def usage():
10    print "Usage: find2dirs dir1 dir2"
11    print
12    print "Given the name of two directories, list all the files in both, one"
13    print "per line, but don't repeat a file even if it is in both directories"
14    sys.exit(1)
15
16
17def getlist(base, ext=""):
18    """Return iterator yielding filenames from directory"""
19    if ext: yield ext
20    else: yield "."
21
22    fullname = os.path.join(base, ext)
23    if stat.S_ISDIR(stat.S_IFMT(os.lstat(fullname)[stat.ST_MODE])):
24        for subfile in os.listdir(fullname):
25            for fn in getlist(base, os.path.join(ext, subfile)):
26                yield fn
27
28
29def main(dir1, dir2):
30    d = {}
31    for fn in getlist(dir1):
32        d[fn] = 1
33    for fn in getlist(dir2):
34        d[fn] = 1
35    for fn in d.keys():
36        print fn
37
38
39if not len(sys.argv) == 3: usage()
40else: main(sys.argv[1], sys.argv[2])
41