import os
import string

if __name__ == '__main__':
    patch_file = 'example.patch'
    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),
    }

    with open('kpatch-patch.spec') as f:
        spec_template = string.Template(f.read())

    print(spec_template.substitute(values))

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),
    }

    return spec_template.substitute(values)

if __name__ == '__main__':

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

    print(generate_rpm_spec(template, 'example.patch'))

Split spec generation into function
Split spec generation into function

  import os
  import string
  
- if __name__ == '__main__':
-     patch_file = 'example.patch'
+ 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),
      }
  
+     return spec_template.substitute(values)
+ 
+ if __name__ == '__main__':
+ 
      with open('kpatch-patch.spec') as f:
-         spec_template = string.Template(f.read())
+         template = f.read()
  
-     print(spec_template.substitute(values))
+     print(generate_rpm_spec(template, 'example.patch'))
  
Split spec generation into function
## Code Before:
import os
import string

if __name__ == '__main__':
    patch_file = 'example.patch'
    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),
    }

    with open('kpatch-patch.spec') as f:
        spec_template = string.Template(f.read())

    print(spec_template.substitute(values))

## Instruction:
Split spec generation into function
## Code After:
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),
    }

    return spec_template.substitute(values)

if __name__ == '__main__':

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

    print(generate_rpm_spec(template, 'example.patch'))
