1 # 2 # Permission is hereby granted, free of charge, to any person obtaining a copy 3 # of this software and associated documentation files (the "Software"), to deal 4 # in the Software without restriction, including without limitation the rights 5 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 6 # copies of the Software, and to permit persons to whom the Software is 7 # furnished to do so, subject to the following conditions: 8 # 9 # The above copyright notice and this permission notice shall be included in 10 # all copies or substantial portions of the Software. 11 # 12 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 15 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 18 # THE SOFTWARE 19 # 20 # Copyright (c) 2013, Joyent Inc. All rights reserved. 21 # 22 23 ''' 24 Process our ignore/exception_list file format. 25 26 The format is broadly similar, if not identical, to .gitignore and .hgignore 27 files. 28 ''' 29 30 import re 31 import fnmatch 32 33 # 34 # It is important that this module not rely on Mercurial 35 # 36 37 def _read_ignore_file(ignorefile): 38 '''Read an ignore file and return an array of regular expressions 39 to match ignored paths.''' 40 41 syntax = 'regex' 42 ignore_list = [] 43 lc = 0 44 45 f = open(ignorefile, 'r') 46 for l in f: 47 lc += 1 48 # Remove comments and blank lines 49 l = re.sub(r'#.*', '', l).strip() 50 if l == '': 51 continue 52 # Process "syntax:" lines 53 m = re.match(r'^syntax:\s*(.*)\s*$', l) 54 if m: 55 syntax = m.group(1) 56 continue 57 # All other lines are considered patterns 58 if (syntax == 'glob'): 59 ignore_list.append(re.compile('.*' + fnmatch.translate(l))) 60 elif (syntax == 'regex'): 61 ignore_list.append(re.compile(l)) 62 else: 63 raise Exception('%s:%d: syntax "%s" is not supported' % 64 (ignorefile, lc, syntax)) 65 f.close() 66 return ignore_list 67 68 69 def ignore(root, ignorefiles): 70 # If we aren't provided any ignore files, we'll never ignore 71 # any paths: 72 if (len(ignorefiles) < 1): 73 return lambda x: False 74 75 ignore_list = [] 76 for ignorefile in ignorefiles: 77 ignore_list += _read_ignore_file(ignorefile) 78 79 # If the ignore files contained no patterns, we'll never ignore 80 # any paths: 81 if (len(ignore_list) < 1): 82 return lambda x: False 83 84 def _ignore_func(path): 85 for regex in ignore_list: 86 if (regex.match(path)): 87 return True 88 return False 89 90 return _ignore_func 91 92 # vim: set expandtab sw=4 ts=4: