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

Add hooks to support PyUSB #6

Merged
merged 8 commits into from
Apr 15, 2012
Merged

Add hooks to support PyUSB #6

merged 8 commits into from
Apr 15, 2012

Conversation

itszero
Copy link
Contributor

@itszero itszero commented Apr 14, 2012

PyUSB use ctypes.util.find_library to load one of its backend library (libusb1.0, libusb0.1, openusb). It is not supported by the pyinstaller's code scanner so the dependent library will not being picked up by pyinstaller.

I wrote a package-time hook to collect one of the available library into the bundle and a run-time hook to make sure it'll pick up the bundled copy not the system one.

This patch has been tested on Win32, Mac, Linux and works on them.

@htgoebel
Copy link
Member

This is great work! It's seldom one takes the time implementing a more complex hook. Thanks a lot!

@itszero
Copy link
Contributor Author

itszero commented Apr 14, 2012

hi, I updated my code by your comments. :)

@itszero
Copy link
Contributor Author

itszero commented Apr 14, 2012

I'm not quiet familiar with pull request, so not sure about should I add a another commit for fixing those or not. I did a rebase on it with those fixes so the commits rev numbers are now changed.

@matysek
Copy link
Member

matysek commented Apr 14, 2012

What python version have you tested it with?

@matysek
Copy link
Member

matysek commented Apr 14, 2012

It looks mostly ok to me.

# NOTE: mind updating run-time hook when adding further libs
libusb_candidates = (
# libusb10
'usb-1.0', 'usb', 'libusb-1.0',
Copy link
Member

Choose a reason for hiding this comment

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

Method _resolveCtypesImports was not able to resolve these libraries?
Do you have an idea how it could be fixed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that method work alright, I actually used that method later to resolve the library once I figured out which one to be included.

The problem is that libusb used this syntax to load library:

def _load_library():
    candidates = ('usb-1.0', 'libusb-1.0', 'usb')
    for candidate in candidates:
        libname = ctypes.util.find_library(candidate)
        if libname is not None: break
    else:
        # ...
    # Windows backend uses stdcall calling convention
    if sys.platform == 'win32':
        l = WinDLL(libname)
    else:
        l = CDLL(libname)
    # ...
    return l

The filename given in CDLL or WinDLL is not a constant, so the code scanner in pyinstaller can't figured out what has been passed in. As for fixes, I don't see how this can be easily fixed. It may require a complex static analysis to make sure things go right in my opinion.

@itszero
Copy link
Contributor Author

itszero commented Apr 14, 2012

I tested with Python 2.5 on Win32, Python 2.7.1 on Mac OS X and Python 2.7.2 on Linux.

@matysek
Copy link
Member

matysek commented Apr 14, 2012

Do you have any simple code example to create a test case from that? This test case would be a part of our test suite could be run regularly in our continuous integration system.

# from ctypes.util.find_library.
bins = [os.path.basename(libname)]
mod.binaries.extend(_resolveCtypesImports(bins))
elif sys.platform == 'cygwin':
Copy link
Member

Choose a reason for hiding this comment

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

Here could be used code like

from PyInstaller.compat import is_cygwin

@itszero
Copy link
Contributor Author

itszero commented Apr 15, 2012

Hi, I tried to address all of the issues mentioned before. Please take a look in case I missed something. :)

and... I found a bug in the pyinstaller cause the cygwin library lookup might not work sometimes. cygusb-1.0.dll(and cygusb0.dll, if libusb 0.1 is installed) is put into /usr/bin (can be obtained/verified by install libusb1.0 pkg in cygwin) and this library cannot be resolved by _resolveCtypesImports. I removed cygwin part code in my package hook since it's written as constant in PyUSB, so it should be work just fine if the pyinstaller's _resolveCtypesImports works.

@itszero
Copy link
Contributor Author

itszero commented Apr 15, 2012

>>> _resolveCtypesImports(['cygusb-1.0.dll', 'cygusb0.dll'])
WARNING: library cygusb-1.0.dll required via ctypes not found
WARNING: library cygusb0.dll required via ctypes not found
[]
~ $ ls /usr/bin/cygusb*
/usr/bin/cygusb-1.0.dll

matysek pushed a commit that referenced this pull request Apr 15, 2012
Add: Add hooks to support PyUSB.
@matysek matysek merged commit 48cf33b into pyinstaller:develop Apr 15, 2012
htgoebel pushed a commit to htgoebel/pyinstaller that referenced this pull request May 24, 2014
More Python 3 fixes and Python 3.4 compatibility.
xoviat pushed a commit to xoviat/pyinstaller that referenced this pull request Apr 7, 2021
xoviat pushed a commit to xoviat/pyinstaller that referenced this pull request Apr 8, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants