-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsiplatform.py
64 lines (53 loc) · 1.95 KB
/
msiplatform.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
#!/usr/bin/env python
import argparse
import os
import tempfile
import shutil
import subprocess
import pipes
import sys
def run(command, verbose):
if verbose:
sys.stdout.write("$ {}\n".format(" ".join(
pipes.quote(word) for word in command)))
out = subprocess.check_output(command)
if verbose:
sys.stdout.write("".join(
"> {}\n".format(line) for line in out.splitlines()))
def set_platform(msi, platform, verbose):
run(["msidump", "-t", msi], verbose)
summary_stream = "_SummaryInformation.idt"
with open(summary_stream) as fh:
lines = [line.rstrip("\r\n").split("\t")
for line in iter(fh.readline, "")]
for line in lines[3:]:
if line[0] == "7":
line[1] = ";".join([platform] + line[1].split(";", 1)[1:])
with open(summary_stream, "w") as fh:
for line in lines:
fh.write("\t".join(line) + "\r\n")
run(["msibuild", msi, "-i", summary_stream], verbose)
def main():
parser = argparse.ArgumentParser(
description='Change the platform field of an MSI installer package.')
parser.add_argument("msi", help="MSI installer file.")
parser.add_argument("platform", help="New value for the platform field.")
parser.add_argument("-v", "--verbose", action="store_true",
help="Log what this script is doing.")
parser.add_argument("-k", "--keep", action="store_true",
help="Don't delete the temporary working directory.")
args = parser.parse_args()
msi = os.path.abspath(args.msi)
msidir = os.path.dirname(msi)
try:
tempdir = tempfile.mkdtemp(dir=msidir)
os.chdir(tempdir)
set_platform(msi, args.platform, args.verbose)
finally:
if args.keep:
sys.stdout.write(
"Retained temporary directory {}\n".format(tempdir))
else:
shutil.rmtree(tempdir)
if __name__ == '__main__':
main()