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

failover to python-magic detection if vendored library not present #17

Closed
wants to merge 1 commit into from

Conversation

tardyp
Copy link
Contributor

@tardyp tardyp commented Mar 31, 2021

This allows scancode to work on platforms not supported by typecode-libmagic
e.g. M1 Macs or windows WSL

100% tests pass with this change on my M1 Mac

Copy link
Member

@pombredanne pombredanne left a comment

Choose a reason for hiding this comment

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

Thank you ++
See some minor nitpicking of mine for your kind consideration.

@@ -79,18 +79,60 @@
TYPECODE_LIBMAGIC_DATABASE = 'typecode.libmagic.db'


def load_lib_failover():
# loader from python-magic
Copy link
Member

Choose a reason for hiding this comment

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

That's nice extra credits but not needed IMHO since Adam is already credited at the top for the whole file.

# Copyright: Adam Hupp
libmagic = None
# Let's try to find magic or magic1
dll = ctypes.util.find_library('magic') \
Copy link
Member

Choose a reason for hiding this comment

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

I am not a big fan of line continuations and end-of-line comments. I reckon you copied the code from upstream, butw what about instead something along these lines:

    dll = (ctypes.util.find_library('magic')
        or ctypes.util.find_library('magic1')
        or ctypes.util.find_library('cygmagic-1')
        or ctypes.util.find_library('libmagic-1')
        or ctypes.util.find_library('msys-magic-1')
    )


if not libmagic or not libmagic._name:
return None
return libmagic
Copy link
Member

Choose a reason for hiding this comment

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

Do you know if there could be a way to test/check the version of this libmagic? Because unless this is close enough to the one provided as a plugin, there could be some magic database inconsistencies.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could do a check, but we don't really know what threshold we should really put there. How would you define close enough?
I would suggest to leave a warning, and let the community report any issue.

We could have a TAINTED flag like linux does which could be put in the typecode dependent reports

Copy link
Member

Choose a reason for hiding this comment

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

Well in hindsight, let's leave it this way.... after all in https://github.com/nexB/scancode-plugins/blob/main/builtins/typecode_libmagic_system_provided/src/typecode_libmagic/__init__.py I do not even check for version

@pombredanne
Copy link
Member

Something that's a head scratcher to me is: where is the magic DB obtained from when not provided by a plugin?

@tardyp tardyp force-pushed the failover branch 2 times, most recently from 5362fc5 to b892dea Compare April 7, 2021 13:57
@tardyp
Copy link
Contributor Author

tardyp commented Apr 7, 2021

as per doc:

 The magic_load() function must be used to load the colon separated
 list of database files passed in as filename, or NULL for the
 default database file before any magic queries can performed.

 The default database file is named by the MAGIC environment
 variable.  If that variable is not set, the default database file
 name is /usr/local/share/misc/magic.  magic_load() adds “.mgc” to
 the database filename as appropriate.

I added the version api which might help in case of a later bug report

This allows scancode to work on platforms not supported by typecode-libmagic
e.g. M1 Macs or windows WSL

Most of this code is from https://github.com/ahupp/python-magic/blob/e0ccc6d/magic/loader.py
by Adam Hupp, MIT License, compatible with typecode's Apache

Signed-off-by: Pierre Tardy <[email protected]>
@tardyp
Copy link
Contributor Author

tardyp commented Apr 12, 2021

@pombredanne shall we merge this? I think you may need to re run flaky azure pipelines.

pombredanne added a commit that referenced this pull request May 30, 2021
@pombredanne
Copy link
Member

Merged in #20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants