-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine.py
119 lines (94 loc) · 2.24 KB
/
combine.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
import argparse
import glob
import os
import re
import shutil
import sys
def parse_args():
p = argparse.ArgumentParser(description="A simple utility to combine files", epilog="Accepts UNIX style wildcards")
p.add_argument(
"-d",
"--directory",
type=str,
default="",
help="location of files to combine"
)
p.add_argument(
"-e",
"--extension",
type=str,
default=".*",
help="extension of files to combine"
)
p.add_argument(
"-n",
"--name",
type=str,
default="*",
help="file naming pattern"
)
p.add_argument(
"-k",
"--keep",
action="store_true",
help="keep original files after combining"
)
p.add_argument(
"-s",
"--sort",
const="all",
nargs="?",
choices=["ASC", "DESC"],
default="ASC",
help="order in which to combine files"
)
p.add_argument(
"-v",
"--version",
help="print the program version",
action="store_true"
)
required = p.add_argument_group("required arguments")
required.add_argument(
"-o",
"--output",
type=argparse.FileType("wb"),
help="output file",
required=('-v' not in sys.argv and '--version' not in sys.argv)
)
return p.parse_args()
def handle_args(args):
if args.version:
print("combine v1.0\nWritten by Adeem Mawani")
sys.exit(0)
# Handle extension input
if args.extension[0] != ".":
args.extension = "." + args.extension
# Handle sorting input
if args.sort.upper() == "DESC":
args.desc = True
else:
args.desc = False
if __name__ == "__main__":
arg = parse_args()
handle_args(arg)
# Pattern to match files
inclusion_pattern = arg.directory + arg.name + arg.extension
# Get all files that match the pattern (ignoring the exclusions)
exclude = ["combine.py", arg.output.name]
file_list = [fn for fn in glob.glob(inclusion_pattern) if os.path.basename(fn) not in exclude]
# Sort the list naturally
file_list.sort(reverse=arg.desc, key=lambda s: [int(t) if t.isdigit() else t.lower() for t in re.split('(\d+)', s)])
# Combine each file
for f in file_list:
with open(f, "rb") as infile:
shutil.copyfileobj(infile, arg.output)
print("[*] Combining: " + os.path.basename(f))
if not arg.keep:
os.remove(f)
# Close output file descriptor
arg.output.close()
if file_list:
print("[*] Done")
else:
print("[!] No files matched")