当前位置: 首页>>代码示例>>Python>>正文


Python Util.shortPath方法代码示例

本文整理汇总了Python中Tardis.Util.shortPath方法的典型用法代码示例。如果您正苦于以下问题:Python Util.shortPath方法的具体用法?Python Util.shortPath怎么用?Python Util.shortPath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Tardis.Util的用法示例。


在下文中一共展示了Util.shortPath方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from Tardis import Util [as 别名]
# 或者: from Tardis.Util import shortPath [as 别名]

#.........这里部分代码省略.........
                            output = open(outname,  "wb")
                        elif outputdir:
                            outname = os.path.join(outputdir, ckname)
                            if os.path.exists(outname) and owMode == OW_NEVER:
                                logger.warning("File %s exists.  Skipping", outname)
                                continue
                            logger.debug("Writing output to %s", outname)
                            output = open(outname,  "wb")
                        elif outname:
                            # Note, this should ONLY be true if only one file
                            if os.path.exists(outname) and owMode == OW_NEVER:
                                logger.warning("File %s exists.  Skipping", outname)
                                continue
                            output = file(outname,  "wb")
                        try:
                            x = f.read(64 * 1024)
                            while x:
                                output.write(x)
                                if hasher:
                                    hasher.update(x)
                                x = f.read(64 * 1024)
                        except Exception as e:
                            logger.error("Unable to read file: {}: {}".format(i, repr(e)))
                            raise
                        finally:
                            f.close()
                            if output is not sys.stdout.buffer:
                                output.close()
                        if args.auth:
                            logger.debug("Checking authentication")
                            outname = doAuthenticate(outname, i, hasher.hexdigest())

                except TardisDB.AuthenticationException as e:
                    logger.error("Authentication failed.  Bad password")
                    #if args.exceptions:
                        #logger.exception(e)
                    sys.exit(1)
                except Exception as e:
                    logger.error("Could not recover: %s: %s", i, e)
                    if args.exceptions:
                        logger.exception(e)
                    retcode += 1

        else: # Not checksum, but acutal pathnames
            for i in args.files:
                try:
                    i = os.path.abspath(i)
                    logger.info("Processing %s", Util.shortPath(i))
                    path = None
                    f = None
                    if args.last:
                        (bset, path, name) = findLastPath(i, args.reduce)
                        if bset is None:
                            logger.error("Unable to find a latest version of %s", i)
                            raise Exception("Unable to find a latest version of " + i)
                        logger.info("Found %s in backup set %s", i, name)
                    elif args.reduce:
                        path = Util.reducePath(tardis, bset, i, args.reduce, crypt)
                        logger.debug("Reduced path %s to %s", path, i)
                        if not path:
                            logger.error("Unable to find a compute path for %s", i)
                            raise Exception("Unable to compute path for " + i)
                    else:
                        path = i

                    if args.crypt and crypt:
                        actualPath = crypt.encryptPath(path)
                    else:
                        actualPath = path
                    logger.debug("Actual path is %s -- %s", actualPath, bset)
                    info = tardis.getFileInfoByPath(actualPath, bset)
                    if info:
                        retcode += recoverObject(r, info, bset, outputdir, path, linkDB, name=outname, authenticate=args.auth)
                    else:
                        logger.error("Could not recover info for %s (File not found)", i)
                        retcode += 1
                except TardisDB.AuthenticationException as e:
                    logger.error("Authentication failed.  Bad password")
                    #if args.exceptions:
                        #logger.exception(e)
                    sys.exit(1)
                except Exception as e:
                    logger.error("Could not recover: %s: %s", i, e)
                    if args.exceptions:
                        logger.exception(e)
    except KeyboardInterrupt:
        logger.error("Recovery interupted")
    except TardisDB.AuthenticationException as e:
        logger.error("Authentication failed.  Bad password")
        if args.exceptions:
            logger.exception(e)
    except Exception as e:
        logger.error("Regeneration failed: %s", e)
        if args.exceptions:
            logger.exception(e)

    if errors:
        logger.warning("%d files could not be recovered.")

    return retcode
开发者ID:koldinger,项目名称:Tardis,代码行数:104,代码来源:Regenerate.py

示例2: recoverObject

# 需要导入模块: from Tardis import Util [as 别名]
# 或者: from Tardis.Util import shortPath [as 别名]
def recoverObject(regenerator, info, bset, outputdir, path, linkDB, name=None, authenticate=True):
    """
    Main recovery routine.  Recover an object, based on the info object, and put it in outputdir.
    """
    retCode = 0
    outname = None
    skip = False
    hasher = None
    try:
        if info:
            realname = info['name']
            if args.crypt and crypt:
                realname = crypt.decryptFilename(realname)

            if name:
                # This should only happen only one file specified.
                outname = name
            elif outputdir:
                outname = os.path.abspath(os.path.join(outputdir, realname))

            if outname and not checkOverwrite(outname, info):
                skip = True
                try:
                    logger.warning("Skipping existing file: %s %s", Util.shortPath(path), notSame(path, outname, '(' + Util.shortPath(outname) + ')'))
                except Exception:
                    pass

            # First, determine if we're in a linking situation
            if linkDB is not None and info['nlinks'] > 1 and not info['dir']:
                key = (info['inode'], info['device'])
                if key in linkDB:
                    logger.info("Linking %s to %s", outname, linkDB[key])
                    os.link(linkDB[key], outname)
                    skip = True
                else:
                    linkDB[key] = outname

            # If it's a directory, create the directory, and recursively process it
            if info['dir']:
                if not outname:
                    #logger.error("Cannot regenerate directory %s without outputdir specified", path)
                    raise Exception("Cannot regenerate directory %s without outputdir specified" % (path))

                try:
                    logger.info("Processing directory %s", Util.shortPath(path))
                except Exception:
                    pass

                contents = list(tardis.readDirectory((info['inode'], info['device']), bset))

                # Make sure an output directory is specified (really only useful at the top level)
                if not os.path.exists(outname):
                    os.mkdir(outname)

                dirInode = (info['inode'], info['device'])
                # For each file in the directory, regenerate it.
                for i in contents:
                    name = i['name']
                    # Get the Info
                    childInfo = tardis.getFileInfoByName(name, dirInode, bset)

                    # Decrypt filename, and make it UTF-8.
                    if args.crypt and crypt:
                        name = crypt.decryptFilename(name)
                    else:
                        name = name

                    # Recurse into the child, if it exists.
                    if childInfo:
                        try:
                            if args.recurse or not childInfo['dir']:
                                recoverObject(regenerator, childInfo, bset, outname, os.path.join(path, name), linkDB, authenticate=authenticate)
                        except Exception as e:
                            logger.error("Could not recover %s in %s", name, path)
                            if args.exceptions:
                                logger.exception(e)
                    else:
                        retCode += 1
            elif not skip:
                myname = outname if outname else "stdout"
                logger.info("Recovering file %s %s", Util.shortPath(path), notSame(path, myname, " => " + Util.shortPath(myname)))

                checksum = info['checksum']
                i = regenerator.recoverChecksum(checksum, authenticate)

                if i:
                    if authenticate:
                        hasher = Util.getHash(crypt)

                    if info['link']:
                        # read and make a link
                        i.seek(0)
                        x = i.read(16 * 1024)
                        if outname:
                            os.symlink(x, outname)
                        else:
                            logger.warning("No name specified for link: %s", x)
                        if hasher:
                            hasher.update(x)
                    else:
#.........这里部分代码省略.........
开发者ID:koldinger,项目名称:Tardis,代码行数:103,代码来源:Regenerate.py


注:本文中的Tardis.Util.shortPath方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。