def test_patch_win_source_unix_patch(patch_files, testing_config): _ensure_win_line_endings('file-modification.txt') _ensure_unix_line_endings(patch_files) apply_patch('.', patch_files, testing_config) with open('file-modification.txt', 'r') as modified: lines = modified.readlines() assert lines[0] == '43770\n'
def test_patch(): basedir = os.getcwd() with TemporaryDirectory() as tmp: os.chdir(tmp) with open(os.path.join(tmp, 'file-deletion.txt'), 'w') as f: f.write('hello\n') with open(os.path.join(tmp, 'file-modification.txt'), 'w') as f: f.write('hello\n') patchfile = os.path.join(tmp, 'patch') with open(patchfile, 'w') as f: f.write('diff file-deletion.txt file-deletion.txt\n') f.write('--- file-deletion.txt 2016-06-07 21:55:59.549798700 +0100\n') f.write('+++ file-deletion.txt 1970-01-01 01:00:00.000000000 +0100\n') f.write('@@ -1 +0,0 @@\n') f.write('-hello\n') f.write('diff file-creation.txt file-creation.txt\n') f.write('--- file-creation.txt 1970-01-01 01:00:00.000000000 +0100\n') f.write('+++ file-creation.txt 2016-06-07 21:55:59.549798700 +0100\n') f.write('@@ -0,0 +1 @@\n') f.write('+hello\n') f.write('diff file-modification.txt file-modification.txt.new\n') f.write('--- file-modification.txt 2016-06-08 18:23:08.384136600 +0100\n') f.write('+++ file-modification.txt.new 2016-06-08 18:23:37.565136200 +0100\n') f.write('@@ -1 +1 @@\n') f.write('-hello\n') f.write('+43770\n') f.close() apply_patch(tmp, patchfile) assert not os.path.exists(os.path.join(tmp, 'file-deletion.txt')) assert os.path.exists(os.path.join(tmp, 'file-creation.txt')) assert os.path.exists(os.path.join(tmp, 'file-modification.txt')) with open('file-modification.txt', 'r') as modified: lines = modified.readlines() assert lines[0] == '43770\n' os.chdir(basedir)
def test_patch(testing_workdir, test_config): with open('file-deletion.txt', 'w') as f: f.write('hello\n') with open('file-modification.txt', 'w') as f: f.write('hello\n') patchfile = 'patch' with open(patchfile, 'w') as f: f.write('diff file-deletion.txt file-deletion.txt\n') f.write('--- file-deletion.txt 2016-06-07 21:55:59.549798700 +0100\n') f.write('+++ file-deletion.txt 1970-01-01 01:00:00.000000000 +0100\n') f.write('@@ -1 +0,0 @@\n') f.write('-hello\n') f.write('diff file-creation.txt file-creation.txt\n') f.write('--- file-creation.txt 1970-01-01 01:00:00.000000000 +0100\n') f.write('+++ file-creation.txt 2016-06-07 21:55:59.549798700 +0100\n') f.write('@@ -0,0 +1 @@\n') f.write('+hello\n') f.write('diff file-modification.txt file-modification.txt.new\n') f.write( '--- file-modification.txt 2016-06-08 18:23:08.384136600 +0100\n') f.write( '+++ file-modification.txt.new 2016-06-08 18:23:37.565136200 +0100\n' ) f.write('@@ -1 +1 @@\n') f.write('-hello\n') f.write('+43770\n') f.close() apply_patch('.', patchfile, test_config) assert not os.path.exists('file-deletion.txt') assert os.path.exists('file-creation.txt') assert os.path.exists('file-modification.txt') with open('file-modification.txt', 'r') as modified: lines = modified.readlines() assert lines[0] == '43770\n'
def test_crlf_source_lf_patch(tmp_path, patch_paths, testing_config): _ensure_CRLF(patch_paths.modification) _ensure_CRLF(patch_paths.deletion) _ensure_LF(patch_paths.diff) with pytest.raises(CalledProcessError): apply_patch(str(tmp_path), patch_paths.diff, testing_config)
def test_patch(testing_workdir, testing_config): with open('file-deletion.txt', 'w') as f: f.write('hello\n') with open('file-modification.txt', 'w') as f: f.write('hello\n') patchfile = 'patch' with open(patchfile, 'w') as f: f.write('diff file-deletion.txt file-deletion.txt\n') f.write('--- file-deletion.txt 2016-06-07 21:55:59.549798700 +0100\n') f.write('+++ file-deletion.txt 1970-01-01 01:00:00.000000000 +0100\n') f.write('@@ -1 +0,0 @@\n') f.write('-hello\n') f.write('diff file-creation.txt file-creation.txt\n') f.write('--- file-creation.txt 1970-01-01 01:00:00.000000000 +0100\n') f.write('+++ file-creation.txt 2016-06-07 21:55:59.549798700 +0100\n') f.write('@@ -0,0 +1 @@\n') f.write('+hello\n') f.write('diff file-modification.txt file-modification.txt.new\n') f.write('--- file-modification.txt 2016-06-08 18:23:08.384136600 +0100\n') f.write('+++ file-modification.txt.new 2016-06-08 18:23:37.565136200 +0100\n') f.write('@@ -1 +1 @@\n') f.write('-hello\n') f.write('+43770\n') f.close() apply_patch('.', patchfile, testing_config) assert not os.path.exists('file-deletion.txt') assert os.path.exists('file-creation.txt') assert os.path.exists('file-modification.txt') with open('file-modification.txt', 'r') as modified: lines = modified.readlines() assert lines[0] == '43770\n'
def run_setuppy(src_dir, temp_dir, args): ''' Patch distutils and then run setup.py in a subprocess. :param src_dir: Directory containing the source code :type src_dir: str :param temp_dir: Temporary directory for doing for storing pkginfo.yaml :type temp_dir: str ''' # Do everything in the build env in case the setup.py install goes # haywire. # TODO: Try with another version of Python if this one fails. Some # packages are Python 2 or Python 3 only. create_env(build_prefix, ['python %s*' % args.python_version, 'pyyaml', 'setuptools'], clear_cache=False) stdlib_dir = join( build_prefix, 'Lib' if sys.platform == 'win32' else 'lib/python%s' % args.python_version) patch = join(temp_dir, 'pypi-distutils.patch') with open(patch, 'w') as f: f.write(DISTUTILS_PATCH.format(temp_dir.replace('\\', '\\\\'))) if exists(join(stdlib_dir, 'distutils', 'core.py-copy')): rm_rf(join(stdlib_dir, 'distutils', 'core.py')) copy2(join(stdlib_dir, 'distutils', 'core.py-copy'), join(stdlib_dir, 'distutils', 'core.py')) # Avoid race conditions. Invalidate the cache. if PY3: rm_rf( join(stdlib_dir, 'distutils', '__pycache__', 'core.cpython-%s%s.pyc' % sys.version_info[:2])) rm_rf( join(stdlib_dir, 'distutils', '__pycache__', 'core.cpython-%s%s.pyo' % sys.version_info[:2])) else: rm_rf(join(stdlib_dir, 'distutils', 'core.pyc')) rm_rf(join(stdlib_dir, 'distutils', 'core.pyo')) else: copy2(join(stdlib_dir, 'distutils', 'core.py'), join(stdlib_dir, 'distutils', 'core.py-copy')) apply_patch(join(stdlib_dir, 'distutils'), patch) # Save PYTHONPATH for later env = os.environ.copy() if 'PYTHONPATH' in env: env[str('PYTHONPATH')] = str(src_dir + ':' + env['PYTHONPATH']) else: env[str('PYTHONPATH')] = str(src_dir) cwd = getcwd() chdir(src_dir) args = [build_python, 'setup.py', 'install'] try: subprocess.check_call(args, env=env) except subprocess.CalledProcessError: print('$PYTHONPATH = %s' % env['PYTHONPATH']) sys.exit('Error: command failed: %s' % ' '.join(args)) finally: chdir(cwd)
def run_setuppy(src_dir, temp_dir, python_version, setup_options): ''' Patch distutils and then run setup.py in a subprocess. :param src_dir: Directory containing the source code :type src_dir: str :param temp_dir: Temporary directory for doing for storing pkginfo.yaml :type temp_dir: str ''' # Do everything in the build env in case the setup.py install goes # haywire. # TODO: Try with another version of Python if this one fails. Some # packages are Python 2 or Python 3 only. create_env(config.build_prefix, ['python %s*' % python_version, 'pyyaml', 'yaml', 'setuptools', 'numpy'], clear_cache=False) stdlib_dir = join(config.build_prefix, 'Lib' if sys.platform == 'win32' else 'lib/python%s' % python_version) patch = join(temp_dir, 'pypi-distutils.patch') with open(patch, 'w') as f: f.write(DISTUTILS_PATCH.format(temp_dir.replace('\\', '\\\\'))) if exists(join(stdlib_dir, 'distutils', 'core.py-copy')): rm_rf(join(stdlib_dir, 'distutils', 'core.py')) copy2(join(stdlib_dir, 'distutils', 'core.py-copy'), join(stdlib_dir, 'distutils', 'core.py')) # Avoid race conditions. Invalidate the cache. if PY3: rm_rf(join(stdlib_dir, 'distutils', '__pycache__', 'core.cpython-%s%s.pyc' % sys.version_info[:2])) rm_rf(join(stdlib_dir, 'distutils', '__pycache__', 'core.cpython-%s%s.pyo' % sys.version_info[:2])) else: rm_rf(join(stdlib_dir, 'distutils', 'core.pyc')) rm_rf(join(stdlib_dir, 'distutils', 'core.pyo')) else: copy2(join(stdlib_dir, 'distutils', 'core.py'), join(stdlib_dir, 'distutils', 'core.py-copy')) apply_patch(join(stdlib_dir, 'distutils'), patch) # Save PYTHONPATH for later env = os.environ.copy() if 'PYTHONPATH' in env: env[str('PYTHONPATH')] = str(src_dir + ':' + env['PYTHONPATH']) else: env[str('PYTHONPATH')] = str(src_dir) cwd = getcwd() chdir(src_dir) cmdargs = [config.build_python, 'setup.py', 'install'] cmdargs.extend(setup_options) try: subprocess.check_call(cmdargs, env=env) except subprocess.CalledProcessError: print('$PYTHONPATH = %s' % env['PYTHONPATH']) sys.exit('Error: command failed: %s' % ' '.join(cmdargs)) finally: chdir(cwd)
def test_patch(patch_files, testing_config): apply_patch('.', patch_files, testing_config) assert not os.path.exists('file-deletion.txt') assert os.path.exists('file-creation.txt') assert os.path.exists('file-modification.txt') with open('file-modification.txt', 'r') as modified: lines = modified.readlines() assert lines[0] == '43770\n'
def test_lf_source_lf_patch(tmp_path, patch_paths, testing_config): _ensure_LF(patch_paths.modification) _ensure_LF(patch_paths.deletion) _ensure_LF(patch_paths.diff) apply_patch(str(tmp_path), patch_paths.diff, testing_config) assert patch_paths.modification.read_text() == "43770\n"
def test_crlf_source_crlf_patch(tmp_path, patch_paths, testing_config): _ensure_CRLF(patch_paths.modification) _ensure_CRLF(patch_paths.deletion) _ensure_CRLF(patch_paths.diff) apply_patch(str(tmp_path), patch_paths.diff, testing_config) assert patch_paths.modification.read_bytes() == b"43770\r\n"
def run_setuppy(src_dir, temp_dir, python_version, config, setup_options): """ Patch distutils and then run setup.py in a subprocess. :param src_dir: Directory containing the source code :type src_dir: str :param temp_dir: Temporary directory for doing for storing pkginfo.yaml :type temp_dir: str """ specs = ["python %s*" % python_version, "pyyaml", "setuptools"] with open(os.path.join(src_dir, "setup.py")) as setup: text = setup.read() if "import numpy" in text or "from numpy" in text: specs.append("numpy") # Do everything in the build env in case the setup.py install goes # haywire. # TODO: Try with another version of Python if this one fails. Some # packages are Python 2 or Python 3 only. create_env(config.build_prefix, specs=specs, clear_cache=False, config=config) stdlib_dir = join(config.build_prefix, "Lib" if sys.platform == "win32" else "lib/python%s" % python_version) patch = join(temp_dir, "pypi-distutils.patch") with open(patch, "w") as f: f.write(DISTUTILS_PATCH.format(temp_dir.replace("\\", "\\\\"))) if exists(join(stdlib_dir, "distutils", "core.py-copy")): rm_rf(join(stdlib_dir, "distutils", "core.py")) copy2(join(stdlib_dir, "distutils", "core.py-copy"), join(stdlib_dir, "distutils", "core.py")) # Avoid race conditions. Invalidate the cache. if PY3: rm_rf(join(stdlib_dir, "distutils", "__pycache__", "core.cpython-%s%s.pyc" % sys.version_info[:2])) rm_rf(join(stdlib_dir, "distutils", "__pycache__", "core.cpython-%s%s.pyo" % sys.version_info[:2])) else: rm_rf(join(stdlib_dir, "distutils", "core.pyc")) rm_rf(join(stdlib_dir, "distutils", "core.pyo")) else: copy2(join(stdlib_dir, "distutils", "core.py"), join(stdlib_dir, "distutils", "core.py-copy")) apply_patch(join(stdlib_dir, "distutils"), patch, config=config) # Save PYTHONPATH for later env = os.environ.copy() if "PYTHONPATH" in env: env[str("PYTHONPATH")] = str(src_dir + ":" + env["PYTHONPATH"]) else: env[str("PYTHONPATH")] = str(src_dir) cwd = getcwd() chdir(src_dir) cmdargs = [config.build_python, "setup.py", "install"] cmdargs.extend(setup_options) try: subprocess.check_call(cmdargs, env=env) except subprocess.CalledProcessError: print("$PYTHONPATH = %s" % env["PYTHONPATH"]) sys.exit("Error: command failed: %s" % " ".join(cmdargs)) finally: chdir(cwd)
def test_patch_paths(tmp_path, patch_paths, testing_config): assert patch_paths.deletion.exists() assert not patch_paths.creation.exists() assert patch_paths.modification.exists() assert patch_paths.modification.read_text() == "hello\n" apply_patch(str(tmp_path), patch_paths.diff, testing_config) assert not patch_paths.deletion.exists() assert patch_paths.creation.exists() assert patch_paths.modification.exists() assert patch_paths.modification.read_text() == "43770\n"
def run_setuppy(src_dir, temp_dir, python_version, extra_specs, config, setup_options): ''' Patch distutils and then run setup.py in a subprocess. :param src_dir: Directory containing the source code :type src_dir: str :param temp_dir: Temporary directory for doing for storing pkginfo.yaml :type temp_dir: str ''' # TODO: we could make everyone's lives easier if we include packaging here, because setuptools # needs it in recent versions. At time of writing, it is not a package in defaults, so this # actually breaks conda-build right now. Omit it until packaging is on defaults. # specs = ['python %s*' % python_version, 'pyyaml', 'setuptools', 'six', 'packaging', 'appdirs'] specs = ['python %s*' % python_version, 'pyyaml', 'setuptools'] with open(os.path.join(src_dir, "setup.py")) as setup: text = setup.read() if 'import numpy' in text or 'from numpy' in text: specs.append('numpy') specs.extend(extra_specs) rm_rf(config.host_prefix) create_env(config.host_prefix, specs_or_actions=specs, env='host', subdir=config.host_subdir, clear_cache=False, config=config) stdlib_dir = join( config.host_prefix, 'Lib' if sys.platform == 'win32' else 'lib/python%s' % python_version) patch = join(temp_dir, 'pypi-distutils.patch') with open(patch, 'w') as f: f.write(DISTUTILS_PATCH.format(temp_dir.replace('\\', '\\\\'))) if exists(join(stdlib_dir, 'distutils', 'core.py-copy')): rm_rf(join(stdlib_dir, 'distutils', 'core.py')) copy2(join(stdlib_dir, 'distutils', 'core.py-copy'), join(stdlib_dir, 'distutils', 'core.py')) # Avoid race conditions. Invalidate the cache. if PY3: rm_rf( join(stdlib_dir, 'distutils', '__pycache__', 'core.cpython-%s%s.pyc' % sys.version_info[:2])) rm_rf( join(stdlib_dir, 'distutils', '__pycache__', 'core.cpython-%s%s.pyo' % sys.version_info[:2])) else: rm_rf(join(stdlib_dir, 'distutils', 'core.pyc')) rm_rf(join(stdlib_dir, 'distutils', 'core.pyo')) else: copy2(join(stdlib_dir, 'distutils', 'core.py'), join(stdlib_dir, 'distutils', 'core.py-copy')) apply_patch(join(stdlib_dir, 'distutils'), patch, config=config) # Save PYTHONPATH for later env = os.environ.copy() if 'PYTHONPATH' in env: env[str('PYTHONPATH')] = str(src_dir + ':' + env['PYTHONPATH']) else: env[str('PYTHONPATH')] = str(src_dir) cwd = getcwd() chdir(src_dir) cmdargs = [config.host_python, 'setup.py', 'install'] cmdargs.extend(setup_options) try: check_call_env(cmdargs, env=env) except subprocess.CalledProcessError: print('$PYTHONPATH = %s' % env['PYTHONPATH']) sys.exit('Error: command failed: %s' % ' '.join(cmdargs)) finally: chdir(cwd)
def run_setuppy(src_dir, temp_dir, python_version, extra_specs, config, setup_options): ''' Patch distutils and then run setup.py in a subprocess. :param src_dir: Directory containing the source code :type src_dir: str :param temp_dir: Temporary directory for doing for storing pkginfo.yaml :type temp_dir: str ''' # TODO: we could make everyone's lives easier if we include packaging here, because setuptools # needs it in recent versions. At time of writing, it is not a package in defaults, so this # actually breaks conda-build right now. Omit it until packaging is on defaults. # specs = ['python %s*' % python_version, 'pyyaml', 'setuptools', 'six', 'packaging', 'appdirs'] specs = ['python %s*' % python_version, 'pyyaml'] with open(os.path.join(src_dir, "setup.py")) as setup: text = setup.read() if 'import numpy' in text or 'from numpy' in text: specs.append('numpy') specs.extend(extra_specs) # Do everything in the build env in case the setup.py install goes # haywire. # TODO: Try with another version of Python if this one fails. Some # packages are Python 2 or Python 3 only. if not os.path.isdir(config.build_prefix) or not os.listdir(config.build_prefix): create_env(config.build_prefix, specs_or_actions=specs, subdir=config.build_subdir, clear_cache=False, config=config) stdlib_dir = join(config.build_prefix, 'Lib' if sys.platform == 'win32' else 'lib/python%s' % python_version) patch = join(temp_dir, 'pypi-distutils.patch') with open(patch, 'w') as f: f.write(DISTUTILS_PATCH.format(temp_dir.replace('\\', '\\\\'))) if exists(join(stdlib_dir, 'distutils', 'core.py-copy')): rm_rf(join(stdlib_dir, 'distutils', 'core.py')) copy2(join(stdlib_dir, 'distutils', 'core.py-copy'), join(stdlib_dir, 'distutils', 'core.py')) # Avoid race conditions. Invalidate the cache. if PY3: rm_rf(join(stdlib_dir, 'distutils', '__pycache__', 'core.cpython-%s%s.pyc' % sys.version_info[:2])) rm_rf(join(stdlib_dir, 'distutils', '__pycache__', 'core.cpython-%s%s.pyo' % sys.version_info[:2])) else: rm_rf(join(stdlib_dir, 'distutils', 'core.pyc')) rm_rf(join(stdlib_dir, 'distutils', 'core.pyo')) else: copy2(join(stdlib_dir, 'distutils', 'core.py'), join(stdlib_dir, 'distutils', 'core.py-copy')) apply_patch(join(stdlib_dir, 'distutils'), patch, config=config) # Save PYTHONPATH for later env = os.environ.copy() if 'PYTHONPATH' in env: env[str('PYTHONPATH')] = str(src_dir + ':' + env['PYTHONPATH']) else: env[str('PYTHONPATH')] = str(src_dir) cwd = getcwd() chdir(src_dir) cmdargs = [config.build_python, 'setup.py', 'install'] cmdargs.extend(setup_options) try: check_call_env(cmdargs, env=env) except subprocess.CalledProcessError: print('$PYTHONPATH = %s' % env['PYTHONPATH']) sys.exit('Error: command failed: %s' % ' '.join(cmdargs)) finally: chdir(cwd)