forked from canonical-web-and-design/bzr-sync
/
bzr_sync.py
82 lines (67 loc) · 2.32 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
80
81
82
from os.path import isdir, join
from sh import bzr, cd, git, rm, ErrorReturnCode
from wsgi_helpers import ShLogger
def sync_git_to_bzr(
project_name,
github_user,
repositories_dir,
bzr_url):
"""
Using the provided {repositories_dir},
pull down the git project from github
(git@github.com:{git_user}/{project_name}.git),
export all commits to a bzr repository,
and push to launchpad at lp:{project_name}.
If the launchpad repo already exists, this will fail the first time
because the history will be entirely different from the existing history.
Therefore, after running this the first time and getting an error,
`cd {project_name}-bzr/` and run `bzr push --overwrite`.
From then on, every subsequent time you run this, for the same
repository directory, it should work fine.
"""
git_dir = join(repositories_dir, project_name + '-git')
bzr_dir = join(repositories_dir, project_name + '-bzr')
git_url = 'git@github.com:{0}/{1}.git'.format(github_user, project_name)
logger = ShLogger()
# Clone the git repo, or pull if it exists
if not isdir(git_dir):
logger.update_for_command(
"Cloning " + git_url,
git.clone(git_url, git_dir)
)
else:
logger.update_for_command(
"Pulling {0} changes".format(project_name),
git('-C', git_dir, 'pull')
)
# Always delete and recreate the bzr repo
logger.update_for_command(
"Removing bzr dir {0}".format(bzr_dir),
rm(bzr_dir, r=True, f=True)
)
try:
logger.update_for_command(
"Creating BZR repo",
bzr('init-repo', bzr_dir)
)
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_dir)
)
logger.update_for_command(
"Updating BZR repo",
bzr(
git('-C', git_dir, '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