1

I want to execute following command on linux terminal using python script

hg log -r "((last(tag())):(first(last(tag(),2))))" work

This command give changesets between last two tags who have affected files in "work" directory

I tried:

import subprocess
releaseNotesFile = 'diff.txt'
with open(releaseNotesFile, 'w') as f:
    f.write(subprocess.call(['hg', 'log', '-r', '"((last(tag())):(first(last(tag(),2))))"', 'work']))

error:

abort: unknown revision '((last(tag())):(first(last(tag(),2))))'!
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    f.write(subprocess.call(['hg', 'log', '-r', '"((last(tag())):(first(last(tag(),2))))"', 'work']))
TypeError: expected a character buffer object

Working with os.popen()

with open(releaseNotesFile, 'w') as file:
    f = os.popen('hg log -r "((last(tag())):(first(last(tag(),2))))" work')
    file.write(f.read())

How to execute that command using subprocess ?

1 Answer 1

2

To solve your problem, change the f.write(subprocess... line to:

f.write(subprocess.call(['hg', 'log', '-r', '((last(tag())):(first(last(tag(),2))))', 'dcpp']))

Explanation

When calling a program from a command line (like bash), will "ignore" the " characters. The two commands below are equivalent:

hg log -r something
hg "log" "-r" "something"

In your specific case, the original version in the shell has to be enclosed in double quotes because it has parenthesis and those have a special meaning in bash. In python that is not necessary since you are enclosing them using single quotes.

Sign up to request clarification or add additional context in comments.

2 Comments

@rudm Error Traceback (most recent call last): File "test.py", line 4, in <module> f.write(subprocess.call(['hg', 'log', '-r', '((last(tag())):(first(last(tag(),2))))', 'work'])) TypeError: expected a character buffer object output was printed on terminal and empty file is created. But i want ouput in file
Solved 4th line should be subprocess.call(['hg', 'log', '-r', '((last(tag())):(first(last(tag(),2))))', 'work'], stdout=f)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.