def register_dist(dist_name, target_prefix): """ register a distribution with conda. """ # build path to site-packages directory get_python_version = UnlinkLinkTransaction.get_python_version python_ver = get_python_version(target_prefix, [], []) sp_short_path = get_python_site_packages_short_path(python_ver) sp_full_path = os.path.join(target_prefix, sp_short_path) # find package details using distlib dist_path = DistributionPath([ sp_full_path, ], include_egg=True) dist = dist_path.get_distribution(dist_name) # create a conda PrefixRecord files = [ os.path.join(sp_short_path, file_path) for file_path, file_hash, file_size in dist.list_installed_files() ] prefix_record = PrefixRecord.from_objects( name=dist.name, version=dist.version, files=files, build=DEFAULT_BUILD_STR, build_number=DEFAULT_BUILD_NUMBER, ) # TODO: conda currently checks that prefix_record.fn ends with .tar.bz2. # This check should be supressed for prefix record entries which are not # derived from tarballs so that prefix_record.fn can be set to None. prefix_record.fn = prefix_record.fn + '.tar.bz2' print("creating linked package record for %s." % dist_name) PrefixData(target_prefix).insert(prefix_record)
def test_scrapy_py27_osx_no_binary(): anchor_file = "lib/python2.7/site-packages/Scrapy-1.5.1-py2.7.egg-info/PKG-INFO" prefix_path = join(ENV_METADATA_DIR, "py27-osx-no-binary") if not isdir(prefix_path): pytest.skip("test files not found: %s" % prefix_path) prefix_rec = read_python_record(prefix_path, anchor_file, "2.7") dumped_rec = json_load(json_dump(prefix_rec.dump())) files = dumped_rec.pop("files") paths_data = dumped_rec.pop("paths_data") print(json_dump(dumped_rec)) assert dumped_rec == { "build": "pypi_0", "build_number": 0, "channel": "https://conda.anaconda.org/pypi", "constrains": [], "depends": [ "cssselect >=0.9", "lxml", "parsel >=1.1", "pydispatcher >=2.0.5", "pyopenssl", "python 2.7.*", "queuelib", "service-identity", "six >=1.5.2", "twisted >=13.1.0", "w3lib >=1.17.0" ], "fn": "Scrapy-1.5.1-py2.7.egg-info", "name": "scrapy", "package_type": "virtual_python_egg_manageable", "subdir": "pypi", "version": "1.5.1" } print(json_dump(files)) print(json_dump(paths_data["paths"])) sp_dir = get_python_site_packages_short_path("2.7") assert sp_dir + "/scrapy/contrib/downloadermiddleware/decompression.py" in files assert sp_dir + "/scrapy/downloadermiddlewares/decompression.pyc" in files assert ("../bin/scrapy" if on_win else "bin/scrapy") in files pd1 = { "_path": sp_dir + "/scrapy/contrib/downloadermiddleware/decompression.py", "path_type": "hardlink" } assert pd1 in paths_data["paths"] pd2 = { "_path": sp_dir + "/scrapy/contrib/downloadermiddleware/decompression.pyc", "path_type": "hardlink" } assert pd2 in paths_data["paths"] pd3 = { "_path": "../bin/scrapy" if on_win else "bin/scrapy", "path_type": "hardlink" } assert pd3 in paths_data["paths"]
def test_CompilePycAction_noarch_python(self): if not softlink_supported(__file__, self.prefix) and on_win: pytest.skip("softlink not supported") target_python_version = '%d.%d' % sys.version_info[:2] sp_dir = get_python_site_packages_short_path(target_python_version) transaction_context = { 'target_python_version': target_python_version, 'target_site_packages_short_path': sp_dir, } package_info = AttrDict(package_metadata=AttrDict(noarch=AttrDict(type=NoarchType.python))) file_link_actions = [ AttrDict( source_short_path='site-packages/something.py', target_short_path=get_python_noarch_target_path('site-packages/something.py', sp_dir), ), AttrDict( # this one shouldn't get compiled source_short_path='something.py', target_short_path=get_python_noarch_target_path('something.py', sp_dir), ), ] axns = CompilePycAction.create_actions(transaction_context, package_info, self.prefix, None, file_link_actions) assert len(axns) == 1 axn = axns[0] assert axn.source_full_path == join(self.prefix, win_path_ok(get_python_noarch_target_path('site-packages/something.py', sp_dir))) assert axn.target_full_path == join(self.prefix, win_path_ok(pyc_path(get_python_noarch_target_path('site-packages/something.py', sp_dir), target_python_version))) # make .py file in prefix that will be compiled mkdir_p(dirname(axn.source_full_path)) with open(axn.source_full_path, 'w') as fh: fh.write("value = 42\n") # symlink the current python python_full_path = join(self.prefix, get_python_short_path(target_python_version)) mkdir_p(dirname(python_full_path)) create_link(sys.executable, python_full_path, LinkType.softlink) axn.execute() assert isfile(axn.target_full_path) # remove the source .py file so we're sure we're importing the pyc file below rm_rf(axn.source_full_path) assert not isfile(axn.source_full_path) if (3,) > sys.version_info >= (3, 5): # we're probably dropping py34 support soon enough anyway imported_pyc_file = load_python_file(axn.target_full_path) assert imported_pyc_file.value == 42 axn.reverse() assert not isfile(axn.target_full_path)
def test_dist_get_paths(): content = 'foo/bar,sha256=1,"45"\nfoo/spam,,\n' temp_path, fpaths = _create_test_files((('', 'SOURCES.txt', content), )) sp_dir = get_python_site_packages_short_path("2.7") dist = PythonEggInfoDistribution(temp_path, "2.7", None) output = dist.get_paths() expected_output = [(join_url(sp_dir, "foo", "bar"), '1', 45), (join_url(sp_dir, "foo", "spam"), None, None)] _print_output(output, expected_output) assert output == expected_output
def test_CompilePycAction_noarch_python(self): target_python_version = '%d.%d' % sys.version_info[:2] sp_dir = get_python_site_packages_short_path(target_python_version) transaction_context = { 'target_python_version': target_python_version, 'target_site_packages_short_path': sp_dir, } package_info = AttrDict(package_metadata=AttrDict(noarch=AttrDict(type=NoarchType.python))) file_link_actions = [ AttrDict( source_short_path='site-packages/something.py', target_short_path=get_python_noarch_target_path('site-packages/something.py', sp_dir), ), AttrDict( # this one shouldn't get compiled source_short_path='something.py', target_short_path=get_python_noarch_target_path('something.py', sp_dir), ), ] axns = CompilePycAction.create_actions(transaction_context, package_info, self.prefix, None, file_link_actions) assert len(axns) == 1 axn = axns[0] assert axn.source_full_path == join(self.prefix, win_path_ok(get_python_noarch_target_path('site-packages/something.py', sp_dir))) assert axn.target_full_path == join(self.prefix, win_path_ok(pyc_path(get_python_noarch_target_path('site-packages/something.py', sp_dir), target_python_version))) # make .py file in prefix that will be compiled mkdir_p(dirname(axn.source_full_path)) with open(axn.source_full_path, 'w') as fh: fh.write("value = 42\n") # symlink the current python python_full_path = join(self.prefix, get_python_short_path(target_python_version)) mkdir_p(dirname(python_full_path)) create_link(sys.executable, python_full_path, LinkType.softlink) axn.execute() assert isfile(axn.target_full_path) # remove the source .py file so we're sure we're importing the pyc file below rm_rf(axn.source_full_path) assert not isfile(axn.source_full_path) if (3, ) > sys.version_info >= (3, 5): # we're probably dropping py34 support soon enough anyway imported_pyc_file = load_python_file(axn.target_full_path) assert imported_pyc_file.value == 42 axn.reverse() assert not isfile(axn.target_full_path)
def test_twilio_py36_osx_whl(): anchor_file = "lib/python3.6/site-packages/twilio-6.16.1.dist-info/RECORD" prefix_path = join(ENV_METADATA_DIR, "py36-osx-whl") if not isdir(prefix_path): pytest.skip("test files not found: %s" % prefix_path) prefix_rec = read_python_record(prefix_path, anchor_file, "3.6") pprint(prefix_rec.depends) pprint(prefix_rec.constrains) dumped_rec = json_load(json_dump(prefix_rec.dump())) files = dumped_rec.pop("files") paths_data = dumped_rec.pop("paths_data") print(json_dump(dumped_rec)) assert dumped_rec == { "build": "pypi_0", "build_number": 0, "channel": "https://conda.anaconda.org/pypi", "constrains": [], "depends": [ "pyjwt >=1.4.2", "pysocks", "python 3.6.*", "pytz", "requests >=2.0.0", "six" ], "fn": "twilio-6.16.1.dist-info", "name": "twilio", "package_type": "virtual_python_wheel", "subdir": "pypi", "version": "6.16.1" } print(json_dump(files)) print(json_dump(paths_data["paths"])) sp_dir = get_python_site_packages_short_path("3.6") assert sp_dir + "/twilio/compat.py" in files assert sp_dir + "/twilio/__pycache__/compat.cpython-36.pyc" in files pd1 = { "_path": sp_dir + "/twilio/compat.py", "path_type": "hardlink", "sha256": "sJ1t7CKvxpipiX5cyH1YwXTf3n_FsLf_taUhuCVsCwE", "size_in_bytes": 517 } assert pd1 in paths_data["paths"] pd2 = { "_path": sp_dir + "/twilio/jwt/__pycache__/compat.cpython-36.pyc", "path_type": "hardlink", "sha256": None, "size_in_bytes": None } assert pd2 in paths_data["paths"]
def test_noarch_python_package_without_entry_points(self): # regression test for #4546 with make_temp_env("-c conda-test itsdangerous") as prefix: py_ver = get_python_version_for_prefix(prefix) sp_dir = get_python_site_packages_short_path(py_ver) py_file = sp_dir + "/itsdangerous.py" pyc_file = pyc_path(py_file, py_ver) assert isfile(join(prefix, py_file)) assert isfile(join(prefix, pyc_file)) run_command(Commands.REMOVE, prefix, "itsdangerous") assert not isfile(join(prefix, py_file)) assert not isfile(join(prefix, pyc_file))
def test_pyjwt_py36_osx_whl(): anchor_file = "lib/python3.6/site-packages/PyJWT-1.6.4.dist-info/RECORD" prefix_path = join(ENV_METADATA_DIR, "py36-osx-whl") if not isdir(prefix_path): pytest.skip("test files not found: %s" % prefix_path) prefix_rec = read_python_record(prefix_path, anchor_file, "3.6") dumped_rec = json_load(json_dump(prefix_rec.dump())) files = dumped_rec.pop("files") paths_data = dumped_rec.pop("paths_data") print(json_dump(dumped_rec)) assert dumped_rec == { "build": "pypi_0", "build_number": 0, "channel": "https://conda.anaconda.org/pypi", "constrains": [ "cryptography >=1.4", "pytest <4,>3" ], "depends": [ "python 3.6.*" ], "fn": "PyJWT-1.6.4.dist-info", "name": "pyjwt", "package_type": "virtual_python_wheel", "subdir": "pypi", "version": "1.6.4" } print(json_dump(files)) print(json_dump(paths_data["paths"])) sp_dir = get_python_site_packages_short_path("3.6") assert ("../bin/pyjwt" if on_win else "bin/pyjwt") in files assert sp_dir + '/jwt/__pycache__/__init__.cpython-36.pyc' in files pd1 = { "_path": "../bin/pyjwt" if on_win else "bin/pyjwt", "path_type": "hardlink", "sha256": "wZET_24uZDEpsMdhAQ78Ass2k-76aQ59yPSE4DTE2To", "size_in_bytes": 260 } assert pd1 in paths_data["paths"] pd2 = { "_path": sp_dir + "/jwt/contrib/__pycache__/__init__.cpython-36.pyc", "path_type": "hardlink", "sha256": None, "size_in_bytes": None } assert pd2 in paths_data["paths"]
def test_twilio_py27_osx_no_binary(): anchor_file = "lib/python2.7/site-packages/twilio-6.16.1-py2.7.egg-info/PKG-INFO" prefix_path = join(ENV_METADATA_DIR, "py27-osx-no-binary") if not isdir(prefix_path): pytest.skip("test files not found: %s" % prefix_path) prefix_rec = read_python_record(prefix_path, anchor_file, "2.7") pprint(prefix_rec.depends) pprint(prefix_rec.constrains) dumped_rec = json_load(json_dump(prefix_rec.dump())) files = dumped_rec.pop("files") paths_data = dumped_rec.pop("paths_data") print(json_dump(dumped_rec)) assert dumped_rec == { "build": "pypi_0", "build_number": 0, "channel": "https://conda.anaconda.org/pypi", "constrains": [], "depends": [ "pyjwt >=1.4.2", "python 2.7.*", "pytz", "requests >=2.0.0", "six" ], "fn": "twilio-6.16.1-py2.7.egg-info", "name": "twilio", "package_type": "virtual_python_egg_manageable", "subdir": "pypi", "version": "6.16.1" } print(json_dump(files)) print(json_dump(paths_data["paths"])) sp_dir = get_python_site_packages_short_path("2.7") assert sp_dir + "/twilio/compat.py" in files assert sp_dir + "/twilio/compat.pyc" in files pd1 = { "_path": sp_dir + "/twilio/compat.py", "path_type": "hardlink" } assert pd1 in paths_data["paths"] pd2 = { "_path": sp_dir + "/twilio/jwt/compat.pyc", "path_type": "hardlink" } assert pd2 in paths_data["paths"]
def test_noarch_python_package_with_entry_points(self): with make_temp_env("-c conda-test flask") as prefix: py_ver = get_python_version_for_prefix(prefix) sp_dir = get_python_site_packages_short_path(py_ver) py_file = sp_dir + "/flask/__init__.py" pyc_file = pyc_path(py_file, py_ver) assert isfile(join(prefix, py_file)) assert isfile(join(prefix, pyc_file)) exe_path = join(prefix, get_bin_directory_short_path(), 'flask') if on_win: exe_path += ".exe" assert isfile(exe_path) run_command(Commands.REMOVE, prefix, "flask") assert not isfile(join(prefix, py_file)) assert not isfile(join(prefix, pyc_file)) assert not isfile(exe_path)
def test_noarch_package(self): with make_temp_env("-c scastellarin flask") as prefix: py_ver = get_python_version_for_prefix(prefix) sp_dir = get_python_site_packages_short_path(py_ver) py_file = sp_dir + "/flask/__init__.py" pyc_file = pyc_path(py_file, py_ver) assert isfile(join(prefix, py_file)) assert isfile(join(prefix, pyc_file)) exe_path = join(prefix, get_bin_directory_short_path(), 'flask') if on_win: exe_path += ".exe" assert isfile(exe_path) run_command(Commands.REMOVE, prefix, "flask") assert not isfile(join(prefix, py_file)) assert not isfile(join(prefix, pyc_file)) assert not isfile(exe_path)
def test_pyjwt_py27_osx_no_binary(): anchor_file = "lib/python2.7/site-packages/PyJWT-1.6.4-py2.7.egg-info/PKG-INFO" prefix_path = join(ENV_METADATA_DIR, "py27-osx-no-binary") if not isdir(prefix_path): pytest.skip("test files not found: %s" % prefix_path) prefix_rec = read_python_record(prefix_path, anchor_file, "2.7") dumped_rec = json_load(json_dump(prefix_rec.dump())) files = dumped_rec.pop("files") paths_data = dumped_rec.pop("paths_data") print(json_dump(dumped_rec)) assert dumped_rec == { "build": "pypi_0", "build_number": 0, "channel": "https://conda.anaconda.org/pypi", "constrains": [ "cryptography >=1.4", "pytest <4,>3" ], "depends": [ "python 2.7.*" ], "fn": "PyJWT-1.6.4-py2.7.egg-info", "name": "pyjwt", "package_type": "virtual_python_egg_manageable", "subdir": "pypi", "version": "1.6.4" } print(json_dump(files)) print(json_dump(paths_data["paths"])) sp_dir = get_python_site_packages_short_path("2.7") assert ('../bin/pyjwt' if on_win else 'bin/pyjwt') in files assert sp_dir + '/jwt/__init__.pyc' in files pd1 = { "_path": "../bin/pyjwt" if on_win else "bin/pyjwt", "path_type": "hardlink" } assert pd1 in paths_data["paths"] pd2 = { "_path": sp_dir + "/jwt/contrib/__init__.pyc", "path_type": "hardlink" } assert pd2 in paths_data["paths"]
def test_CompileMultiPycAction_noarch_python(self): if not softlink_supported(__file__, self.prefix) and on_win: pytest.skip("softlink not supported") target_python_version = '%d.%d' % sys.version_info[:2] sp_dir = get_python_site_packages_short_path(target_python_version) transaction_context = { 'target_python_version': target_python_version, 'target_site_packages_short_path': sp_dir, } package_info = AttrDict(package_metadata=AttrDict(noarch=AttrDict(type=NoarchType.python))) file_link_actions = [ AttrDict( source_short_path='site-packages/something.py', target_short_path=get_python_noarch_target_path('site-packages/something.py', sp_dir), ), AttrDict( source_short_path='site-packages/another.py', target_short_path=get_python_noarch_target_path('site-packages/another.py', sp_dir), ), AttrDict( # this one shouldn't get compiled source_short_path='something.py', target_short_path=get_python_noarch_target_path('something.py', sp_dir), ), AttrDict( # this one shouldn't get compiled source_short_path='another.py', target_short_path=get_python_noarch_target_path('another.py', sp_dir), ), ] axns = CompileMultiPycAction.create_actions(transaction_context, package_info, self.prefix, None, file_link_actions) assert len(axns) == 1 axn = axns[0] source_full_paths = tuple(axn.source_full_paths) source_full_path0 = source_full_paths[0] source_full_path1 = source_full_paths[1] assert len(source_full_paths) == 2 assert source_full_path0 == join(self.prefix, win_path_ok(get_python_noarch_target_path('site-packages/something.py', sp_dir))) assert source_full_path1 == join(self.prefix, win_path_ok(get_python_noarch_target_path('site-packages/another.py', sp_dir))) target_full_paths = tuple(axn.target_full_paths) target_full_path0 = target_full_paths[0] target_full_path1 = target_full_paths[1] assert len(target_full_paths) == 2 assert target_full_path0 == join(self.prefix, win_path_ok(pyc_path(get_python_noarch_target_path('site-packages/something.py', sp_dir), target_python_version))) assert target_full_path1 == join(self.prefix, win_path_ok(pyc_path(get_python_noarch_target_path('site-packages/another.py', sp_dir), target_python_version))) # make .py file in prefix that will be compiled mkdir_p(dirname(source_full_path0)) with open(source_full_path0, 'w') as fh: fh.write("value = 42\n") mkdir_p(dirname(source_full_path1)) with open(source_full_path1, 'w') as fh: fh.write("value = 43\n") # symlink the current python python_full_path = join(self.prefix, get_python_short_path(target_python_version)) mkdir_p(dirname(python_full_path)) create_link(sys.executable, python_full_path, LinkType.softlink) axn.execute() assert isfile(target_full_path0) assert isfile(target_full_path1) # remove the source .py file so we're sure we're importing the pyc file below rm_rf(source_full_path0) assert not isfile(source_full_path0) rm_rf(source_full_path1) assert not isfile(source_full_path1) imported_pyc_file = load_python_file(target_full_path0) assert imported_pyc_file.value == 42 imported_pyc_file = load_python_file(target_full_path1) assert imported_pyc_file.value == 43 axn.reverse() assert not isfile(target_full_path0) assert not isfile(target_full_path1)
def test_scrapy_py36_osx_whl(): anchor_file = "lib/python3.6/site-packages/Scrapy-1.5.1.dist-info/RECORD" prefix_path = join(ENV_METADATA_DIR, "py36-osx-whl") if not isdir(prefix_path): pytest.skip("test files not found: %s" % prefix_path) prefix_rec = read_python_record(prefix_path, anchor_file, "3.6") dumped_rec = json_load(json_dump(prefix_rec.dump())) files = dumped_rec.pop("files") paths_data = dumped_rec.pop("paths_data") print(json_dump(dumped_rec)) assert dumped_rec == { "build": "pypi_0", "build_number": 0, "channel": "https://conda.anaconda.org/pypi", "constrains": [], "depends": [ "cssselect >=0.9", "lxml", "parsel >=1.1", "pydispatcher >=2.0.5", "pyopenssl", "python 3.6.*", "queuelib", "service-identity", "six >=1.5.2", "twisted >=13.1.0", "w3lib >=1.17.0" ], "fn": "Scrapy-1.5.1.dist-info", "name": "scrapy", "package_type": "virtual_python_wheel", "subdir": "pypi", "version": "1.5.1" } print(json_dump(files)) print(json_dump(paths_data["paths"])) sp_dir = get_python_site_packages_short_path("3.6") assert sp_dir + "/scrapy/core/scraper.py" in files assert sp_dir + "/scrapy/core/__pycache__/scraper.cpython-36.pyc" in files pd1 = { "_path": sp_dir + "/scrapy/core/scraper.py", "path_type": "hardlink", "sha256": "2559X9n2z1YKdFV9ElMRD6_88LIdqH1a2UwQimStt2k", "size_in_bytes": 9960 } assert pd1 in paths_data["paths"] pd2 = { "_path": sp_dir + "/scrapy/core/__pycache__/scraper.cpython-36.pyc", "path_type": "hardlink", "sha256": None, "size_in_bytes": None } assert pd2 in paths_data["paths"] pd3 = { "_path": "../bin/scrapy" if on_win else "bin/scrapy", "path_type": "hardlink", "sha256": "RncAAoxSEnSi_0VIopaRxsq6kryQGL61YbEweN2TW3g", "size_in_bytes": 268 } assert pd3 in paths_data["paths"]