Skip to content

Commit 8dbf3f1

Browse files
committed
type hint: add type hint for checksum and xpak
1 parent 7c9bb2c commit 8dbf3f1

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

Diff for: lib/portage/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,15 @@ def _decode_argv(argv):
226226
return [_unicode_decode(x.encode(fs_encoding, "surrogateescape")) for x in argv]
227227

228228

229-
def _unicode_encode(s, encoding=_encodings["content"], errors="backslashreplace"):
229+
def _unicode_encode(
230+
s, encoding=_encodings["content"], errors="backslashreplace"
231+
) -> bytes:
230232
if isinstance(s, str):
231233
s = s.encode(encoding, errors)
232234
return s
233235

234236

235-
def _unicode_decode(s, encoding=_encodings["content"], errors="replace"):
237+
def _unicode_decode(s, encoding=_encodings["content"], errors="replace") -> str:
236238
if isinstance(s, bytes):
237239
s = str(s, encoding=encoding, errors=errors)
238240
return s

Diff for: lib/portage/xpak.py

+28-25
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import array
3737
import errno
38+
from typing import Optional, Any
3839

3940
import portage
4041
from portage import os
@@ -48,7 +49,7 @@
4849
from portage.util.file_copy import copyfile
4950

5051

51-
def addtolist(mylist, curdir):
52+
def addtolist(mylist: list[str], curdir: str | bytes) -> None:
5253
"""(list, dir) --- Takes an array(list) and appends all files from dir down
5354
the directory tree. Returns nothing. list is modified."""
5455
curdir = normalize_path(
@@ -73,7 +74,7 @@ def addtolist(mylist, curdir):
7374
mylist.append(os.path.join(parent, x)[len(curdir) + 1 :])
7475

7576

76-
def encodeint(myint):
77+
def encodeint(myint: int) -> bytes:
7778
"""Takes a 4 byte integer and converts it into a string of 4 characters.
7879
Returns the characters in a string."""
7980
a = array.array("B")
@@ -88,7 +89,7 @@ def encodeint(myint):
8889
return a.tostring()
8990

9091

91-
def decodeint(mystring):
92+
def decodeint(mystring: str | bytes) -> int:
9293
"""Takes a 4 byte string and converts it into a 4 byte integer.
9394
Returns an integer."""
9495
myint = 0
@@ -99,7 +100,7 @@ def decodeint(mystring):
99100
return myint
100101

101102

102-
def xpak(rootdir, outfile=None):
103+
def xpak(rootdir, outfile=None) -> bytes:
103104
"""(rootdir, outfile) -- creates an xpak segment of the directory 'rootdir'
104105
and under the name 'outfile' if it is specified. Otherwise it returns the
105106
xpak segment."""
@@ -133,7 +134,7 @@ def xpak(rootdir, outfile=None):
133134
return xpak_segment
134135

135136

136-
def xpak_mem(mydata):
137+
def xpak_mem(mydata: dict) -> bytes:
137138
"""Create an xpack segment from a map object."""
138139

139140
mydata_encoded = {}
@@ -174,7 +175,7 @@ def xpak_mem(mydata):
174175
)
175176

176177

177-
def xsplit(infile):
178+
def xsplit(infile: str | bytes) -> bool:
178179
"""(infile) -- Splits the infile into two files.
179180
'infile.index' contains the index segment.
180181
'infile.dat' contains the data segment."""
@@ -204,7 +205,7 @@ def xsplit(infile):
204205
return True
205206

206207

207-
def xsplit_mem(mydat):
208+
def xsplit_mem(mydat: bytes) -> Optional[tuple[bytes, bytes]]:
208209
if mydat[0:8] != b"XPAKPACK":
209210
return None
210211
if mydat[-8:] != b"XPAKSTOP":
@@ -213,7 +214,7 @@ def xsplit_mem(mydat):
213214
return (mydat[16 : indexsize + 16], mydat[indexsize + 16 : -8])
214215

215216

216-
def getindex(infile):
217+
def getindex(infile: str | bytes) -> None | bytes:
217218
"""(infile) -- grabs the index segment from the infile and returns it."""
218219
myfile = open(
219220
_unicode_encode(infile, encoding=_encodings["fs"], errors="strict"), "rb"
@@ -228,7 +229,7 @@ def getindex(infile):
228229
return myindex
229230

230231

231-
def getboth(infile):
232+
def getboth(infile: str | bytes):
232233
"""(infile) -- grabs the index and data segments from the infile.
233234
Returns an array [indexSegment, dataSegment]"""
234235
myfile = open(
@@ -246,13 +247,13 @@ def getboth(infile):
246247
return myindex, mydata
247248

248249

249-
def listindex(myindex):
250+
def listindex(myindex) -> None:
250251
"""Print to the terminal the filenames listed in the indexglob passed in."""
251252
for x in getindex_mem(myindex):
252253
print(x)
253254

254255

255-
def getindex_mem(myindex):
256+
def getindex_mem(myindex) -> list[Any]:
256257
"""Returns the filenames listed in the indexglob passed in."""
257258
myindexlen = len(myindex)
258259
startpos = 0
@@ -264,7 +265,7 @@ def getindex_mem(myindex):
264265
return myret
265266

266267

267-
def searchindex(myindex, myitem):
268+
def searchindex(myindex, myitem) -> tuple[int, int]:
268269
"""(index, item) -- Finds the offset and length of the file 'item' in the
269270
datasegment via the index 'index' provided."""
270271
myitem = _unicode_encode(
@@ -288,7 +289,7 @@ def searchindex(myindex, myitem):
288289
startpos = startpos + mytestlen + 12
289290

290291

291-
def getitem(myid, myitem):
292+
def getitem(myid, myitem) -> list[Any]:
292293
myindex = myid[0]
293294
mydata = myid[1]
294295
myloc = searchindex(myindex, myitem)
@@ -297,7 +298,7 @@ def getitem(myid, myitem):
297298
return mydata[myloc[0] : myloc[0] + myloc[1]]
298299

299300

300-
def xpand(myid, mydest):
301+
def xpand(myid, mydest) -> None:
301302
mydest = normalize_path(mydest) + os.sep
302303
myindex = myid[0]
303304
mydata = myid[1]
@@ -340,7 +341,7 @@ def __init__(self, myfile):
340341
self.indexpos = None
341342
self.datapos = None
342343

343-
def decompose(self, datadir, cleanup=1):
344+
def decompose(self, datadir, cleanup: int = 1) -> int:
344345
"""Alias for unpackinfo() --- Complement to recompose() but optionally
345346
deletes the destination directory. Extracts the xpak from the tbz2 into
346347
the directory provided. Raises IOError if scan() fails.
@@ -353,11 +354,13 @@ def decompose(self, datadir, cleanup=1):
353354
os.makedirs(datadir)
354355
return self.unpackinfo(datadir)
355356

356-
def compose(self, datadir, cleanup=0):
357+
def compose(self, datadir, cleanup: int = 0) -> None:
357358
"""Alias for recompose()."""
358359
return self.recompose(datadir, cleanup)
359360

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:
361364
"""Creates an xpak segment from the datadir provided, truncates the tbz2
362365
to the end of regular data if an xpak segment already exists, and adds
363366
the new segment to the file with terminating info."""
@@ -366,7 +369,7 @@ def recompose(self, datadir, cleanup=0, break_hardlinks=True):
366369
if cleanup:
367370
self.cleanup(datadir)
368371

369-
def recompose_mem(self, xpdata, break_hardlinks=True):
372+
def recompose_mem(self, xpdata, break_hardlinks: bool = True) -> int:
370373
"""
371374
Update the xpak segment.
372375
@param xpdata: A new xpak segment to be written, like that returned
@@ -402,7 +405,7 @@ def recompose_mem(self, xpdata, break_hardlinks=True):
402405
myfile.close()
403406
return 1
404407

405-
def cleanup(self, datadir):
408+
def cleanup(self, datadir) -> None:
406409
datadir_split = os.path.split(datadir)
407410
if len(datadir_split) >= 2 and len(datadir_split[1]) > 0:
408411
# This is potentially dangerous,
@@ -415,7 +418,7 @@ def cleanup(self, datadir):
415418
else:
416419
raise oe
417420

418-
def scan(self):
421+
def scan(self) -> int:
419422
"""Scans the tbz2 to locate the xpak segment and setup internal values.
420423
This function is called by relevant functions already."""
421424
a = None
@@ -480,7 +483,7 @@ def scan(self):
480483
if a is not None:
481484
a.close()
482485

483-
def filelist(self):
486+
def filelist(self) -> Optional[list[Any]]:
484487
"""Return an array of each file listed in the index."""
485488
if not self.scan():
486489
return None
@@ -501,14 +504,14 @@ def getfile(self, myfile, mydefault=None):
501504
a.close()
502505
return myreturn
503506

504-
def getelements(self, myfile):
507+
def getelements(self, myfile) -> list:
505508
"""A split/array representation of tbz2.getfile()"""
506509
mydat = self.getfile(myfile)
507510
if not mydat:
508511
return []
509512
return mydat.split()
510513

511-
def unpackinfo(self, mydest):
514+
def unpackinfo(self, mydest) -> int:
512515
"""Unpacks all the files from the dataSegment into 'mydest'."""
513516
if not self.scan():
514517
return 0
@@ -551,7 +554,7 @@ def unpackinfo(self, mydest):
551554
a.close()
552555
return 1
553556

554-
def get_data(self):
557+
def get_data(self) -> dict[bytes, bytes]:
555558
"""Returns all the files from the dataSegment as a map object."""
556559
if not self.scan():
557560
return {}
@@ -575,7 +578,7 @@ def get_data(self):
575578
a.close()
576579
return mydata
577580

578-
def getboth(self):
581+
def getboth(self) -> tuple[bytes, bytes]:
579582
"""Returns an array [indexSegment, dataSegment]"""
580583
if not self.scan():
581584
return None

0 commit comments

Comments
 (0)