Skip to content

Instantly share code, notes, and snippets.

@LeoHuckvale
Created January 8, 2016 15:21
Show Gist options
  • Save LeoHuckvale/f6098d80b0c1d6943e9f to your computer and use it in GitHub Desktop.
Save LeoHuckvale/f6098d80b0c1d6943e9f to your computer and use it in GitHub Desktop.
Running parallel commands in multiple similar directories
#!/usr/bin/env python
# Command Repeater
#
# For a directory tree like:
#
# MainProject/
# ├── project-one
# ├── project-two
# ├── project-three
# └── rep # This script
#
# ... this script will run the same command in each subdirectory,
# as long as that subdirectory exists in the `project_dirs` variable below
#
# For example:
#
# ./rep git status
#
# ... will show, e.g.:
#
# ********************************
# PROJECT: project-one
# ********************************
#
# On branch master
# nothing to commit, working directory clean
#
#
# ********************************
# PROJECT: project-two
# ********************************
#
# On branch master
# nothing to commit, working directory clean
#
#
# ********************************
# PROJECT: project-three
# ********************************
#
# On branch master
# nothing to commit, working directory clean
#
import os
import sys
import subprocess
# Subdirectories
project_dirs = ['project-' + project for project in ['one', 'two', 'three]]
cwd = os.getcwd()
separator = '*' * 32 + '\n' + 'PROJECT: {}\n' + '*' * 32 + '\n'
for directory in project_dirs:
os.chdir(os.path.join(cwd, directory))
print(separator.format(directory))
proc = subprocess.Popen(sys.argv[1:])
returncode = proc.wait()
os.chdir(cwd)
print('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment