我有一个~/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 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)
富国沪深
相关分类