1111import os
1212import shutil
1313import signal
14+ from typing import Iterator
1415
1516from contextlib import ExitStack
1617
@@ -39,15 +40,15 @@ class BuildError(Exception):
3940class LinuxSourceTreeOperations (object ):
4041 """An abstraction over command line operations performed on a source tree."""
4142
42- def make_mrproper (self ):
43+ def make_mrproper (self ) -> None :
4344 try :
4445 subprocess .check_output (['make' , 'mrproper' ], stderr = subprocess .STDOUT )
4546 except OSError as e :
4647 raise ConfigError ('Could not call make command: ' + str (e ))
4748 except subprocess .CalledProcessError as e :
4849 raise ConfigError (e .output .decode ())
4950
50- def make_olddefconfig (self , build_dir , make_options ):
51+ def make_olddefconfig (self , build_dir , make_options ) -> None :
5152 command = ['make' , 'ARCH=um' , 'olddefconfig' ]
5253 if make_options :
5354 command .extend (make_options )
@@ -60,7 +61,7 @@ def make_olddefconfig(self, build_dir, make_options):
6061 except subprocess .CalledProcessError as e :
6162 raise ConfigError (e .output .decode ())
6263
63- def make_allyesconfig (self , build_dir , make_options ):
64+ def make_allyesconfig (self , build_dir , make_options ) -> None :
6465 kunit_parser .print_with_timestamp (
6566 'Enabling all CONFIGs for UML...' )
6667 command = ['make' , 'ARCH=um' , 'allyesconfig' ]
@@ -82,7 +83,7 @@ def make_allyesconfig(self, build_dir, make_options):
8283 kunit_parser .print_with_timestamp (
8384 'Starting Kernel with all configs takes a few minutes...' )
8485
85- def make (self , jobs , build_dir , make_options ):
86+ def make (self , jobs , build_dir , make_options ) -> None :
8687 command = ['make' , 'ARCH=um' , '--jobs=' + str (jobs )]
8788 if make_options :
8889 command .extend (make_options )
@@ -100,7 +101,7 @@ def make(self, jobs, build_dir, make_options):
100101 if stderr : # likely only due to build warnings
101102 print (stderr .decode ())
102103
103- def linux_bin (self , params , timeout , build_dir ):
104+ def linux_bin (self , params , timeout , build_dir ) -> None :
104105 """Runs the Linux UML binary. Must be named 'linux'."""
105106 linux_bin = get_file_path (build_dir , 'linux' )
106107 outfile = get_outfile_path (build_dir )
@@ -110,41 +111,41 @@ def linux_bin(self, params, timeout, build_dir):
110111 stderr = subprocess .STDOUT )
111112 process .wait (timeout )
112113
113- def get_kconfig_path (build_dir ):
114+ def get_kconfig_path (build_dir ) -> str :
114115 return get_file_path (build_dir , KCONFIG_PATH )
115116
116- def get_kunitconfig_path (build_dir ):
117+ def get_kunitconfig_path (build_dir ) -> str :
117118 return get_file_path (build_dir , KUNITCONFIG_PATH )
118119
119- def get_outfile_path (build_dir ):
120+ def get_outfile_path (build_dir ) -> str :
120121 return get_file_path (build_dir , OUTFILE_PATH )
121122
122123class LinuxSourceTree (object ):
123124 """Represents a Linux kernel source tree with KUnit tests."""
124125
125- def __init__ (self ):
126+ def __init__ (self ) -> None :
126127 self ._ops = LinuxSourceTreeOperations ()
127128 signal .signal (signal .SIGINT , self .signal_handler )
128129
129- def clean (self ):
130+ def clean (self ) -> bool :
130131 try :
131132 self ._ops .make_mrproper ()
132133 except ConfigError as e :
133134 logging .error (e )
134135 return False
135136 return True
136137
137- def create_kunitconfig (self , build_dir , defconfig = DEFAULT_KUNITCONFIG_PATH ):
138+ def create_kunitconfig (self , build_dir , defconfig = DEFAULT_KUNITCONFIG_PATH ) -> None :
138139 kunitconfig_path = get_kunitconfig_path (build_dir )
139140 if not os .path .exists (kunitconfig_path ):
140141 shutil .copyfile (defconfig , kunitconfig_path )
141142
142- def read_kunitconfig (self , build_dir ):
143+ def read_kunitconfig (self , build_dir ) -> None :
143144 kunitconfig_path = get_kunitconfig_path (build_dir )
144145 self ._kconfig = kunit_config .Kconfig ()
145146 self ._kconfig .read_from_file (kunitconfig_path )
146147
147- def validate_config (self , build_dir ):
148+ def validate_config (self , build_dir ) -> bool :
148149 kconfig_path = get_kconfig_path (build_dir )
149150 validated_kconfig = kunit_config .Kconfig ()
150151 validated_kconfig .read_from_file (kconfig_path )
@@ -158,7 +159,7 @@ def validate_config(self, build_dir):
158159 return False
159160 return True
160161
161- def build_config (self , build_dir , make_options ):
162+ def build_config (self , build_dir , make_options ) -> bool :
162163 kconfig_path = get_kconfig_path (build_dir )
163164 if build_dir and not os .path .exists (build_dir ):
164165 os .mkdir (build_dir )
@@ -170,7 +171,7 @@ def build_config(self, build_dir, make_options):
170171 return False
171172 return self .validate_config (build_dir )
172173
173- def build_reconfig (self , build_dir , make_options ):
174+ def build_reconfig (self , build_dir , make_options ) -> bool :
174175 """Creates a new .config if it is not a subset of the .kunitconfig."""
175176 kconfig_path = get_kconfig_path (build_dir )
176177 if os .path .exists (kconfig_path ):
@@ -186,7 +187,7 @@ def build_reconfig(self, build_dir, make_options):
186187 print ('Generating .config ...' )
187188 return self .build_config (build_dir , make_options )
188189
189- def build_um_kernel (self , alltests , jobs , build_dir , make_options ):
190+ def build_um_kernel (self , alltests , jobs , build_dir , make_options ) -> bool :
190191 try :
191192 if alltests :
192193 self ._ops .make_allyesconfig (build_dir , make_options )
@@ -197,7 +198,7 @@ def build_um_kernel(self, alltests, jobs, build_dir, make_options):
197198 return False
198199 return self .validate_config (build_dir )
199200
200- def run_kernel (self , args = [], build_dir = '' , timeout = None ):
201+ def run_kernel (self , args = [], build_dir = '' , timeout = None ) -> Iterator [ str ] :
201202 args .extend (['mem=1G' , 'console=tty' ])
202203 self ._ops .linux_bin (args , timeout , build_dir )
203204 outfile = get_outfile_path (build_dir )
@@ -206,6 +207,6 @@ def run_kernel(self, args=[], build_dir='', timeout=None):
206207 for line in file :
207208 yield line
208209
209- def signal_handler (self , sig , frame ):
210+ def signal_handler (self , sig , frame ) -> None :
210211 logging .error ('Build interruption occurred. Cleaning console.' )
211212 subprocess .call (['stty' , 'sane' ])
0 commit comments