20
20
PRCS source for the Mercurial convert extension
21
21
"""
22
22
23
+ from __future__ import absolute_import , unicode_literals
23
24
import re
24
25
import os
25
26
import sys
26
- from mercurial import extensions
27
27
from hgext .convert .common import NoRepo , commit , converter_source
28
- from hgext .convert .convcmd import source_converters
29
- from prcslib import PrcsVersion , PrcsProject , PrcsError , PrcsCommandError
28
+ from prcslib import PrcsVersion , PrcsProject , PrcsError
30
29
31
30
# Regular expression pattern that checks for main branches.
32
31
_MAIN_BRANCH_RE = re .compile (r"^(\d+)$" )
@@ -36,28 +35,26 @@ class prcs_source(converter_source):
36
35
PRCS source class.
37
36
"""
38
37
39
- def __init__ (self , ui , type , path , revs = None ):
38
+ def __init__ (self , ui , scm , path , revs = None ):
40
39
"""
41
40
initialize a PRCS source
42
41
"""
43
- super (prcs_source , self ).__init__ (ui , type , path , revs )
42
+ super (prcs_source , self ).__init__ (ui , scm , path , revs )
44
43
45
44
try :
46
- self ._prcs = PrcsProject (path )
47
- self ._revisions = self ._prcs .versions ()
45
+ self ._project = PrcsProject (path . decode () )
46
+ self ._revisions = self ._project .versions ()
48
47
except PrcsError :
49
48
raise NoRepo (b"%s does not look like a PRCS project" % path )
50
49
51
50
self ._cached_descriptor = {}
52
51
53
52
def _descriptor (self , version ):
54
53
"""Return a revision descriptor with caching."""
55
- if not isinstance (version , str ):
56
- version = str (version )
57
- if self ._cached_descriptor .has_key (version ):
54
+ if version in self ._cached_descriptor :
58
55
return self ._cached_descriptor [version ]
59
56
60
- descriptor = self ._prcs .descriptor (version )
57
+ descriptor = self ._project .descriptor (version )
61
58
self ._cached_descriptor [version ] = descriptor
62
59
return descriptor
63
60
@@ -81,44 +78,50 @@ def _nearest_ancestor(self, version):
81
78
return version
82
79
83
80
def getheads (self ):
84
- last_minor_version = {}
85
- for v in self ._revisions .iterkeys ():
86
- if not self ._revisions [v ]['deleted' ]:
87
- v = PrcsVersion (v )
88
- if last_minor_version .get (v .major (), 0 ) < v .minor ():
89
- last_minor_version [v .major ()] = v .minor ()
81
+ """
82
+ return all the head versions of the PRCS source
83
+ """
84
+ last_minors = {}
85
+ for key in self ._revisions :
86
+ if not self ._revisions [key ]["deleted" ]:
87
+ version = PrcsVersion (key )
88
+ if last_minors .get (version .major (), 0 ) < version .minor ():
89
+ last_minors [version .major ()] = version .minor ()
90
90
return map (
91
- lambda item : str (PrcsVersion (item [ 0 ], item [ 1 ]) ),
92
- last_minor_version . iteritems ())
91
+ lambda item : str (PrcsVersion (* item )). encode ( ),
92
+ last_minors . items ())
93
93
94
94
def getfile (self , name , version ):
95
- self .ui .debug ("prcs_source.getfile: " , name , " " , version , "\n " )
96
- revision = self ._revisions [version ]
95
+ """
96
+ get the content of a file
97
+ """
98
+ version = version .decode ()
97
99
descriptor = self ._descriptor (version )
98
-
99
100
files = descriptor .files ()
100
- try :
101
- a = files [name ]
102
- if a .has_key ('symlink' ):
103
- return (a ['symlink' ], 'l' )
104
-
105
- self ._prcs .checkout (version , [name ])
106
- file = open (name , 'rb' )
107
- content = file .read ()
108
- file .close ()
101
+ if name .decode () in files :
102
+ attr = files [name .decode ()]
103
+ if "symlink" in attr :
104
+ return attr ["symlink" ].encode (), b"l"
105
+
106
+ self ._project .checkout (version , files = [name .decode ()])
107
+
108
+ with open (name , "rb" ) as stream :
109
+ content = stream .read ()
110
+
109
111
# NOTE: Win32 does not always releases the file name.
110
- if sys .platform != ' win32' :
112
+ if sys .platform != " win32" :
111
113
os .unlink (name )
112
114
dir = os .path .dirname (name )
113
115
if dir :
114
116
os .removedirs (dir )
115
- return (content , 'x' if a ['mode' ] & (0x1 << 6 ) else '' )
116
- except KeyError :
117
- # The file with the specified name was deleted.
118
- return None , None
117
+
118
+ return content , b"x" if attr ["mode" ] & (0x1 << 6 ) else b""
119
+
120
+ # The file with the specified name was deleted.
121
+ return None , None
119
122
120
123
def getchanges (self , version , full = False ):
121
- self . ui . debug ( "prcs_source.getchanges: " , version , " \n " )
124
+ version = version . decode ( )
122
125
revision = self ._revisions [version ]
123
126
descriptor = self ._descriptor (version )
124
127
@@ -130,48 +133,47 @@ def getchanges(self, version, full=False):
130
133
p = self ._nearest_ancestor (p )
131
134
if full or p is None :
132
135
# This is the initial checkin so all files are affected.
133
- for name in f . iterkeys () :
134
- files .append ((name , version ))
136
+ for name in f :
137
+ files .append ((name . encode () , version . encode () ))
135
138
else :
136
139
pf = self ._descriptor (p ).files ()
137
140
# Handling added or changed files.
138
- for name , a in f .iteritems ():
139
- if pf . has_key ( name ) :
141
+ for name , a in f .items ():
142
+ if name in pf :
140
143
pa = pf [name ]
141
- if a . has_key ( ' symlink' ) :
142
- if not pa . has_key ( 'symlink' ) :
144
+ if " symlink" in a :
145
+ if "symlink" not in pa :
143
146
# Changed from a regular file to a symlink.
144
- files .append ((name , version ))
145
- elif pa . has_key ( ' symlink' ) :
147
+ files .append ((name . encode () , version . encode () ))
148
+ elif " symlink" in pa :
146
149
# Changed from a symlink to a regular file.
147
- files .append ((name , version ))
150
+ files .append ((name . encode () , version . encode () ))
148
151
elif a ['id' ] != pa ['id' ] \
149
152
or a ['revision' ] != pa ['revision' ] \
150
153
or (a ['mode' ] ^ pa ['mode' ]) & (0x1 << 6 ):
151
- files .append ((name , version ))
154
+ files .append ((name . encode () , version . encode () ))
152
155
else :
153
156
# Added.
154
- files .append ((name , version ))
157
+ files .append ((name . encode () , version . encode () ))
155
158
# Handling deleted or renamed files.
156
159
pnamebyid = {}
157
- for pname , pa in pf .iteritems ():
158
- if not f . has_key ( pname ) :
160
+ for pname , pa in pf .items ():
161
+ if pname not in f :
159
162
# Removed (or renamed).
160
- files .append ((pname , version ))
161
- if not pa . has_key ( 'symlink' ) :
163
+ files .append ((pname . encode () , version . encode () ))
164
+ if "symlink" not in pa :
162
165
pnamebyid [pa ['id' ]] = pname
163
166
# Handling renamed files for copies.
164
- for name , a in f .iteritems ():
165
- if not a .has_key ('symlink' ) and \
166
- pnamebyid .has_key (a ['id' ]):
167
+ for name , a in f .items ():
168
+ if "symlink" not in a and a ['id' ] in pnamebyid :
167
169
pname = pnamebyid [a ['id' ]]
168
170
if name != pname :
169
- self .ui .note (pname , " was renamed to " , name , " \n " )
170
- copies [name ] = pname
171
+ self .ui .note (b"%s was renamed to %s \n " % ( pname . encode (), name . encode ()) )
172
+ copies [name . encode () ] = pname . encode ()
171
173
return files , copies , set ()
172
174
173
175
def getcommit (self , version ):
174
- self . ui . debug ( "prcs_source.getcommit: " , version , " \n " )
176
+ version = version . decode ( )
175
177
revision = self ._revisions [version ]
176
178
descriptor = self ._descriptor (version )
177
179
@@ -180,19 +182,19 @@ def getcommit(self, version):
180
182
# Preparing for a deleted parent.
181
183
p = self ._nearest_ancestor (p )
182
184
if p is not None :
183
- parents .append (str (p ))
185
+ parents .append (str (p ). encode () )
184
186
for mp in descriptor .mergeparents ():
185
187
# Preparing for a deleted merge parent.
186
188
mp = self ._nearest_ancestor (mp )
187
189
if mp is not None :
188
- parents .append (str (mp ))
190
+ parents .append (str (mp ). encode () )
189
191
190
192
branch = PrcsVersion (version ).major ()
191
193
if _MAIN_BRANCH_RE .match (branch ):
192
194
branch = None
193
195
return commit (
194
- revision ['author' ], revision ['date' ].isoformat (" " ),
195
- descriptor .message (), parents , branch )
196
+ revision ['author' ]. encode () , revision ['date' ].isoformat (). encode ( ),
197
+ descriptor .message (). encode (), parents , branch )
196
198
197
199
def gettags (self ):
198
200
"""Return an empty dictionary since PRCS has no tags."""
0 commit comments