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,42 @@ 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- self ._ops = LinuxSourceTreeOperations ()
126+ def __init__ (self , build_dir : str , load_config = True , defconfig = DEFAULT_KUNITCONFIG_PATH ) -> None :
127127 signal .signal (signal .SIGINT , self .signal_handler )
128128
129- def clean (self ):
130- try :
131- self ._ops .make_mrproper ()
132- except ConfigError as e :
133- logging .error (e )
134- return False
135- return True
129+ self ._ops = LinuxSourceTreeOperations ()
130+
131+ if not load_config :
132+ return
136133
137- def create_kunitconfig (self , build_dir , defconfig = DEFAULT_KUNITCONFIG_PATH ):
138134 kunitconfig_path = get_kunitconfig_path (build_dir )
139135 if not os .path .exists (kunitconfig_path ):
140136 shutil .copyfile (defconfig , kunitconfig_path )
141137
142- def read_kunitconfig (self , build_dir ):
143- kunitconfig_path = get_kunitconfig_path (build_dir )
144138 self ._kconfig = kunit_config .Kconfig ()
145139 self ._kconfig .read_from_file (kunitconfig_path )
146140
147- def validate_config (self , build_dir ):
141+ def clean (self ) -> bool :
142+ try :
143+ self ._ops .make_mrproper ()
144+ except ConfigError as e :
145+ logging .error (e )
146+ return False
147+ return True
148+
149+ def validate_config (self , build_dir ) -> bool :
148150 kconfig_path = get_kconfig_path (build_dir )
149151 validated_kconfig = kunit_config .Kconfig ()
150152 validated_kconfig .read_from_file (kconfig_path )
@@ -158,7 +160,7 @@ def validate_config(self, build_dir):
158160 return False
159161 return True
160162
161- def build_config (self , build_dir , make_options ):
163+ def build_config (self , build_dir , make_options ) -> bool :
162164 kconfig_path = get_kconfig_path (build_dir )
163165 if build_dir and not os .path .exists (build_dir ):
164166 os .mkdir (build_dir )
@@ -170,7 +172,7 @@ def build_config(self, build_dir, make_options):
170172 return False
171173 return self .validate_config (build_dir )
172174
173- def build_reconfig (self , build_dir , make_options ):
175+ def build_reconfig (self , build_dir , make_options ) -> bool :
174176 """Creates a new .config if it is not a subset of the .kunitconfig."""
175177 kconfig_path = get_kconfig_path (build_dir )
176178 if os .path .exists (kconfig_path ):
@@ -186,7 +188,7 @@ def build_reconfig(self, build_dir, make_options):
186188 print ('Generating .config ...' )
187189 return self .build_config (build_dir , make_options )
188190
189- def build_um_kernel (self , alltests , jobs , build_dir , make_options ):
191+ def build_um_kernel (self , alltests , jobs , build_dir , make_options ) -> bool :
190192 try :
191193 if alltests :
192194 self ._ops .make_allyesconfig (build_dir , make_options )
@@ -197,7 +199,7 @@ def build_um_kernel(self, alltests, jobs, build_dir, make_options):
197199 return False
198200 return self .validate_config (build_dir )
199201
200- def run_kernel (self , args = [], build_dir = '' , timeout = None ):
202+ def run_kernel (self , args = [], build_dir = '' , timeout = None ) -> Iterator [ str ] :
201203 args .extend (['mem=1G' , 'console=tty' ])
202204 self ._ops .linux_bin (args , timeout , build_dir )
203205 outfile = get_outfile_path (build_dir )
@@ -206,6 +208,6 @@ def run_kernel(self, args=[], build_dir='', timeout=None):
206208 for line in file :
207209 yield line
208210
209- def signal_handler (self , sig , frame ):
211+ def signal_handler (self , sig , frame ) -> None :
210212 logging .error ('Build interruption occurred. Cleaning console.' )
211213 subprocess .call (['stty' , 'sane' ])
0 commit comments