Print this page
5025 import and use mandoc
Reviewed by: Hans Rosenfeld <hans.rosenfeld@nexenta.com>
Reviewed by: Igor Kozhukhov <ikozhukhov@gmail.com>
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Albert Lee <trisk@nexenta.com>
Approved by: TBD
   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)
  70 
  71     try:


 265     output.write("C style:\n")
 266     for f in flist(lambda x: x.endswith('.c') or x.endswith('.h')):
 267         fh = open(f, 'r')
 268         ret |= CStyle.cstyle(fh, output=output, picky=True,
 269                              check_posix_types=True,
 270                              check_continuation=True)
 271         fh.close()
 272     return ret
 273 
 274 
 275 def jstyle(root, parent, flist, output):
 276     ret = 0
 277     output.write("Java style:\n")
 278     for f in flist(lambda x: x.endswith('.java')):
 279         fh = open(f, 'r')
 280         ret |= JStyle.jstyle(fh, output=output, picky=True)
 281         fh.close()
 282     return ret
 283 
 284 










 285 def keywords(root, parent, flist, output):
 286     ret = 0
 287     output.write("SCCS Keywords:\n")
 288     for f in flist():
 289         fh = open(f, 'r')
 290         ret |= Keywords.keywords(fh, output=output)
 291         fh.close()
 292     return ret
 293 
 294 
 295 def run_checks(root, parent, cmds, paths='', opts={}):
 296     """Run the checks given in 'cmds', expected to have well-known signatures,
 297     and report results for any which fail.
 298 
 299     Return failure if any of them did.
 300 
 301     NB: the function name of the commands passed in is used to name the NOT
 302     file which excepts files from them."""
 303 
 304     ret = 0


 306     for cmd in cmds:
 307         s = StringIO()
 308 
 309         exclude = not_check(root, cmd.func_name)
 310         result = cmd(root, parent, gen_files(root, parent, paths, exclude),
 311                      output=s)
 312         ret |= result
 313 
 314         if result != 0:
 315             print s.getvalue()
 316 
 317     return ret
 318 
 319 
 320 def nits(root, parent, paths):
 321     cmds = [copyright,
 322             cstyle,
 323             hdrchk,
 324             jstyle,
 325             keywords,

 326             mapfilechk]
 327     run_checks(root, parent, cmds, paths)
 328 
 329 
 330 def pbchk(root, parent, paths):
 331     cmds = [comchk,
 332             copyright,
 333             cstyle,
 334             hdrchk,
 335             jstyle,
 336             keywords,

 337             mapfilechk]
 338     run_checks(root, parent, cmds)
 339 
 340 
 341 def main(cmd, args):
 342     parent_branch = None
 343 
 344     try:
 345         opts, args = getopt.getopt(args, 'b:')
 346     except getopt.GetoptError, e:
 347         sys.stderr.write(str(e) + '\n')
 348         sys.stderr.write("Usage: %s [-b branch] [path...]\n" % cmd)
 349         sys.exit(1)
 350 
 351     for opt, arg in opts:
 352         if opt == '-b':
 353             parent_branch = arg
 354 
 355     if not parent_branch:
 356         parent_branch = git_parent_branch(git_branch())
   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 2014 Garrett D'Amore <garrett@damore.org>
  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 # This is necessary because, in a fit of pique, we used hg-format ignore lists
  33 # for NOT files.
  34 from mercurial import ignore
  35 
  36 #
  37 # Adjust the load path based on our location and the version of python into
  38 # which it is being loaded.  This assumes the normal onbld directory
  39 # structure, where we are in bin/ and the modules are in
  40 # lib/python(version)?/onbld/Scm/.  If that changes so too must this.
  41 #
  42 sys.path.insert(1, os.path.join(os.path.dirname(__file__), "..", "lib",
  43                                 "python%d.%d" % sys.version_info[:2]))
  44 
  45 #
  46 # Add the relative path to usr/src/tools to the load path, such that when run
  47 # from the source tree we use the modules also within the source tree.
  48 #
  49 sys.path.insert(2, os.path.join(os.path.dirname(__file__), ".."))
  50 
  51 from onbld.Checks import Comments, Copyright, CStyle, HdrChk
  52 from onbld.Checks import JStyle, Keywords, ManLint, Mapfile
  53 
  54 
  55 class GitError(Exception):
  56     pass
  57 
  58 def git(command):
  59     """Run a command and return a stream containing its stdout (and write its
  60     stderr to its stdout)"""
  61 
  62     if type(command) != list:
  63         command = command.split()
  64 
  65     command = ["git"] + command
  66 
  67     try:
  68         tmpfile = tempfile.TemporaryFile(prefix="git-nits")
  69     except EnvironmentError, e:
  70         raise GitError("Could not create temporary file: %s\n" % e)
  71 
  72     try:


 266     output.write("C style:\n")
 267     for f in flist(lambda x: x.endswith('.c') or x.endswith('.h')):
 268         fh = open(f, 'r')
 269         ret |= CStyle.cstyle(fh, output=output, picky=True,
 270                              check_posix_types=True,
 271                              check_continuation=True)
 272         fh.close()
 273     return ret
 274 
 275 
 276 def jstyle(root, parent, flist, output):
 277     ret = 0
 278     output.write("Java style:\n")
 279     for f in flist(lambda x: x.endswith('.java')):
 280         fh = open(f, 'r')
 281         ret |= JStyle.jstyle(fh, output=output, picky=True)
 282         fh.close()
 283     return ret
 284 
 285 
 286 def manlint(root, parent, flist, output):
 287     ret = 0
 288     output.write("Man page format:\n")
 289     ManfileRE = re.compile(r'.*\.[0-9][a-z]*$', re.IGNORECASE)
 290     for f in flist(lambda x: ManfileRE.match(x)):
 291         fh = open(f, 'r')
 292         ret |= ManLint.manlint(fh, output=output, picky=True)
 293         fh.close()
 294     return ret
 295 
 296 def keywords(root, parent, flist, output):
 297     ret = 0
 298     output.write("SCCS Keywords:\n")
 299     for f in flist():
 300         fh = open(f, 'r')
 301         ret |= Keywords.keywords(fh, output=output)
 302         fh.close()
 303     return ret
 304 
 305 
 306 def run_checks(root, parent, cmds, paths='', opts={}):
 307     """Run the checks given in 'cmds', expected to have well-known signatures,
 308     and report results for any which fail.
 309 
 310     Return failure if any of them did.
 311 
 312     NB: the function name of the commands passed in is used to name the NOT
 313     file which excepts files from them."""
 314 
 315     ret = 0


 317     for cmd in cmds:
 318         s = StringIO()
 319 
 320         exclude = not_check(root, cmd.func_name)
 321         result = cmd(root, parent, gen_files(root, parent, paths, exclude),
 322                      output=s)
 323         ret |= result
 324 
 325         if result != 0:
 326             print s.getvalue()
 327 
 328     return ret
 329 
 330 
 331 def nits(root, parent, paths):
 332     cmds = [copyright,
 333             cstyle,
 334             hdrchk,
 335             jstyle,
 336             keywords,
 337             manlint,
 338             mapfilechk]
 339     run_checks(root, parent, cmds, paths)
 340 
 341 
 342 def pbchk(root, parent, paths):
 343     cmds = [comchk,
 344             copyright,
 345             cstyle,
 346             hdrchk,
 347             jstyle,
 348             keywords,
 349             manlint,
 350             mapfilechk]
 351     run_checks(root, parent, cmds)
 352 
 353 
 354 def main(cmd, args):
 355     parent_branch = None
 356 
 357     try:
 358         opts, args = getopt.getopt(args, 'b:')
 359     except getopt.GetoptError, e:
 360         sys.stderr.write(str(e) + '\n')
 361         sys.stderr.write("Usage: %s [-b branch] [path...]\n" % cmd)
 362         sys.exit(1)
 363 
 364     for opt, arg in opts:
 365         if opt == '-b':
 366             parent_branch = arg
 367 
 368     if not parent_branch:
 369         parent_branch = git_parent_branch(git_branch())