forked from wgnet/SCMReplication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
P4SvnReplicate.py
executable file
·133 lines (104 loc) · 4.15 KB
/
P4SvnReplicate.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
#!/usr/bin/python3
'''P4 to Svn replication script
'''
import os
import sys
import tempfile
from lib.buildlogger import getLogger
from lib.buildcommon import remove_dir_contents
from lib.scmrepargs import get_arguments
from lib.p4server import P4Server
from lib.PerforceToSubversion import PerforceToSubversion
#from lib.SvnPython import SvnPython, SvnPythonException
from P4P4Replicate import (create_p4_workspace,
delete_p4_workspace,)
logger = getLogger(__name__)
logger.setLevel('INFO')
def create_P42Svn_cfg_file(src_cfg, dst_cfg):
'''create cfg file for svn2p4 replicating script
A temporary file is generated by this function.
@param src_cfg dict instance for svn cfgs
@param dst_cfg dict instance for p4 cfgs
'''
svn_cfg_item = ['svn_client_root', 'svn_repo_label',
'svn_project_dir', 'svn_repo_url', 'svn_user',
'svn_passwd', 'rev_counter']
p4_cfg_item = ['p4client', 'p4port', 'p4user', 'p4passwd', 'counter',
'endchange', ]
src_cfg_str = '\n'.join(['%s=%s' % (k, v)
for k, v in src_cfg.items()
if k in p4_cfg_item and v is not None])
tgt_cfg_str = '\n'.join(['%s=%s' % (k, v)
for k, v in dst_cfg.items()
if k in svn_cfg_item and v is not None])
src_content = '[source]\n' + src_cfg_str
tgt_content = '[target]\n' + tgt_cfg_str
gen_content = '[general]\n'
cfg_content = '\n'.join([src_content, tgt_content, gen_content])
cfg_fd, cfg_path = tempfile.mkstemp(suffix='.cfg', text=True)
os.write(cfg_fd, str.encode(cfg_content))
os.close(cfg_fd)
return cfg_path
def replicate(args):
'''Create temporary workspace and cfg file for svn2p4 replicating
script and call it to replicate.
'''
src_cfg = {'p4port': args.source_port,
'p4user': args.source_user,
'p4passwd': args.source_passwd,
'counter': args.source_counter,
'endchange': args.source_last_changeset,
'ws_root': args.workspace_root,
'mappingcfg': args.source_workspace_view_cfgfile, }
dst_cfg = {'svn_repo_url': args.target_port,
'svn_user': args.target_user,
'svn_passwd': args.target_passwd,
'svn_project_dir': None,
'rev_counter': 0,
'ws_root': args.workspace_root,
'mappingcfg': args.target_workspace_view_cfgfile,
'svn_repo_label': args.target_port,
'svn_client_root': args.workspace_root, }
# create target p4 workspace
src_p4 = P4Server(
src_cfg['p4port'],
src_cfg['p4user'],
src_cfg['p4passwd'])
create_p4_workspace(src_p4, src_cfg)
src_cfg['p4client'] = src_p4.client
# update project dir
svn_replicate_dir = None
with open(dst_cfg['mappingcfg'], 'rt') as f:
svn_replicate_dirs = list(f)
svn_replicate_dir = svn_replicate_dirs[0].strip()
dst_cfg['svn_project_dir'] = svn_replicate_dir
dry_run = hasattr(args, 'dry_run') and args.dry_run
try:
# create cfg file
p4_to_svn_rep_cfg = create_P42Svn_cfg_file(src_cfg, dst_cfg)
# make a new argv for svn2p4 script
sys.argv = [__name__, '-c', p4_to_svn_rep_cfg]
if args.maximum:
sys.argv.extend(['-m', str(args.maximum)])
if dry_run:
sys.argv.append('--dry-run')
if args.verbose:
sys.argv.extend(['--verbose', args.verbose])
if (hasattr(args, 'prefix_description_with_replication_info')
and args.prefix_description_with_replication_info):
sys.argv.append('--prefix-description-with-replication-info')
# let's go
ret = PerforceToSubversion()
except Exception as e:
logger.error(e)
raise
else:
remove_dir_contents(src_cfg['ws_root'])
finally:
os.remove(p4_to_svn_rep_cfg)
delete_p4_workspace(src_p4)
return ret
if __name__ == '__main__':
args = get_arguments('P4', 'SVN')
logger.setLevel(args.verbose)
replicate(args)