import argparse
import os
import string


def generate_rpm_spec(template, patch_file):
    spec_template = string.Template(template)

    base_name, _ = os.path.splitext(patch_file)

    values = {
        'name': 'kpatch-module-{}'.format(base_name),
        'patch_file': patch_file,
        'kmod_filename': 'kpatch-{}.ko'.format(base_name),
        'description': 'Package generated from {} by '
                       'kpatch-package-builder'.format(patch_file),
        'target_kernel': '3.10.0-229.el7',
        'target_arch': 'x86_64',
    }

    return spec_template.substitute(values)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Generate RPM spec file to '
                                                 'build a kpatch package')
    parser.add_argument('patch', metavar='PATCH',
                        help='patch file from which to build the livepatch '
                             'module')
    parser.add_argument('-o', '--output', metavar='FILE', default=None,
                        help='name of output spec file')

    args = parser.parse_args()

    with open('kpatch-patch.spec') as f:
        template = f.read()

    spec_content = generate_rpm_spec(template, args.patch)

    with open(args.output, 'w') as f:
        f.write(spec_content)

import argparse
import os
import string


def generate_rpm_spec(template, patch_file):
    spec_template = string.Template(template)

    base_name, _ = os.path.splitext(patch_file)

    values = {
        'name': 'kpatch-module-{}'.format(base_name),
        'patch_file': patch_file,
        'kmod_filename': 'kpatch-{}.ko'.format(base_name),
        'description': 'Package generated from {} by '
                       'kpatch-package-builder'.format(patch_file),
        'target_kernel': '3.10.0-229.el7',
        'target_arch': 'x86_64',
    }

    return spec_template.substitute(values)


def get_args():
    parser = argparse.ArgumentParser(description='Generate RPM spec file to '
                                                 'build a kpatch package')
    parser.add_argument('patch', metavar='PATCH',
                        help='patch file from which to build the livepatch '
                             'module')
    parser.add_argument('-o', '--output', metavar='FILE', default=None,
                        help='name of output spec file')

    return parser.parse_args()

if __name__ == '__main__':
    args = get_args()

    with open('kpatch-patch.spec') as f:
        template = f.read()

    spec_content = generate_rpm_spec(template, args.patch)

    with open(args.output, 'w') as f:
        f.write(spec_content)

Split arg parsing into function
Split arg parsing into function

  import argparse
  import os
  import string
  
  
  def generate_rpm_spec(template, patch_file):
      spec_template = string.Template(template)
  
      base_name, _ = os.path.splitext(patch_file)
  
      values = {
          'name': 'kpatch-module-{}'.format(base_name),
          'patch_file': patch_file,
          'kmod_filename': 'kpatch-{}.ko'.format(base_name),
          'description': 'Package generated from {} by '
                         'kpatch-package-builder'.format(patch_file),
          'target_kernel': '3.10.0-229.el7',
          'target_arch': 'x86_64',
      }
  
      return spec_template.substitute(values)
  
- if __name__ == '__main__':
+ 
+ def get_args():
      parser = argparse.ArgumentParser(description='Generate RPM spec file to '
                                                   'build a kpatch package')
      parser.add_argument('patch', metavar='PATCH',
                          help='patch file from which to build the livepatch '
                               'module')
      parser.add_argument('-o', '--output', metavar='FILE', default=None,
                          help='name of output spec file')
  
-     args = parser.parse_args()
+     return parser.parse_args()
+ 
+ if __name__ == '__main__':
+     args = get_args()
  
      with open('kpatch-patch.spec') as f:
          template = f.read()
  
      spec_content = generate_rpm_spec(template, args.patch)
  
      with open(args.output, 'w') as f:
          f.write(spec_content)
  
Split arg parsing into function
## Code Before:
import argparse
import os
import string


def generate_rpm_spec(template, patch_file):
    spec_template = string.Template(template)

    base_name, _ = os.path.splitext(patch_file)

    values = {
        'name': 'kpatch-module-{}'.format(base_name),
        'patch_file': patch_file,
        'kmod_filename': 'kpatch-{}.ko'.format(base_name),
        'description': 'Package generated from {} by '
                       'kpatch-package-builder'.format(patch_file),
        'target_kernel': '3.10.0-229.el7',
        'target_arch': 'x86_64',
    }

    return spec_template.substitute(values)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Generate RPM spec file to '
                                                 'build a kpatch package')
    parser.add_argument('patch', metavar='PATCH',
                        help='patch file from which to build the livepatch '
                             'module')
    parser.add_argument('-o', '--output', metavar='FILE', default=None,
                        help='name of output spec file')

    args = parser.parse_args()

    with open('kpatch-patch.spec') as f:
        template = f.read()

    spec_content = generate_rpm_spec(template, args.patch)

    with open(args.output, 'w') as f:
        f.write(spec_content)

## Instruction:
Split arg parsing into function
## Code After:
import argparse
import os
import string


def generate_rpm_spec(template, patch_file):
    spec_template = string.Template(template)

    base_name, _ = os.path.splitext(patch_file)

    values = {
        'name': 'kpatch-module-{}'.format(base_name),
        'patch_file': patch_file,
        'kmod_filename': 'kpatch-{}.ko'.format(base_name),
        'description': 'Package generated from {} by '
                       'kpatch-package-builder'.format(patch_file),
        'target_kernel': '3.10.0-229.el7',
        'target_arch': 'x86_64',
    }

    return spec_template.substitute(values)


def get_args():
    parser = argparse.ArgumentParser(description='Generate RPM spec file to '
                                                 'build a kpatch package')
    parser.add_argument('patch', metavar='PATCH',
                        help='patch file from which to build the livepatch '
                             'module')
    parser.add_argument('-o', '--output', metavar='FILE', default=None,
                        help='name of output spec file')

    return parser.parse_args()

if __name__ == '__main__':
    args = get_args()

    with open('kpatch-patch.spec') as f:
        template = f.read()

    spec_content = generate_rpm_spec(template, args.patch)

    with open(args.output, 'w') as f:
        f.write(spec_content)
