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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
# cvstree.py -- cvs tree utilities
# Copyright 1998-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
from __future__ import print_function
import codecs
import re
import stat
import sys
import time
from portage import os
from portage import _encodings
from portage import _unicode_encode
if sys.hexversion >= 0x3000000:
long = int
# [D]/Name/Version/Date/Flags/Tags
def pathdata(entries, path):
"""(entries,path)
Returns the data(dict) for a specific file/dir at the path specified."""
mysplit=path.split("/")
myentries=entries
mytarget=mysplit[-1]
mysplit=mysplit[:-1]
for mys in mysplit:
if mys in myentries["dirs"]:
myentries=myentries["dirs"][mys]
else:
return None
if mytarget in myentries["dirs"]:
return myentries["dirs"][mytarget]
elif mytarget in myentries["files"]:
return myentries["files"][mytarget]
else:
return None
def fileat(entries, path):
return pathdata(entries,path)
def isadded(entries, path):
"""(entries,path)
Returns true if the path exists and is added to the cvs tree."""
mytarget=pathdata(entries, path)
if mytarget:
if "cvs" in mytarget["status"]:
return 1
basedir=os.path.dirname(path)
filename=os.path.basename(path)
try:
myfile = codecs.open(
_unicode_encode(os.path.join(basedir, 'CVS', 'Entries'),
encoding=_encodings['fs'], errors='strict'),
mode='r', encoding=_encodings['content'], errors='strict')
except IOError:
return 0
mylines=myfile.readlines()
myfile.close()
rep=re.compile("^\/"+re.escape(filename)+"\/");
for x in mylines:
if rep.search(x):
return 1
return 0
def findnew(entries,recursive=0,basedir=""):
"""(entries,recursive=0,basedir="")
Recurses the entries tree to find all elements that have been added but
have not yet been committed. Returns a list of paths, optionally prepended
with a basedir."""
if basedir and basedir[-1]!="/":
basedir=basedir+"/"
mylist=[]
for myfile in entries["files"]:
if "cvs" in entries["files"][myfile]["status"]:
if "0" == entries["files"][myfile]["revision"]:
mylist.append(basedir+myfile)
if recursive:
for mydir in entries["dirs"]:
mylist+=findnew(entries["dirs"][mydir],recursive,basedir+mydir)
return mylist
def findoption(entries, pattern, recursive=0, basedir=""):
"""(entries, pattern, recursive=0, basedir="")
Iterate over paths of cvs entries for which the pattern.search() method
finds a match. Returns a list of paths, optionally prepended with a
basedir."""
if not basedir.endswith("/"):
basedir += "/"
for myfile, mydata in entries["files"].items():
if "cvs" in mydata["status"]:
if pattern.search(mydata["flags"]):
yield basedir+myfile
if recursive:
for mydir, mydata in entries["dirs"].items():
for x in findoption(mydata, pattern,
recursive, basedir+mydir):
yield x
def findchanged(entries,recursive=0,basedir=""):
"""(entries,recursive=0,basedir="")
Recurses the entries tree to find all elements that exist in the cvs tree
and differ from the committed version. Returns a list of paths, optionally
prepended with a basedir."""
if basedir and basedir[-1]!="/":
basedir=basedir+"/"
mylist=[]
for myfile in entries["files"]:
if "cvs" in entries["files"][myfile]["status"]:
if "current" not in entries["files"][myfile]["status"]:
if "exists" in entries["files"][myfile]["status"]:
if entries["files"][myfile]["revision"]!="0":
mylist.append(basedir+myfile)
if recursive:
for mydir in entries["dirs"]:
mylist+=findchanged(entries["dirs"][mydir],recursive,basedir+mydir)
return mylist
def findmissing(entries,recursive=0,basedir=""):
"""(entries,recursive=0,basedir="")
Recurses the entries tree to find all elements that are listed in the cvs
tree but do not exist on the filesystem. Returns a list of paths,
optionally prepended with a basedir."""
if basedir and basedir[-1]!="/":
basedir=basedir+"/"
mylist=[]
for myfile in entries["files"]:
if "cvs" in entries["files"][myfile]["status"]:
if "exists" not in entries["files"][myfile]["status"]:
if "removed" not in entries["files"][myfile]["status"]:
mylist.append(basedir+myfile)
if recursive:
for mydir in entries["dirs"]:
mylist+=findmissing(entries["dirs"][mydir],recursive,basedir+mydir)
return mylist
def findunadded(entries,recursive=0,basedir=""):
"""(entries,recursive=0,basedir="")
Recurses the entries tree to find all elements that are in valid cvs
directories but are not part of the cvs tree. Returns a list of paths,
optionally prepended with a basedir."""
if basedir and basedir[-1]!="/":
basedir=basedir+"/"
mylist=[]
#ignore what cvs ignores.
for myfile in entries["files"]:
if "cvs" not in entries["files"][myfile]["status"]:
mylist.append(basedir+myfile)
if recursive:
for mydir in entries["dirs"]:
mylist+=findunadded(entries["dirs"][mydir],recursive,basedir+mydir)
return mylist
def findremoved(entries,recursive=0,basedir=""):
"""(entries,recursive=0,basedir="")
Recurses the entries tree to find all elements that are in flagged for cvs
deletions. Returns a list of paths, optionally prepended with a basedir."""
if basedir and basedir[-1]!="/":
basedir=basedir+"/"
mylist=[]
for myfile in entries["files"]:
if "removed" in entries["files"][myfile]["status"]:
mylist.append(basedir+myfile)
if recursive:
for mydir in entries["dirs"]:
mylist+=findremoved(entries["dirs"][mydir],recursive,basedir+mydir)
return mylist
def findall(entries, recursive=0, basedir=""):
"""(entries,recursive=0,basedir="")
Recurses the entries tree to find all new, changed, missing, and unadded
entities. Returns a 4 element list of lists as returned from each find*()."""
if basedir and basedir[-1]!="/":
basedir=basedir+"/"
mynew = findnew(entries,recursive,basedir)
mychanged = findchanged(entries,recursive,basedir)
mymissing = findmissing(entries,recursive,basedir)
myunadded = findunadded(entries,recursive,basedir)
myremoved = findremoved(entries,recursive,basedir)
return [mynew, mychanged, mymissing, myunadded, myremoved]
ignore_list = re.compile("(^|/)(RCS(|LOG)|SCCS|CVS(|\.adm)|cvslog\..*|tags|TAGS|\.(make\.state|nse_depinfo)|.*~|(\.|)#.*|,.*|_$.*|.*\$|\.del-.*|.*\.(old|BAK|bak|orig|rej|a|olb|o|obj|so|exe|Z|elc|ln)|core)$")
def apply_cvsignore_filter(list):
x=0
while x < len(list):
if ignore_list.match(list[x].split("/")[-1]):
list.pop(x)
else:
x+=1
return list
def getentries(mydir,recursive=0):
"""(basedir,recursive=0)
Scans the given directory and returns an datadict of all the entries in
the directory seperated as a dirs dict and a files dict."""
myfn=mydir+"/CVS/Entries"
# entries=[dirs, files]
entries={"dirs":{},"files":{}}
if not os.path.exists(mydir):
return entries
try:
myfile = codecs.open(_unicode_encode(myfn,
encoding=_encodings['fs'], errors='strict'),
mode='r', encoding=_encodings['content'], errors='strict')
mylines=myfile.readlines()
myfile.close()
except SystemExit as e:
raise
except:
mylines=[]
for line in mylines:
if line and line[-1]=="\n":
line=line[:-1]
if not line:
continue
if line=="D": # End of entries file
break
mysplit=line.split("/")
if len(mysplit)!=6:
print("Confused:",mysplit)
continue
if mysplit[0]=="D":
entries["dirs"][mysplit[1]]={"dirs":{},"files":{},"status":[]}
entries["dirs"][mysplit[1]]["status"]=["cvs"]
if os.path.isdir(mydir+"/"+mysplit[1]):
entries["dirs"][mysplit[1]]["status"]+=["exists"]
entries["dirs"][mysplit[1]]["flags"]=mysplit[2:]
if recursive:
rentries=getentries(mydir+"/"+mysplit[1],recursive)
entries["dirs"][mysplit[1]]["dirs"]=rentries["dirs"]
entries["dirs"][mysplit[1]]["files"]=rentries["files"]
else:
# [D]/Name/revision/Date/Flags/Tags
entries["files"][mysplit[1]]={}
entries["files"][mysplit[1]]["revision"]=mysplit[2]
entries["files"][mysplit[1]]["date"]=mysplit[3]
entries["files"][mysplit[1]]["flags"]=mysplit[4]
entries["files"][mysplit[1]]["tags"]=mysplit[5]
entries["files"][mysplit[1]]["status"]=["cvs"]
if entries["files"][mysplit[1]]["revision"][0]=="-":
entries["files"][mysplit[1]]["status"]+=["removed"]
for file in apply_cvsignore_filter(os.listdir(mydir)):
if file=="CVS":
continue
if os.path.isdir(mydir+"/"+file):
if file not in entries["dirs"]:
entries["dirs"][file]={"dirs":{},"files":{}}
# It's normal for a directory to be unlisted in Entries
# when checked out without -P (see bug #257660).
rentries=getentries(mydir+"/"+file,recursive)
entries["dirs"][file]["dirs"]=rentries["dirs"]
entries["dirs"][file]["files"]=rentries["files"]
if "status" in entries["dirs"][file]:
if "exists" not in entries["dirs"][file]["status"]:
entries["dirs"][file]["status"]+=["exists"]
else:
entries["dirs"][file]["status"]=["exists"]
elif os.path.isfile(mydir+"/"+file):
if file not in entries["files"]:
entries["files"][file]={"revision":"","date":"","flags":"","tags":""}
if "status" in entries["files"][file]:
if "exists" not in entries["files"][file]["status"]:
entries["files"][file]["status"]+=["exists"]
else:
entries["files"][file]["status"]=["exists"]
try:
mystat=os.stat(mydir+"/"+file)
mytime = time.asctime(time.gmtime(mystat[stat.ST_MTIME]))
if "status" not in entries["files"][file]:
entries["files"][file]["status"]=[]
if mytime==entries["files"][file]["date"]:
entries["files"][file]["status"]+=["current"]
except SystemExit as e:
raise
except Exception as e:
print("failed to stat",file)
print(e)
return
else:
print()
print("File of unknown type:",mydir+"/"+file)
print()
return entries
|