38
38
import typing
39
39
from collections import OrderedDict
40
40
from pathlib import Path , PurePath , PurePosixPath
41
- from typing import Optional , Union
41
+ from typing import Iterator , Optional , Union
42
42
43
43
from .utils import fatal_error , status_update , warning_message
44
44
@@ -117,7 +117,7 @@ def __repr__(self) -> str:
117
117
118
118
class MtreeSubtree (collections .abc .MutableMapping ):
119
119
def __init__ (self ):
120
- self .entry : MtreeEntry = None
120
+ self .entry : "Optional[ MtreeEntry]" = None
121
121
self .children : "dict[str, MtreeSubtree]" = OrderedDict ()
122
122
123
123
@staticmethod
@@ -172,7 +172,7 @@ def __len__(self):
172
172
ret += len (c )
173
173
return ret
174
174
175
- def _glob (self , patfrags , prefix , * , case_sensitive = False ):
175
+ def _glob (self , patfrags : "list[str]" , prefix : MtreePath , * , case_sensitive = False ) -> Iterator [ MtreePath ] :
176
176
if len (patfrags ) == 0 :
177
177
if self .entry is not None :
178
178
yield prefix
@@ -185,11 +185,12 @@ def _glob(self, patfrags, prefix, *, case_sensitive=False):
185
185
return
186
186
for k , v in self .children .items ():
187
187
if fnmatch .fnmatch (k , patfrag ):
188
+ # noinspection PyProtectedMember
188
189
yield from v ._glob (patfrags , prefix / k , case_sensitive = case_sensitive )
189
190
190
- def glob (self , pattern , * , case_sensitive = False ):
191
+ def glob (self , pattern : str , * , case_sensitive = False ) -> Iterator [ MtreePath ] :
191
192
if len (pattern ) == 0 :
192
- return
193
+ raise StopIteration () # pytype does not like plain " return"
193
194
head , tail = os .path .split (pattern )
194
195
patfrags = [tail ]
195
196
while head :
@@ -202,7 +203,7 @@ def _walk(self, top, prefix):
202
203
if split is not None :
203
204
return self .children [split [0 ]]._walk (split [1 ], prefix / split [0 ])
204
205
if self .entry is not None and self .entry .attributes ["type" ] != "dir" :
205
- return
206
+ raise StopIteration () # pytype does not like plain " return"
206
207
files = []
207
208
dirs = []
208
209
for k , v in self .children .items ():
@@ -268,7 +269,7 @@ def _ensure_mtree_mode_fmt(mode: "Union[str, int]") -> str:
268
269
return mode
269
270
270
271
@staticmethod
271
- def _ensure_mtree_path_fmt (path : str ) -> str :
272
+ def _ensure_mtree_path_fmt (path : str ) -> MtreePath :
272
273
# The path in mtree always starts with ./
273
274
assert not path .endswith ("/" )
274
275
assert path , "PATH WAS EMPTY?"
@@ -394,7 +395,7 @@ def add_dir(self, path, mode=None, uname="root", gname="wheel", print_status=Tru
394
395
status_update ("Adding dir" , path , "to mtree as" , entry , file = sys .stderr )
395
396
self ._mtree [mtree_path ] = entry
396
397
397
- def add_from_mtree (self , mtree_file , path , print_status = True ):
398
+ def add_from_mtree (self , mtree_file : "MtreeFile" , path : "Union[PurePath, str]" , print_status = True ):
398
399
if isinstance (path , PurePath ):
399
400
path = str (path )
400
401
assert not path .startswith ("/" )
@@ -471,7 +472,7 @@ def get(self, key):
471
472
def root (self ):
472
473
return self ._mtree
473
474
474
- def glob (self , pattern , * , case_sensitive = False ):
475
+ def glob (self , pattern : str , * , case_sensitive = False ) -> Iterator [ MtreePath ] :
475
476
return self ._mtree .glob (pattern , case_sensitive = case_sensitive )
476
477
477
478
def walk (self , top ):
0 commit comments