X-Git-Url: https://git.phdru.name/?p=extfs.d.git;a=blobdiff_plain;f=obexftp;h=12c9f529859583cb3be38ccefe5eeef8890bd67a;hp=7b34288d3028f3b4deb60c78feed12bbc126ac56;hb=5d995f7bdac3b755a500761544308db433ef433c;hpb=a96601f24ae6fa8c6cca96daedbeee9ecdda89a9 diff --git a/obexftp b/obexftp index 7b34288..12c9f52 100755 --- a/obexftp +++ b/obexftp @@ -15,7 +15,7 @@ script is written in Python because I love Python, the best of all languages ;), and I need to parse XML directory listings from obexftp. The script requires Midnight Commander 3.1+ (http://www.ibiblio.org/mc/), -Python 2.2+ (http://www.python.org/), +Python 2.3+ (http://www.python.org/), OpenOBEX 1.0.1+ (http://openobex.sourceforge.net/) and ObexFTP 0.10.4+ (http://triq.net/obexftp). @@ -59,11 +59,14 @@ transport#obexftp command. Sometimes even this doesn't help - Midnight Commander shows the same cached VFS image. Exit Midnight Commander and restart it. +If something goes wrong set the logging level (see setLevel() below) to INFO +or DEBUG and look in the obexftp-mcextfs.log file(s). + """ -__version__ = "1.0.2" -__revision__ = "$Id: obexftp,v 1.13 2004/07/27 15:39:54 phd Exp $" -__date__ = "$Date: 2004/07/27 15:39:54 $"[7:-2] +__version__ = "1.1.0" +__revision__ = "$Id: obexftp,v 1.16 2004/07/27 17:55:15 phd Exp $" +__date__ = "$Date: 2004/07/27 17:55:15 $"[7:-2] __author__ = "Oleg Broytmann " __copyright__ = "Copyright (C) 2004 PhiloSoft Design" @@ -78,47 +81,27 @@ import xml.dom.minidom from tempfile import mkdtemp -def log_error(msg): - sys.stderr.write(msg + '\n') - -def error(msg): - log_error(msg) - sys.exit(1) +import logging +logger = logging.getLogger('obexftp-mcextfs') +logger.addHandler(logging.FileHandler('obexftp-mcextfs.log')) +logger.setLevel(logging.ERROR) if len(sys.argv) < 2: - error("""\ + logger.error("""\ ObexFTP Virtual FileSystem for Midnight Commander version %s Author: %s %s -Put it in /usr/lib/mc/extfs. For more information read the source!""" % ( +Put it in /usr/lib/mc/extfs. For more information read the source!""", __version__, __author__, __copyright__ -)) - - -def setup_transport(): - """Setup transport parameters for the obexftp program""" - transport_file = open(sys.argv[2], 'r') - line = transport_file.readline() - transport_file.close() - - parts = line.strip().split() - transport = parts[0].lower() - - if transport == "bluetooth": - return ' '.join(["-b", parts[1], "-B", parts[2]]) - elif transport == "tty": - return ' '.join(["-t", parts[1]]) - elif transport == "irda": - return "-i" - else: - error("Unknown transport '%s'; expected 'bluetooth', 'tty' or 'irda'" % transport) +) + sys.exit(1) # Parse ObexFTP XML directory listings class DirectoryEntry(object): - """Represent remote files and directories""" + """Represent a remote file or a directory""" def __init__(self, type): self.type = type @@ -167,13 +150,27 @@ def get_entries(dom, type): return entries -def recursive_list(obexftp_args, directory): +def _run(*args): + """Run the obexftp binary catching errors""" + command = "%s %s %s" % (obexftp_prog, obexftp_args, ' '.join(args)) + logger.debug("Running command %s", command) + w, r, e = os.popen3(command, 'r') + w.close() + errors = e.read() + e.close() + result = r.read() + r.close() + logger.debug(" errors: %s", errors) + logger.debug(" result: %s", result) + return result, errors + + +def recursive_list(directory='/'): """List the directory recursively""" - pipe = os.popen("%s %s -l '%s' 2>/dev/null" % (obexftp_prog, obexftp_args, directory), 'r') - listing = pipe.read() - pipe.close() + listing, errors = _run("-l '%s'" % directory) if not listing: + logger.error("Error reading XML listing: %s", errors) return dom = xml.dom.minidom.parseString(listing) @@ -189,16 +186,14 @@ def recursive_list(obexftp_args, directory): fullpath = "%s/%s" % (directory, entry.name) if fullpath.startswith('//'): fullpath = fullpath[1:] time.sleep(1) - recursive_list(obexftp_args, fullpath) + recursive_list(fullpath) def mcobex_list(): """List the entire VFS""" - obexftp_args = setup_transport() - recursive_list(obexftp_args, '/') + recursive_list() # A unique directory for temporary files - tmpdir_name = None def setup_tmpdir(): @@ -213,47 +208,48 @@ def cleanup_tmpdir(): def mcobex_copyout(): """Get a file from the VFS""" - obexftp_args = setup_transport() obex_filename = sys.argv[3] real_filename = sys.argv[4] setup_tmpdir() - os.system("%s %s -g '%s' 2>/dev/null" % (obexftp_prog, obexftp_args, obex_filename)) try: - os.rename(os.path.basename(obex_filename), real_filename) - except OSError: - pass - cleanup_tmpdir() + _run("-g '%s'" % obex_filename) + try: + os.rename(os.path.basename(obex_filename), real_filename) + except OSError: + logger.exception("CopyOut %s to %s", obex_filename, real_filename) + finally: + cleanup_tmpdir() def mcobex_copyin(): """Put a file to the VFS""" - obexftp_args = setup_transport() obex_filename = sys.argv[3] real_filename = sys.argv[4] dirname, filename = os.path.split(obex_filename) setup_tmpdir() - os.rename(real_filename, filename) - os.system("%s %s -c '%s' -p '%s' 2>/dev/null" % (obexftp_prog, obexftp_args, - dirname, filename - )) - os.rename(filename, real_filename) # by some reason MC wants the file back - cleanup_tmpdir() + try: + try: + os.rename(real_filename, filename) + _run("-c '%s' -p '%s'" % (dirname, filename)) + os.rename(filename, real_filename) # by some reason MC wants the file back + except OSError: + logger.exception("CopyIn %s to %s", real_filename, obex_filename) + finally: + cleanup_tmpdir() def mcobex_rm(): """Remove a file from the VFS""" - obexftp_args = setup_transport() obex_filename = sys.argv[3] - os.system("%s %s -k '%s' 2>/dev/null" % (obexftp_prog, obexftp_args, obex_filename)) + _run("-k '%s'" % obex_filename) def mcobex_mkdir(): """Create a directory in the VFS""" - obexftp_args = setup_transport() obex_dirname = sys.argv[3] - os.system("%s %s -C '%s' 2>/dev/null" % (obexftp_prog, obexftp_args, obex_dirname)) + _run("-C '%s'" % obex_dirname) mcobex_rmdir = mcobex_rm @@ -264,6 +260,36 @@ command = sys.argv[1] procname = "mcobex_" + command if not g.has_key(procname): - error("Unknown command %s" % command) + logger.error("Unknown command %s", command) + sys.exit(1) + + +def setup_transport(): + """Setup transport parameters for the obexftp program""" + transport_file = open(sys.argv[2], 'r') + line = transport_file.readline() + transport_file.close() + + parts = line.strip().split() + transport = parts[0].lower() + + if transport == "bluetooth": + return ' '.join(["-b", parts[1], "-B", parts[2]]) + elif transport == "tty": + return ' '.join(["-t", parts[1]]) + elif transport == "irda": + return "-i" + else: + logger.error("Unknown transport '%s'; expected 'bluetooth', 'tty' or 'irda'", transport) + sys.exit(1) + +try: + obexftp_args = setup_transport() +except: + logger.exception("Exception while parsing the transport file") + sys.exit(1) -g[procname]() +try: + g[procname]() +except: + logger.exception("Exception during run")