-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinearc.py
executable file
·120 lines (104 loc) · 4.32 KB
/
winearc.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
#!/usr/bin/python
# utility to make a config for a windows archiver based on a few variables.
import argparse
import sys
import subprocess
import os.path
jsontemplate = """{
"name": "#NAME#",
"comment": "http://fileformats.archiveteam.org/wiki/#NAME#",
"extensions": ["#EXT#"],
"install": {
"method": "apt",
"packages": ["wine"],
"tool":"#BIN#",
"container":"win32/#EXT#/#DIST#"#DEPBLOCK#
},
"pack": {
"exe": "wine",
"cmdline": "$tool $tools/#BIN# #ADD# $file.#EXT# $file",
"type": "wine"
},
"unpack": {
"exe": "wine",
"cmdline": "cd $destdir && $tool $tools/#BIN# #EXTRACT# z:$archive",
"extension": "#EXT#",
"force_extension": true
},
"test": {
"blob": "#BLOB#",
"file": "0",
"content": "#MSG#",
"delete": true
}
}
"""
def main(argv):
ap = argparse.ArgumentParser(description="Create a dos archiver profile (JSON config)")
ap.add_argument("-e","--extension",required=True, help="file extension (without .), will be used as the profile name and archive payload too by default")
ap.add_argument("-b", "--binary", help="archiver executable name. Default is whatever the extension is e.g. arj")
ap.add_argument("-u", "--unarchiver", help="unarchiver executable name. Default is whatever the extension is e.g. arj")
ap.add_argument("-d", "--distfile", help="Archive distfile from dos/*/??.zip")
ap.add_argument("-n", "--name", help="Profile name, default is whatever the extension is in uppercase. e.g. arj -> ARJ")
ap.add_argument("-m", "--msg", help="Archive message, default is whatever the extension is in uppercase. e.g. arj -> ARJ")
ap.add_argument("-a", "--addfiles", default="a", help="The command the archiver uses to add files to an archive.")
ap.add_argument("-x", "--extfiles", default="x", help="The command the archiver uses to extract files from an archive.")
args = ap.parse_args(argv)
os.chdir('..')
def_file = os.path.join("defs", args.extension + ".json")
if os.path.isfile(def_file):
print(f"{def_file} already exists, aborting.")
return
binary = args.binary
if not args.binary:
binary = args.extension
tool = os.path.join("tools", binary.upper() + ".EXE")
else:
tool = os.path.join("tools", binary)
depblock = ""
binary2 = args.unarchiver
if not args.unarchiver:
binary2 = binary
else:
depblock = f",\n \"dependencies\":[\"{binary2}\"]"
if not os.path.isfile(tool):
print(f"binary {tool} doesn't exist so maketest will fail later.")
name = args.name
if not args.name:
name = args.extension.upper()
msg = args.msg
if not args.msg:
msg = args.extension.upper()
config = jsontemplate.replace("#BIN#", binary)
config = config.replace("#EXT#", args.extension)
config = config.replace("#NAME#", name)
config = config.replace("#ADD#", args.addfiles)
config = config.replace("#EXTRACT#", args.extfiles)
config = config.replace("#MSG#", msg)
config = config.replace("#DIST#", args.distfile)
config = config.replace("#BIN2#", binary2)
config = config.replace("#DEPBLOCK#", depblock)
print(f'writing config: {def_file}')
open(def_file,'w').write(config)
print(f'asking maketests.py to make me a test blob')
cmdline = ["./maketests.py","-s",name]
p = subprocess.Popen(cmdline,stderr=subprocess.PIPE, stdout=subprocess.PIPE)
outs, errs = p.communicate()
if 'H4s' not in outs.decode():
print(f"blob making failed with this cmdline: {' '.join(cmdline)}")
return
blob = outs.decode().split(': ')[1].strip()
config = jsontemplate.replace("#BIN#", binary)
config = config.replace("#EXT#", args.extension)
config = config.replace("#NAME#", name)
config = config.replace("#ADD#", args.addfiles)
config = config.replace("#EXTRACT#", args.extfiles)
config = config.replace("#MSG#", msg)
config = config.replace("#BLOB#", blob)
config = config.replace("#DIST#", args.distfile)
config = config.replace("#BIN2#", binary2)
config = config.replace("#DEPBLOCK#", depblock)
print(f'writing config w/blob this time: {def_file}')
open(def_file,'w').write(config)
if __name__ == "__main__":
main(sys.argv[1:])