raimon49 / pip-licenses

Dump the license list of packages installed with pip.
MIT License
307 stars 43 forks source link

Error when a package stores the license file in a subdirectory #121

Closed vaiojarsad closed 2 years ago

vaiojarsad commented 2 years ago

My project started pulling version 2.5.2 for platformdirs. This platformdirs version started storing the LICENSE.txt file in a subdir (.\venv\Lib\site-packages\platformdirs-2.5.2.dist-info\license_files).

On line 171, (piplicense.py) pip-licenses tries to open the path (that it's a dir) as a "regular" file and it fails with a "PermissionError: [Errno 13] Permission denied" error:

Traceback (most recent call last): File "c:\pablo\python\python38\lib\runpy.py", line 194, in _run_module_as_main return _run_code(code, main_globals, None, File "c:\pablo\python\python38\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "C:\Proyectos\AttackIQ\agent\aiq_agent_go\attackgraph\venv\Scripts\pip-licenses.exe\__main__.py", line 7, in <module> File "C:\Proyectos\AttackIQ\agent\aiq_agent_go\attackgraph\venv\lib\site-packages\piplicenses.py", line 893, in main output_string = create_output_string(args) File "C:\Proyectos\AttackIQ\agent\aiq_agent_go\attackgraph\venv\lib\site-packages\piplicenses.py", line 551, in create_output_string table = create_licenses_table(args, output_fields) File "C:\Proyectos\AttackIQ\agent\aiq_agent_go\attackgraph\venv\lib\site-packages\piplicenses.py", line 296, in create_licenses_table for pkg in get_packages(args): File "C:\Proyectos\AttackIQ\agent\aiq_agent_go\attackgraph\venv\lib\site-packages\piplicenses.py", line 258, in get_packages pkg_info = get_pkg_info(pkg) File "C:\Proyectos\AttackIQ\agent\aiq_agent_go\attackgraph\venv\lib\site-packages\piplicenses.py", line 178, in get_pkg_info (license_file, license_text) = get_pkg_included_file( File "C:\Proyectos\AttackIQ\agent\aiq_agent_go\attackgraph\venv\lib\site-packages\piplicenses.py", line 171, in get_pkg_included_file with open(test_file, encoding='utf-8', PermissionError: [Errno 13] Permission denied: 'c:\\proyectos\\attackiq\\agent\\aiq_agent_go\\attackgraph\\venv\\lib\\site-packages\\platformdirs-2.5.2.dist-info\\license_files'

raimon49 commented 2 years ago

Thanks for the report.

It did not happen in my macOS or Linux environment.

$ pip-licenses -l --format=json
[
  {
    "License": "MIT License",
    "LicenseFile": "UNKNOWN",
    "LicenseText": "UNKNOWN",
    "Name": "platformdirs",
    "Version": "2.5.2"
  }
]

However, I have not been able to successfully discover site-packages/platformdirs-2.5.2.dist-info/license_files/LICENSE.txt, and I will need to work with the improvements in the Windows environment.

vaiojarsad commented 2 years ago

Hi raimon49! Thank you so much for your quick feedback.

Yes, it's a Windows-only issue (I mean the error... the logic not being able to find the license file in *nix is another related issue)...

You are calling get_pkg_included_file with ('LICENSE', 'LICENCE', 'COPYING*') as an argument for file_names.

In *nix (filesystem names are case sensitive) there is no match against the license_files subdirectory (so we get no error), but in Windows (filesystem names are case insensitive), there is a match and the logic is trying to open the subdirectory as a regular file (open call).

Best!

Pablo

eUgEntOptIc44 commented 2 years ago

Hi @raimon49 @vaiojarsad

looks like I found a bug fix for this issue:

for test_file in patterns:
         if os.path.exists(test_file) and os.path.isdir(test_file) is not True: # added 'and os.path.isdir(test_file) is not True' to fix #121
                included_file = test_file
                with open(test_file, encoding='utf-8',
                          errors='backslashreplace') as included_file_handle:
                    included_text = included_file_handle.read()
                break
        return (included_file, included_text)

Afterwards I it did run flawless. Hope this might help you.

eUgEntOptIc44 commented 2 years ago

I previously wondered why there's little activity happening in the last months. Well after experiencing the maintainers strange behaviour in #122 leading to this issue still not being resolved it does not surprise me anymore. I'll leave the both of you here. Hope this might be resolved at some point in the future. But I definitely won't be involved in the process anymore. I've been a happy user of this project until today. Goodbye

raimon49 commented 2 years ago

@vaiojarsad We have released version 3.5.4, which incorporates suggestions to resolve this issue. https://pypi.org/project/pip-licenses/3.5.4/

I do not have a Windows environment to try it out right away. Could you please try when you have time to see if your problem has been resolved?

vaiojarsad commented 2 years ago

@raimon49 Fixed!

Thank you!

Best!