我想添加一个具有扩展功能的build
命令。具体来说,它编译Go共享库。这是一个漫长的过程,Go源很少被修改。因此...
有没有办法让setuptools
记住文件运行时的最后修改日期,或者我需要一个更严肃的构建工具(如SCons)。我不在乎它是否通过时间戳或任何其他简单的策略确定新近度。
以下是有问题的代码:
class BuildGo(build_py):
'''
Build Go bindings before building Python extension
'''
def run(self):
# What should go into go_sources_modified?
if self.go_sources_modified():
self.compile_go()
build_py.run(self)
def compile_go(self):
version = None
refusal = 'Will not compile Go bindings'
try:
version = subprocess.check_output(['go', 'version'])
except subprocess.CalledProcessError:
print('Didn\'t find Go compiler.')
print(refusal)
return
match = re.search(br'go(\d+)\.(\d+)\.(\d+)', version)
if match:
version = tuple(map(int, match.group(1, 2, 3)))
else:
print('Unrecognized version of Go compiler: {}'.format(version))
print(refusal)
return
req_version = 1, 9, 1
if version < req_version:
print('You need Go compiler version higher than: {}'.format(
req_version,
))
print(refusal)
return
try:
result = subprocess.check_output([
'go',
'build',
'-v',
'-buildmode=c-shared',
'-o',
SHLIB_LOCATION,
SHLIB_PACKAGE,
])
print(result)
except subprocess.CalledProcessError as e:
print(e.stderr)
raise
答案 0 :(得分:1)
您可以使用distutils.dep_util
模块中的函数来检查自上次构建库以来是否已修改任何go源:
from distutils.dep_util import newer_group
class BuildGo(build_py):
...
def go_sources_modified(self):
gosrc = pathlib.Path('go/src/lib')
go_sources = list(str(f.resolve()) for f in gosrc.rglob('*.go'))
golib = 'go/pkg/mylib.a'
return newer_group(go_sources, golib)