def set_source_abs(self): """ Create source directory absolute path. Make sure that the source exists and is a directory. First assume that computer is the source, then try the device. #NOTE: 1. implementation does not support path expansion #NOTE: 2. implementation supports only directory sync """ source = readlink(self.source) for prefix in (os.getcwd(), self.mtp_gvfs_path): # Get absolute path for the specified source # Prepend prefix if given source is a relative path if not os.path.isabs(source): source_abs = os.path.join(prefix, source) else: source_abs = source if not os.path.exists(source_abs): continue if not os.path.isdir(source_abs): raise OSError('"{source}" is not a directory' .format(source=source_abs)) self.source_abs = source_abs break if self.source_abs is None: raise OSError('"{source}" does not exists on computer ' 'neither on device'.format(source=source_abs))
def set_destination_abs(self): """ Create destination directory absolute path. #NOTE: implementation does not allow device only sync, i.e. that both #NOTE: source and destination are on the device """ destination = readlink(self.destination) if self.source_abs is None: raise OSError('Source directory is not defined') if 'mtp:host' not in self.source_abs: # device is destination destination_abs = os.path.join(self.mtp_gvfs_path, destination) else: # computer is destination if not os.path.isabs(destination): destination_abs = os.path.join(os.getcwd(), destination) else: destination_abs = destination self.destination_abs = destination_abs
def test_readlink(): """ Test if the auxiliary `readlink` function works as expected. """ assert readlink('') == '' assert readlink('.') == os.getcwd() assert readlink('..') == os.path.dirname(os.getcwd()) assert readlink('/..') == os.sep assert readlink('./') == os.getcwd() assert readlink('../') == os.path.dirname(os.getcwd()) assert readlink(os.sep) == os.sep assert readlink('~') == '/home/{user}'.format(user=getpass.getuser()) assert readlink('~/Music') == '/home/{user}/Music'.format(user=getpass.getuser()) # NOQA assert readlink('~/..') == '/home' assert readlink('../../..') == '/home' phone_card = 'Phone/Card' assert readlink(phone_card) == phone_card nonexisting = 'nonexisting_path' assert readlink(nonexisting) == os.path.join(os.getcwd(), nonexisting)