code: def main(argv=None):
      if argv is None:
          argv = sys.argv[1:]
  
      parser = argparse.ArgumentParser(
          description="Pretxncommit hook for Mercurial to check for whitespace issues"
      )
      parser.add_argument(
          "-n",
          "--no-indentation",
          action="store_const",
          default=False,
          const=True,
          help="don't check indentation, just basic parsing",
      )
      parser.add_argument(
          "-i",
          "--incremental",
          action="store_const",
          default=False,
          const=True,
          help="only block on newly introduced indentation problems; ignore all others",
      )
      parser.add_argument(
          "-p",
          "--incremental-with-patch",
          action="store_const",
          default=False,
          const=True,
          help="only block on newly introduced indentation problems; propose a patch for all others",
      )
      parser.add_argument(
          "-s",
          "--skip-after-failure",
          action="store_const",
          default=False,
          const=True,
          help="when this pre-commit hook fails, don't run it on the next commit; "
          "this lets you check in your changes and then check in "
          "any necessary whitespace changes in the subsequent commit",
      )
      args = parser.parse_args(argv)
  
      # -i and -s are incompatible; if you skip checking, you end up with a not-correctly-indented
      # file, which -i then causes you to ignore!
      if args.skip_after_failure and args.incremental:
          print(
              "*** check whitespace hook misconfigured! -i and -s are incompatible.",
              file=sys.stderr,
          )
          return 1
  
      if is_merge():
          # don't inspect merges: (a) they're complex and (b) they don't really introduce new code
          return 0
  
      if args.skip_after_failure and should_skip_commit():
          # we're set up to skip this one, so skip it, but
          # first, make sure we don't skip the next one as well :)
          os.remove(SKIP_WHITESPACE_CHECK_FILENAME)
          return 0
  
      block_commit = False
  
      diffs = []
  
      added_filenames = added_files()
      changed_filenames = changed_files()
  
      for filename in filter(
          is_python_file, added_filenames + changed_filenames
      ):
          code = get_file_contents(filename)
          parse_error = get_parse_error(code)
          if parse_error is not None:
              print(
                  "*** %s has parse error: %s"
                  % (filename, parse_error),
                  file=sys.stderr,
              )
              block_commit = True
          else:
              # parsing succeeded, it is safe to check indentation
              if not args.no_indentation:
                  was_clean = None  # unknown
                  # only calculate was_clean if it will matter to us
                  if args.incremental or args.incremental_with_patch:
                      if filename in changed_filenames:
                          old_file_contents = get_file_contents(
                              filename, revision=parent_commit()
                          )
                          was_clean = (
                              get_correct_indentation_diff(
                                  old_file_contents, ""
                              )
                              is None
                          )
                      else:
                          was_clean = True  # by default -- it was newly added and thus had no prior problems
  
                  check_indentation = was_clean or not args.incremental
                  if check_indentation:
                      indentation_diff = get_correct_indentation_diff(
                          code, filename
                      )
                      if indentation_diff is not None:
                          if (
                              was_clean
                              or not args.incremental_with_patch
                          ):
                              block_commit = True
                          diffs.append(indentation_diff)
                          print(
                              "%s is not correctly indented" % filename,
                              file=sys.stderr,
                          )
  
      if len(diffs) > 0:
          diffs_filename = ".hg/indentation_fixes.patch"
          save_diffs(diffs, diffs_filename)
          print(
              "*** To fix all indentation issues, run: cd `hg root` && patch -p0 < %s"
              % diffs_filename,
              file=sys.stderr,
          )
  
      if block_commit:
          save_filename = ".hg/commit_message.saved"
          save_commit_message(save_filename)
          print(
              "*** Commit message saved to %s" % save_filename,
              file=sys.stderr,
          )
  
          if args.skip_after_failure:
              save_skip_next_commit()
              print(
                  "*** Next commit attempt will not be checked. To change this, rm %s"
                  % SKIP_WHITESPACE_CHECK_FILENAME,
                  file=sys.stderr,
              )
  
      return int(block_commit)
  
imports: from __future__ import absolute_import, print_function, division
  import difflib
  import operator
  import os
  import string
  from subprocess import Popen, PIPE
  import sys
  import tabnanny
  import tokenize
  import reindent
  from six import StringIO
label_desc: Use image node_id 16 for calling a global function with example usage: main(argv) and returns: int, int, int, int