old_contents: # BEGIN_COPYRIGHT
  #
  # Copyright (C) 2014-2016 CRS4.
  #
  # Licensed under the Apache License, Version 2.0 (the "License"); you may not
  # use this file except in compliance with the License. You may obtain a copy
  # of the License at
  #
  # http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  # License for the specific language governing permissions and limitations
  # under the License.
  #
  # END_COPYRIGHT
  
  """\
  Serialize image data to BioImgPlane records.
  
  All args are passed to it.crs4.features.ImageToAvro.
  """
  
  import subprocess as sp
  
  from pyfeatures import JAR_PATH
  
  
  def run(args, extra_argv=None):
      if extra_argv is None:
          extra_argv = []
      java = ["java", "-cp", JAR_PATH]
      if args.java_d:
          for prop in args.java_d:
              java.append('-D%s' % prop)
      sp_argv = java + ["it.crs4.features.ImageToAvro"] + extra_argv
      try:
          sp.check_call(sp_argv, stdout=args.stdout, stderr=args.stderr)
      except sp.CalledProcessError:
          if extra_argv:
              raise
      return 0
  
  
  def add_parser(subparsers):
      parser = subparsers.add_parser(
          "serialize",
          description=__doc__,
          epilog="Run without arguments to print the Java help."
      )
      parser.add_argument('-D', dest='java_d', metavar='JAVA_PROPERTY',
                          nargs='+', help='Java properties')
      parser.set_defaults(func=run)
      return parser
  
new_contents: # BEGIN_COPYRIGHT
  #
  # Copyright (C) 2014-2016 CRS4.
  #
  # Licensed under the Apache License, Version 2.0 (the "License"); you may not
  # use this file except in compliance with the License. You may obtain a copy
  # of the License at
  #
  # http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  # License for the specific language governing permissions and limitations
  # under the License.
  #
  # END_COPYRIGHT
  
  """\
  Serialize image data to BioImgPlane records.
  
  All args are passed to it.crs4.features.ImageToAvro.
  """
  
  import subprocess as sp
  
  from pyfeatures import JAR_PATH
  
  
  def run(args, extra_argv=None):
      if extra_argv is None:
          extra_argv = []
      java = ["java", "-cp", JAR_PATH]
      if args.java_d:
          for prop in args.java_d:
              java.append('-D%s' % prop)
      sp_argv = java + ["it.crs4.features.ImageToAvro"] + extra_argv
      stdout = None
      stderr = None
      try:
          if args.stdout:
              stdout = open(args.stdout, 'a')
          if args.stderr:
              stderr = open(args.stderr, 'a')
          sp.check_call(sp_argv, stdout=stdout, stderr=stderr)
      except sp.CalledProcessError:
          if extra_argv:
              raise
  
      if stdout:
          stdout.close()
      if stderr:
          stderr.close()
      return 0
  
  
  def add_parser(subparsers):
      parser = subparsers.add_parser(
          "serialize",
          description=__doc__,
          epilog="Run without arguments to print the Java help."
      )
      parser.add_argument('-D', dest='java_d', metavar='JAVA_PROPERTY',
                          nargs='+', help='Java properties')
      parser.set_defaults(func=run)
      return parser
  
subject: Fix stdout/stderr in python java call