-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileops.py
executable file
·222 lines (179 loc) · 6.47 KB
/
fileops.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
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
#!/usr/bin/python
"""
Module fileops is a cli tool for set operations on files.
"""
import argparse
import operator
import os
import os.path as path
import sys
from contextlib import suppress
from itertools import chain, takewhile
from shutil import copyfile
S_OPEN = "("
S_CLOSE = ")"
_alldirs = {}
_indirs = []
help_text = """
Directory names may not contain '$', '(', ')', ' ' or start with a number.
All filenames are passed as strings. Relative filenames are allowed.
An S-expression is build by "(" + <operation> + <...arguments> + ")",
where arguments can be S-exp themselves.
Use $n to refer to the in files strictly in order or enter them in-place.
The following set operations are defined and their shorhands:
union - (u <set> <set>) -> <set>
intersection - (n <set> <set>) -> <set>
difference - (\\ <set> <set>) -> <set>
symmetric_difference - (s <set> <set>) -> <set>
cardinality - (# <set>) -> <num>
if conditional - (if (<bool>) (<exp>) (<exp>)) -> <exp>
contains - (in <set> <set>) -> <bool>
disjoint - (d <set> <set>) -> <bool>
equals - (eq <num> <num>) -> <bool>
less - (lt <num> <num>) -> <bool>
greater - (gt <num> <num>) -> <bool>
"""
execdir = {
# adding new operations is trivial
# n-length argument support?
"u": (frozenset.union, 2),
"n": (frozenset.intersection, 2),
"\\": (frozenset.difference, 2),
"s": (frozenset.symmetric_difference, 2),
"in": (frozenset.issubset, 2),
"d": (frozenset.isdisjoint, 2),
"#": (len, 1),
"eq": (operator.eq, 2),
"lt": (operator.lt, 2),
"gt": (operator.gt, 2),
"if": (lambda a, b, c: b if a else c, 3),
}
accept_number = ["lt", "eq", "gt"]
def tokenize(s):
"""generator tokenize is an iterator over tokens of an s-expression"""
for val in s.split():
last = 0
while val[0] == S_OPEN:
val = val[1:]
yield S_OPEN
for char in reversed(val):
if char == S_CLOSE:
val = val[:-1]
last += 1
if val != "":
yield val
for _ in range(last):
yield S_CLOSE
def next_paren_it():
depth = 0
def inner(token):
nonlocal depth
if token == S_OPEN:
depth += 1
elif token == S_CLOSE:
depth -= 1
return depth != 0
return inner
def dir_to_set(dir_) -> frozenset:
"""returns a set of all filenames of a directory"""
if path.isfile(path.realpath(dir_)):
return frozenset(dir_)
dir_ = path.realpath(dir_)
if dir_[-1] != "/":
dir_ += "/"
_alldirs.update((x, dir_+x)
for x in os.listdir(dir_)
if path.isfile(dir_ + x))
return frozenset(x for x in os.listdir(dir_) if path.isfile(dir_ + x))
class ParseError(ValueError):
pass
def parse(l):
"""returns the result of a given S-expression"""
tokens = tokenize(l) if isinstance(l, str) else l
try:
token = next(tokens)
if token != S_OPEN:
raise ParseError(f"expected '{S_OPEN}', got '{token}'")
token = next(tokens)
if token == S_CLOSE:
return None
elif token not in execdir:
raise ParseError(f"expected operation, got '{token}'")
op = token
args = []
token = next(tokens)
while token != S_CLOSE: # number of args
if token == S_OPEN:
tmp = chain(takewhile(next_paren_it(), chain([S_OPEN], tokens)),
[S_CLOSE])
args.append(parse(tmp))
elif token[0] == "$":
try:
args.append(dir_to_set(_indirs[int(token[1:])-1]))
except IndexError:
raise ParseError(f"file '{int(token[1:])}' not specified")
except ValueError:
raise ParseError(f"error converting '{token}' to int")
elif token.isdigit():
i = int(token)
if op in accept_number:
args.append(i)
else:
raise ParseError(f"num literal outside context '{op}'")
elif token == S_CLOSE:
raise ParseError("expected value got ')', to few arguments")
else:
args.append(dir_to_set(token))
token = next(tokens)
except StopIteration:
raise ParseError(
f"possibly missing closing parentheses - last value was '{token}'")
if len(args) != execdir[op][1]:
raise ParseError(
f"too many or few arguments for operation '{op}' - expected {execdir[op][1]} got {len(args)}")
try:
next(tokens)
raise ParseError("too many closing parentheses")
except StopIteration:
pass
# print(op, args, execdir[op][0](*args))
return execdir[op][0](*args)
def main():
"""parses flags and prints result of the provided S-expression"""
argp = argparse.ArgumentParser(epilog=help_text, allow_abbrev=False,
formatter_class=argparse.RawDescriptionHelpFormatter)
argp.add_argument("S-exp", type=str,
help="Lisp-like S-expression for set operations.")
argp.add_argument("-f", default=False, dest="force", action='store_true',
help="Don't ask for override.")
argp.add_argument("-o", type=str, dest="outdir",
help="If the expression result is a set copy files to <o> directory. (default print to std.out)")
argp.add_argument('indirs', nargs='*',
help="files refernced form sexp with $<n>")
args = vars(argp.parse_args())
global _indirs
_indirs = args["indirs"]
outdir = args["outdir"]
force = args["force"]
try:
res = parse(args["S-exp"])
except ParseError as pe:
print("error while parsing S-expression:", pe)
sys.exit(1)
if outdir and isinstance(res, frozenset):
if force:
with suppress(FileExistsError):
os.mkdir(outdir)
else:
try:
os.mkdir(outdir)
except FileExistsError:
inp = input("directory already present still proceed? [y/N]: ")
if inp != "y":
sys.exit(2)
for x in res:
copyfile(_alldirs[x], outdir+x)
else:
print(res)
if __name__ == "__main__":
main()