pythonwise / pfilter

#!/usr/bin/env python
'''Path filter, to be used in pipes to filter out paths.

* Unix test commands (such as -f can be specified as well)
* {} replaces file name

    # List only files in current directory
    ls -a | pfilter -f

    # Find files not versioned in svn 
    # (why, oh why, does svn *always* return 0?)
    find . | pfilter 'test -n "`svn info {} 2>&1 | grep Not`"'

__author__ = "Miki Tebeka <>"

from os import system

def pfilter(path, command):
    '''Filter path according to command'''

    if "{}" in command:
        command = command.replace("{}", path)
        command = "%s %s" % (command, path)

    if command.startswith("-"):
        command = "test %s" %  command

    # FIXME: win32 support
    command += " 2>&1 > /dev/null"

    return system(command) == 0

def main(argv=None):
    if argv is None:
        import sys
        argv = sys.argv

    from sys import stdin
    from itertools import imap, ifilter
    from string import strip
    from functools import partial

    if len(argv) != 2:
        from os.path import basename
        from sys import stderr
        print >> stderr, "usage: %s COMMAND" % basename(argv[0])
        print >> stderr
        print >> stderr, __doc__
        raise SystemExit(1)

    command = argv[1]
    # Don't you love functional programming?
    for path in ifilter(partial(pfilter, command=command), imap(strip, stdin)):
        print path

if __name__ == "__main__":
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.