1# No shebang: this is only used on Windows. We use a shell script on Linux
2from __future__ import print_function
3
4import rclexecm
5import sys
6import os
7import shutil
8import platform
9import subprocess
10import glob
11
12
13def _msg(s):
14    rclexecm.logmsg(s)
15
16
17sysplat = platform.system()
18if sysplat != "Windows":
19    _msg("rcluncomp.py: only for Windows")
20    sys.exit(1)
21
22try:
23    import msvcrt
24    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
25except Exception as err:
26    _msg("setmode binary failed: %s" % str(err))
27
28sevenz = rclexecm.which("7z")
29if not sevenz:
30    _msg("rcluncomp.py: can't find 7z exe. Maybe set recollhelperpath " \
31          "in recoll.conf ?")
32    sys.exit(2)
33
34# Params: uncompression program, input file name, temp directory.
35# We ignore the uncomp program, and always use 7z on Windows
36
37infile = sys.argv[2]
38outdir = sys.argv[3]
39# _msg("rcluncomp.py infile [%s], outdir [%s]" % (infile, outdir))
40
41# There is apparently no way to suppress 7z output. Hopefully the
42# possible deadlock described by the subprocess module doc can't occur
43# here because there is little data printed. AFAIK nothing goes to stderr anyway
44try:
45    cmd = [sevenz, "e", "-bd", "-y", "-o" + outdir, infile]
46    subprocess.check_output(cmd, stderr = subprocess.PIPE)
47    # Don't use os.path.join, we always want to use '/'
48    outputname = glob.glob(outdir + "/*")
49    # There should be only one file in there..
50    print(outputname[0])
51except Exception as err:
52    _msg("%s" % (str(err),))
53    sys.exit(4)
54
55sys.exit(0)
56