Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify license deployer, support nested folders #162

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 17 additions & 30 deletions extensions/deployers/licenses.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,29 @@
import os
import zipfile
from conan.api.output import ConanOutput
from conan.tools.files import copy, rmdir


# USE **KWARGS to be robust against changes
def deploy(graph, output_folder, **kwargs):
out = ConanOutput("deployer(licenses)")
conanfile = graph.root.conanfile
files = []

# Cleanup before we start :)
tmp_dir = os.path.join(output_folder, "licenses")
if os.path.exists(tmp_dir):
rmdir(conanfile, tmp_dir)

# For each dep
for r, d in conanfile.dependencies.items():
if d.package_folder is None:
continue

# Look for a licenses folder
search_dir = os.path.join(d.package_folder, "licenses") # This is the CCI convention
if not os.path.isdir(search_dir):
continue
with zipfile.ZipFile(os.path.join(output_folder, 'licenses.zip'), 'w') as licenses_zip:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like somehow that the deployer has separated the recollection and the zipping, so users that don't want the final zip, they can just remove those lines from their fork of this deployer.

But I think it is also good as this proposes, not a big deal, clear enough ,and it handles the nested folder, so already an improvement over the existing one, good for me.

for r, d in conanfile.dependencies.items():
if d.package_folder is None:
continue

# Grab all the files and copy them to a temp dir (this is what we will zip)
for f in os.listdir(search_dir):
src = os.path.join(search_dir)
# Let's kep the name and version so we know which belongs to whats
dst = os.path.join(tmp_dir, str(d.ref.name), str(d.ref.version))
out.debug(src)
out.debug(dst)
copy(conanfile, f, src, dst) # Using the conan help because it make's parent folders
files.append(os.path.join(str(d.ref.name), str(d.ref.version), f))
# Look for a licenses folder
search_dir = os.path.join(d.package_folder, "licenses") # This is the CCI convention
if not os.path.isdir(search_dir):
continue

out.trace(files)
with zipfile.ZipFile(os.path.join(output_folder, 'licenses.zip'), 'w') as licenses_zip:
for f in files:
file = os.path.join(tmp_dir, f)
licenses_zip.write(file, arcname=f, compress_type=zipfile.ZIP_DEFLATED)
os.remove(file) # Delete all the files we copied! This is so the source control stays clean
# Grab all the files and write them in the zipfile
for root, _, files in os.walk(search_dir):
# Let's keep the name and version so we know which belongs to what
for file in files:
src = os.path.join(root, file)
dst = os.path.join(str(d.ref.name), str(d.ref.version), os.path.relpath(root, search_dir), file)
out.debug(f"Copying {src} to {dst}")
licenses_zip.write(src, arcname=dst, compress_type=zipfile.ZIP_DEFLATED)
5 changes: 5 additions & 0 deletions tests/test_deploy_licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def conan_test():
os.environ.clear()
os.environ.update(old_env)


def test_deploy_licenses():
repo = os.path.join(os.path.dirname(__file__), "..")
run(f"conan config install {repo}")
Expand All @@ -38,6 +39,8 @@ class Pkg(ConanFile):

def package(self):
save(self, os.path.join(self.package_folder, "licenses", "hello.txt"), "example licenses")
save(self, os.path.join(self.package_folder, "licenses", "extra", "license2.txt"), "example licenses")
save(self, os.path.join(self.package_folder, "licenses", "extra", "tooling", "license.txt"), "example licenses")
""")

# Let's build a application to bundle
Expand All @@ -48,3 +51,5 @@ def package(self):
run("conan install --requires hello/0.1 --deployer=licenses")
shutil.unpack_archive("licenses.zip", "zip_contents")
assert os.path.isfile(os.path.join("zip_contents", "hello", "0.1", "hello.txt"))
assert os.path.isfile(os.path.join("zip_contents", "hello", "0.1", "extra", "license2.txt"))
assert os.path.isfile(os.path.join("zip_contents", "hello", "0.1", "extra", "tooling", "license.txt"))
Loading