-
Notifications
You must be signed in to change notification settings - Fork 13
/
silentslice
executable file
·90 lines (69 loc) · 2.46 KB
/
silentslice
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
#!/usr/bin/env python
import os
import sys
# if you pip installed this is wrong but you have silent_tools installed anyways
silent_tools_dir = os.path.dirname(os.path.realpath(__file__))
if silent_tools_dir not in sys.path:
sys.path.append(silent_tools_dir)
import silent_tools
from silent_tools import eprint
import stat
# Don't throw an error when someone uses head
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE, SIG_DFL)
all_args = sys.argv[1:]
e_flag = False
for i in range(len(all_args)-1, -1, -1):
elem = all_args[i]
if elem[0] != '-':
continue
if elem[1] == 'e':
e_flag = True
all_args.pop(i)
break
assert False, 'silentslice: Unknown flag ' + elem
if (len(all_args) == 0):
eprint("")
eprint('silentslice by bcov - a tool to allow you extract a smaller silent file from a larger one')
eprint("Usage:")
eprint(" cat list_of_tags.list | silentslice myfile.silent > new.silent")
eprint(" or")
eprint(" silentslice myfile.silent tag1 tag2 tag3 > new.silent")
eprint("Flags:")
eprint(" -e Old behavior that preserves input order of tags (new way is file-order which is faster")
sys.exit(1)
silent_file = all_args[0]
tag_buffers = []
if ( stat.S_ISFIFO(os.stat("/dev/stdin").st_mode) ):
tag_buffers += sys.stdin.readlines()
tag_buffers += all_args[1:]
tags = []
for line in tag_buffers:
line = line.strip()
if (len(line) == 0):
continue
sp = line.split(" ")
for tag in sp:
tags.append(tag)
if (len(tags) != len(set(tags))):
eprint("silentslice: Warning: duplicate tags specified")
silent_index = silent_tools.get_silent_index( silent_file )
sys.stdout.write( silent_tools.silent_header( silent_index ) )
sys.stdout.flush()
if not e_flag:
existing_tags = []
for tag in tags:
if ( tag not in silent_index['index'] ):
eprint("silentslice: Unable to find tag: %s"%tag)
continue
else:
existing_tags.append(tag)
tags = sorted(existing_tags, key=lambda x: silent_index['index'][x]['seek'])
with open(silent_file, errors='ignore') as sf:
for tag in tags:
if ( tag not in silent_index['index'] ):
eprint("silentslice: Unable to find tag: %s"%tag)
continue
structure = silent_tools.get_silent_structure_file_open( sf, silent_index, tag )
sys.stdout.write("".join(structure))
sys.stdout.flush()