Created
January 1, 2015 21:25
-
-
Save cr5315/1eb393909c02897784dd to your computer and use it in GitHub Desktop.
Python script to auto cherry-pick changes, reads from files that have a .cp extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import subprocess | |
import time | |
OFFSET = raw_input("Please enter any path between this script and your ROM source (blank for none): ") | |
# Do some setup | |
STARTING_DIR = os.getcwd() | |
STARTING_TIME = time.time() | |
# Check to see if there is a *.cp file in the current directory | |
files = [] | |
for (dirpath, dirnames, filenames) in os.walk(STARTING_DIR): | |
for f in filenames: | |
if f.endswith(".cp"): | |
files.append(f) | |
break | |
dirs = {} # Dirs and the commands to be run in them | |
# {"device/lge/g3-common": ["git fetch...", "git fetch...:]} | |
# Go through each file and populate the dirs dict | |
for _file in files: | |
print "Reading file: %s" % _file | |
fopen = open(_file, "r") | |
lines = fopen.readlines() | |
fopen.close() | |
for line in lines: | |
line = line.strip() | |
if line.startswith("#") or line == "": | |
continue | |
# Check to make sure the line is a cherry-pick | |
words = line.split() | |
if words[0] != "git" or words[1] != "fetch": | |
continue | |
# Get the directory each change is for | |
url = words[2] | |
url = url[url.rfind("/") + 1:] | |
d = url.replace("_", "/") | |
d = d[len("android") + 1:] | |
# Add the cherry-pick to this directory | |
if dirs.setdefault(d, [line]) != [line]: | |
dirs[d].append(line) | |
num_dirs = len(dirs) | |
num_changes = 0 | |
for key in dirs: | |
for i in dirs[key]: | |
num_changes += 1 | |
print "%d changes in %d directories" % (num_changes, num_dirs) | |
# Go from directory to directory and cherry-pick | |
for key in dirs: | |
goto = STARTING_DIR + "/" + OFFSET + "/" + key | |
print "Entering %s" % goto | |
os.chdir(goto) | |
# Run the commands | |
for command in dirs[key]: | |
subprocess.call(command, shell=True) | |
# Return to the starting dir | |
print "Entering %s" % STARTING_DIR | |
os.chdir(goto) | |
print "Completed in %s seconds" % str(time.time() - STARTING_TIME) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment