代码之家  ›  专栏  ›  技术社区  ›  Dave

查找所有使用“打印”但没有“从未来导入打印”功能的文件`

  •  0
  • Dave  · 技术社区  · 6 年前

    我正在将代码库从2.7迁移到3.6,并希望确保所有使用 print __future__ 进口

    我该怎么办 find/grep/ack 递归地通过多包代码库查找所有使用 打印 但你没有 from __future__ import print_function ?

    我知道 2to3 应该自动处理,但我见过一个例子,其中有一个表单的打印表达式 print("updated database: ", db_name) 在一个文件中 包括打印功能导入。跑步 2to3-2.7 在此文件上,将行转换为 print(("updated database: ", db_name)) ,这会改变输出。我希望找到所有可能出现此问题的实例,以便在运行自动工具之前修复它们

    1 回复  |  直到 6 年前
        1
  •  1
  •   jfaccioni    6 年前

    如果您不介意在Python中这样做:

    import os
    
    for folder, subfolder, files in os.walk('/my/project/dir'): 
        scripts = [f for f in files if f.endswith('.py')] 
        for script in scripts: 
            path = os.path.join(folder, script) 
            with open(path, 'r') as file: 
                text = file.read() 
            if "print" in text and "print_function" not in text: 
                print("future print import not found in file", path) 
    
        2
  •  0
  •   denis    6 年前

    2个egrep(与Mac egrep和gnu egrep合作,其他人不知道)--

    #!/bin/bash
    
    fileswithprint=` egrep --files-with-matches --include '*.py' --recursive  -w print  $* `
        # -R: Follow all symbolic links, unlike -r
        # too long: see xargs
    
    egrep --files-without-match '^from __future .*print_function'  $fileswithprint