forked from udacity/ud851-Sunshine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flatten.py
executable file
·88 lines (63 loc) · 2.43 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
#! /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"
def flatten():
repo = git.Repo(os.getcwd())
remove_local_branches(repo, STUDENT, DEVELOP)
repo.git.clean("-fdx")
try:
temp_dir = tempfile.mkdtemp()
to_temp_dir(repo, os.getcwd(), DEVELOP, temp_dir)
copy_snapshots(repo, STUDENT, temp_dir, os.getcwd())
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):
for branch in repo.branches:
if branch.name != student and branch.name != develop:
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")
target_dir = os.path.join(temp_dir, 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):
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)
def main():
flatten()
if __name__ == "__main__":
sys.exit(main())