File: stacks-dist-extract

package info (click to toggle)
stacks 2.68%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,092 kB
  • sloc: cpp: 41,356; sh: 3,497; python: 1,410; perl: 1,388; makefile: 148
file content (275 lines) | stat: -rwxr-xr-x 7,331 bytes parent folder | download
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python3
#
# Copyright 2021-2023, Julian Catchen <jcatchen@illinois.edu>
#
# This file is part of Stacks.
#
# Stacks is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Stacks is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Stacks.  If not, see <http://www.gnu.org/licenses/>.
#

import argparse
import os, sys
import gzip

#
# Global configuration variables.
#
in_path  = ""
out_path = ""
section  = ""
pretty   = False

def parse_command_line():
    global in_path
    global out_path
    global section
    global pretty

    usage = '''
    %(prog)s logfile [section]
    %(prog)s [--pretty] [--out-path path] logfile [section]
    cat logfile | %(prog)s [--pretty] [--section section]'''

    desc  = '''Export a paricular section of a Stacks log or distribs file. If you \
    supply a log path alone, %(prog)s will print the available sections to output. \
    The log file can also be supplied via stdin.'''

    p = argparse.ArgumentParser(description=desc, usage=usage)

    #
    # Add options.
    #
    p.add_argument("-p", "--pretty", action="store_true",
                   help="Output data as a table with columns lined up.")
    p.add_argument("-o", "--out-path", type=str, metavar='path',
                   help="Path to output file.")
    p.add_argument("-s", "--section", type=str, metavar='section', 
                   help="Name of section to output from the log file.")
    
    #
    # Parse the command line
    #
    args, posargs = p.parse_known_args()

    if args.pretty != None:
        pretty = args.pretty
    if args.section != None:
        section = args.section
    if args.out_path != None:
        out_path = args.out_path

    if len(posargs) > 0:
        in_path = posargs[0]
    if len(posargs) > 1 and args.section == None:
        section = posargs[1]

    #
    # Test input file path.
    #
    if len(in_path) > 0 and os.path.exists(in_path) == False:
        print("Input log file path does not exist ('{}')".format(in_path), file=sys.stderr)
        p.print_help()
        sys.exit()


def scan_log_file(fh):
    sections = []

    for line in fh:
        if line[0:6] == "BEGIN ":
            section = line[6:]
            section = section.strip("\n")
            sections.append(section)
    fh.seek(0)

    return sections


def find_section(sections, section):
    section_hash = {}
    for s in sections:
        section_hash[s] = 0

    if section in section_hash:
        return section

    slen = len(section)

    for s in section_hash:
        if s[0:slen] == section:
            section_hash[s] += 1

    sorted_sections = sorted(section_hash.items(), key=lambda x:x[1], reverse=True)

    s = ""
    if sorted_sections[0][1] == 0:
        print("Section '{}' was not found.".format(section), file=sys.stderr)
        
    elif sorted_sections[0][1] == 1 and sorted_sections[1][1] == 0:
        s = sorted_sections[0][0]
        
    elif sorted_sections[0][1] > 0 and sorted_sections[1][1] > 0:
        print("Section '{}' is ambiguous and could refer to more than one section of the log:".format(section), file=sys.stderr)
        for i in range(len(sorted_sections)):
            if sorted_sections[i][1] > 0:
                print("    " + sorted_sections[i][0], file=sys.stderr)
            else:
                break

    return s


def pretty_print(stream):
    colcnts = []
    for line in stream:
        if line[0] == '#':
            continue

        cols = line.split('\t')

        while len(colcnts) < len(cols):
            colcnts.append(0)

        for i in range(len(cols)):
            if colcnts[i] < len(cols[i]):
                colcnts[i] = len(cols[i])

    s = ""
    for line in stream:
        if line[0] == '#':
            s += line + "\n"
            continue
        
        cols = line.split('\t')
        for i in range(len(cols)):
            s += str(cols[i]).ljust(colcnts[i]) + "  "
        s += "\n"

    return s


def output_section(in_fh, out_fh, sections, section):

    start_output = False
    s = []

    for line in in_fh:
        line = line.strip("\n")

        if start_output == False and line == "BEGIN " + section:
            start_output = True
            continue
        
        if start_output == True:
            if line != "END " + section:
                s.append(line)
            else:
                if pretty == True:
                    s = pretty_print(s)
                    out_fh.write(s)
                else:
                    out_fh.write("\n".join(s) + "\n")
                return


def stream_section(in_fh, out_fh, section):

    start_output = False
    s = []

    for line in in_fh:
        line = line.strip("\n")

        if len(section) == 0 and line[0:6] == "BEGIN ":
            print(line[6:])
            continue
        
        if start_output == False and line == "BEGIN " + section:
            start_output = True
            continue
        
        if start_output == True:
            if line != "END " + section:
                s.append(line)
            else:
                if pretty == True:
                    s = pretty_print(s)
                    out_fh.write(s)
                else:
                    out_fh.write("\n".join(s) + "\n")
                return

    if len(section) > 0 and start_output == False:
        print("Section '{}' was not found.".format(section), file=sys.stderr)
        return


def main():
    parse_command_line()

    #
    # Open input log file, if no file path is given, assume stdin.
    #
    in_fh  = None
    out_fh = None

    if len(in_path) > 0:
        if in_path[-3:] == ".gz":
            in_fh = gzip.open(in_path, 'rb')
        else:
            in_fh = open(in_path)
    else:
        in_fh = sys.stdin

    #
    # Set output file handle
    #
    if len(out_path) > 0:
        out_fh = open(out_path)
    else:
        out_fh = sys.stdout

    if in_fh == sys.stdin:
        stream_section(in_fh, out_fh, section)
    else:
        #
        # Scan the log file for section headings
        #
        sections = scan_log_file(in_fh)

        if len(sections) == 0:
            print("No printable sections in file, '{}'.".format(in_path))
            return

        if len(section) == 0:
            for s in sections:
                out_fh.write(s + "\n")
            return

        #
        # If a section was supplied, but it is ambiguous, try to match it.
        #
        found_section = find_section(sections, section)

        if len(found_section) == 0:
            return
    
        output_section(in_fh, out_fh, sections, found_section)


#                                                                              #
#------------------------------------------------------------------------------#
#                                                                              #
if __name__ == "__main__":
    main()