Print this page
OS-1823 git-pbchk should not depend on mercurial (rich feedback)

Split Close
Expand all
Collapse all
          --- old/usr/src/tools/onbld/Scm/Ignore.py
          +++ new/usr/src/tools/onbld/Scm/Ignore.py
↓ open down ↓ 22 lines elided ↑ open up ↑
  23   23  '''
  24   24  Process our ignore/exception_list file format.
  25   25  
  26   26  The format is broadly similar, if not identical, to .gitignore and .hgignore
  27   27  files.
  28   28  '''
  29   29  
  30   30  import re
  31   31  import fnmatch
  32   32  
       33 +RE_SYNTAX = re.compile(r'^syntax:\s*(.*)\s*$')
       34 +
  33   35  #
  34   36  # It is important that this module not rely on Mercurial
  35   37  #
  36   38  
  37   39  def _read_ignore_file(ignorefile):
  38   40      '''Read an ignore file and return an array of regular expressions
  39   41      to match ignored paths.'''
  40   42  
  41   43      syntax = 'regex'
  42   44      ignore_list = []
  43   45      lc = 0
  44   46  
  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()
       47 +    with open(ignorefile, 'r') as f:
       48 +        for l in f:
       49 +            lc += 1
       50 +            # Remove comments and blank lines
       51 +            l = l.split('#', 2)[0].strip()
       52 +            if l == '':
       53 +                continue
       54 +            # Process "syntax:" lines
       55 +            m = RE_SYNTAX.match(l)
       56 +            if m:
       57 +                syntax = m.group(1)
       58 +                continue
       59 +            # All other lines are considered patterns
       60 +            if (syntax == 'glob'):
       61 +                ignore_list.append(re.compile('.*' + fnmatch.translate(l)))
       62 +            elif (syntax == 'regex'):
       63 +                ignore_list.append(re.compile(l))
       64 +            else:
       65 +                raise Exception('%s:%d: syntax "%s" is not supported' %
       66 +                    (ignorefile, lc, syntax))
       67 +
  66   68      return ignore_list
  67   69  
  68      -
  69   70  def ignore(root, ignorefiles):
  70   71      # If we aren't provided any ignore files, we'll never ignore
  71   72      # any paths:
  72   73      if (len(ignorefiles) < 1):
  73   74          return lambda x: False
  74   75  
  75   76      ignore_list = []
  76   77      for ignorefile in ignorefiles:
  77      -        ignore_list += _read_ignore_file(ignorefile)
       78 +        ignore_list.extend(_read_ignore_file(ignorefile))
  78   79  
  79   80      # If the ignore files contained no patterns, we'll never ignore
  80   81      # any paths:
  81   82      if (len(ignore_list) < 1):
  82   83          return lambda x: False
  83   84  
  84   85      def _ignore_func(path):
  85   86          for regex in ignore_list:
  86   87              if (regex.match(path)):
  87   88                  return True
  88   89          return False
  89   90  
  90   91      return _ignore_func
  91      -
  92      -# vim: set expandtab sw=4 ts=4:
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX