friendly-find / ffind

  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
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
#!/usr/bin/env python
# -*- coding: utf8 -*-

#    ____  ____  __  ____  __ _  ____  __    _  _     ____  __  __ _  ____
#   (  __)(  _ \(  )(  __)(  ( \(    \(  )  ( \/ )___(  __)(  )(  ( \(    \
#    ) _)  )   / )(  ) _) /    / ) D (/ (_/\ )  /(___)) _)  )( /    / ) D (
#   (__)  (__\_)(__)(____)\_)__)(____/\____/(__/     (__)  (__)\_)__)(____/
#
#                         The friendlier file finder.

import time
import os
import optparse
import string
import sys
import re
from optparse import OptionParser, OptionGroup


# Constants -------------------------------------------------------------------
CASE_SENSITIVE = 1
CASE_INSENSITIVE = 2
CASE_SMART = 3

BYTE = 1
KILOBYTE = 1024 * BYTE
MEGABYTE = 1024 * KILOBYTE
GIGABYTE = 1024 * MEGABYTE
TERABYTE = 1024 * GIGABYTE
PETABYTE = 1024 * TERABYTE

VCS_DIRS = ['.hg', '.git', '.svn']

TYPE_FILE_REAL = 1
TYPE_FILE_SYMLINK = 2
TYPE_DIR_REAL = 3
TYPE_DIR_SYMLINK = 4

TYPES_FILE_REAL = set([TYPE_FILE_REAL])
TYPES_FILE_SYMLINK = set([TYPE_FILE_SYMLINK])
TYPES_DIR_REAL = set([TYPE_DIR_REAL])
TYPES_DIR_SYMLINK = set([TYPE_DIR_SYMLINK])

TYPES_FILE = TYPES_FILE_REAL | TYPES_FILE_SYMLINK
TYPES_DIR = TYPES_DIR_REAL | TYPES_DIR_SYMLINK

TYPES_REAL = TYPES_FILE_REAL | TYPES_DIR_REAL
TYPES_SYMLINK = TYPES_FILE_SYMLINK | TYPES_DIR_SYMLINK

TYPES_ALL = TYPES_FILE | TYPES_DIR

SECOND = 1
MINUTE = 60 * SECOND
HOUR = 60 * MINUTE
DAY = 24 * HOUR
WEEK = 7 * DAY
MONTH = 30 * DAY
YEAR = int(365.2425 * DAY)

IGNORE_SYNTAX_REGEX = 1
IGNORE_SYNTAX_GLOB = 2
IGNORE_SYNTAX_LITERAL = 3

IGNORE_MODE_RESTRICTED = 1
IGNORE_MODE_SEMI = 2
IGNORE_MODE_UNRESTRICTED = 3
IGNORE_MODE_ALL = 4


# Regexes ---------------------------------------------------------------------
SIZE_RE = re.compile(r'^(\d+(?:\.\d+)?)([bkmgtp])?[a-z]*$', re.IGNORECASE)

AGO_RE = re.compile(r'''
                    (\d+(?:\.\d+)?)           # The number (float/int)
                    \s*                       # Optional whitespace
                    (                         # Units
                        y(?:ears?)?           # y/year/years
                      | mos?(?:nths?)?        # mo/mos/month/months
                      | w(?:eeks?)?           # w/week/weeks
                      | d(?:ays?)?            # d/day/days
                      | h(?:ours?)?           # h/hour/hours
                      | m(?:ins?(?:utes?)?)?  # m/min/mins/minute/minutes
                      | s(?:ecs?(?:onds?)?)?  # s/sec/secs/second/seconds
                    )
                    ''', re.VERBOSE | re.IGNORECASE)

IGNORE_SYNTAX_RE = re.compile(r'^\s*syntax:\s*(glob|regexp|regex|re|literal)\s*$',
                              re.IGNORECASE)
IGNORE_COMMENT_RE = re.compile(r'^\s*#')
IGNORE_BLANK_RE = re.compile(r'^\s*$')

GITIGNORE_COMMENT_RE = re.compile(r'^\s*#')
GITIGNORE_BLANK_RE = re.compile(r'^\s*$')
GITIGNORE_NEGATE_RE = re.compile(r'^\s*!')

HGIGNORE_SYNTAX_RE = re.compile(r'^\s*syntax:\s*(glob|regexp|re)\s*$',
                                re.IGNORECASE)
HGIGNORE_COMMENT_RE = re.compile(r'^\s*#')
HGIGNORE_BLANK_RE = re.compile(r'^\s*$')


# Global Options --------------------------------------------------------------
# (it's a prototype, shut up)
options = None


# Output ----------------------------------------------------------------------
def out(s, line_ending='\n'):
    sys.stdout.write(s + line_ending)

def err(s):
    sys.stderr.write(s + '\n')

def die(s, exitcode=1):
    err('error: ' + s)
    sys.exit(exitcode)

def warn(s):
    sys.stderr.write('warning: ' + s + '\n')


# Ingore Files ----------------------------------------------------------------
def compile_re(line):
    try:
        r = re.compile(line)
        return lambda s: r.search(s)
    except:
        warn('could not compile regular expression "%s"' % line)
        return lambda s: False

def glob_to_re(glob):
    pat = ''

    chs = list(glob)
    while chs:
        ch = chs.pop(0)
        if ch == '\\':
            pat += re.escape(chs.pop(0))
        elif ch == '?':
            pat += '.'
        elif ch == '*':
            if chs and chs[0] == '*':
                chs.pop(0)
                pat += '.*'
            else:
                pat += '[^/]*'
        elif ch == '[':
            pat += '['
            ch = chs.pop(0)
            while chs and ch != ']':
                pat += ch
                ch = chs.pop(0)
            pat += ']'
        else:
            pat += re.escape(ch)

    return pat

def compile_literal(line):
    l = line
    return lambda s: l in s

def compile_git(line):
    original_line = line
    pat = ''

    # From man gitignore 5:
    #     If the pattern ends with a slash, it is removed for the purpose of the
    #     following description, but it would only find a match with
    #     a directory. In other words, foo/ will match a directory foo and paths
    #     underneath it, but will not match a regular file or a symbolic link
    #     foo (this is consistent with the way how pathspec works in general in
    #     git).
    #
    #     A leading slash matches the beginning of the pathname. For example,
    #     "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".
    #
    #     If the pattern does not contain a slash /, git treats it as a shell
    #     glob pattern and checks for a match against the pathname relative to
    #     the location of the .gitignore file (relative to the toplevel of the
    #     work tree if not from a .gitignore file).
    #
    #     Otherwise, git treats the pattern as a shell glob suitable for
    #     consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the
    #     pattern will not match a / in the pathname. For example,
    #     "Documentation/*.html" matches "Documentation/git.html" but not
    #     "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html".
    #
    # If you can't tell what the hell this means you're not alone, because git's
    # documentation is fucking inscrutable.  Here's what I've come up with from
    # trial and error:
    # 
    # 0. Patterns ending in a slash will only match directories, and then you
    #    can ignore that slash for the rest of these rules.
    # 1. Patterns are shell globs, except * doesn't match / and there's no **.
    # 2. Patterns without a slash search the basename of the path, for example:
    #    the 'file.txt' in '/foo/bar/file.txt'.
    # 3. Patterns with a slash search against the entire path.
    # 4. All matching must match the entire string it's searching.  For example:
    #
    #    'am' will not ignore '/foo/bar/spam'
    #    it matches against the basename 'spam' but does not match all of it
    #
    #    'bar/spam' will not ignore '/foo/bar/spam'
    #    it matches against the full path (because it has a slash) but does not
    #    match all of it.
    # 5. A leading slash doesn't affect the matching, but does turn a
    #    "pattern with no slash" into a "pattern with a slash".  So:
    #
    #    'bar' will ignore '/foo/bar/spam' (actually it'll ignore bar entirely)
    #    it matches against the basename 'bar' (because there's no slash) when
    #    at that level
    #
    #    '/bar' will not ignore '/foo/bar/spam'
    #    it matches against the entire path '/foo/bar' (because there is
    #    a slash) when at that level

    if line.endswith('/'):
        # TODO: Deal with this.
        # directories_only = True
        line = line[:-1]

    has_slash = '/' in line

    line = line.lstrip('/')

    if has_slash:
        # Patterns with a slash have to match against the entire pathname.  So
        # they need to be rooted at the beginning.
        pat += '^./'
    else:
        # Patterns without a slash match against just the basename, which we'll
        # simulate by including the (final) divider in the pattern.
        pat += '/'

    # The rest of the pattern is git's variation on shell globs.
    # Mostly normal shell globs, but there's no **.
    chs = list(line)
    while chs:
        ch = chs.pop(0)
        if ch == '?':
            pat += '.'
        elif ch == '*':
            pat += '[^/]*'
        elif ch == '[':
            pat += '['
            ch = chs.pop(0)
            while chs and ch != ']':
                pat += ch
                ch = chs.pop(0)
            pat += ']'
        else:
            pat += re.escape(ch)

    # Patterns always have the be anchored at the end.
    pat += '$'

    try:
        return compile_re(pat)
    except:
        warn("could not parse gitignore pattern '%s'" % original_line)
        return lambda s: True

def compile_hg_glob(line):
    pat = glob_to_re(line)

    # Mercurial ignore globs are quasi-rooted at directory boundaries or the
    # beginning of the pattern.
    pat = '(^|/)' + pat

    # Mercurial globs also have to match to the end of the pattern.
    pat = pat + '$'

    try:
        regex = re.compile(pat)
        return lambda s: regex.search(s[2:] if s.startswith('./') else s)
    except:
        warn("could not parse hgignore pattern '%s'" % line)
        return lambda s: True

def compile_ff_glob(line):
    pat = glob_to_re(line)

    try:
        return compile_re(pat)
    except:
        warn("could not parse ffignore pattern '%s'" % line)
        return lambda s: True


def parse_gitignore_file(path):
    if not os.path.isfile(path):
        return []

    ignorers = []
    with open(path) as f:
        for line in f.readlines():
            line = line.rstrip('\n')
            if GITIGNORE_BLANK_RE.match(line):
                continue
            elif GITIGNORE_COMMENT_RE.match(line):
                continue
            elif GITIGNORE_NEGATE_RE.match(line):
                # TODO: This bullshit feature.
                continue
            else:
                # This line is a gitignore pattern.
                ignorers.append(compile_git(line))

    return ignorers

def parse_hgignore_file(path):
    if not os.path.isfile(path):
        return []

    syntax = IGNORE_SYNTAX_REGEX
    ignorers = []
    with open(path) as f:
        for line in f.readlines():
            line = line.rstrip('\n')
            if HGIGNORE_BLANK_RE.match(line):
                continue
            elif HGIGNORE_COMMENT_RE.match(line):
                continue
            elif HGIGNORE_SYNTAX_RE.match(line):
                s = HGIGNORE_SYNTAX_RE.match(line).groups()[0].lower()
                if s == 'glob':
                    syntax = IGNORE_SYNTAX_GLOB
                elif s in ['re', 'regexp']:
                    syntax = IGNORE_SYNTAX_REGEX
            else:
                # This line is a pattern.
                if syntax == IGNORE_SYNTAX_REGEX:
                    ignorers.append(compile_re(line))
                elif syntax == IGNORE_SYNTAX_GLOB:
                    ignorers.append(compile_hg_glob(line))

    return ignorers

def parse_ffignore_file(path):
    if not os.path.isfile(path):
        return []

    syntax = IGNORE_SYNTAX_REGEX
    ignorers = []
    with open(path) as f:
        for line in f.readlines():
            line = line.rstrip('\n')
            if IGNORE_BLANK_RE.match(line):
                continue
            elif IGNORE_COMMENT_RE.match(line):
                continue
            elif IGNORE_SYNTAX_RE.match(line):
                s = IGNORE_SYNTAX_RE.match(line).groups()[0].lower()
                if s == 'literal':
                    syntax = IGNORE_SYNTAX_LITERAL
                elif s == 'glob':
                    syntax = IGNORE_SYNTAX_GLOB
                elif s in ['re', 'regex', 'regexp']:
                    syntax = IGNORE_SYNTAX_REGEX
            else:
                # This line is a pattern.
                if syntax == IGNORE_SYNTAX_LITERAL:
                    ignorers.append(compile_literal(line))
                elif syntax == IGNORE_SYNTAX_REGEX:
                    ignorers.append(compile_re(line))
                elif syntax == IGNORE_SYNTAX_GLOB:
                    ignorers.append(compile_ff_glob(line))

    return ignorers

def parse_ignore_files(dir):
    ignorers = []
    for filename in options.ignore_files:
        target = os.path.join(dir, filename)
        if filename == '.ffignore':
            ignorers.extend(parse_ffignore_file(target))
        elif filename == '.gitignore':
            ignorers.extend(parse_gitignore_file(target))
        elif filename == '.hgignore':
            ignorers.extend(parse_hgignore_file(target))
    return ignorers


def get_initial_ignorers():
    if '.ffignore' in options.ignore_files:
        home = os.environ.get('HOME')
        if home:
            return parse_ffignore_file(os.path.join(home, '.ffignore'))
        else:
            return []
    else:
        return []


# Searching! ------------------------------------------------------------------
def get_type(path):
    link = os.path.islink(path)
    dir = os.path.isdir(path)

    if link and dir:
        return TYPE_DIR_SYMLINK
    elif link and not dir:
        return TYPE_FILE_SYMLINK
    elif not link and dir:
        return TYPE_DIR_REAL
    elif not link and not dir:
        return TYPE_FILE_REAL

def should_ignore(basename, path, ignorers):
    if options.ignore_vcs_dirs and basename in VCS_DIRS:
        return True

    for i in ignorers:
        if i(path):
            return True

    return False

def match(query, path, basename):
    def _match():
        if options.type != TYPES_ALL:
            if get_type(path) not in options.type:
                return False

        if not query(path if options.entire else basename):
            return False

        stat = os.lstat(path)
        if options.larger_than:
            if stat.st_size < options.larger_than:
                return False

        if options.smaller_than:
            if stat.st_size > options.smaller_than:
                return False

        if options.before:
            if stat.st_mtime > options.before:
                return False

        if options.after:
            if stat.st_mtime < options.after:
                return False

        if not options.binary:
            # We open in non-blocking mode so things like file-based sockets
            # don't hang while waiting for their full kb.
            # TODO: Ignore those altogether for the binary check?
            fd = os.open(path, os.O_NONBLOCK)
            with os.fdopen(fd) as f:
                if '\0' in f.read(1024):
                    return False

        return True


    result = _match()
    return not result if options.invert else result


def _search(query, dir, depth, ignorers):
    ignorers = ignorers + parse_ignore_files(dir)

    contents = os.listdir(dir)
    next = []

    for item in contents:
        path = os.path.join(dir, item)
        if not should_ignore(item, path, ignorers):
            if match(query, path, item):
                out(path, '\0' if options.zero else '\n')

            is_dir = os.path.isdir(path)
            if is_dir:
                if options.follow or not os.path.islink(path):
                    next.append(path)


    if depth < options.depth:
        for d in next:
            _search(query, d, depth + 1, ignorers)

def search(query, dir='.', depth=0, ignorers=None):
    _search(query, '.', 0, get_initial_ignorers())


# Option Parsing and Main -----------------------------------------------------
def build_option_parser():
    p = OptionParser("usage: %prog [options] PATTERN")

    # Main options
    p.add_option('-d', '--dir', default='.',
                 help='root the search in DIR (default .)',
                 metavar='DIR')
    p.add_option('-D', '--depth', default='25',
                 help='search at most N directories deep (default 25)',
                 metavar='N')
    p.add_option('-f', '--follow',
                 action='store_true', default=False,
                 help='follow symlinked directories and search their contents')
    p.add_option('-F', '--no-follow',
                 dest='follow', action='store_false',
                 help="don't follow symlinked directories (default)")
    p.add_option('-0', '--print0', dest='zero',
                 action='store_true', default=False,
                 help='separate matches with a null byte in output')
    p.add_option('-l', '--literal',
                 action='store_true', default=False,
                 help='force literal search, even if it looks like a regex')
    p.add_option('-v', '--invert',
                 action='store_true', default=False,
                 help='invert match')
    p.add_option('-e', '--entire',
                 action='store_true', default=False,
                 help='match PATTERN against the entire path string')
    p.add_option('-E', '--non-entire', dest='entire',
                 action='store_false',
                 help='match PATTERN against only the filenames (default)')

    # Case sensitivity
    g = OptionGroup(p, "Configuring Case Sensitivity")
    g.add_option('-s', '--case-sensitive',
                 dest='case', action='store_const', const=CASE_SENSITIVE,
                 default=CASE_SENSITIVE,
                 help='case sensitive matching (default)')
    g.add_option('-i', '--case-insensitive',
                 dest='case', action='store_const', const=CASE_INSENSITIVE,
                 help='case insensitive matching')
    g.add_option('-S', '--case-smart',
                 dest='case', action='store_const', const=CASE_SMART,
                 help='smart case matching (sensitive if any uppercase chars '
                      'are in the pattern, insensitive otherwise)')
    p.add_option_group(g)

    # Ignoring
    g = OptionGroup(p, "Configuring Ignoring")

    g.add_option('-b', '--binary',
                 dest='binary', action='store_true', default=True,
                 help="allow binary files (default)")

    g.add_option('-B', '--no-binary',
                 dest='binary', action='store_false',
                 help='ignore binary files')

    g.add_option('-r', '--restricted', dest='ignore_mode',
                 action='store_const', const=IGNORE_MODE_RESTRICTED,
                 default=IGNORE_MODE_RESTRICTED,
                 help="restricted search (skip VCS directories, "
                      "parse all ignore files) (default)")

    g.add_option('-q', '--semi-restricted', dest='ignore_mode',
                 action='store_const', const=IGNORE_MODE_SEMI,
                 help="semi-restricted search (don't parse VCS ignore files, "
                      "but still skip VCS directories and parse .ffignore)")

    g.add_option('-u', '--unrestricted', dest='ignore_mode',
                 action='store_const', const=IGNORE_MODE_UNRESTRICTED,
                 help="unrestricted search (don't parse ignore files, but "
                      "still skip VCS directories)")

    g.add_option('-a', '--all', dest='ignore_mode',
                 action='store_const', const=IGNORE_MODE_ALL,
                 help="don't ignore anything (ALL files can match)")

    g.add_option('-I', '--ignore', metavar='PATTERN',
                 action='append',
                 help="add a pattern to be ignored (can be given multiple times)")

    p.add_option_group(g)

    # Time filtering
    g = OptionGroup(p, "Time Filtering")
    g.add_option('--before',
                 help='match files modified < TIME',
                 metavar='TIME')
    g.add_option('--after',
                 help='match files modified > TIME',
                 metavar='TIME')
    g.add_option('--until',
                 help='match files modified <= TIME',
                 metavar='TIME')
    g.add_option('--since',
                 help='match files modified >= TIME',
                 metavar='TIME')
    g.add_option('--at',
                 help='match files modified at TIME',
                 metavar='TIME')
    g.add_option('--created-before',
                 help='match files created < TIME',
                 metavar='TIME')
    g.add_option('--created-after',
                 help='match files created > TIME',
                 metavar='TIME')
    g.add_option('--created-until',
                 help='match files created <= TIME',
                 metavar='TIME')
    g.add_option('--created-since',
                 help='match files created >= TIME',
                 metavar='TIME')
    g.add_option('--created-at',
                 help='match files created at TIME',
                 metavar='TIME')
    # TODO
    # p.add_option_group(g)

    # Size filtering
    g = OptionGroup(p, "Size Filtering",
                    "Sizes can be given as a number followed by a prefix.  Some examples: "
                    "1k, 5kb, 1.5gb, 2g, 1024b")
    g.add_option('--larger-than',
                 help='match files larger than SIZE (inclusive)',
                 metavar='SIZE')
    g.add_option('--bigger-than', dest='larger_than',
                 help=optparse.SUPPRESS_HELP)
    g.add_option('--smaller-than',
                 help='match files smaller than SIZE (inclusive)',
                 metavar='SIZE')
    p.add_option_group(g)

    # Type filtering
    g = OptionGroup(p, "Type Filtering",
                    "Possible types are "
                    "a (all), "
                    "f (files), "
                    "d (dirs), "
                    "r (real), "
                    "s (symlinked), "
                    "e (real files), "
                    "c (real dirs), "
                    "x (symlinked files), "
                    "y (symlinked dirs). "
                    "If multiple types are given they will be unioned together:  "
                    "--type 'es' would match real files and all symlinks.")
    g.add_option('-t', '--type',
                 action='store', default=False, metavar='TYPE(S)',
                 help='match only specific types of things (files, dirs, non-symlinks, symlinks)')
    p.add_option_group(g)

    return p

def build_type_set(types):
    if not types:
        return TYPES_ALL

    result = set()
    for c in types:
        result = result | {
            'a': TYPES_ALL,

            'e': TYPES_FILE_REAL,
            'x': TYPES_FILE_SYMLINK,
            'c': TYPES_DIR_REAL,
            'y': TYPES_DIR_SYMLINK,

            'f': TYPES_FILE,
            'd': TYPES_DIR,

            'r': TYPES_REAL,
            's': TYPES_SYMLINK,
        }[c.lower()]

    return result

def parse_size(size):
    size = size.replace(' ', '') if size else size

    if not size:
        return None

    m = SIZE_RE.match(size)
    if not m:
        die('invalid size "%s"' % size)

    n, unit = m.groups()

    try:
        n = float(n)
    except ValueError:
        die('invalid size "%s"' % size)

    unit = {
        'b': BYTE,
        'k': KILOBYTE,
        'm': MEGABYTE,
        'g': GIGABYTE,
        't': TERABYTE,
        'p': PETABYTE,
    }[unit or 'b']

    return int(n * unit)

def is_re(s):
    """Try to guess if the string is a regex.

    Err on the side of "True", because treating a literal like a regex only
    slows you down a bit, but the other way around is broken behaviour.

    """

    return not all(c.lower() in string.letters + '_-' for c in s)


def clean_ago_piece(n, unit):
    n = float(n)

    if unit in ['s', 'sec', 'secs', 'second', 'seconds']:
        unit = SECOND
    if unit in ['m', 'min', 'mins', 'minute', 'minutes']:
        unit = MINUTE
    if unit in ['h', 'hour', 'hours']:
        unit = HOUR
    if unit in ['d', 'day', 'days']:
        unit = DAY
    if unit in ['w', 'week', 'weeks']:
        unit = WEEK
    if unit in ['mo', 'mos', 'month', 'months']:
        unit = MONTH
    if unit in ['y', 'year', 'years']:
        unit = YEAR

    return n, unit

def parse_ago(start_time, timestr):
    pieces = AGO_RE.findall(timestr)

    units = set()
    result = start_time

    for piece in pieces:
        n, unit = clean_ago_piece(*piece)

        if unit in units:
            die('duplicate "%s" in time specification' % unit)

        units.add(unit)
        result -= n * unit

    return int(result)

def parse_time(timestr):
    """Parse a time string into milliseconds past the epoch."""
    start_time = int(time.time())

    timestr = timestr.strip().lower()

    if AGO_RE.match(timestr):
        return parse_ago(start_time, timestr)

    return None


def main():
    global options

    (options, args) = build_option_parser().parse_args()

    # PATTERN
    if len(args) > 1:
        die("only one search pattern can be given")
        sys.exit(1)

    query = args[0] if args else ''

    # --dir
    if options.dir:
        try:
            os.chdir(options.dir)
        except OSError:
            die('could not change to directory "%s"' % options.dir)

    # --depth
    try:
        options.depth = int(options.depth)
    except ValueError:
        die('depth must be a non-negative integer (got "%s")' % options.depth)

    # --case-*
    if options.case == CASE_SMART:
        if any(c in string.uppercase for c in query):
            options.case = CASE_SENSITIVE
        else:
            options.case = CASE_INSENSITIVE

    # --type
    options.type = build_type_set(options.type)

    # --larger-than, --smaller-than
    options.larger_than = parse_size(options.larger_than)
    options.smaller_than = parse_size(options.smaller_than)

    if options.larger_than or options.smaller_than:
        # Directory sizes are not supported.
        options.type = options.type - TYPES_DIR

    # time filtering
    if options.before:
        options.before = parse_time(options.before)

    if options.after:
        options.after = parse_time(options.after)

    # Ignore files
    if options.ignore_mode == IGNORE_MODE_RESTRICTED:
        options.ignore_files = ['.ffignore', '.gitignore', '.hgignore']
        options.ignore_vcs_dirs = True
    elif options.ignore_mode == IGNORE_MODE_SEMI:
        options.ignore_files = ['.ffignore']
        options.ignore_vcs_dirs = True
    elif options.ignore_mode == IGNORE_MODE_UNRESTRICTED:
        options.ignore_files = []
        options.ignore_vcs_dirs = True
    elif options.ignore_mode == IGNORE_MODE_ALL:
        options.ignore_files = []
        options.ignore_vcs_dirs = False

    # Build the query matcher.
    if options.literal or not is_re(query):
        if options.case == CASE_SENSITIVE:
            literal = query
            query = lambda s: literal in s
        else:
            literal = query.lower()
            query = lambda s: literal in s.lower()
    else:
        if options.case == CASE_SENSITIVE:
            r = re.compile(query)
        else:
            r = re.compile(query, re.IGNORECASE)
        query = lambda s: r.search(s)

    # Go!
    search(query)


if __name__ == '__main__':
    import signal
    def sigint_handler(signal, frame):
        sys.stdout.write('\n')
        sys.exit(130)
    signal.signal(signal.SIGINT, sigint_handler)
    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 ProjectModifiedEvent.java.
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.