|
| 1 | +from __future__ import print_function |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import argparse |
| 6 | +import logging |
| 7 | +import sqlite3 |
| 8 | +from hpss import hpss_get |
| 9 | +from settings import config, CACHE, BLOCK_SIZE, DB_FILENAME |
| 10 | + |
| 11 | + |
| 12 | +def ls(): |
| 13 | + """ |
| 14 | + List all of the files in the HPSS path. |
| 15 | + Supports the '-l' argument for more information. |
| 16 | + """ |
| 17 | + parser = argparse.ArgumentParser( |
| 18 | + usage='zstash ls [<args>]', |
| 19 | + description='List the files from an existing archive') |
| 20 | + optional = parser.add_argument_group('optional named arguments') |
| 21 | + optional.add_argument('--hpss', type=str, help='path to HPSS storage') |
| 22 | + optional.add_argument('-l', dest='long', action='store_const', const=True, |
| 23 | + help='show more information for the files') |
| 24 | + parser.add_argument('files', nargs='*', default=['*']) |
| 25 | + args = parser.parse_args(sys.argv[2:]) |
| 26 | + |
| 27 | + # Open database |
| 28 | + logging.debug('Opening index database') |
| 29 | + if not os.path.exists(DB_FILENAME): |
| 30 | + # Will need to retrieve from HPSS |
| 31 | + if args.hpss is not None: |
| 32 | + config.hpss = args.hpss |
| 33 | + hpss_get(config.hpss, DB_FILENAME) |
| 34 | + else: |
| 35 | + logging.error('--hpss argument is required when local copy of ' |
| 36 | + 'database is unavailable') |
| 37 | + raise Exception |
| 38 | + |
| 39 | + global con, cur |
| 40 | + con = sqlite3.connect(DB_FILENAME, detect_types=sqlite3.PARSE_DECLTYPES) |
| 41 | + cur = con.cursor() |
| 42 | + |
| 43 | + # Retrieve configuration from database |
| 44 | + for attr in dir(config): |
| 45 | + value = getattr(config, attr) |
| 46 | + if not callable(value) and not attr.startswith("__"): |
| 47 | + cur.execute(u"select value from config where arg=?", (attr,)) |
| 48 | + value = cur.fetchone()[0] |
| 49 | + setattr(config, attr, value) |
| 50 | + config.maxsize = int(config.maxsize) |
| 51 | + config.keep = bool(int(config.keep)) |
| 52 | + # The command line arg should always have precedence |
| 53 | + if args.hpss is not None: |
| 54 | + config.hpss = args.hpss |
| 55 | + |
| 56 | + # Start doing actual work |
| 57 | + logging.debug('Running zstash ls') |
| 58 | + logging.debug('HPSS path : %s' % (config.hpss)) |
| 59 | + |
| 60 | + # Find matching files |
| 61 | + matches = [] |
| 62 | + for file in args.files: |
| 63 | + cur.execute(u"select * from files where name GLOB ? or tar GLOB ?", (file, file)) |
| 64 | + matches = matches + cur.fetchall() |
| 65 | + |
| 66 | + # Remove duplicates |
| 67 | + matches = list(set(matches)) |
| 68 | + |
| 69 | + # Sort by tape and order within tapes (offset) |
| 70 | + matches = sorted(matches, key=lambda x: (x[5], x[6])) |
| 71 | + |
| 72 | + if args.long: |
| 73 | + # Get the names of the cols |
| 74 | + cur.execute(u"PRAGMA table_info(files);") |
| 75 | + cols = [str(col_info[1]) for col_info in cur.fetchall()] |
| 76 | + print('\t'.join(cols)) |
| 77 | + |
| 78 | + # Print the results |
| 79 | + for match in matches: |
| 80 | + if args.long: |
| 81 | + # Print all contents of each match |
| 82 | + for col in match: |
| 83 | + print(col, end='\t') |
| 84 | + print('') |
| 85 | + else: |
| 86 | + # Just print the file name |
| 87 | + print(match[1]) |
0 commit comments