forked from udacity/ud851-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flatten.py
executable file
·142 lines (101 loc) · 4.21 KB
/
flatten.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#! /usr/local/bin/python
import argparse
import os
import shutil
import sys
import tempfile
import git
IGNORE_PATTERNS = ('.git', ".DS_Store")
SAFE_CHARS = ["-", "_", "."]
MAX_LENGTH = 100
STUDENT = "student"
DEVELOP = "develop-"
DEVELOP_DEFAULT = "all develop branches"
def flatten(repo_dir, target_dir, student, develop_branches, remove_branches, links):
repo = git.Repo(repo_dir)
if develop_branches == DEVELOP_DEFAULT:
develop_branches = [branch for branch in repo.branches if DEVELOP in branch.name]
remove_local_branches(repo, student, develop_branches)
try:
temp_dir = tempfile.mkdtemp()
for develop in develop_branches:
to_temp_dir(repo, repo_dir, develop, temp_dir)
copy_snapshots(repo, student, temp_dir, target_dir)
finally:
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
print "Done! Review and commit the", student, "branch at your leisure."
print "Then run $ git push --all --prune"
def remove_local_branches(repo, student, develop_branches):
for branch in repo.branches:
if branch.name != student and branch not in develop_branches:
print "Removing local branch:", branch.name
repo.git.branch(branch.name, "-D")
def to_temp_dir(repo, repo_dir, develop, temp_dir):
for rev in repo.git.rev_list(develop).split("\n"):
commit = repo.commit(rev)
branch_name = clean_commit_message(commit.message)
if "Exercise" in branch_name or "Solution" in branch_name:
if branch_name in repo.branches:
repo.git.branch(branch_name, "-D")
new_branch = repo.create_head(branch_name)
new_branch.set_commit(rev)
repo.git.checkout(commit)
print "Saving snapshot of:", branch_name
repo.git.clean("-fdx")
folder_name = develop.name.split("-",1)[1]
target_dir = os.path.join(temp_dir, folder_name, branch_name)
shutil.copytree(repo_dir, target_dir,
ignore=shutil.ignore_patterns(*IGNORE_PATTERNS))
def clean_commit_message(message):
first_line = message.split("\n")[0]
safe_message = "".join(
c for c in first_line if c.isalnum() or c in SAFE_CHARS).strip()
return safe_message[:MAX_LENGTH] if len(safe_message) > MAX_LENGTH else safe_message
def copy_snapshots(repo, student, temp_dir, target_dir):
if target_dir == os.getcwd():
repo.git.checkout(student)
for item in os.listdir(temp_dir):
source_dir = os.path.join(temp_dir, item)
dest_dir = os.path.join(target_dir, item)
if os.path.exists(dest_dir):
shutil.rmtree(dest_dir)
print "Copying: ", item
shutil.copytree(source_dir, dest_dir)
DESCRIPTION = "This script "
EPILOG = " To make changes to "
def main():
parser = argparse.ArgumentParser(
description=DESCRIPTION,
epilog=EPILOG,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-b', '--remove',
action='store_true',
help='delete all local branches except the student and develop branches')
parser.add_argument('-d', '--directory',
default=os.getcwd(),
help="the directory of the source repository")
parser.add_argument('-t', '--target',
default=os.getcwd(),
help="target directory")
parser.add_argument('-s', '--student',
default=STUDENT,
help="branch where snapshots will be copied")
parser.add_argument('-l', '--links',
action='store_true',
help="Add links to branches and diff to README files")
parser.add_argument('develop_branches',
nargs="*",
default=DEVELOP_DEFAULT,
help="the branches where snapshots will be copied from")
parsed = parser.parse_args()
flatten(
parsed.directory,
parsed.target,
parsed.student,
parsed.develop_branches,
parsed.remove,
parsed.links
)
if __name__ == "__main__":
sys.exit(main())