-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3nav.py
115 lines (98 loc) · 2.97 KB
/
s3nav.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from pathlib import Path, PurePath
from minio.deleteobjects import DeleteObject
from time import time
import click
from skin import _e, _i, _p
from pfs import PsuedoFileSystem, fmt_date, fmt_size, rm
from s3core import get_client, lswild
def _handle_argument(target):
"""
Extract the interesting stuff from the simple command target argument
"""
p = PurePath(target)
parts = p.parts
alias = parts[0]
bucket = parts[1]
therest = '/'.join(list(parts[2:]))
client = get_client(alias)
return client, bucket, therest
def do_rm(client, bucket, pattern):
"""
Wild card removal of files in object store.
Be careful
"""
files = lswild(client, bucket, pattern, objects=True)
if len(files) == 0:
click.echo(_i(f'Nothing to delete matching [{pattern}] found in [{bucket}] in [{client.alias_name}]'))
return
rm(client, bucket, files)
def do_ls(client, bucket, pattern, columns=None, size=None, date=None):
"""
Wild card listing of files in object store
"""
files = lswild(client, bucket, pattern)
if size is not None or date is not None:
columns = 1
d = ['Object Name']
if size:
d.append('Size')
if date:
d.append('Last Modified')
data = [d]
m = [len(x) for x in d]
fmts = ['>' for x in d]
fmts[0]='<'
objects = lswild(client, bucket, pattern, objects=True)
for o in objects:
d = [o.object_name]
if size:
d.append(fmt_size(o.size))
if date:
d.append(fmt_date(o.last_modified))
data.append(d)
m = [max(i,len(j)) for i,j in zip(m,d)]
for i in range(1,len(m)):
m[i]+=5
files = []
for row in data:
files.append(' '.join(f"{item:{fmt}{max_len}}" for item, fmt, max_len in zip(row, fmts, m)))
else:
files = lswild(client, bucket, pattern)
if len(files):
for file in files:
print(file)
else:
print(f'No files found matching "{pattern}" in [{bucket}] at [{client.alias_name}]')
@click.group()
def cli():
pass
@cli.command()
@click.argument('action')
def rm(action):
client, bucket, therest = _handle_argument(action)
do_rm(client, bucket, therest)
@cli.command()
@click.argument('action')
def ls(action):
client, bucket, therest = _handle_argument(action)
do_ls(client, bucket, therest)
@cli.command()
@click.argument('target')
def manage(target):
""" This provides a command line pseudo shell method for managing files """
bits = target.split('/')
if len(bits) > 2:
cwd = "/".join(bits[2:])
bucket = bits[1]
alias = bits[0]
elif len(bits) == 2:
cwd = ""
bucket = bits[1]
alias = bits[0]
else:
bucket = None
cwd = ""
alias = target
pfs = PsuedoFileSystem(alias, bucket, cwd)
if __name__ == "__main__":
cli()