Skip to content

Commit 28234ac

Browse files
authored
Merge pull request #18 from E3SM-Project/list
List
2 parents a98f4b7 + 238dbcd commit 28234ac

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

tests/test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ def stop():
9898
str_in(output+err, 'Transferring file to HPSS')
9999
str_not_in(output+err, 'ERROR')
100100

101+
print('Testing ls')
102+
cmd = 'zstash ls --hpss={}'.format(HPSS_PATH)
103+
output, err = run_cmd(cmd)
104+
str_in(output+err, 'file0.txt')
105+
str_not_in(output+err, 'ERROR')
106+
cmd = 'zstash ls -l --hpss={}'.format(HPSS_PATH)
107+
output, err = run_cmd(cmd)
108+
str_in(output+err, 'tar')
109+
str_not_in(output+err, 'ERROR')
110+
101111
print('Testing chgrp')
102112
GROUP = 'acme'
103113
print('First, make sure that the files are not already in the {} group'.format(GROUP))

zstash/extract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def extract(keep_files=True):
2727
# Open database
2828
logging.debug('Opening index database')
2929
if not os.path.exists(DB_FILENAME):
30-
# will need to retrieve from HPSS
30+
# Will need to retrieve from HPSS
3131
if args.hpss is not None:
3232
config.hpss = args.hpss
3333
hpss_get(config.hpss, DB_FILENAME)

zstash/ls.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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])

zstash/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from extract import extract
1212
from chgrp import chgrp
1313
from check import check
14+
from ls import ls
1415

1516

1617
# -----------------------------------------------------------------------------
@@ -27,6 +28,7 @@ def main():
2728
extract extract files from archive
2829
chgrp change the group of an archive
2930
check check the integrity of the files in the archive
31+
ls list the files in an archive
3032
3133
For help with a specific command
3234
zstash command --help
@@ -47,6 +49,8 @@ def main():
4749
chgrp()
4850
elif args.command == 'check':
4951
check()
52+
elif args.command == 'ls':
53+
ls()
5054
else:
5155
print 'Unrecognized command'
5256
parser.print_help()

0 commit comments

Comments
 (0)