forked from douban/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
132 lines (101 loc) · 5.17 KB
/
utils.py
File metadata and controls
132 lines (101 loc) · 5.17 KB
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
import os
import tempfile
import shutil
from mock import Mock
from contextlib import contextmanager
from vilya.libs import gyt
from vilya.libs.permdir import get_repo_root
from ellen.repo import Jagare
from vilya.models.project import CodeDoubanProject
TEMP_PROJECT_OWNER = 'testuser'
TEMP_PROJECT_DESCRIPTION = 'This is a test project.'
BARE_REPO_PATH = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'data', 'bare_repo1')
content_a = """a_hunk = [('idem', u'/* highlight style */'), ('rem', u'.highlight { color: #008000; font-weight: bold; } /* Keyword */'), ('add', u'.highlight .hll { background-color: #ffffcc; }'), ('add', u'.highlight { background: #ffffff; }'), ('add', u'.highlight .c { color: #808080; } /* Comment */'), ('add', u'.highlight .err { color: #F00000; background-color: #F0A0A0; } /* Error */'), ('add', u'.highlight .k { color: #008000; font-weight: bold; } /* Keyword */'), ('idem', u'.highlight .o { color: #303030; } /* Operator */'), ('idem', u'.highlight .cm { color: #808080; } /* Comment.Multiline */'), ('idem', u'.highlight .cp { color: #507090; } /* Comment.Preproc */')]""" # noqa
content_b = """a_hunk = [('idem', u'/* highlight style */'), ('rem', u'.highlight { color: #008000; font-weight: bold; } /* Keyword */'), ('add', u'.highlight .hll { background-color: #ffffcc; }'), ('add', u'.highlight { backggggground: #ffffff; }'), ('add', u'.high .c { color: #808080; } /* Coomment */'), ('add', u'.highlight .err { color: #F00000; background-color: #F00A0A0; } /* Error */'), ('add', u'.highlight .k { color: #008000; font-weight: bold; } /* Keyword */'), ('idem', u'.highlight .o { color: #303; } /* Operator ****/'), ('idem', u'.highlight .cm { color: #808080; } /* Comment.Multiline */'),('idem', u'adfadsfadsf'), ('idem', u'.highlight .cp { color: #507090; } /* Comment.Preproc */')]""" # noqa
@contextmanager
def mkdtemp():
tmpdir = tempfile.mkdtemp()
yield tmpdir
shutil.rmtree(tmpdir)
@contextmanager
def chdir(dir):
cwd = os.getcwd()
os.chdir(dir)
yield
os.chdir(cwd)
class FakeEnv(object):
"""A fake trac env"""
def __init__(self, name):
self.project_name = name
@contextmanager
def new_git_bare_repo():
with mkdtemp() as tmpdir:
gyt.call(['git', 'init', '--bare', tmpdir])
yield tmpdir
@contextmanager
def clone(git_dir):
with mkdtemp() as work_path:
gyt.call(['git', 'clone', git_dir, work_path])
assert os.path.exists(work_path+'/.git')
yield work_path
with chdir(work_path):
gyt.call(['git', 'config', 'user.name', 'test author'])
gyt.call(['git', 'add', "."])
gyt.call(['git', 'commit', '-m', 'test'], _raise=True)
gyt.call(['git', 'push', 'origin', 'master'], _raise=True)
def mock_method(real_method):
mock = Mock(wraps=real_method)
def _(*a, **kw):
return mock(*a, **kw)
_.mock = mock
return _
def setup_repos(tmpdir, prj_name='test_proj'):
origin_project = CodeDoubanProject.add(prj_name, 1,
create_trac=False)
path = origin_project.git_real_path
with clone(path) as workdir:
with open(os.path.join(workdir, 'origin'), 'w') as f:
f.write(content_a)
fork_project = CodeDoubanProject.add(prj_name + '_fork', 2,
fork_from=origin_project.id,
create_trac=False)
fork_path = fork_project.git_real_path
repo = origin_project
fork_repo = fork_project
return path, repo, fork_path, fork_repo
@contextmanager
def setup2repos(prj_name):
"""setup_2_repos_and_make_changes_in_fork"""
with mkdtemp() as tmpdir:
path, repo, fork_path, fork_repo = setup_repos(tmpdir, prj_name)
with clone(fork_path) as work_path:
with open(os.path.join(work_path, 'a'), 'w') as f:
f.write(content_b)
yield path, repo, fork_path, fork_repo
def get_temp_project(origin=None, repo_path=BARE_REPO_PATH):
if origin:
prefix_path = get_repo_root()
temp_repo_path = tempfile.mkdtemp(suffix=".git",
prefix="test_",
dir=prefix_path)
project_name = temp_repo_path[len(prefix_path) + 1:][:-4]
project = CodeDoubanProject.add(project_name,
TEMP_PROJECT_OWNER,
TEMP_PROJECT_DESCRIPTION,
fork_from=origin.id,
create_trac=False)
return project
prefix_path = get_repo_root()
temp_repo_path = tempfile.mkdtemp(suffix=".git",
prefix="test_",
dir=prefix_path)
project_name = temp_repo_path[len(prefix_path) + 1:][:-4]
project = CodeDoubanProject.add(project_name, TEMP_PROJECT_OWNER,
TEMP_PROJECT_DESCRIPTION)
shutil.rmtree(temp_repo_path)
repo = Jagare(repo_path)
repo.clone(temp_repo_path, bare=True)
return project