0

I am experiencing the following strange behavior with git from sh python module:

Here is the python script:

import sh
from datetime import datetime

now = str(datetime.now())

filename = "config.cfg"
file = filename
f = open(file, 'w')
f.write(now)

now = str(datetime.now())
comment = "-m " + "\"new version" + " registrered " + now + "\""


print(sh.sudo.git("add", filename))
print(sh.sudo.git("commit", comment))

BEHAVIOR: Manually everything works fine:

$ echo "new content" > config.cfg
$ git add config.cfg
$ sudo git commit -m "ospf configuration registrered 2014-10-24 15:30:33.531963"
[master 142cfd5] ospf configuration registrered 2014-10-24 15:30:33.531963
 1 file changed, 1 insertion(+)

But not the implementation through python sh module

I-### 1st execution is OK

python sh1.py
[master 6d13519]  "new version registrered 2014-10-24 15:37:42.534595"
 1 file changed, 1 deletion(-)

II-### any further execution gives the following:

python sh1.py

Traceback (most recent call last):
  File "sh1.py", line 24, in <module>
    print(sh.sudo.git("commit", comment))
  File "/home/user/anaconda/lib/python2.7/site-packages/sh.py", line 769, in __call__
    return RunningCommand(cmd, call_args, stdin, stdout, stderr)
  File "/home/user/anaconda/lib/python2.7/site-packages/sh.py", line 330, in __init__
    self.wait()
  File "/home/user/anaconda/lib/python2.7/site-packages/sh.py", line 334, in wait
    self._handle_exit_code(self.process.wait())
  File "/home/user/anaconda/lib/python2.7/site-packages/sh.py", line 348, in _handle_exit_code
    self.process.stderr
sh.ErrorReturnCode_1: 

  RAN: '/usr/bin/sudo git commit -m "new version registrered 2014-10-24 15:38:02.528213"'

  STDOUT:
On branch master
Untracked files:
    sh1.py

nothing added to commit but untracked files present


  STDERR:

III- ### everything works fine after manual add and commit:

$ sudo git add config.cfg

$ sudo git commit -m "new version registrered 2014-10-24 15:34:35.2415245"
[master 5d5fe35] new version registrered 2014-10-24 15:34:35.2415245
 1 file changed, 1 insertion(+)

And now back to 1st behavior I, and so on...


The user is in sudoers and set NOPASSWD to avoid sudo password

1 Answer 1

5

You need to close the file object before you invoke the git commands.

f.close()

If you ran 'git status', you'd see that Git was complaining that after the script ran, config.cfg still had changes that were unstaged for commit.

I have a feeling that python flushes an EOF character or something to the file when it's closed, and therefore alters the file since you called 'git add' from your script. I don't know for sure unfortunately, only that the issue was resolved for me when I closed the file prior to 'git add' 'git commit' from the script.

Edit: Useless has corrected me and noted that the reason is because the file is buffered.

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

3 Comments

The file is buffered - there's no EOF, just a change that wasn't flushed to disk.
Can confirm. File is definitely buffered.
Thanks Guys. Issue resolved, your answer is definitly useful P-

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.