1 | #!/usr/bin/env python
|
---|
2 | #
|
---|
3 | # Usage bk2darcs local-bk-repo local-darcs-repo
|
---|
4 | #
|
---|
5 | # options:
|
---|
6 | # -i : create/initialize a new darcs repo
|
---|
7 | # example:
|
---|
8 | # bk2darcs.py /sandbox/petsc/petsc-dev-bk /sandbox/petsc/petsc-dev-darcs
|
---|
9 | #
|
---|
10 | # local-bk-repo is a valid bk repository
|
---|
11 | # local-darcs-repo is a new location
|
---|
12 |
|
---|
13 | import sys
|
---|
14 | import os
|
---|
15 |
|
---|
16 | def main():
|
---|
17 | createdarcsrepo=0
|
---|
18 | if '-i' in sys.argv:
|
---|
19 | sys.argv.remove('-i')
|
---|
20 | createdarcsrepo=1
|
---|
21 |
|
---|
22 | arg_len = len(sys.argv)
|
---|
23 | if arg_len != 3:
|
---|
24 | print 'Error Insufficient arguments.'
|
---|
25 | print 'Usage:', sys.argv[0], '[-i] local-bk-repo local-darcs-repo'
|
---|
26 | print 'Example:'
|
---|
27 | print ' bk2darcs.py /sandbox/petsc/petsc-dev-bk /sandbox/petsc/petsc-dev-darcs'
|
---|
28 | sys.exit()
|
---|
29 | bk_repo = sys.argv[1]
|
---|
30 | darcs_repo= sys.argv[2]
|
---|
31 |
|
---|
32 | # get absolute paths - this way - os.chdir() works
|
---|
33 | bk_repo = os.path.realpath(bk_repo)
|
---|
34 | darcs_repo = os.path.realpath(darcs_repo)
|
---|
35 |
|
---|
36 | # verify if bkdir exists
|
---|
37 | if not os.path.exists(bk_repo):
|
---|
38 | print 'Error! specified path does not exist: ' + bk_repo
|
---|
39 | sys.exit()
|
---|
40 |
|
---|
41 | # if createdarcsrepo - then create & initialize the repo [if dir exists]
|
---|
42 | # otherwise - make sure the dir exists [if not error]
|
---|
43 | if os.path.exists(darcs_repo):
|
---|
44 | if createdarcsrepo:
|
---|
45 | print 'Warning! ignoring option -i as specified darcsrepo exists: ' + darcs_repo
|
---|
46 | else:
|
---|
47 | if createdarcsrepo:
|
---|
48 | print 'Creating darcsrepo: ' + darcs_repo
|
---|
49 | os.mkdir(darcs_repo)
|
---|
50 | os.chdir(darcs_repo)
|
---|
51 | os.system("darcs initialize")
|
---|
52 | else:
|
---|
53 | print 'Error! specified path does not exist: ' + darcs_repo
|
---|
54 | print 'If you need to create a new darcs-repo, use option: -i'
|
---|
55 | sys.exit()
|
---|
56 |
|
---|
57 | # verify the specified dirs are valid repositories
|
---|
58 | os.chdir(bk_repo)
|
---|
59 | if os.system("bk changes -r+ > /dev/null 2>&1"):
|
---|
60 | print 'Error! specified path is not a bk repository: ' + bk_repo
|
---|
61 | sys.exit()
|
---|
62 |
|
---|
63 | os.chdir(darcs_repo)
|
---|
64 | if os.system("darcs changes --last=1 > /dev/null 2>&1"):
|
---|
65 | print 'Error! specified path is not a darcs repository: ' + darcs_repo
|
---|
66 | sys.exit()
|
---|
67 |
|
---|
68 | # now get the latest bk cset number
|
---|
69 | os.chdir(bk_repo)
|
---|
70 | fd=os.popen('bk changes -k -r+')
|
---|
71 | bk_cset_max=fd.read().strip()
|
---|
72 | fd.close()
|
---|
73 |
|
---|
74 | # similarly get the latest darcs cset number
|
---|
75 | os.chdir(darcs_repo)
|
---|
76 | fd=os.popen('darcs changes --last=1')
|
---|
77 | buf=fd.read()
|
---|
78 | fd.close()
|
---|
79 | if buf == '':
|
---|
80 | bk_cset_min = '1.0'
|
---|
81 | else:
|
---|
82 | bk_cset_min = buf.splitlines()[2].strip()
|
---|
83 |
|
---|
84 | if bk_cset_min == bk_cset_max:
|
---|
85 | print 'No new changesets Quitting! Last commit:', bk_cset_min
|
---|
86 | sys.exit()
|
---|
87 |
|
---|
88 | log_file=os.path.join(bk_repo,'darcs_log.tmp')
|
---|
89 | if os.path.isfile(log_file):
|
---|
90 | os.remove(log_file)
|
---|
91 |
|
---|
92 | # find the bk-changesets that need to be exported to darcs
|
---|
93 | # using -end:KEY avoids duplicate listing of TAGS [causes too much grief]
|
---|
94 | os.chdir(bk_repo)
|
---|
95 | fd=os.popen('bk changes -end:KEY: -f -r'+'"'+bk_cset_min+'".."'+bk_cset_max+'"')
|
---|
96 | buf=fd.read()
|
---|
97 | fd.close()
|
---|
98 | revs=buf.splitlines()
|
---|
99 |
|
---|
100 | if revs: lastrev = revs[-1]
|
---|
101 | #now process each revision [ignore the first]
|
---|
102 | for rev in revs:
|
---|
103 | # rev - basic string
|
---|
104 | # revq - rev with quotes [useable with -r]
|
---|
105 | # revn - rev number [ 1.234.4 etc..]
|
---|
106 | # revi - just the 2nd number in rev
|
---|
107 | os.chdir(bk_repo)
|
---|
108 | revq='"'+rev+'"'
|
---|
109 | # get the rev-number
|
---|
110 | fd=os.popen('bk changes -and:I: -r'+revq)
|
---|
111 | revn = fd.read().splitlines()[0]
|
---|
112 | fd.close()
|
---|
113 | # Don't know how to handle branch changesets
|
---|
114 | if len(revn.split('.')) > 2:
|
---|
115 | print 'Ignoring changeset : '+revn
|
---|
116 | continue
|
---|
117 |
|
---|
118 | print 'Processing changeset: '+revn
|
---|
119 | # get revi
|
---|
120 | revi = int(revn.split('.')[1])
|
---|
121 | # get username
|
---|
122 | fd=os.popen('bk changes -and:USER:@:HOST: -r'+revq)
|
---|
123 | auth_email=fd.read().splitlines()[0].strip()
|
---|
124 | fd.close()
|
---|
125 | auth_email=auth_email.replace('.(none)','')
|
---|
126 |
|
---|
127 | #get comment string
|
---|
128 | fd=os.popen('bk changes -r'+revq+' | grep -v ^ChangeSet@')
|
---|
129 | buf=fd.read()
|
---|
130 | fd.close()
|
---|
131 | msg = 'bk-changeset-'+revn + '\n' + rev + '\n'+ buf.strip() + '\n'
|
---|
132 | fd=open(log_file,'w')
|
---|
133 | fd.write(msg)
|
---|
134 | fd.close()
|
---|
135 |
|
---|
136 | os.chdir(darcs_repo)
|
---|
137 | # verify darcs again
|
---|
138 | if os.system("darcs changes --last=1 > /dev/null 2>&1"):
|
---|
139 | print 'Error! specified path is not a darcs repository: ' + darcs_repo
|
---|
140 | sys.exit()
|
---|
141 |
|
---|
142 | # Now remove the old files - and export the new modified files
|
---|
143 | os.system('ls -a | grep -v _darcs | xargs rm -rf >/dev/null 2>&1')
|
---|
144 | os.system('bk export -r'+revq+' ' + bk_repo + ' ' + darcs_repo)
|
---|
145 | os.system('darcs record --test -l -a -A ' + auth_email + ' --delete-logfile --logfile='+log_file + '> /dev/null 2>&1')
|
---|
146 |
|
---|
147 | # optimize/checkpoint every 250 patches
|
---|
148 | if revi%250 == 0 and revi != 0 and rev != lastrev:
|
---|
149 | print 'checkpointing/optimizing changeset-'+ revn
|
---|
150 | os.system('darcs tag -A snapshot@petsc snapshot-'+revn +'> /dev/null 2>&1')
|
---|
151 | os.system('darcs optimize --checkpoint -t snapshot-'+revn +'> /dev/null 2>&1')
|
---|
152 | return 0
|
---|
153 |
|
---|
154 | # The classes in this file can also
|
---|
155 | # be used in other python-programs by using 'import'
|
---|
156 | if __name__ == '__main__':
|
---|
157 | main()
|
---|
158 |
|
---|
159 |
|
---|