Completed
Push — master ( 5f68ae...c3dae1 )
by Bjorn
01:12
created

pfind()   C

Complexity

Conditions 7

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
c 1
b 0
f 0
dl 0
loc 22
rs 5.7894

1 Method

Rating   Name   Duplication   Size   Complexity  
A parents() 0 8 3
1
#!/usr/bin/python
2
"""CLI usage: ``pfind path filename`` will find the closest ancestor directory
3
   conataining filename (used for finding syncspec.txt and config files).
4
"""
5
import os
6
import sys
7
8
9
def pfind(path, fname):
10
    """Find fname in the closest ancestor directory.
11
       For the purposes of this function, we are our own closest ancestor.
12
    """
13
14
    wd = os.path.abspath(path)
15
    assert os.path.isdir(wd)
16
17
    def parents():
18
        parent = wd
19
        yield parent
20
        while 1:
21
            parent, dirname = os.path.split(parent)
22
            if not dirname:
23
                return
24
            yield parent
25
26
    for d in parents():
27
        if fname in os.listdir(d):
28
            return os.path.join(d, fname)
29
30
    return None
31
32
33
if __name__ == "__main__":  # pragma: nocover
34
    _path, filename = sys.argv[1], sys.argv[2]
35
    print pfind(_path, filename)
36