Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Popen in run_cmd and pipe outputs to logger #263

Merged
merged 5 commits into from
Aug 16, 2018

Conversation

skulltech
Copy link
Contributor

Fixes #211

Old behaviour

>>> from conu.utils import run_cmd
>>> ret = run_cmd(['ls'])
CHANGELOG.md	  docs			pytest.ini	   setup.cfg
CONTRIBUTING.md   examples		README.md	   setup.py
conu		  LICENSE		release-conf.yaml  test-requirements.sh
conu.spec	  Makefile		requirements.in    tests
Dockerfile	  MANIFEST.in		requirements.sh    Vagrantfile
Dockerfile.tests  pytest-container.ini	requirements.txt
>>> ret
>>> ret = run_cmd(['ls'], return_output=True)
>>> ret
'CHANGELOG.md\nCONTRIBUTING.md\nconu\nconu.spec\nDockerfile\nDockerfile.tests\ndocs\nexamples\nLICENSE\nMakefile\nMANIFEST.in\npytest-container.ini\npytest.ini\nREADME.md\nrelease-conf.yaml\nrequirements.in\nrequirements.sh\nrequirements.txt\nsetup.cfg\nsetup.py\ntest-requirements.sh\ntests\nVagrantfile\n'

New behaviour

>>> from conu.utils import run_cmd
>>> ret = run_cmd(['ls'])
>>> ret
>>> ret = run_cmd(['ls'], return_output=True)
>>> ret
'CHANGELOG.md\nCONTRIBUTING.md\nconu\nconu.spec\nDockerfile\nDockerfile.tests\ndocs\nexamples\nLICENSE\nMakefile\nMANIFEST.in\npytest-container.ini\npytest.ini\nREADME.md\nrelease-conf.yaml\nrequirements.in\nrequirements.sh\nrequirements.txt\nsetup.cfg\nsetup.py\ntest-requirements.sh\ntests\nVagrantfile\n'

@user-cont user-cont deleted a comment from centos-ci Aug 14, 2018
Copy link
Member

@TomasTomecek TomasTomecek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice changes! Please correct the behavior when the command fails and we are good to merge.

Hm, I just realized we don't have any tests for the function. Could you please throw one or two tests over here? https://github.com/user-cont/conu/blob/master/tests/integration/test_utils.py

universal_newlines=True,
**kwargs)
else:
subprocess.check_call(cmd, **kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should also check for return code of the command; if it's > 0, an exception should be raised

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay got it. That makes sense.

@@ -123,13 +123,13 @@ def run_cmd(cmd, return_output=False, ignore_status=False, **kwargs):
"""
logger.debug('command: "%s"' % ' '.join(cmd))
try:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
universal_newlines=True, **kwargs)
output = process.communicate()[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping that communicate would iteratively print output after every newline; unfortunately, it returns all at once.

I guess it's fine for now, we can improve it later.

As a good example here is:

run_cmd(["bash", "-c", "for x in `seq 1 10`; do echo $x; sleep 1; done"])

@skulltech
Copy link
Contributor Author

@TomasTomecek Done! Made changes as you recommended! Please review it.

@user-cont user-cont deleted a comment Aug 15, 2018
@user-cont user-cont deleted a comment Aug 15, 2018
@user-cont user-cont deleted a comment Aug 15, 2018
@user-cont user-cont deleted a comment Aug 15, 2018
Copy link
Member

@TomasTomecek TomasTomecek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more suggestion for sleep so the tests are faster.



def test_run_cmd():
ret = run_cmd(["sh", "-c", "for x in `seq 1 5`; do echo $x; sleep 1; done"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please let's sleep for 0.01 so we don't waste time

Copy link
Contributor Author

@skulltech skulltech Aug 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah of course. On it.

Edit. Done! Please review.

else:
return cpe.returncode
return process.returncode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit confusing to me, but we can leave it for now

@TomasTomecek
Copy link
Member

[test]

@TomasTomecek
Copy link
Member

moar [test]

@TomasTomecek
Copy link
Member

Once the CI is green, we can merge; nicely done!

@TomasTomecek TomasTomecek merged commit ea5b0bd into user-cont:master Aug 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants