35
35
36
36
import array
37
37
import errno
38
+ from typing import Optional , Any
38
39
39
40
import portage
40
41
from portage import os
48
49
from portage .util .file_copy import copyfile
49
50
50
51
51
- def addtolist (mylist , curdir ) :
52
+ def addtolist (mylist : list [ str ] , curdir : str | bytes ) -> None :
52
53
"""(list, dir) --- Takes an array(list) and appends all files from dir down
53
54
the directory tree. Returns nothing. list is modified."""
54
55
curdir = normalize_path (
@@ -73,7 +74,7 @@ def addtolist(mylist, curdir):
73
74
mylist .append (os .path .join (parent , x )[len (curdir ) + 1 :])
74
75
75
76
76
- def encodeint (myint ) :
77
+ def encodeint (myint : int ) -> bytes :
77
78
"""Takes a 4 byte integer and converts it into a string of 4 characters.
78
79
Returns the characters in a string."""
79
80
a = array .array ("B" )
@@ -88,7 +89,7 @@ def encodeint(myint):
88
89
return a .tostring ()
89
90
90
91
91
- def decodeint (mystring ) :
92
+ def decodeint (mystring : str | bytes ) -> int :
92
93
"""Takes a 4 byte string and converts it into a 4 byte integer.
93
94
Returns an integer."""
94
95
myint = 0
@@ -99,7 +100,7 @@ def decodeint(mystring):
99
100
return myint
100
101
101
102
102
- def xpak (rootdir , outfile = None ):
103
+ def xpak (rootdir , outfile = None ) -> bytes :
103
104
"""(rootdir, outfile) -- creates an xpak segment of the directory 'rootdir'
104
105
and under the name 'outfile' if it is specified. Otherwise it returns the
105
106
xpak segment."""
@@ -133,7 +134,7 @@ def xpak(rootdir, outfile=None):
133
134
return xpak_segment
134
135
135
136
136
- def xpak_mem (mydata ) :
137
+ def xpak_mem (mydata : dict ) -> bytes :
137
138
"""Create an xpack segment from a map object."""
138
139
139
140
mydata_encoded = {}
@@ -174,7 +175,7 @@ def xpak_mem(mydata):
174
175
)
175
176
176
177
177
- def xsplit (infile ) :
178
+ def xsplit (infile : str | bytes ) -> bool :
178
179
"""(infile) -- Splits the infile into two files.
179
180
'infile.index' contains the index segment.
180
181
'infile.dat' contains the data segment."""
@@ -204,7 +205,7 @@ def xsplit(infile):
204
205
return True
205
206
206
207
207
- def xsplit_mem (mydat ) :
208
+ def xsplit_mem (mydat : bytes ) -> Optional [ tuple [ bytes , bytes ]] :
208
209
if mydat [0 :8 ] != b"XPAKPACK" :
209
210
return None
210
211
if mydat [- 8 :] != b"XPAKSTOP" :
@@ -213,7 +214,7 @@ def xsplit_mem(mydat):
213
214
return (mydat [16 : indexsize + 16 ], mydat [indexsize + 16 : - 8 ])
214
215
215
216
216
- def getindex (infile ) :
217
+ def getindex (infile : str | bytes ) -> None | bytes :
217
218
"""(infile) -- grabs the index segment from the infile and returns it."""
218
219
myfile = open (
219
220
_unicode_encode (infile , encoding = _encodings ["fs" ], errors = "strict" ), "rb"
@@ -228,7 +229,7 @@ def getindex(infile):
228
229
return myindex
229
230
230
231
231
- def getboth (infile ):
232
+ def getboth (infile : str | bytes ):
232
233
"""(infile) -- grabs the index and data segments from the infile.
233
234
Returns an array [indexSegment, dataSegment]"""
234
235
myfile = open (
@@ -246,13 +247,13 @@ def getboth(infile):
246
247
return myindex , mydata
247
248
248
249
249
- def listindex (myindex ):
250
+ def listindex (myindex ) -> None :
250
251
"""Print to the terminal the filenames listed in the indexglob passed in."""
251
252
for x in getindex_mem (myindex ):
252
253
print (x )
253
254
254
255
255
- def getindex_mem (myindex ):
256
+ def getindex_mem (myindex ) -> list [ Any ] :
256
257
"""Returns the filenames listed in the indexglob passed in."""
257
258
myindexlen = len (myindex )
258
259
startpos = 0
@@ -264,7 +265,7 @@ def getindex_mem(myindex):
264
265
return myret
265
266
266
267
267
- def searchindex (myindex , myitem ):
268
+ def searchindex (myindex , myitem ) -> tuple [ int , int ] :
268
269
"""(index, item) -- Finds the offset and length of the file 'item' in the
269
270
datasegment via the index 'index' provided."""
270
271
myitem = _unicode_encode (
@@ -288,7 +289,7 @@ def searchindex(myindex, myitem):
288
289
startpos = startpos + mytestlen + 12
289
290
290
291
291
- def getitem (myid , myitem ):
292
+ def getitem (myid , myitem ) -> list [ Any ] :
292
293
myindex = myid [0 ]
293
294
mydata = myid [1 ]
294
295
myloc = searchindex (myindex , myitem )
@@ -297,7 +298,7 @@ def getitem(myid, myitem):
297
298
return mydata [myloc [0 ] : myloc [0 ] + myloc [1 ]]
298
299
299
300
300
- def xpand (myid , mydest ):
301
+ def xpand (myid , mydest ) -> None :
301
302
mydest = normalize_path (mydest ) + os .sep
302
303
myindex = myid [0 ]
303
304
mydata = myid [1 ]
@@ -340,7 +341,7 @@ def __init__(self, myfile):
340
341
self .indexpos = None
341
342
self .datapos = None
342
343
343
- def decompose (self , datadir , cleanup = 1 ) :
344
+ def decompose (self , datadir , cleanup : int = 1 ) -> int :
344
345
"""Alias for unpackinfo() --- Complement to recompose() but optionally
345
346
deletes the destination directory. Extracts the xpak from the tbz2 into
346
347
the directory provided. Raises IOError if scan() fails.
@@ -353,11 +354,13 @@ def decompose(self, datadir, cleanup=1):
353
354
os .makedirs (datadir )
354
355
return self .unpackinfo (datadir )
355
356
356
- def compose (self , datadir , cleanup = 0 ) :
357
+ def compose (self , datadir , cleanup : int = 0 ) -> None :
357
358
"""Alias for recompose()."""
358
359
return self .recompose (datadir , cleanup )
359
360
360
- def recompose (self , datadir , cleanup = 0 , break_hardlinks = True ):
361
+ def recompose (
362
+ self , datadir , cleanup : int = 0 , break_hardlinks : bool = True
363
+ ) -> None :
361
364
"""Creates an xpak segment from the datadir provided, truncates the tbz2
362
365
to the end of regular data if an xpak segment already exists, and adds
363
366
the new segment to the file with terminating info."""
@@ -366,7 +369,7 @@ def recompose(self, datadir, cleanup=0, break_hardlinks=True):
366
369
if cleanup :
367
370
self .cleanup (datadir )
368
371
369
- def recompose_mem (self , xpdata , break_hardlinks = True ):
372
+ def recompose_mem (self , xpdata , break_hardlinks : bool = True ) -> int :
370
373
"""
371
374
Update the xpak segment.
372
375
@param xpdata: A new xpak segment to be written, like that returned
@@ -402,7 +405,7 @@ def recompose_mem(self, xpdata, break_hardlinks=True):
402
405
myfile .close ()
403
406
return 1
404
407
405
- def cleanup (self , datadir ):
408
+ def cleanup (self , datadir ) -> None :
406
409
datadir_split = os .path .split (datadir )
407
410
if len (datadir_split ) >= 2 and len (datadir_split [1 ]) > 0 :
408
411
# This is potentially dangerous,
@@ -415,7 +418,7 @@ def cleanup(self, datadir):
415
418
else :
416
419
raise oe
417
420
418
- def scan (self ):
421
+ def scan (self ) -> int :
419
422
"""Scans the tbz2 to locate the xpak segment and setup internal values.
420
423
This function is called by relevant functions already."""
421
424
a = None
@@ -480,7 +483,7 @@ def scan(self):
480
483
if a is not None :
481
484
a .close ()
482
485
483
- def filelist (self ):
486
+ def filelist (self ) -> Optional [ list [ Any ]] :
484
487
"""Return an array of each file listed in the index."""
485
488
if not self .scan ():
486
489
return None
@@ -501,14 +504,14 @@ def getfile(self, myfile, mydefault=None):
501
504
a .close ()
502
505
return myreturn
503
506
504
- def getelements (self , myfile ):
507
+ def getelements (self , myfile ) -> list :
505
508
"""A split/array representation of tbz2.getfile()"""
506
509
mydat = self .getfile (myfile )
507
510
if not mydat :
508
511
return []
509
512
return mydat .split ()
510
513
511
- def unpackinfo (self , mydest ):
514
+ def unpackinfo (self , mydest ) -> int :
512
515
"""Unpacks all the files from the dataSegment into 'mydest'."""
513
516
if not self .scan ():
514
517
return 0
@@ -551,7 +554,7 @@ def unpackinfo(self, mydest):
551
554
a .close ()
552
555
return 1
553
556
554
- def get_data (self ):
557
+ def get_data (self ) -> dict [ bytes , bytes ] :
555
558
"""Returns all the files from the dataSegment as a map object."""
556
559
if not self .scan ():
557
560
return {}
@@ -575,7 +578,7 @@ def get_data(self):
575
578
a .close ()
576
579
return mydata
577
580
578
- def getboth (self ):
581
+ def getboth (self ) -> tuple [ bytes , bytes ] :
579
582
"""Returns an array [indexSegment, dataSegment]"""
580
583
if not self .scan ():
581
584
return None
0 commit comments