Skip to content

Commit

Permalink
Fix libimobiledevice failing to load library on unsupported linux builds
Browse files Browse the repository at this point in the history
Also some pep8 cleanup
  • Loading branch information
kovidgoyal committed Nov 28, 2015
1 parent b4c9081 commit ceed1a1
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions src/calibre/devices/idevice/libimobiledevice.py
Expand Up @@ -20,12 +20,13 @@
c_char, c_char_p, c_int, c_long, c_ubyte, c_uint, c_ulonglong, c_void_p,
POINTER, string_at, Structure)

from calibre.constants import DEBUG, islinux, isosx, iswindows
from calibre.constants import DEBUG, isosx, iswindows
from calibre.devices.idevice.parse_xml import XmlPropertyListParser
from calibre.devices.usbms.driver import debug_print


class libiMobileDeviceException(Exception):

def __init__(self, value):
self.value = value

Expand All @@ -34,6 +35,7 @@ def __str__(self):


class libiMobileDeviceIOException(Exception):

def __init__(self, value):
self.value = value

Expand All @@ -42,6 +44,7 @@ def __str__(self):


class AFC_CLIENT_T(Structure):

'''
http://www.libimobiledevice.org/docs/html/structafc__client__private.html
'''
Expand Down Expand Up @@ -74,6 +77,7 @@ class AFC_CLIENT_T(Structure):


class HOUSE_ARREST_CLIENT_T(Structure):

'''
http://www.libimobiledevice.org/docs/html/structhouse__arrest__client__private.html
'''
Expand All @@ -94,6 +98,7 @@ class HOUSE_ARREST_CLIENT_T(Structure):


class IDEVICE_T(Structure):

'''
http://www.libimobiledevice.org/docs/html/structidevice__private.html
'''
Expand All @@ -104,6 +109,7 @@ class IDEVICE_T(Structure):


class INSTPROXY_CLIENT_T(Structure):

'''
http://www.libimobiledevice.org/docs/html/structinstproxy__client__private.html
'''
Expand All @@ -129,6 +135,7 @@ class INSTPROXY_CLIENT_T(Structure):


class LOCKDOWND_CLIENT_T(Structure):

'''
http://www.libimobiledevice.org/docs/html/structlockdownd__client__private.html
'''
Expand All @@ -152,6 +159,7 @@ class LOCKDOWND_CLIENT_T(Structure):


class LOCKDOWND_SERVICE_DESCRIPTOR(Structure):

'''
from libimobiledevice/include/libimobiledevice/lockdown.h
'''
Expand All @@ -162,6 +170,7 @@ class LOCKDOWND_SERVICE_DESCRIPTOR(Structure):


class libiMobileDevice():

'''
Wrapper for libiMobileDevice
'''
Expand Down Expand Up @@ -323,7 +332,7 @@ def disconnect_idevice(self):
if self.device_mounted:
self._afc_client_free()
self._house_arrest_client_free()
#self._lockdown_goodbye()
# self._lockdown_goodbye()
self._idevice_free()
self.device_mounted = False
else:
Expand All @@ -332,7 +341,7 @@ def disconnect_idevice(self):
def dismount_ios_media_folder(self):
if self.device_mounted:
self._afc_client_free()
#self._lockdown_goodbye()
# self._lockdown_goodbye()
self._idevice_free()
self.device_mounted = False

Expand Down Expand Up @@ -438,10 +447,10 @@ def listdir(self, path, get_stats=True):
return self._afc_read_directory(path, get_stats=get_stats)

def load_library(self):
if islinux:
env = "linux"
self.lib = cdll.LoadLibrary('libimobiledevice.so.5')
self.plist_lib = cdll.LoadLibrary('libplist.so.3')
if iswindows:
env = "Windows"
self.lib = cdll.LoadLibrary('libimobiledevice.dll')
self.plist_lib = cdll.LoadLibrary('libplist.dll')
elif isosx:
env = "OS X"

Expand All @@ -458,11 +467,13 @@ def load_library(self):
self.plist_lib = cdll.LoadLibrary(os.path.join(getattr(sys, 'frameworks_dir'), path))
else:
self.plist_lib = cdll.LoadLibrary(path)
elif iswindows:
env = "Windows"
self.lib = cdll.LoadLibrary('libimobiledevice.dll')
self.plist_lib = cdll.LoadLibrary('libplist.dll')

else:
env = "linux"
try:
self.lib = cdll.LoadLibrary('libimobiledevice.so.5')
except EnvironmentError:
self.lib = cdll.LoadLibrary('libimobiledevice.so')
self.plist_lib = cdll.LoadLibrary('libplist.so.3')
self._log_location(env)
self._log(" libimobiledevice loaded from '{0}'".format(self.lib._name))
self._log(" libplist loaded from '{0}'".format(self.plist_lib._name))
Expand Down Expand Up @@ -506,7 +517,7 @@ def mount_ios_app(self, app_name=None, app_id=None):
self._instproxy_client_options_free()
self._instproxy_client_free()

if not app_name in self.installed_apps:
if app_name not in self.installed_apps:
self._log(" {0} not installed on this iDevice".format(repr(app_name)))
self.disconnect_idevice()
else:
Expand Down Expand Up @@ -1388,7 +1399,7 @@ def _idevice_get_device_list(self):
device_list.append(devices[index].contents.value)
index += 1
self._log(" {0}".format(repr(device_list)))
#self.lib.idevice_device_list_free()
# self.lib.idevice_device_list_free()
return device_list

def _idevice_new(self):
Expand Down Expand Up @@ -1458,8 +1469,8 @@ def _instproxy_browse(self, applist=[]):
raise libiMobileDeviceException(error_description)
else:
# Get the number of apps
#app_count = self.lib.plist_array_get_size(apps)
#self._log(" app_count: {0}".format(app_count))
# app_count = self.lib.plist_array_get_size(apps)
# self._log(" app_count: {0}".format(app_count))

# Convert the app plist to xml
xml = POINTER(c_void_p)()
Expand Down Expand Up @@ -1628,7 +1639,7 @@ def _lockdown_client_new_with_handshake(self):
self._log_location()

lockdownd_client_t = POINTER(LOCKDOWND_CLIENT_T)()
#SERVICE_NAME = create_string_buffer('calibre')
# SERVICE_NAME = create_string_buffer('calibre')
SERVICE_NAME = c_void_p()
error = self.lib.lockdownd_client_new_with_handshake(byref(self.device),
byref(lockdownd_client_t),
Expand Down Expand Up @@ -1719,7 +1730,6 @@ def _lockdown_get_device_name(self):
return device_name

def _lockdown_get_value(self, requested_items=[]):

'''
Retrieves a preferences plist using an optional domain and/or key name.
Expand Down Expand Up @@ -1937,4 +1947,3 @@ def _log_location(self, *args):

def __null(self, *args, **kwargs):
pass

0 comments on commit ceed1a1

Please sign in to comment.