conan-io / conan-extensions

Some extra Conan commands for different purposes, like artifactory tasks, conan-center-index, etc
MIT License
28 stars 26 forks source link

[bug] licenses deployer can't handle subdirectories in licenses directory #161

Open lia-viam opened 1 day ago

lia-viam commented 1 day ago

The issue shows up for packages where the licenses directory has subdirectories in addition to a text file. For example

~/.conan2/p/nlohm0567ffc90cfc1/p/licenses/
├── LICENSE.MIT
└── LICENSES
    ├── Apache-2.0.txt
    ├── BSD-3-Clause.txt
    ├── GPL-3.0-only.txt
    └── MIT.txt
~/.conan2/p/b/opens7feba37473515/p/licenses
├── LICENSE.txt
└── external
    └── perl
        └── Text-Template-1.56
            └── LICENSE

Here is some sample output from conan install --requires=nlohmann_json/3.11.3 --deployer=licenses -vvv, omitting the stuff at the beginning with profiles and such

<snip>
======== Installing packages ========
nlohmann_json/3.11.3: Already installed! (1 of 1)

======== Finalizing install (deploy, generators) ========
deployer(licenses): /Users/lia/.conan2/p/nlohm0567ffc90cfc1/p/licenses
deployer(licenses): /Users/lia/repos/licenses-test/licenses/nlohmann_json/3.11.3
cli: copy(pattern=LICENSE.MIT) copied 1 files
  from /Users/lia/.conan2/p/nlohm0567ffc90cfc1/p/licenses/
  to /Users/lia/repos/licenses-test/licenses/nlohmann_json/3.11.3
  Files:
    LICENSE.MIT
deployer(licenses): /Users/lia/.conan2/p/nlohm0567ffc90cfc1/p/licenses
deployer(licenses): /Users/lia/repos/licenses-test/licenses/nlohmann_json/3.11.3
cli: copy(pattern=LICENSES) copied 0 files
  from /Users/lia/.conan2/p/nlohm0567ffc90cfc1/p/licenses/
  to /Users/lia/repos/licenses-test/licenses/nlohmann_json/3.11.3
  Files:

deployer(licenses): ['nlohmann_json/3.11.3/LICENSE.MIT', 'nlohmann_json/3.11.3/LICENSES']
Traceback (most recent call last):
  File "/Users/lia/repos/envs/conan1/lib/python3.12/site-packages/conan/cli/cli.py", line 193, in run
    command.run(self._conan_api, args[0][1:])
  File "/Users/lia/repos/envs/conan1/lib/python3.12/site-packages/conan/cli/command.py", line 171, in run
    info = self._method(conan_api, parser, *args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lia/repos/envs/conan1/lib/python3.12/site-packages/conan/cli/commands/install.py", line 75, in install
    conan_api.install.install_consumer(deps_graph, args.generator, source_folder, output_folder,
  File "/Users/lia/repos/envs/conan1/lib/python3.12/site-packages/conan/api/subapi/install.py", line 79, in install_consumer
    do_deploys(self.conan_api, deps_graph, deploy, deploy_package, base_folder)
  File "/Users/lia/repos/envs/conan1/lib/python3.12/site-packages/conan/internal/deploy.py", line 74, in do_deploys
    deployer(graph=graph, output_folder=deploy_folder)
  File "/Users/lia/.conan2/extensions/deployers/licenses.py", line 41, in deploy
    licenses_zip.write(file, arcname=f, compress_type=zipfile.ZIP_DEFLATED)
  File "/opt/homebrew/Cellar/python@3.12/3.12.6/Frameworks/Python.framework/Versions/3.12/lib/python3.12/zipfile/__init__.py", line 1841, in write
    zinfo = ZipInfo.from_file(filename, arcname,
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.6/Frameworks/Python.framework/Versions/3.12/lib/python3.12/zipfile/__init__.py", line 557, in from_file
    st = os.stat(filename)
         ^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '/Users/lia/repos/licenses-test/licenses/nlohmann_json/3.11.3/LICENSES'

ERROR: Traceback (most recent call last):
  File "/Users/lia/repos/envs/conan1/lib/python3.12/site-packages/conan/cli/cli.py", line 294, in main
    cli.run(args)
  File "/Users/lia/repos/envs/conan1/lib/python3.12/site-packages/conan/cli/cli.py", line 193, in run
    command.run(self._conan_api, args[0][1:])
  File "/Users/lia/repos/envs/conan1/lib/python3.12/site-packages/conan/cli/command.py", line 171, in run
    info = self._method(conan_api, parser, *args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lia/repos/envs/conan1/lib/python3.12/site-packages/conan/cli/commands/install.py", line 75, in install
    conan_api.install.install_consumer(deps_graph, args.generator, source_folder, output_folder,
  File "/Users/lia/repos/envs/conan1/lib/python3.12/site-packages/conan/api/subapi/install.py", line 79, in install_consumer
    do_deploys(self.conan_api, deps_graph, deploy, deploy_package, base_folder)
  File "/Users/lia/repos/envs/conan1/lib/python3.12/site-packages/conan/internal/deploy.py", line 74, in do_deploys
    deployer(graph=graph, output_folder=deploy_folder)
  File "/Users/lia/.conan2/extensions/deployers/licenses.py", line 41, in deploy
    licenses_zip.write(file, arcname=f, compress_type=zipfile.ZIP_DEFLATED)
  File "/opt/homebrew/Cellar/python@3.12/3.12.6/Frameworks/Python.framework/Versions/3.12/lib/python3.12/zipfile/__init__.py", line 1841, in write
    zinfo = ZipInfo.from_file(filename, arcname,
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.6/Frameworks/Python.framework/Versions/3.12/lib/python3.12/zipfile/__init__.py", line 557, in from_file
    st = os.stat(filename)
         ^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '/Users/lia/repos/licenses-test/licenses/nlohmann_json/3.11.3/LICENSES'

ERROR: [Errno 2] No such file or directory: '/Users/lia/repos/licenses-test/licenses/nlohmann_json/3.11.3/LICENSES'
AbrilRBS commented 1 day ago

Thanks for the report @lia-viam, we appreciate it :)

I've proposed https://github.com/conan-io/conan-extensions/pull/162 to fix this issue, and took the chance to clean up some of the code.

memsharded commented 1 day ago

Hi @lia-viam

While https://github.com/conan-io/conan-extensions/pull/162 fixes this, I'd also like to recommend that a good way to use the extensions from this repo is actually to fork them, and use them from your fork. This way you can both customize them at your will if necessary, but it will also be more robust in case these extensions change in the future in some way that might break your usage, as the extensions are not subject to the stability commitment of the Conan client.