This repository has been archived by the owner on May 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bzr_sync.py
79 lines (66 loc) · 2.06 KB
/
bzr_sync.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
from os.path import isdir, join, abspath, dirname
from sh import bzr, cd, git, rm, ErrorReturnCode
from wsgi_helpers import ShLogger
def sync_git_to_bzr(
git_url,
bzr_url,
repositories_dir=join(dirname(abspath(__file__)), 'repositories'),
):
"""
Using the provided {repositories_dir},
pull down the git project from a git host (git_url),
export all commits to a bzr repository,
and push to a bzr host (bzr_url).
"""
git_dir = git_url.replace('/', '-').replace(':', '-')
bzr_dir = bzr_url.replace('/', '-').replace(':', '-')
git_path = join(repositories_dir, git_dir)
bzr_path = join(repositories_dir, bzr_dir)
logger = ShLogger()
# Clone the git repo, or pull if it exists
if not isdir(git_path):
logger.update_for_command(
"Cloning " + git_url,
git.clone(git_url, git_path)
)
else:
cd(git_path)
logger.update_for_command(
"Fetching " + git_url,
git.fetch(all=True)
)
logger.update_for_command(
"Resetting to HEAD",
git.reset('--hard', 'origin/master')
)
# Always delete and recreate the bzr repo
logger.update_for_command(
"Removing bzr dir {0}".format(bzr_path),
rm(bzr_path, r=True, f=True)
)
try:
logger.update_for_command(
"Creating BZR repo",
bzr('init-repo', bzr_path)
)
except ErrorReturnCode, sh_error:
logger.update('init-repo returned error code {0}. Message: {1}'.format(
str(sh_error.exit_code), sh_error.message
))
# Update the BZR repo with commits from git
logger.update_for_command(
"Entering bzr repo",
cd(bzr_path)
)
logger.update_for_command(
"Updating BZR repo",
bzr(
git('-C', git_path, 'fast-export', M=True, all=True),
'fast-import', '-'
)
)
logger.update_for_command(
"Pushing BZR changes",
bzr.push(bzr_url, overwrite=True, directory="trunk")
)
return logger.log