-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyignore.py
57 lines (46 loc) · 1.53 KB
/
pyignore.py
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
#!/usr/bin/env python
# coding: utf-8
#
__version__ = '0.1'
import fnmatch
import os
class Ignore(object):
def __init__(self, filename_or_lines):
if isinstance(filename_or_lines, basestring):
with open(filename_or_lines) as file:
lines = file.readlines()
else:
assert isinstance(filename_or_lines, list)
lines = filename_or_lines
self._pats = []
for line in lines:
line = line.strip()
if not line or line.startswith('#'):
continue
self._pats.append(line)
def __str__(self):
return 'ignore: ' + str(self._pats)
def match(self, name):
base = os.path.basename(name)
for pat in self._pats:
if fnmatch.fnmatch(name, pat):
return True
if base and fnmatch.fnmatch(base, pat):
return True
if name.endswith('/'+pat):
return True
def filter(self, names):
''' Return the subset of the list NAMES that match PAT '''
return [n for n in names if self.match(n)]
def exclude(self, names):
return [n for n in names if not self.match(n)]
if __name__ == '__main__':
# test
ign = Ignore(['foo/', '/*/*.pyc', '*.txt'])
print ign
names = ['/log/foo/', '/foo', '/bar/', '/x.pyc', '/foo/a.pyc', '/foo/a.py', '/foo/a.txt']
print 'names', names
print ign.exclude(names)
import os
ign = Ignore('.gitignore')
print '\n'.join(ign.exclude(os.listdir('.')))