python 子进程 Popen 在本地和远程系统上的行为不同

标签 python django travis-ci

我正在使用 django 并将构建发送到 travis-ci,但在我的测试中构建失败。有问题的行就在这个函数中......

from subprocess import Popen, PIPE

def make_mp3(path, blob):
    process = Popen(
        ['lame', '-', '--comp', '40', path], stdin=PIPE, stdout=PIPE, stderr=PIPE)
    stdout_data = process.communicate(input=blob.read())
    return stdout_data

调用该函数的行位于此处:

make_mp3(mp3_path, request.FILES['audio_file'])

在我的本地系统上,测试运行良好并通过。我已经使用这个函数有一段时间了,它总是按预期运行。但是当我将它发送到 travis-ci 进行构建时,它给了我这个回溯......

======================================================================
ERROR: test_post_upload_audio_word (define.tests.ViewTests)
Testing the audio upload function for word audio
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/travis/build/deltaskelta/langalang/define/tests.py", line 827, in test_post_upload_audio_word
    'audio_file': audio_file})
  File "/home/travis/virtualenv/python2.7.12/lib/python2.7/site-packages/django/test/client.py", line 541, in post
    secure=secure, **extra)
  File "/home/travis/virtualenv/python2.7.12/lib/python2.7/site-packages/django/test/client.py", line 343, in post
    secure=secure, **extra)
  File "/home/travis/virtualenv/python2.7.12/lib/python2.7/site-packages/django/test/client.py", line 409, in generic
    return self.request(**r)
  File "/home/travis/virtualenv/python2.7.12/lib/python2.7/site-packages/django/test/client.py", line 494, in request
    six.reraise(*exc_info)
  File "/home/travis/virtualenv/python2.7.12/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/home/travis/virtualenv/python2.7.12/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/travis/virtualenv/python2.7.12/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/travis/virtualenv/python2.7.12/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/travis/virtualenv/python2.7.12/lib/python2.7/site-packages/django/utils/decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "/home/travis/virtualenv/python2.7.12/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/travis/virtualenv/python2.7.12/lib/python2.7/site-packages/django/utils/decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/home/travis/virtualenv/python2.7.12/lib/python2.7/site-packages/django/utils/decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "/home/travis/virtualenv/python2.7.12/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/travis/virtualenv/python2.7.12/lib/python2.7/site-packages/django/utils/decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/home/travis/build/deltaskelta/langalang/define/views.py", line 21, in dispatch
    return super(Define, self).dispatch(*args, **kwargs)
  File "/home/travis/virtualenv/python2.7.12/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/travis/build/deltaskelta/langalang/define/views.py", line 89, in post
    return upload_audio(request, context)
  File "/home/travis/build/deltaskelta/langalang/define/views_func.py", line 251, in upload_audio
    make_mp3(mp3_path, request.FILES['audio_file'])
  File "/home/travis/build/deltaskelta/langalang/define/views_func.py", line 25, in make_mp3
    ['lame', '-', '--comp', '40', path], stdin=PIPE, stdout=PIPE, stderr=PIPE)
  File "/opt/python/2.7.12/lib/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/opt/python/2.7.12/lib/python2.7/subprocess.py", line 1343, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

我不明白为什么这会在我的系统上通过测试但在构建时失败。输入文件显然在那里,因为 request.FILES 找到了它。该目录存在..我检查过。那么它告诉我什么不存在?

最佳答案

失败原因:执行命令无法执行不存在的文件。在你的例子中它是蹩脚。引用自doc section :

The most common exception raised is OSError. This occurs, for example, when trying to execute a non-existent file. Applications should prepare for OSError exceptions.

您还可以在 subprocess.py 末尾处检查 _execute_child 方法的源代码。

关于python 子进程 Popen 在本地和远程系统上的行为不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39363629/

相关文章:

django - 如何在不同的页面模板中访问 Django/Mezzanine 库内容

python - 如何将脚本作为 Travis CI 构建的一部分运行?

haskell - 如何指定 Travis 应该用来测试我的 Haskell 包的解析器(和 GHC)?

python - 使用数组元组中的列构建 DataFrame

python - 这两个 Python pandas dataframe 命令有什么区别?

python - 如何过滤 jinja2 模板中的集合?

python - 为 Django 构建自动化

python - 源 toastr 启动失败,错误启动 django 尽管它存在

perl - 通过 Travis CI 使用 perl-helpers 时如何强制 Devel::Cover 忽略文件夹

Python 序列翻译