如何将python集成到automake并在python语法错误时退出构建过程

Posted

技术标签:

【中文标题】如何将python集成到automake并在python语法错误时退出构建过程【英文标题】:How to integrate python into automake and exit the build process upon python syntax error 【发布时间】:2019-12-19 19:27:39 【问题描述】:

我已经完成了寻找解决方案的家庭作业。我有一个带有一些简单 python 脚本的 autoconf 项目。

在我的 configure.ac 中(仅显示相关片段):

AM_PATH_PYTHON([3.8])

我正在尝试开始使用 3.x,因为 2.x 将于明年(2020 年)被弃用。

我的 Makefile.am:

python_PYTHON = uptocloud.py loadvariant.py
# I am not sure the following line is execute or the above one execute
# more likely the above one got executed
loadvariant : loadvariant.py
   $PYTHON -m py_compile $^ || exit 1
   @echo -e "#!/bin/sh\n$PYTHON $pythondir/$@.pyo \$$" > $@
   sed -i '2s,$$,*,' $@
   chmod +x $@

配置 --prefix $HOME 后,make,make install(仅在这一步编译了 python)我收到以下错误消息:

 /usr/bin/install -c -m 644 uptocloud.py loadvariant.py load1variant.py '/home/myhome/lib/python3.8/site-packages'
<string>:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
Byte-compiling python modules...
uptocloud.pyloadvariant.py  File "/home/myhome/lib/python3.8/site-packages/loadvariant.py", line 57
    print "Connected to  on !\n".format(pgdbname, hostname)
          ^
SyntaxError: invalid syntax

 Byte-compiling python modules (optimized versions) ...
 uptocloud.pyloadvariant.py

报错信息是典型的python2与python3不兼容,这里不是重点。我希望让制作过程在这里停止,但它一直持续到最后并处理其他构建任务。 $PYTHON -m py_compile FOO.py ||出口 1 似乎在这里不起作用。这是覆盖自动生成的隐式规则的手写规则。

我的问题是如何让 make 停在这里并要求我修复 python 脚本中的语法错误。

【问题讨论】:

【参考方案1】:

这是一个很好的答案,但有 50% 的正确率。在我修复了上面提到的打印语法错误之后。这次我再次尝试 make install ,构建过程确实停止了。不知道为什么会这样。这是错误消息的简短版本(删除多余的)。

make[2]: Entering directory '/home/myhome/coding/projname/scripts'
/usr/local/bin/python3 -m py_compile loadvariant.py || exit 1
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/py_compile.py", line 144, in compile
    code = loader.source_to_code(source_bytes, dfile or file,
  File "<frozen importlib._bootstrap_external>", line 846, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "loadvariant.py", line 186
    print sqlstr
          ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(sqlstr)?

During handling of the above exception, another exception occurred:
# more lines of error removed here

NameError: name 'quiet' is not defined
Makefile:860: recipe for target 'loadvariant' failed
make[2]: *** [loadvariant] Error 1

看起来手写规则似乎有效。修复更多语法错误后,有时 make 过程会跳过一些错误,有时会停止。我在这里仍然缺少一些东西。

【讨论】:

以上是关于如何将python集成到automake并在python语法错误时退出构建过程的主要内容,如果未能解决你的问题,请参考以下文章

将使用 Tensorflow 库的 Python 代码实现到 HTML 中?

如何将共享库与 --as-needed 与 automake 链接?

使用 autoconf/automake,如何指定包含文件路径?

Automake:如何在不安装的情况下构建共享库

将 paypal 按钮与自定义表单集成,并在单击该按钮时将客户信息保存到数据库中

如何在 autoconf/automake 中使用协议缓冲区?