为了账号安全,请及时绑定邮箱和手机立即绑定

从 python 中运行符号链接目录中的 Makefile

从 python 中运行符号链接目录中的 Makefile

ABOUTYOU 2023-10-06 10:46:59
我有一个~/PROJECTS包含几个子目录的目录,其中一些是到其他目录的符号链接。. ├── proj1_symlink_dir ├── proj2_symlink_dir ├── proj3_symlink_dir ├── backup_1_dir ├── backup_2_dir每个符号链接目录(指向我的硬盘驱动器上的其他目录)即 [proj1_symlink_dir, proj2_symlink_dir, proj3_symlink_dir] 每个都包含一个Makefile.我想写一个python脚本:仅循环活动目录中的符号链接对于每个符号链接,进入目录并运行make clean(或允许包含 make 命令的字符串参数)有人可以协助编写一个紧凑的 pythonic 脚本来帮助执行上述任务吗?到目前为止,我有以下内容来打印符号链接(改编自此处):dirname = os.getcwd()for name in os.listdir(dirname):    if name not in (os.curdir, os.pardir):        full = os.path.join(dirname, name)        if os.path.islink(full):            print(name, '->', os.readlink(full))我不知道如何Makefile安全地处理 python 中运行的命令更新在@Marat 的帮助下,我现在创建了以下名为 的脚本 runmke.py。#!/Usr/bin/env pythonimport argparseimport osimport jsondef run_symlink_makefile_cmd(dirname, make_cmds, verbose):    """    Run common make commands from makefiles from     all symlinked directories that are located     in a specified directory    """    make_cmds_str = " ".join(make_cmds)    for name in os.listdir(dirname):        if name not in (os.curdir, os.pardir):            full = os.path.join(dirname, name)            if os.path.islink(full):                if verbose:                    print(f"\n>>>>> Running the Make command:")                    print(f"make -C {full} {make_cmds_str}")                os.system(f"make -C {full} {make_cmds_str}")def main(dirname, make_cmds, verbose):    # Display parameters passed for the given run (includes defaults)    print(f"""The parameters for this run are:\n {json.dumps(locals(), indent=2, default=str)}""")    run_symlink_makefile_cmd(dirname=dirname,                             make_cmds=make_cmds,                             verbose=verbose)
查看完整描述

1 回答

?
富国沪深

TA贡献1790条经验 获得超9个赞

我现在创建了以下名为 的脚本 runmke.py。


#!/Usr/bin/env python


import argparse

import os

import json


def run_symlink_makefile_cmd(dirname, make_cmds, verbose):

    """

    Run common make commands from makefiles from 

    all symlinked directories that are located 

    in a specified directory

    """

    make_cmds_str = " ".join(make_cmds)

    for name in os.listdir(dirname):

        if name not in (os.curdir, os.pardir):

            full = os.path.join(dirname, name)

            if os.path.islink(full):

                if verbose:

                    print(f"\n>>>>> Running the Make command:")

                    print(f"make -C {full} {make_cmds_str}")

                os.system(f"make -C {full} {make_cmds_str}")


def main(dirname, make_cmds, verbose):

    # Display parameters passed for the given run (includes defaults)

    print(f"""The parameters for this run are:\n {json.dumps(locals(), indent=2, default=str)}""")

    run_symlink_makefile_cmd(dirname=dirname,

                             make_cmds=make_cmds,

                             verbose=verbose)

    

if __name__ == '__main__':

    parser = argparse.ArgumentParser()

    parser.add_argument('-d', '--dirname', action='store', default=os.getcwd(),

                        help='The directory in which symlink directories, default is the current directory')

    parser.add_argument('-m', '--make_cmds', nargs='+',

                        default=["clean", "latex_style"],

                        help='These are the Makefile commands to run')

    parser.add_argument('-v', '--verbose', action='store_true', default=True,

                        help='If true, print updates while processing.')

    argument_parsed = parser.parse_args()


    main(

        dirname=argument_parsed.dirname,

        make_cmds=argument_parsed.make_cmds,

        verbose=argument_parsed.verbose

    )

它可以在终端使用运行./runmke.py -m latex_style -v


查看完整回答
反对 回复 2023-10-06
  • 1 回答
  • 0 关注
  • 56 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信