Python 如何在cmake中当前目录的所有子目录中生成_init__uuu.py?
我使用CMake的树外构建。 我有一个CMake自定义命令,它从proto文件生成*_pb2.py文件。 由于proto文件可能位于数量未知的子目录(包名称空间),如Python 如何在cmake中当前目录的所有子目录中生成_init__uuu.py?,python,cmake,protocol-buffers,Python,Cmake,Protocol Buffers,我使用CMake的树外构建。 我有一个CMake自定义命令,它从proto文件生成*_pb2.py文件。 由于proto文件可能位于数量未知的子目录(包名称空间),如$SRC/package1/package2/file.proto,因此构建目录将包含类似$BLD/package1/package2/file_pb2.py的内容 我想从自动生成的*u pb2.py文件隐式生成包,因此,我想在所有子文件夹中自动生成uu init_uuuuuy.py文件($BLD/package1,$BLD/pac
$SRC/package1/package2/file.proto
,因此构建目录将包含类似$BLD/package1/package2/file_pb2.py
的内容
我想从自动生成的*u pb2.py文件隐式生成包,因此,我想在所有子文件夹中自动生成uu init_uuuuuy.py文件($BLD/package1
,$BLD/package1/package2
等),然后安装它们
我该怎么做
另外,我尝试了宏from(将GLOB更改为GLOB\u RECURSE),但它只返回包含文件的子目录。我无法从上面的示例中获取
package1
subdir。如果您在*NIX操作系统(包括mac)下工作,可以使用shell find命令,如:
ROOT="./"
for DIR in $(find $ROOT -type d); do
touch $DIR/__init__.py
done
或者使用python脚本:
from os.path import isdir, walk, join
root = "/path/to/project"
finit = '__init__.py'
def visitor(arg, dirname, fnames):
fnames = [fname for fname in fnames if isdir(fname)]
# here you could do some additional checks ...
print "adding %s to : %s" %(finit, dirname)
with open(join(dirname, finit), 'w') as file_: file_.write('')
walk(root, visitor, None)
以下内容应提供变量
allpath
中所需的目录列表:
# Get paths to all .py files (relative to build dir)
file(GLOB_RECURSE SubDirs RELATIVE ${CMAKE_BINARY_DIR} "${CMAKE_BINARY_DIR}/*.py")
# Clear the variable AllPaths ready to take the list of results
set(AllPaths)
foreach(SubDir ${SubDirs})
# Strip the filename from the path
get_filename_component(SubDir ${SubDir} PATH)
# Change the path to a semi-colon separated list
string(REPLACE "/" ";" PathParts ${SubDir})
# Incrementally rebuild path, appending each partial path to list of results
set(RebuiltPath ${CMAKE_BINARY_DIR})
foreach(PathPart ${PathParts})
set(RebuiltPath "${RebuiltPath}/${PathPart}")
set(AllPaths ${AllPaths} ${RebuiltPath})
endforeach()
endforeach()
# Remove duplicates
list(REMOVE_DUPLICATES AllPaths)