forked from rsgalloway/pyseq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlss
executable file
·180 lines (153 loc) · 5.84 KB
/
lss
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------------------------
# Copyright (c) 2011-2016, Ryan Galloway ([email protected])
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# - Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# - Neither the name of the software nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# -----------------------------------------------------------------------------
import os
import sys
import glob
import pyseq
import logging
import optparse
def tree(source, level, seq_format):
"""Recusrively walk from the source and display all the the folders and
sequences.
"""
if sys.stdout.isatty():
blue = "\033[94m"
endc = "\033[0m"
else:
blue = ""
endc = ""
ends = {}
done = []
print "%s%s" % (blue, os.path.relpath(source))
for root, dirs, seqs in pyseq.walk(source, level):
if len(dirs) > 0:
ends[root] = dirs[-1]
else:
ends[root] = None
sp = ""
if root != sorted(source):
p = root
while p != source:
dir_name, base = os.path.split(p)
if dir_name == source:
break
elif dir_name in done:
sp = " " + sp
elif ends[dir_name] != base:
sp = "│ " + sp
elif ends[dir_name] == base:
sp = "│ " + sp
else:
sp = " " + sp
p = dir_name
base = os.path.basename(root)
if root == source:
pass
elif ends[os.path.dirname(root)] == base:
print "".join([sp, "└── ", "%s%s%s" % (blue, base, endc)])
done.append(root)
ends[os.path.dirname(root)] = None
sp += " "
else:
print "".join([sp, "├── ", "%s%s%s" % (blue, base, endc)])
sp += "│ "
l = len(seqs)
for i, seq in enumerate(seqs):
if i == (l - 1) and len(dirs) == 0:
print "".join([sp, "└── ", seq.format(seq_format)])
else:
print "".join([sp, "├── ", seq.format(seq_format)])
print endc
def _recur_cb(option, opt_str, value, parser):
"""Callback for the `recursive` argument.
"""
if value is None:
value = -1
else:
value = int(value)
setattr(parser.values, option.dest, value)
def main():
"""Command-line interface.
"""
usage = """
lss [path] [-f format] [-d] [-r]
Formatting options:
You can format the output of lss using the --format option and passing
in a format string.
Default format string is "%s"
Supported directives:
%%s sequence start
%%e sequence end
%%l sequence length
%%f list of found files
%%m list of missing files
%%p padding, e.g. %%06d
%%r absolute range, start-end
%%R expanded range, start-end [missing]
%%d disk usage
%%h string preceding sequence number
%%t string after the sequence number
Format directives support padding, for example: "%%04l".
""" % pyseq.global_format
parser = optparse.OptionParser(usage=usage,
version="%prog " + pyseq.__version__)
parser.add_option("-f", "--format", dest="format",
default=None, help="format the output string")
parser.add_option("-d", "--debug", dest="debug", action="store_true",
default=False, help="set logging level to debug (or $PYSEQ_LOG_LEVEL)")
parser.add_option("-r", "--recursive", dest="recursive", action="callback",
callback=_recur_cb, help="Walks the entire directory structure.")
(options, args) = parser.parse_args()
if options.debug:
pyseq.log.setLevel(logging.DEBUG)
if len(args) == 0:
args = [os.getcwd()]
items = []
for path in args:
if os.path.isdir(path):
join = os.path.join
items = [join(path, x) for x in os.listdir(path)]
else:
items.extend(glob.glob(path))
if options.recursive is None:
for seq in pyseq.get_sequences(items):
print(seq.format(options.format or pyseq.global_format))
else:
level = options.recursive
for path in args:
path = os.path.abspath(path.rstrip(os.sep))
if not os.path.isdir(path):
continue
tree(path, level, options.format or "%h%r%t")
return 0
if __name__ == '__main__':
sys.exit(main())