import textwrap import os import sys import shutil import subprocess import contextlib def make_file(file_path, content=''): base = os.path.dirname(file_path) if base: if not os.path.exists(base): os.makedirs(base) with open(file_path, 'w') as out: out.write(content) @contextlib.contextmanager def make_tree(): try: make_file('pkg/__init__.py') make_file('pkg/file with spaces.py') make_file('setup.py', textwrap.dedent(""" from distutils.core import setup setup(name='issue21153-package', packages=['pkg']) """)) make_file('install.txt', textwrap.dedent(""" python setup.py install -O1 --root=$RPM_BUILD_ROOT --record=INSTALLED_FILES python /home/vagrant/quote-files.py INSTALLED_FILES """).lstrip()) make_file('quote-files.py', textwrap.dedent(""" import sys with open(sys.argv[1]) as f: lines = list(f) lines = ['"' + line + '"' for line in lines] with open(sys.argv[1], 'w') as f: f.writelines(lines) """)) yield finally: if os.path.exists('pkg'): shutil.rmtree('pkg') if os.path.exists('setup.py'): os.remove('setup.py') def run(): with make_tree(): subprocess.check_call(['python', 'setup.py', 'bdist_rpm'] + sys.argv[1:]) if __name__ == '__main__': run()