"Fossies" - the Fresh Open Source Software Archive

Member "Tardis-1.2.1/tools/findPath.py" (9 Jun 2021, 5561 Bytes) of package /linux/privat/Tardis-1.2.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "findPath.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.1.5_vs_1.2.1.

    1 #! /usr/bin/env python3
    2 # vim: set et sw=4 sts=4 fileencoding=utf-8:
    3 #
    4 # Tardis: A Backup System
    5 # Copyright 2013-2020, Eric Koldinger, All Rights Reserved.
    6 # kolding@washington.edu
    7 #
    8 # Redistribution and use in source and binary forms, with or without
    9 # modification, are permitted provided that the following conditions are met:
   10 #
   11 #     * Redistributions of source code must retain the above copyright
   12 #       notice, this list of conditions and the following disclaimer.
   13 #     * Redistributions in binary form must reproduce the above copyright
   14 #       notice, this list of conditions and the following disclaimer in the
   15 #       documentation and/or other materials provided with the distribution.
   16 #     * Neither the name of the copyright holder nor the
   17 #       names of its contributors may be used to endorse or promote products
   18 #       derived from this software without specific prior written permission.
   19 #
   20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
   24 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   30 # POSSIBILITY OF SUCH DAMAGE.
   31 
   32 from Tardis import Defaults, Util, TardisDB, TardisCrypto, CacheDir, librsync, Regenerator, Config, RemoteDB
   33 import sqlite3
   34 import argparse, logging
   35 import os.path
   36 import os
   37 import sys
   38 import base64
   39 import hashlib
   40 import progressbar
   41 import urllib.parse
   42 
   43 logger = None
   44 
   45 def reader(quiet):
   46     import readline
   47     prompt = '' if quiet else '--> '
   48     try:
   49         while True:
   50             yield input(prompt)
   51     except EOFError:
   52         return
   53 
   54 def processArgs():
   55     parser = argparse.ArgumentParser(description='Extract paths for a changeset', fromfile_prefix_chars='@', add_help=False)
   56 
   57     (_, remaining) = Config.parseConfigOptions(parser)
   58     Config.addCommonOptions(parser)
   59     Config.addPasswordOptions(parser)
   60 
   61     parser.add_argument('--quiet', '-q', dest='quiet', default=False, action='store_true', help="Only print the translation, not the input strings")
   62     parser.add_argument('--backup', '-b', dest='backup', default=None, help='Look in specific backupset')
   63     parser.add_argument('--chain', '-c', dest='chain', default=False, action='store_true', help="Print file info on all stages in the chain")
   64 
   65     parser.add_argument('--help', '-h',     action='help');
   66     parser.add_argument('checksums',          nargs='*', help="List of checksums to extract")
   67 
   68     Util.addGenCompletions(parser)
   69 
   70     args = parser.parse_args(remaining)
   71 
   72     return args
   73 
   74 _paths = {(0, 0): '/'}
   75 
   76 def _decryptFilename(name, crypt):
   77     return crypt.decryptFilename(name) if crypt else name
   78 
   79 def _path(db, crypt, bset, inode):
   80     global _paths
   81     if inode in _paths:
   82         return _paths[inode]
   83     else:
   84         fInfo = db.getFileInfoByInode(inode, bset)
   85         if fInfo:
   86             parent = (fInfo['parent'], fInfo['parentdev'])
   87             prefix = _path(db, crypt, bset, parent)
   88 
   89             name = _decryptFilename(fInfo['name'], crypt)
   90             path = os.path.join(prefix, name)
   91             _paths[inode] = path
   92             return path
   93         else:
   94             return ''
   95 
   96 def main():
   97     global logger
   98     logging.basicConfig(level=logging.INFO)
   99     logger = logging.getLogger('')
  100     args = processArgs()
  101     password = Util.getPassword(args.password, args.passwordfile, args.passwordprog)
  102 
  103     (tardis, _, crypto) = Util.setupDataConnection(args.database, args.client, password, args.keys, args.dbname, args.dbdir)
  104 
  105     if args.backup is not None:
  106         bsetInfo = Util.getBackupSet(tardis, args.backup)
  107         if bsetInfo:
  108             bset = bsetInfo['backupset']
  109         else:
  110             logger.critical("No backupset at for name: %s", args.backup)
  111             sys.exit(1)
  112     else:
  113         bset = False
  114 
  115     data = args.checksums
  116     if not data:
  117         tty = os.isatty(0)
  118         if not tty:
  119             data = list(map(str.strip, sys.stdin.readlines()))
  120         else:
  121             data = reader(args.quiet)
  122 
  123     prevInode = None
  124     for i in data:
  125         try:
  126             for finfo in tardis.getFileInfoByChecksum(i, bset):
  127                 inode = (finfo['inode'], finfo['device'])
  128                 if inode == prevInode:
  129                     next
  130                 prevInode = inode
  131                 if args.quiet:
  132                     print(_path(tardis, crypto, bset, inode))
  133                 else:
  134                     print(f"{i} => [{finfo['firstset']}, {finfo['lastset']}] ({finfo['inode']}, {finfo['device']})\t{_path(tardis, crypto, bset, inode)}")
  135             if args.chain:
  136                 info = tardis.getChecksumInfoChain(i)
  137                 x = 0
  138                 for j in info:
  139                     print(f"  {x:2}: {j['checksum']} Size: {j['size']:8} File: {bool(j['isfile'])} Compressed: {j['compressed']} Encrypted: {bool(j['encrypted'])} DiskSize: {j['disksize']}")
  140                     x += 1
  141                 print("")
  142 
  143         except Exception as e:
  144             print("Caught exception: " + str(e))
  145 
  146 
  147 if __name__ == "__main__":
  148     main()