【问题标题】:How to generate __init__.py in all subdirectories of current directory in cmake?如何在cmake的当前目录的所有子目录中生成__init__.py?
【发布时间】:2012-07-12 09:43:19
【问题描述】:

我使用 CMake 的树外构建。 我有一个 CMake 自定义命令,可以从原始文件生成 *_pb2.py 文件。 由于 proto-files 可能驻留在未知数量的子目录(包命名空间)中,例如 $SRC/package1/package2/file.proto,因此构建目录将包含类似 $BLD/package1/package2/file_pb2.py 的内容。

我想从自动生成的 *_pb2.py 文件中隐式地制作包,因此,我想在所有子文件夹($BLD/package1$BLD/package1/package2 等)中自动生成 __init__.py 文件,然后安装它们.

我该怎么做?

P.S. 我已经尝试了 CMake : How to get the name of all subdirectories of a directory? 的宏(将 GLOB 更改为 GLOB_RECURSE),但它只返回包含文件的子目录。我无法从上面的示例中获取 package1 子目录。

【问题讨论】:

    标签: python cmake protocol-buffers


    【解决方案1】:

    如果您在 *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)
    

    【讨论】:

      【解决方案2】:

      以下内容应为您提供变量AllPaths 中所需的目录列表:

      # 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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-08
        • 2010-11-01
        • 2011-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-21
        相关资源
        最近更新 更多