Skip to content

Commit 00493e8

Browse files
committedApr 18, 2023
Add a list of packages to the licenses folder
This can be used for license compliance purposes.
1 parent 8581932 commit 00493e8

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed
 

‎changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v1.5.2 | 2023-04-18
4+
5+
- Added a list of all installed packages to `licenses/packages.txt`.
6+
37
## v1.5.1 | 2023-03-13
48

59
- Fixed bug where embedded_python will only use local `zlib` instead of the `tool_requires` one.

‎conanfile.py

+32-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import pathlib
3+
import re
34
from conan import ConanFile
45
from conan.tools.files import get, replace_in_file, download, rmdir, copy
56
from conan.tools.scm import Version
@@ -10,7 +11,7 @@
1011
# noinspection PyUnresolvedReferences
1112
class EmbeddedPython(ConanFile):
1213
name = "embedded_python"
13-
version = "1.5.1" # of the Conan package, `options.version` is the Python version
14+
version = "1.5.2" # of the Conan package, `options.version` is the Python version
1415
license = "PSFL"
1516
description = "Embedded distribution of Python"
1617
topics = "embedded", "python"
@@ -86,16 +87,11 @@ def pyver(self):
8687
"""Two-digit integer version, e.g. 3.7.3 -> 37"""
8788
return "".join(self.pyversion.split(".")[:2])
8889

89-
def make_requirements_file(self, extra_packages=None):
90-
"""Create a `requirements.txt` based on `self.options.packages` and return its path
91-
92-
We accept `self.options.packages` as either a space-separated list of packages (as
93-
you would pass to `pip install <packages>`) or the full contents of a `requirements.txt`
94-
file (as you would pass to `pip install -r <file>`). But in either case, we generate
95-
a `requirements.txt` file internally for installation.
9690

97-
The `extra_packages` can be used to add extra packages (as a Python `list`) to be
98-
installed in addition to `self.options.packages`.
91+
def make_package_list(self):
92+
"""Create a list of package names based on `self.options.packages`
93+
94+
For details of the `self.options.packages` format see `make_requirements_file`
9995
"""
10096

10197
def split_lines(string):
@@ -110,7 +106,21 @@ def split_lines(string):
110106
return string.split(" ")
111107

112108
packages_str = str(self.options.packages).strip()
113-
packages_list = split_lines(packages_str)
109+
return split_lines(packages_str)
110+
111+
112+
def make_requirements_file(self, extra_packages=None):
113+
"""Create a `requirements.txt` based on `self.options.packages` and return its path
114+
115+
We accept `self.options.packages` as either a space-separated list of packages (as
116+
you would pass to `pip install <packages>`) or the full contents of a `requirements.txt`
117+
file (as you would pass to `pip install -r <file>`). But in either case, we generate
118+
a `requirements.txt` file internally for installation.
119+
120+
The `extra_packages` can be used to add extra packages (as a Python `list`) to be
121+
installed in addition to `self.options.packages`.
122+
"""
123+
packages_list = self.make_package_list()
114124
if extra_packages:
115125
packages_list.extend(extra_packages)
116126

@@ -136,6 +146,15 @@ def _gather_licenses(self, bootstrap):
136146
f" --with-license-file --no-license-path --output-file=package_licenses.txt"
137147
)
138148

149+
def _gather_packages(self):
150+
"""Gather all the required packages into a file for future reference"""
151+
matcher = re.compile(r"^([\w.-]+)==[\w.-]+$")
152+
matches = map(matcher.match, self.make_package_list())
153+
package_names = (match.group(1) for match in filter(None, matches))
154+
with open("packages.txt", "w") as output:
155+
output.write("\n".join(package_names))
156+
157+
139158
def source(self):
140159
replace_in_file(self, "embedded_python.cmake", "${self.pyversion}", str(self.pyversion))
141160

@@ -160,6 +179,7 @@ def build(self):
160179
self.build_helper.enable_site_packages()
161180
bootstrap = self.build_helper.build_bootstrap()
162181
self._gather_licenses(bootstrap)
182+
self._gather_packages()
163183

164184
# Some modules always assume that `setuptools` is installed (e.g. pytest)
165185
requirements = self.make_requirements_file(
@@ -176,6 +196,7 @@ def package(self):
176196
copy(self, "embedded_python*", src, self.package_folder)
177197
copy(self, "embedded_python/LICENSE.txt", src, license_folder, keep_path=False)
178198
copy(self, "package_licenses.txt", src, license_folder, keep_path=False)
199+
copy(self, "packages.txt", src, license_folder, keep_path=False)
179200

180201
def package_info(self):
181202
self.env_info.PYTHONPATH.append(self.package_folder)

‎test_package/conanfile.py

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def _test_licenses(self):
6363
license_files = [license_dir / "LICENSE.txt"]
6464
if self.options.env:
6565
license_files += [license_dir / "package_licenses.txt"]
66+
license_files += [license_dir / "packages.txt"]
6667

6768
for file in license_files:
6869
print(f"{file}: {file.stat().st_size}")

0 commit comments

Comments
 (0)
Please sign in to comment.