The log is also displayed on the terminal.
-Each line is printed in the format
-<defconfig_name> : <action>
+The log is printed for each defconfig as follows:
-<defconfig_name> is the name of the defconfig
-(without the suffix _defconfig).
+<defconfig_name>
+ <action1>
+ <action2>
+ <action3>
+ ...
-<action> shows what the tool did for that defconfig.
+<defconfig_name> is the name of the defconfig.
+
+<action*> shows what the tool did for that defconfig.
It looks like one of the followings:
- Move 'CONFIG_... '
Exit immediately if Make exits with a non-zero status while processing
a defconfig file.
+ -s, --force-sync
+ Do "make savedefconfig" forcibly for all the defconfig files.
+ If not specified, "make savedefconfig" only occurs for cases
+ where at least one CONFIG was moved.
+
-H, --headers-only
Only cleanup the headers; skip the defconfig processing
"""
+import filecmp
import fnmatch
import multiprocessing
import optparse
def color_text(color_enabled, color, string):
"""Return colored string."""
if color_enabled:
- return '\033[' + color + 'm' + string + '\033[0m'
+ # LF should not be surrounded by the escape sequence.
+ # Otherwise, additional whitespace or line-feed might be printed.
+ return '\n'.join([ '\033[' + color + 'm' + s + '\033[0m' if s else ''
+ for s in string.split('\n') ])
else:
return string
-def log_msg(color_enabled, color, defconfig, msg):
- """Return the formated line for the log."""
- return defconfig[:-len('_defconfig')].ljust(37) + ': ' + \
- color_text(color_enabled, color, msg) + '\n'
-
def update_cross_compile(color_enabled):
"""Update per-arch CROSS_COMPILE via environment variables
re_arch = re.compile(r'CONFIG_SYS_ARCH="(.*)"')
re_cpu = re.compile(r'CONFIG_SYS_CPU="(.*)"')
- def __init__(self, configs, options, progress, build_dir):
+ def __init__(self, configs, options, build_dir):
"""Create a new parser.
Arguments:
configs: A list of CONFIGs to move.
options: option flags.
- progress: A progress indicator
build_dir: Build directory.
"""
self.configs = configs
self.options = options
- self.progress = progress
- self.build_dir = build_dir
+ self.dotconfig = os.path.join(build_dir, '.config')
+ self.autoconf = os.path.join(build_dir, 'include', 'autoconf.mk')
+ self.config_autoconf = os.path.join(build_dir, 'include', 'config',
+ 'auto.conf')
+ self.defconfig = os.path.join(build_dir, 'defconfig')
def get_cross_compile(self):
"""Parse .config file and return CROSS_COMPILE.
"""
arch = ''
cpu = ''
- dotconfig = os.path.join(self.build_dir, '.config')
- for line in open(dotconfig):
+ for line in open(self.dotconfig):
m = self.re_arch.match(line)
if m:
arch = m.group(1)
return (ACTION_MOVE, new_val)
- def update_dotconfig(self, defconfig):
+ def update_dotconfig(self):
"""Parse files for the config options and update the .config.
This function parses the generated .config and include/autoconf.mk
searching the target options.
Move the config option(s) to the .config as needed.
- Also, display the log to show what happened to the .config.
Arguments:
defconfig: defconfig name.
+
+ Returns:
+ Return a tuple of (updated flag, log string).
+ The "updated flag" is True if the .config was updated, False
+ otherwise. The "log string" shows what happend to the .config.
"""
- dotconfig_path = os.path.join(self.build_dir, '.config')
- autoconf_path = os.path.join(self.build_dir, 'include', 'autoconf.mk')
results = []
+ updated = False
- with open(dotconfig_path) as f:
+ with open(self.dotconfig) as f:
dotconfig_lines = f.readlines()
- with open(autoconf_path) as f:
+ with open(self.autoconf) as f:
autoconf_lines = f.readlines()
for config in self.configs:
else:
sys.exit("Internal Error. This should not happen.")
- log += log_msg(self.options.color, log_color, defconfig, actlog)
+ log += color_text(self.options.color, log_color, actlog) + '\n'
- # Some threads are running in parallel.
- # Print log in one shot to not mix up logs from different threads.
- print log,
- self.progress.show()
-
- with open(dotconfig_path, 'a') as f:
+ with open(self.dotconfig, 'a') as f:
for (action, value) in results:
if action == ACTION_MOVE:
f.write(value + '\n')
+ updated = True
+
+ self.results = results
+ os.remove(self.config_autoconf)
+ os.remove(self.autoconf)
+
+ return (updated, log)
+
+ def check_defconfig(self):
+ """Check the defconfig after savedefconfig
+
+ Returns:
+ Return additional log if moved CONFIGs were removed again by
+ 'make savedefconfig'.
+ """
+
+ log = ''
+
+ with open(self.defconfig) as f:
+ defconfig_lines = f.readlines()
+
+ for (action, value) in self.results:
+ if action != ACTION_MOVE:
+ continue
+ if not value + '\n' in defconfig_lines:
+ log += color_text(self.options.color, COLOR_YELLOW,
+ "'%s' was removed by savedefconfig.\n" %
+ value)
- os.remove(os.path.join(self.build_dir, 'include', 'config', 'auto.conf'))
- os.remove(autoconf_path)
+ return log
class Slot:
self.build_dir = tempfile.mkdtemp()
self.devnull = devnull
self.make_cmd = (make_cmd, 'O=' + self.build_dir)
- self.parser = KconfigParser(configs, options, progress, self.build_dir)
+ self.parser = KconfigParser(configs, options, self.build_dir)
self.state = STATE_IDLE
self.failed_boards = []
This function makes sure the temporary directory is cleaned away
even if Python suddenly dies due to error. It should be done in here
- because it is guranteed the destructor is always invoked when the
+ because it is guaranteed the destructor is always invoked when the
instance of the class gets unreferenced.
If the subprocess is still running, wait until it finishes.
"""
if self.state != STATE_IDLE:
return False
- cmd = list(self.make_cmd)
- cmd.append(defconfig)
- self.ps = subprocess.Popen(cmd, stdout=self.devnull,
- stderr=subprocess.PIPE)
+
self.defconfig = defconfig
- self.state = STATE_DEFCONFIG
+ self.log = ''
+ self.do_defconfig()
return True
def poll(self):
If the configuration is successfully finished, assign a new
subprocess to build include/autoconf.mk.
If include/autoconf.mk is generated, invoke the parser to
- parse the .config and the include/autoconf.mk, and then set the
- slot back to the idle state.
+ parse the .config and the include/autoconf.mk, moving
+ config options to the .config as needed.
+ If the .config was updated, run "make savedefconfig" to sync
+ it, update the original defconfig, and then set the slot back
+ to the idle state.
Returns:
Return True if the subprocess is terminated, False otherwise
return False
if self.ps.poll() != 0:
- print >> sys.stderr, log_msg(self.options.color, COLOR_LIGHT_RED,
- self.defconfig, "Failed to process."),
- if self.options.verbose:
- print >> sys.stderr, color_text(self.options.color,
- COLOR_LIGHT_CYAN,
- self.ps.stderr.read())
- self.progress.inc()
- self.progress.show()
- if self.options.exit_on_error:
- sys.exit("Exit on error.")
- # If --exit-on-error flag is not set, skip this board and continue.
- # Record the failed board.
- self.failed_boards.append(self.defconfig)
- self.state = STATE_IDLE
- return True
+ self.handle_error()
+ elif self.state == STATE_DEFCONFIG:
+ self.do_autoconf()
+ elif self.state == STATE_AUTOCONF:
+ self.do_savedefconfig()
+ elif self.state == STATE_SAVEDEFCONFIG:
+ self.update_defconfig()
+ else:
+ sys.exit("Internal Error. This should not happen.")
- if self.state == STATE_AUTOCONF:
- self.parser.update_dotconfig(self.defconfig)
+ return True if self.state == STATE_IDLE else False
- """Save off the defconfig in a consistent way"""
- cmd = list(self.make_cmd)
- cmd.append('savedefconfig')
- self.ps = subprocess.Popen(cmd, stdout=self.devnull,
- stderr=subprocess.PIPE)
- self.state = STATE_SAVEDEFCONFIG
- return False
+ def handle_error(self):
+ """Handle error cases."""
- if self.state == STATE_SAVEDEFCONFIG:
- if not self.options.dry_run:
- shutil.move(os.path.join(self.build_dir, 'defconfig'),
- os.path.join('configs', self.defconfig))
- self.progress.inc()
- self.state = STATE_IDLE
- return True
+ self.log += color_text(self.options.color, COLOR_LIGHT_RED,
+ "Failed to process.\n")
+ if self.options.verbose:
+ self.log += color_text(self.options.color, COLOR_LIGHT_CYAN,
+ self.ps.stderr.read())
+ self.finish(False)
+
+ def do_defconfig(self):
+ """Run 'make <board>_defconfig' to create the .config file."""
+
+ cmd = list(self.make_cmd)
+ cmd.append(self.defconfig)
+ self.ps = subprocess.Popen(cmd, stdout=self.devnull,
+ stderr=subprocess.PIPE)
+ self.state = STATE_DEFCONFIG
+
+ def do_autoconf(self):
+ """Run 'make include/config/auto.conf'."""
self.cross_compile = self.parser.get_cross_compile()
if self.cross_compile is None:
- print >> sys.stderr, log_msg(self.options.color, COLOR_YELLOW,
- self.defconfig,
- "Compiler is missing. Do nothing."),
- self.progress.inc()
- self.progress.show()
- if self.options.exit_on_error:
- sys.exit("Exit on error.")
- # If --exit-on-error flag is not set, skip this board and continue.
- # Record the failed board.
- self.failed_boards.append(self.defconfig)
- self.state = STATE_IDLE
- return True
+ self.log += color_text(self.options.color, COLOR_YELLOW,
+ "Compiler is missing. Do nothing.\n")
+ self.finish(False)
+ return
cmd = list(self.make_cmd)
if self.cross_compile:
self.ps = subprocess.Popen(cmd, stdout=self.devnull,
stderr=subprocess.PIPE)
self.state = STATE_AUTOCONF
- return False
+
+ def do_savedefconfig(self):
+ """Update the .config and run 'make savedefconfig'."""
+
+ (updated, log) = self.parser.update_dotconfig()
+ self.log += log
+
+ if not self.options.force_sync and not updated:
+ self.finish(True)
+ return
+ if updated:
+ self.log += color_text(self.options.color, COLOR_LIGHT_GREEN,
+ "Syncing by savedefconfig...\n")
+ else:
+ self.log += "Syncing by savedefconfig (forced by option)...\n"
+
+ cmd = list(self.make_cmd)
+ cmd.append('savedefconfig')
+ self.ps = subprocess.Popen(cmd, stdout=self.devnull,
+ stderr=subprocess.PIPE)
+ self.state = STATE_SAVEDEFCONFIG
+
+ def update_defconfig(self):
+ """Update the input defconfig and go back to the idle state."""
+
+ self.log += self.parser.check_defconfig()
+ orig_defconfig = os.path.join('configs', self.defconfig)
+ new_defconfig = os.path.join(self.build_dir, 'defconfig')
+ updated = not filecmp.cmp(orig_defconfig, new_defconfig)
+
+ if updated:
+ self.log += color_text(self.options.color, COLOR_LIGHT_BLUE,
+ "defconfig was updated.\n")
+
+ if not self.options.dry_run and updated:
+ shutil.move(new_defconfig, orig_defconfig)
+ self.finish(True)
+
+ def finish(self, success):
+ """Display log along with progress and go to the idle state.
+
+ Arguments:
+ success: Should be True when the defconfig was processed
+ successfully, or False when it fails.
+ """
+ # output at least 30 characters to hide the "* defconfigs out of *".
+ log = self.defconfig.ljust(30) + '\n'
+
+ log += '\n'.join([ ' ' + s for s in self.log.split('\n') ])
+ # Some threads are running in parallel.
+ # Print log atomically to not mix up logs from different threads.
+ print >> (sys.stdout if success else sys.stderr), log
+
+ if not success:
+ if self.options.exit_on_error:
+ sys.exit("Exit on error.")
+ # If --exit-on-error flag is not set, skip this board and continue.
+ # Record the failed board.
+ self.failed_boards.append(self.defconfig)
+
+ self.progress.inc()
+ self.progress.show()
+ self.state = STATE_IDLE
def get_failed_boards(self):
"""Returns a list of failed boards (defconfigs) in this slot.
options: option flags
"""
if len(configs) == 0:
- print 'Nothing to do. exit.'
- sys.exit(0)
-
- print 'Move %s (jobs: %d)' % (', '.join(configs), options.jobs)
+ if options.force_sync:
+ print 'No CONFIG is specified. You are probably syncing defconfigs.',
+ else:
+ print 'Neither CONFIG nor --force-sync is specified. Nothing will happen.',
+ else:
+ print 'Move ' + ', '.join(configs),
+ print '(jobs: %d)\n' % options.jobs
if options.defconfigs:
defconfigs = [line.strip() for line in open(options.defconfigs)]
while not slots.empty():
time.sleep(SLEEP_TIME)
- progress.show()
print ''
slots.show_failed_boards()
parser.add_option('-e', '--exit-on-error', action='store_true',
default=False,
help='exit immediately on any error')
+ parser.add_option('-s', '--force-sync', action='store_true', default=False,
+ help='force sync by savedefconfig')
parser.add_option('-H', '--headers-only', dest='cleanup_headers_only',
action='store_true', default=False,
help='only cleanup the headers')
(options, configs) = parser.parse_args()
- if len(configs) == 0:
+ if len(configs) == 0 and not options.force_sync:
parser.print_usage()
sys.exit(1)
if not options.cleanup_headers_only:
move_config(configs, options)
- cleanup_headers(configs, options.dry_run)
+ if configs:
+ cleanup_headers(configs, options.dry_run)
if __name__ == '__main__':
main()