Print this page
OS-1823 git-pbchk should not depend on mercurial
   1 #!/usr/bin/python2.6
   2 #
   3 #  This program is free software; you can redistribute it and/or modify
   4 #  it under the terms of the GNU General Public License version 2
   5 #  as published by the Free Software Foundation.
   6 #
   7 #  This program is distributed in the hope that it will be useful,
   8 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
   9 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10 #  GNU General Public License for more details.
  11 #
  12 #  You should have received a copy of the GNU General Public License
  13 #  along with this program; if not, write to the Free Software
  14 #  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15 #
  16 
  17 #
  18 # Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  19 # Copyright 2008, 2012 Richard Lowe

  20 #
  21 
  22 import getopt
  23 import os
  24 import re
  25 import subprocess
  26 import sys
  27 import tempfile
  28 
  29 from cStringIO import StringIO
  30 
  31 # This is necessary because, in a fit of pique, we used hg-format ignore lists
  32 # for NOT files.
  33 from mercurial import ignore
  34 
  35 #
  36 # Adjust the load path based on our location and the version of python into
  37 # which it is being loaded.  This assumes the normal onbld directory
  38 # structure, where we are in bin/ and the modules are in
  39 # lib/python(version)?/onbld/Scm/.  If that changes so too must this.
  40 #
  41 sys.path.insert(1, os.path.join(os.path.dirname(__file__), "..", "lib",
  42                                 "python%d.%d" % sys.version_info[:2]))
  43 
  44 #
  45 # Add the relative path to usr/src/tools to the load path, such that when run
  46 # from the source tree we use the modules also within the source tree.
  47 #
  48 sys.path.insert(2, os.path.join(os.path.dirname(__file__), ".."))
  49 

  50 from onbld.Checks import Comments, Copyright, CStyle, HdrChk
  51 from onbld.Checks import JStyle, Keywords, Mapfile
  52 
  53 
  54 class GitError(Exception):
  55     pass
  56 
  57 def git(command):
  58     """Run a command and return a stream containing its stdout (and write its
  59     stderr to its stdout)"""
  60 
  61     if type(command) != list:
  62         command = command.split()
  63 
  64     command = ["git"] + command
  65 
  66     try:
  67         tmpfile = tempfile.TemporaryFile(prefix="git-nits")
  68     except EnvironmentError, e:
  69         raise GitError("Could not create temporary file: %s\n" % e)


 160 
 161     if not p:
 162         sys.stderr.write("Failed building file-list from git\n")
 163         sys.exit(err)
 164 
 165     ret = set()
 166     for fname in p:
 167         if fname and not fname.isspace() and fname not in ret:
 168             ret.add(fname.strip())
 169 
 170     return ret
 171 
 172 
 173 def not_check(root, cmd):
 174     """Return a function which returns True if a file given as an argument
 175     should be excluded from the check named by 'cmd'"""
 176 
 177     ignorefiles = filter(os.path.exists,
 178                          [os.path.join(root, ".git", "%s.NOT" % cmd),
 179                           os.path.join(root, "exception_lists", cmd)])
 180     if len(ignorefiles) > 0:
 181         return ignore.ignore(root, ignorefiles, sys.stderr.write)
 182     else:
 183         return lambda x: False
 184 
 185 
 186 def gen_files(root, parent, paths, exclude):
 187     """Return a function producing file names, relative to the current
 188     directory, of any file changed on this branch (limited to 'paths' if
 189     requested), and excluding files for which exclude returns a true value """
 190 
 191     # Taken entirely from Python 2.6's os.path.relpath which we would use if we
 192     # could.
 193     def relpath(path, here):
 194         c = os.path.abspath(os.path.join(root, path)).split(os.path.sep)
 195         s = os.path.abspath(here).split(os.path.sep)
 196         l = len(os.path.commonprefix((s, c)))
 197         return os.path.join(*[os.path.pardir] * (len(s)-l) + c[l:])
 198 
 199     def ret(select=None):
 200         if not select:
 201             select = lambda x: True
 202 
 203         for f in git_file_list(parent, paths):


   1 #!/usr/bin/python2.6
   2 #
   3 #  This program is free software; you can redistribute it and/or modify
   4 #  it under the terms of the GNU General Public License version 2
   5 #  as published by the Free Software Foundation.
   6 #
   7 #  This program is distributed in the hope that it will be useful,
   8 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
   9 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10 #  GNU General Public License for more details.
  11 #
  12 #  You should have received a copy of the GNU General Public License
  13 #  along with this program; if not, write to the Free Software
  14 #  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15 #
  16 
  17 #
  18 # Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  19 # Copyright 2008, 2012 Richard Lowe
  20 # Copyright (c) 2013, Joyent Inc. All rights reserved.
  21 #
  22 
  23 import getopt
  24 import os
  25 import re
  26 import subprocess
  27 import sys
  28 import tempfile
  29 
  30 from cStringIO import StringIO
  31 




  32 #
  33 # Adjust the load path based on our location and the version of python into
  34 # which it is being loaded.  This assumes the normal onbld directory
  35 # structure, where we are in bin/ and the modules are in
  36 # lib/python(version)?/onbld/Scm/.  If that changes so too must this.
  37 #
  38 sys.path.insert(1, os.path.join(os.path.dirname(__file__), "..", "lib",
  39                                 "python%d.%d" % sys.version_info[:2]))
  40 
  41 #
  42 # Add the relative path to usr/src/tools to the load path, such that when run
  43 # from the source tree we use the modules also within the source tree.
  44 #
  45 sys.path.insert(2, os.path.join(os.path.dirname(__file__), ".."))
  46 
  47 from onbld.Scm import Ignore
  48 from onbld.Checks import Comments, Copyright, CStyle, HdrChk
  49 from onbld.Checks import JStyle, Keywords, Mapfile
  50 
  51 
  52 class GitError(Exception):
  53     pass
  54 
  55 def git(command):
  56     """Run a command and return a stream containing its stdout (and write its
  57     stderr to its stdout)"""
  58 
  59     if type(command) != list:
  60         command = command.split()
  61 
  62     command = ["git"] + command
  63 
  64     try:
  65         tmpfile = tempfile.TemporaryFile(prefix="git-nits")
  66     except EnvironmentError, e:
  67         raise GitError("Could not create temporary file: %s\n" % e)


 158 
 159     if not p:
 160         sys.stderr.write("Failed building file-list from git\n")
 161         sys.exit(err)
 162 
 163     ret = set()
 164     for fname in p:
 165         if fname and not fname.isspace() and fname not in ret:
 166             ret.add(fname.strip())
 167 
 168     return ret
 169 
 170 
 171 def not_check(root, cmd):
 172     """Return a function which returns True if a file given as an argument
 173     should be excluded from the check named by 'cmd'"""
 174 
 175     ignorefiles = filter(os.path.exists,
 176                          [os.path.join(root, ".git", "%s.NOT" % cmd),
 177                           os.path.join(root, "exception_lists", cmd)])
 178     return Ignore.ignore(root, ignorefiles)



 179 
 180 
 181 def gen_files(root, parent, paths, exclude):
 182     """Return a function producing file names, relative to the current
 183     directory, of any file changed on this branch (limited to 'paths' if
 184     requested), and excluding files for which exclude returns a true value """
 185 
 186     # Taken entirely from Python 2.6's os.path.relpath which we would use if we
 187     # could.
 188     def relpath(path, here):
 189         c = os.path.abspath(os.path.join(root, path)).split(os.path.sep)
 190         s = os.path.abspath(here).split(os.path.sep)
 191         l = len(os.path.commonprefix((s, c)))
 192         return os.path.join(*[os.path.pardir] * (len(s)-l) + c[l:])
 193 
 194     def ret(select=None):
 195         if not select:
 196             select = lambda x: True
 197 
 198         for f in git_file_list(parent, paths):