def main()

in xar/deprecated/mount_xar.py [0:0]


def main(args):
    # Ensure we don't wait forever to, say, acquire a lock.
    logging.basicConfig(
        level=logging.DEBUG, format="%(asctime)s %(levelname)s %(message)s"
    )

    p = optparse.OptionParser(__doc__)
    p.add_option(
        "--xarexec",
        default="/usr/bin/env xarexec_fuse",
        help="xarexec executable to use to mount XAR files",
    )
    p.add_option("--symlink-dir", help="directory to maintain symlinks in")
    p.add_option(
        "--cleanup", action="store_true", help="unmount unused XAR mountpoints"
    )
    p.add_option("--verbose", action="store_true", help="print debugging info")
    p.add_option(
        "--timeout",
        type=int,
        default=15,
        help="time, in minutes, after a xar was mounted to attempt " "to unmount it",
    )
    opts, files = p.parse_args(args)

    # Don't print much unless --verbose is specified.
    if not opts.verbose:
        logger.setLevel(logging.ERROR)

    if opts.cleanup:
        if "darwin" in sys.platform:
            mounts = macos_mounts()
        else:
            mounts = linux_mounts()

        unmount(mounts, opts)

    xar_files = {}
    filenames = []
    for file_or_dir in files:
        if os.path.isdir(file_or_dir):
            filenames.extend(glob.glob(os.path.join(file_or_dir, "*.xar")))
        else:
            filenames.append(file_or_dir)

    for filename in filenames:
        xar_file = XarFile(filename)
        if (
            xar_file.alias not in xar_files
            or xar_file.version > xar_files[xar_file.alias].version
        ):
            xar_files[xar_file.alias] = xar_file

    # Mount each xarfile and, optionally, create our symlink.
    for xar_file in xar_files.values():
        if xar_file.mount(opts.xarexec) and opts.symlink_dir:
            xar_file.symlink(opts.symlink_dir)

    # Remove dangling symlinks; unfortunately this is racey, as it
    # cannot be done atomically (ie we can't remove the symlink only
    # if the contents are something we expect).
    if opts.symlink_dir:
        for entry in os.listdir(opts.symlink_dir):
            path = os.path.join(opts.symlink_dir, entry)
            if not os.access(path, os.R_OK) or not is_mounted(path):
                logger.info("Removing symlink to unmounted image: %s" % path)
                os.unlink(path)