0

From a Python script I want to create a RAR file. I will need to communicate with Rar.exe because I only want the first RAR volume from a multi-volume archive set, nothing more. The -vp switch makes sure Create next volume ? [Y]es, [N]o, [A]ll is asked after each volume. The first time this question pops up, I want to answer No. How do I accomplish this?

I've been reading and trying a lot of things and I found out something like this can be accomplished with pexpect. I've been trying the two different Windows ports: wexpect and winpexpect. The result is that my script will hang. No RAR file is created. This is my code:

import wexpect
import sys

rarexe = "C:\Program Files\WinRAR\Rar.exe"
args = ['a', '-vp', '-v2000000b', 'only_first.rar', 'a_big_file.ext']

child = wexpect.spawn(rarexe, args)
child.logfile = sys.stdout
index = child.expect(["Create next volume ? [Y]es, [N]o, [A]ll", 
        wexpect.EOF, wexpect.TIMEOUT], timeout=10)
if index == 0:
     child.sendline("N")
else:
     print('error')

Other approaches are welcome too.

Gfy
  • 8,173
  • 3
  • 26
  • 46
  • On what line does it hang? – f13o Feb 17 '13 at 12:52
  • @f13o: In the above code it hangs on `child = wexpect.spawn(rarexe, args)` and in wexpect it hangs on `msg = GetMessage(0, 0, 0)`. [That is this function](http://msdn.microsoft.com/en-us/library/windows/desktop/ms644936(v=vs.85).aspx). – Gfy Feb 17 '13 at 20:43

2 Answers2

0

I had the same issue, because there are several (buggy) version of wexpect on the web.

Check out of my variant, which is a copy of an instance, which worked for me.

This can be installed using

pip install wexpect

betontalpfa
  • 3,454
  • 1
  • 33
  • 65
  • I see you got down voted. I just tried it out on the provided script but it doesn't work for me either. After the time-out it shows `Create next volume ? [Y]es, [N]o, [A]ll error`. It is an improvement on my past experience since I don't recall seeing `error` printed. Is it an issue with wexpect or my script above? I used RAR 5.50 x64 now. – Gfy Aug 11 '19 at 13:51
  • Thanks for, not just dropping a down vote. When do you get this message? (During installaion? During runnin (at start, while waiting prompt)?) Can you post it as new issue with more details (https://github.com/raczben/wexpect/issues) ? – betontalpfa Aug 12 '19 at 07:30
  • The message is the output of the program in the question. There I display `error` myself after the question prompt of rar.exe that wexpect didn't detect. I'm running it on Windows. – Gfy Aug 12 '19 at 18:47
  • Your program's error maybe caused by the the stderr handling error of wexpect, which is a known issue. https://github.com/raczben/wexpect/issues/2 – betontalpfa Aug 13 '19 at 07:47
0

The answer to my problem has two parts.

As betontalpfa indicated, I must use his version of wexpect. It can be installed easily:

pip install wexpect

The expect_exact documentation of Pexpect explains that it uses plain string matching instead of compiled regular expressions patterns in the list. This means the parameters must be properly escaped or the expect_exact method must be used instead of expect. It gave me this working code:

import wexpect
import sys

rarexe = "C:\Program Files\WinRAR\Rar.exe"
args = ['a', '-vp', '-v2000000b', 'only_first.rar', 'a_big_file.ext']

child = wexpect.spawn(rarexe, args)
# child.logfile = sys.stdout
rar_prompts = [
        "Create next volume \? \[Y\]es, \[N\]o, \[A\]ll",
        "\[Y\]es, \[N\]o, \[A\]ll, n\[E\]ver, \[R\]ename, \[Q\]uit",
        wexpect.EOF, wexpect.TIMEOUT]
index = child.expect(rar_prompts, timeout=8)

while index < 2:
        # print(child.before)
        if index == 0:
                print("No next volume")
                child.sendline("N")
        elif index == 1:
                print("Overwriting existing volume")
                child.sendline("Y")
        index = child.expect(rar_prompts, timeout=8)
else:
        print('Index: %d' % index)
        if index == 2:
                print("Success!")
        else:
                print("Time out!")

And the output gives:

Overwriting existing volume
No next volume
Index: 2
Success!
Gfy
  • 8,173
  • 3
  • 26
  • 46