Skip to content

Commit 1af6d15

Browse files
authored
Merge pull request #289 from nexB/280-index-conan-packages
Support conan package indexing
2 parents b5d0713 + 4cc823b commit 1af6d15

7 files changed

Lines changed: 472 additions & 0 deletions

File tree

minecode/tests/test_conan.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# purldb is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/nexB/purldb for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
11+
import os
12+
13+
import saneyaml
14+
from django.test import TestCase
15+
from mock import patch
16+
from packageurl import PackageURL
17+
18+
import packagedb
19+
from minecode.utils_test import JsonBasedTesting
20+
from minecode.visitors import conan
21+
22+
23+
class ConanPriorityQueueTests(JsonBasedTesting, TestCase):
24+
test_data_dir = os.path.join(os.path.dirname(__file__), "testfiles")
25+
26+
def setUp(self):
27+
super(ConanPriorityQueueTests, self).setUp()
28+
self.package_url1 = PackageURL.from_string("pkg:conan/zlib@1.3.1")
29+
zlib_conanfile_loc = self.get_test_loc("conan/zlib/manifest/conanfile.py")
30+
zlib_conandata_loc = self.get_test_loc("conan/zlib/manifest/conandata.yml")
31+
zlib_config_loc = self.get_test_loc("conan/zlib/manifest/config.yml")
32+
33+
with open(zlib_conanfile_loc) as f:
34+
self.zlib_conanfile_contents = f.read()
35+
36+
with open(zlib_config_loc) as f:
37+
self.zlib_config_contents = f.read()
38+
39+
with open(zlib_conandata_loc) as f:
40+
self.zlib_conandata_contents = f.read()
41+
42+
self.zlib_conandata_contents_dict = saneyaml.load(self.zlib_conandata_contents)
43+
44+
@patch("requests.get")
45+
def test_get_conan_recipe(self, mock_get):
46+
mock_get.side_effect = [
47+
type(
48+
"Response",
49+
(),
50+
{
51+
"content": self.zlib_config_contents.encode(),
52+
"raise_for_status": lambda: None,
53+
},
54+
),
55+
type(
56+
"Response",
57+
(),
58+
{
59+
"content": self.zlib_conandata_contents.encode(),
60+
"raise_for_status": lambda: None,
61+
},
62+
),
63+
type(
64+
"Response",
65+
(),
66+
{
67+
"text": self.zlib_conanfile_contents,
68+
"raise_for_status": lambda: None,
69+
},
70+
),
71+
]
72+
result_conanfile, result_conandata = conan.get_conan_recipe(
73+
self.package_url1.name, self.package_url1.version
74+
)
75+
76+
self.assertEqual(result_conanfile, self.zlib_conanfile_contents)
77+
self.assertEqual(result_conandata, self.zlib_conandata_contents_dict)
78+
79+
def test_get_download_info(self):
80+
result_download_url, result_sha256 = conan.get_download_info(
81+
self.zlib_conandata_contents_dict, self.package_url1.version
82+
)
83+
expected_zlib_download_url = "https://zlib.net/fossils/zlib-1.3.1.tar.gz"
84+
expected_zlib_sha256 = (
85+
"9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23"
86+
)
87+
88+
self.assertEqual(result_download_url, expected_zlib_download_url)
89+
self.assertEqual(result_sha256, expected_zlib_sha256)
90+
91+
@patch("minecode.visitors.conan.get_conan_recipe")
92+
def test_map_conan_package(self, mock_get_conan_recipe):
93+
mock_get_conan_recipe.return_value = (
94+
self.zlib_conanfile_contents,
95+
self.zlib_conandata_contents_dict,
96+
)
97+
98+
package_count = packagedb.models.Package.objects.all().count()
99+
self.assertEqual(package_count, 0)
100+
101+
conan.map_conan_package(self.package_url1)
102+
package_count = packagedb.models.Package.objects.all().count()
103+
self.assertEqual(package_count, 1)
104+
package = packagedb.models.Package.objects.all().first()
105+
expected_zlib_download_url = "https://zlib.net/fossils/zlib-1.3.1.tar.gz"
106+
107+
self.assertEqual(package.purl, str(self.package_url1))
108+
self.assertEqual(package.download_url, expected_zlib_download_url)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
about_resource: manifest/
2+
name: conan-center-index
3+
version: 30c5f6b4aeeb9bce3614b9b2f1db7ea3324d8cdc
4+
download_url: https://github.com/conan-io/conan-center-index/tree/30c5f6b4aeeb9bce3614b9b2f1db7ea3324d8cdc/recipes/zlib/
5+
vcs_repository: https://github.com/conan-io/conan-center-index.git
6+
license: mit
7+
license_text: manifest.LICENSE
8+
description: Recipes for the ConanCenter repository.
9+
notes: recipe used for testing conan package indexing
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Conan.io
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
sources:
2+
"1.3.1":
3+
url:
4+
- "https://zlib.net/fossils/zlib-1.3.1.tar.gz"
5+
- "https://github.com/madler/zlib/releases/download/v1.3/zlib-1.3.1.tar.gz"
6+
sha256: "9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23"
7+
"1.3":
8+
url:
9+
- "https://zlib.net/fossils/zlib-1.3.tar.gz"
10+
- "https://github.com/madler/zlib/releases/download/v1.3/zlib-1.3.tar.gz"
11+
sha256: "ff0ba4c292013dbc27530b3a81e1f9a813cd39de01ca5e0f8bf355702efa593e"
12+
"1.2.13":
13+
url:
14+
- "https://zlib.net/fossils/zlib-1.2.13.tar.gz"
15+
- "https://github.com/madler/zlib/releases/download/v1.2.13/zlib-1.2.13.tar.gz"
16+
sha256: "b3a24de97a8fdbc835b9833169501030b8977031bcb54b3b3ac13740f846ab30"
17+
"1.2.12":
18+
url: "https://zlib.net/fossils/zlib-1.2.12.tar.gz"
19+
sha256: "91844808532e5ce316b3c010929493c0244f3d37593afd6de04f71821d5136d9"
20+
"1.2.11":
21+
url: "https://zlib.net/fossils/zlib-1.2.11.tar.gz"
22+
sha256: "c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1"
23+
patches:
24+
"1.3.1":
25+
- patch_file: "patches/1.3.1/0001-fix-cmake.patch"
26+
patch_description: "separate static/shared builds, disable debug suffix"
27+
patch_type: "conan"
28+
"1.3":
29+
- patch_file: "patches/1.3/0001-fix-cmake.patch"
30+
patch_description: "separate static/shared builds, disable debug suffix, disable building examples"
31+
patch_type: "conan"
32+
"1.2.13":
33+
- patch_file: "patches/1.2.13/0001-Fix-cmake.patch"
34+
patch_description: "separate static/shared builds, disable debug suffix, disable building examples"
35+
patch_type: "conan"
36+
"1.2.12":
37+
- patch_file: "patches/1.2.x/0001-fix-cmake.patch"
38+
patch_description: "separate static/shared builds, disable debug suffix, disable building examples"
39+
patch_type: "conan"
40+
- patch_file: "patches/1.2.x/0004-Fix-a-bug-when-getting-a-gzip-header-extra-field-wit.patch"
41+
patch_description: "CVE-2022-37434: Fix a bug when getting a gzip header extra field with inflate()"
42+
patch_type: "vulnerability"
43+
patch_source: "https://github.com/madler/zlib/commit/eff308af425b67093bab25f80f1ae950166bece1"
44+
sha256: "15e3c177dc2a034a22e02490a97ba5b1719aae3f8129a06c16d727b661d1650f"
45+
- patch_file: "patches/1.2.x/0005-Fix-extra-field-processing-bug-that-dereferences-NUL.patch"
46+
patch_description: "CVE-2022-37434: Fix extra field processing bug that dereferences NULL state->head"
47+
patch_type: "vulnerability"
48+
patch_source: "https://github.com/madler/zlib/commit/1eb7682f845ac9e9bf9ae35bbfb3bad5dacbd91d"
49+
sha256: "cdd69eb3251728b1875c8ecae6427b50aa750b4045ef984ab79b6c07b7e6dd3a"
50+
"1.2.11":
51+
- patch_file: "patches/1.2.x/0001-fix-cmake.patch"
52+
patch_description: "separate static/shared builds, disable debug suffix, disable building examples"
53+
patch_type: "conan"
54+
- patch_file: "patches/1.2.x/0003-gzguts-fix-widechar-condition.patch"
55+
patch_description: "fix condition for WIDECHAR usage"
56+
patch_type: "portability"
57+
patch_source: "https://github.com/madler/zlib/issues/268"
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
class ConanFile:
2+
pass
3+
4+
required_conan_version = ">=1.53.0"
5+
6+
7+
class ZlibConan(ConanFile):
8+
name = "zlib"
9+
package_type = "library"
10+
url = "https://github.com/conan-io/conan-center-index"
11+
homepage = "https://zlib.net"
12+
license = "Zlib"
13+
description = ("A Massively Spiffy Yet Delicately Unobtrusive Compression Library "
14+
"(Also Free, Not to Mention Unencumbered by Patents)")
15+
topics = ("zlib", "compression")
16+
17+
settings = "os", "arch", "compiler", "build_type"
18+
options = {
19+
"shared": [True, False],
20+
"fPIC": [True, False],
21+
}
22+
default_options = {
23+
"shared": False,
24+
"fPIC": True,
25+
}
26+
27+
@property
28+
def _is_mingw(self):
29+
return self.settings.os == "Windows" and self.settings.compiler == "gcc"
30+
31+
def export_sources(self):
32+
export_conandata_patches(self)
33+
34+
def config_options(self):
35+
if self.settings.os == "Windows":
36+
del self.options.fPIC
37+
38+
def configure(self):
39+
if self.options.shared:
40+
self.options.rm_safe("fPIC")
41+
self.settings.rm_safe("compiler.libcxx")
42+
self.settings.rm_safe("compiler.cppstd")
43+
44+
def layout(self):
45+
cmake_layout(self, src_folder="src")
46+
47+
def source(self):
48+
get(self, **self.conan_data["sources"][self.version],
49+
destination=self.source_folder, strip_root=True)
50+
51+
def generate(self):
52+
tc = CMakeToolchain(self)
53+
tc.variables["SKIP_INSTALL_ALL"] = False
54+
tc.variables["SKIP_INSTALL_LIBRARIES"] = False
55+
tc.variables["SKIP_INSTALL_HEADERS"] = False
56+
tc.variables["SKIP_INSTALL_FILES"] = True
57+
# Correct for misuse of "${CMAKE_INSTALL_PREFIX}/" in CMakeLists.txt
58+
tc.variables["INSTALL_LIB_DIR"] = "lib"
59+
tc.variables["INSTALL_INC_DIR"] = "include"
60+
tc.variables["ZLIB_BUILD_EXAMPLES"] = False
61+
tc.generate()
62+
63+
def _patch_sources(self):
64+
apply_conandata_patches(self)
65+
66+
is_apple_clang12 = self.settings.compiler == "apple-clang" and Version(self.settings.compiler.version) >= "12.0"
67+
if not is_apple_clang12:
68+
for filename in ['zconf.h', 'zconf.h.cmakein', 'zconf.h.in']:
69+
filepath = os.path.join(self.source_folder, filename)
70+
replace_in_file(self, filepath,
71+
'#ifdef HAVE_UNISTD_H '
72+
'/* may be set to #if 1 by ./configure */',
73+
'#if defined(HAVE_UNISTD_H) && (1-HAVE_UNISTD_H-1 != 0)')
74+
replace_in_file(self, filepath,
75+
'#ifdef HAVE_STDARG_H '
76+
'/* may be set to #if 1 by ./configure */',
77+
'#if defined(HAVE_STDARG_H) && (1-HAVE_STDARG_H-1 != 0)')
78+
79+
def build(self):
80+
self._patch_sources()
81+
cmake = CMake(self)
82+
cmake.configure()
83+
cmake.build()
84+
85+
def _extract_license(self):
86+
tmp = load(self, os.path.join(self.source_folder, "zlib.h"))
87+
license_contents = tmp[2:tmp.find("*/", 1)]
88+
return license_contents
89+
90+
def package(self):
91+
save(self, os.path.join(self.package_folder, "licenses", "LICENSE"), self._extract_license())
92+
cmake = CMake(self)
93+
cmake.install()
94+
95+
def package_info(self):
96+
self.cpp_info.set_property("cmake_find_mode", "both")
97+
self.cpp_info.set_property("cmake_file_name", "ZLIB")
98+
self.cpp_info.set_property("cmake_target_name", "ZLIB::ZLIB")
99+
self.cpp_info.set_property("pkg_config_name", "zlib")
100+
if self.settings.os == "Windows" and not self._is_mingw:
101+
libname = "zdll" if self.options.shared else "zlib"
102+
else:
103+
libname = "z"
104+
self.cpp_info.libs = [libname]
105+
106+
self.cpp_info.names["cmake_find_package"] = "ZLIB"
107+
self.cpp_info.names["cmake_find_package_multi"] = "ZLIB"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
versions:
2+
"1.3.1":
3+
folder: all
4+
"1.3":
5+
folder: all
6+
"1.2.13":
7+
folder: all
8+
"1.2.12":
9+
folder: all
10+
"1.2.11":
11+
folder: all

0 commit comments

Comments
 (0)