亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 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 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號