Repository: pyinstaller/pyinstaller

Issue:
Can't package pyusb, 'Unable to find "libusb-1.0.so.0"'
To reproduce

1. `virtualenv env`
`source env/bin/activate`
`pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip`
`pip install pyusb`
`pip freeze > requirements.txt`
2. Create simple python file which imports pyusb
test.py
```
import usb.core
print 'Success!
```
3. Run the python file normally
```
$ python test.py
Success!
```
4. Package it with pyinstaller
```
pyinstaller --log-level=DEBUG test.py &> log.txt
```
log.txt
```
14 INFO: PyInstaller: 3.3.dev0+5fd013bfe
14 INFO: Python: 2.7.12+
14 INFO: Platform: Linux-4.8.0-53-generic-x86_64-with-Ubuntu-16.10-yakkety
14 INFO: wrote /home/joel/Code/random/pyinstaller/test.spec
15 DEBUG: Testing for UPX ...
17 INFO: UPX is not available.
17 DEBUG: script: /home/joel/Code/random/pyinstaller/test.py
17 INFO: Extending PYTHONPATH with paths
['/home/joel/Code/random/pyinstaller', '/home/joel/Code/random/pyinstaller']
17 INFO: checking Analysis
17 INFO: Building Analysis because out00-Analysis.toc is non existent
17 INFO: Initializing module dependency graph...
18 INFO: Initializing module graph hooks...
19 DEBUG: Hidden import: codecs
65 DEBUG: Hidden import 'codecs' already found
65 INFO: running Analysis out00-Analysis.toc
65 DEBUG: Analyzing /home/joel/Code/random/pyinstaller/env/bin/python
71 DEBUG: Skipping libdl.so.2 dependency of python
71 DEBUG: Skipping libc.so.6 dependency of python
71 DEBUG: Skipping libutil.so.1 dependency of python
71 DEBUG: Skipping libpthread.so.0 dependency of python
71 DEBUG: Skipping libm.so.6 dependency of python
72 DEBUG: Adding libz.so.1 dependency of python from /lib/x86_64-linux-gnu/libz.so.1
72 DEBUG: Analyzing /lib/x86_64-linux-gnu/libz.so.1
78 DEBUG: Skipping libc.so.6 dependency of libz.so.1
78 INFO: Caching module hooks...
81 INFO: Analyzing /home/joel/Code/random/pyinstaller/test.py
1078 INFO: Loading module hooks...
1079 INFO: Loading module hook "hook-usb.py"...
Unable to find "libusb-1.0.so.0" when adding binary and data files.
```
This causes the packaging process to halt with an empty `dist/` and `build/test/` folders.

I dug around and found out, that `hook-usb.py` reports the binaries to be `[('libusb-1.0.so.0', '')]` and I think this causes that  `src_root_path_or_glob`(`utils.py`, lines 450+) to just contain 'libusb1.0.so.0', which produces just `[]`after it get's run through the `glob.glob()` and not the real path, which I think is`/lib/x86_64-linux-gnu/libusb-1.0.so.0` on my computer.

Environment:
```
Python 2.7.12+
Ubuntu 16.10
Virtualenv 15.1.0
```
requirements.txt
```
appdirs==1.4.3
packaging==16.8
PyInstaller==3.3.dev0+5fd013bfe
pyparsing==2.2.0
pyusb==1.0.0
six==1.10.0
```

Also, this seems to be somewhat related to #1682 and commit https://github.com/pyinstaller/pyinstaller/commit/23901eb1ce7e60f1aac2a73e6cb3b02f1a34b9b1


Discussion:
I also have this problem

Can you tell me how to solve it?
I haven't tried to solve it/dig deeper into yet, as my project doesn't yet depend on `pyusb`. 
Please help tracking this down:
- Add `import pdb; pdb.set_trace()` near the top of `hook-usb.py` (just behind the import statements)
- In the shell run `export PYUSB_DEBUG=debug`
- run PyInstaller `pyinstaller test.py` (no output-redirection)
- step through `hook-usb.py` to gain more insights.

Thanks.
Sorry for the delay, but I managed to find something to get me forward.

The custom mechanism of `hook-usb.py` as defined in [here](https://github.com/pyinstaller/pyinstaller/commit/23901eb1ce7e60f1aac2a73e6cb3b02f1a34b9b1#diff-8e99511b3f6d0c631ac915fd6ca91a80R50) seems to find the right binaries for my system. The 'pyusb library locator' (lines 29-46) makes the `binaries` to be `[('libusb-1.0.so.0', '')]`, meaning the branch `if not binaries:` on line 50 isn't executed even though we don't have a path to the binaries.

I changed line 50 to `if binaries:` for testing purposes and `$ pyinstaller test.py` runs fine and the usb module works as intended. I also tested it on a small script (writes to an ESC/POS printer with pyusb) and it worked fine.

So yeah, I don't know the underlying issue of why the `usblib._name` doesn't resolve into the full path (I guess it should?) but this 'hack' circumvents the problem.

Resolving patch:
diff --git a/PyInstaller/hooks/hook-usb.py b/PyInstaller/hooks/hook-usb.py
--- a/PyInstaller/hooks/hook-usb.py
+++ b/PyInstaller/hooks/hook-usb.py
@@ -40,7 +40,9 @@
     binaries = []
     for usblib in [getattr(usb.backend, be)._lib for be in backends]:
         if usblib is not None:
-            binaries = [(usblib._name, '')]
+            binaries = _resolveCtypesImports([os.path.basename(usblib._name)])
+            assert len(binaries[0]) == 3
+            binaries = [(binaries[0][1], '')]
 
 except (ValueError, usb.core.USBError) as exc:
     logger.warning("%s", exc)


Test patch: