tools/genboardscfg.py: fix minor problems on termination

This tool deletes the incomplete boards.cfg
if it encounters an error or is is terminated by the user.

I notice some problems even though they rarely happen.

[1] The boards.cfg is removed if the program is terminated
during __gen_boards_cfg() function but before boards.cfg
is actually touched.  In this case, the previous boards.cfg
should be kept as it is.

[2] If an error occurs while deleting the incomplete boards.cfg,
the program throws another exception.  This hides the privious
exception and we will not be able to know the real cause.

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>
Acked-by: Simon Glass <sjg@chromium.org>
master
Masahiro Yamada 10 years ago committed by Tom Rini
parent d6538d22de
commit 79d45d32bc
  1. 156
      tools/genboardscfg.py

@ -413,63 +413,95 @@ class Indicator:
sys.stdout.write('\r' + msg) sys.stdout.write('\r' + msg)
sys.stdout.flush() sys.stdout.flush()
def __gen_boards_cfg(jobs): class BoardsFileGenerator:
"""Generate boards.cfg file.
Arguments: """Generator of boards.cfg."""
jobs: The number of jobs to run simultaneously
Note: def __init__(self):
The incomplete boards.cfg is left over when an error (including """Prepare basic things for generating boards.cfg."""
the termination by the keyboard interrupt) occurs on the halfway. # All the defconfig files to be processed
""" defconfigs = []
check_top_directory() for (dirpath, dirnames, filenames) in os.walk(CONFIG_DIR):
print 'Generating %s ... (jobs: %d)' % (BOARD_FILE, jobs) dirpath = dirpath[len(CONFIG_DIR) + 1:]
for filename in fnmatch.filter(filenames, '*_defconfig'):
# All the defconfig files to be processed if fnmatch.fnmatch(filename, '.*'):
defconfigs = [] continue
for (dirpath, dirnames, filenames) in os.walk(CONFIG_DIR): defconfigs.append(os.path.join(dirpath, filename))
dirpath = dirpath[len(CONFIG_DIR) + 1:] self.defconfigs = defconfigs
for filename in fnmatch.filter(filenames, '*_defconfig'): self.indicator = Indicator(len(defconfigs))
if fnmatch.fnmatch(filename, '.*'):
continue # Parse all the MAINTAINERS files
defconfigs.append(os.path.join(dirpath, filename)) maintainers_database = MaintainersDatabase()
for (dirpath, dirnames, filenames) in os.walk('.'):
# Parse all the MAINTAINERS files if 'MAINTAINERS' in filenames:
maintainers_database = MaintainersDatabase() maintainers_database.parse_file(os.path.join(dirpath,
for (dirpath, dirnames, filenames) in os.walk('.'): 'MAINTAINERS'))
if 'MAINTAINERS' in filenames: self.maintainers_database = maintainers_database
maintainers_database.parse_file(os.path.join(dirpath,
'MAINTAINERS')) def __del__(self):
"""Delete the incomplete boards.cfg
# Output lines should be piped into the reformat tool
reformat_process = subprocess.Popen(REFORMAT_CMD, stdin=subprocess.PIPE, This destructor deletes boards.cfg if the private member 'in_progress'
stdout=open(BOARD_FILE, 'w')) is defined as True. The 'in_progress' member is set to True at the
pipe = reformat_process.stdin beginning of the generate() method and set to False at its end.
pipe.write(COMMENT_BLOCK) So, in_progress==True means generating boards.cfg was terminated
on the way.
indicator = Indicator(len(defconfigs)) """
slots = Slots(jobs, pipe, maintainers_database)
if hasattr(self, 'in_progress') and self.in_progress:
# Main loop to process defconfig files: try:
# Add a new subprocess into a vacant slot. os.remove(BOARD_FILE)
# Sleep if there is no available slot. except OSError as exception:
for defconfig in defconfigs: # Ignore 'No such file or directory' error
while not slots.add(defconfig): if exception.errno != errno.ENOENT:
while not slots.available(): raise
# No available slot: sleep for a while print 'Removed incomplete %s' % BOARD_FILE
time.sleep(SLEEP_TIME)
indicator.inc() def generate(self, jobs):
"""Generate boards.cfg
# wait until all the subprocesses finish
while not slots.empty(): This method sets the 'in_progress' member to True at the beginning
time.sleep(SLEEP_TIME) and sets it to False on success. The boards.cfg should not be
print '' touched before/after this method because 'in_progress' is used
to detect the incomplete boards.cfg.
# wait until the reformat tool finishes
reformat_process.communicate() Arguments:
if reformat_process.returncode != 0: jobs: The number of jobs to run simultaneously
sys.exit('"%s" failed' % REFORMAT_CMD[0]) """
self.in_progress = True
print 'Generating %s ... (jobs: %d)' % (BOARD_FILE, jobs)
# Output lines should be piped into the reformat tool
reformat_process = subprocess.Popen(REFORMAT_CMD,
stdin=subprocess.PIPE,
stdout=open(BOARD_FILE, 'w'))
pipe = reformat_process.stdin
pipe.write(COMMENT_BLOCK)
slots = Slots(jobs, pipe, self.maintainers_database)
# Main loop to process defconfig files:
# Add a new subprocess into a vacant slot.
# Sleep if there is no available slot.
for defconfig in self.defconfigs:
while not slots.add(defconfig):
while not slots.available():
# No available slot: sleep for a while
time.sleep(SLEEP_TIME)
self.indicator.inc()
# wait until all the subprocesses finish
while not slots.empty():
time.sleep(SLEEP_TIME)
print ''
# wait until the reformat tool finishes
reformat_process.communicate()
if reformat_process.returncode != 0:
sys.exit('"%s" failed' % REFORMAT_CMD[0])
self.in_progress = False
def gen_boards_cfg(jobs): def gen_boards_cfg(jobs):
"""Generate boards.cfg file. """Generate boards.cfg file.
@ -480,17 +512,9 @@ def gen_boards_cfg(jobs):
Arguments: Arguments:
jobs: The number of jobs to run simultaneously jobs: The number of jobs to run simultaneously
""" """
try: check_top_directory()
__gen_boards_cfg(jobs) generator = BoardsFileGenerator()
except: generator.generate(jobs)
# We should remove incomplete boards.cfg
try:
os.remove(BOARD_FILE)
except OSError as exception:
# Ignore 'No such file or directory' error
if exception.errno != errno.ENOENT:
raise
raise
def main(): def main():
parser = optparse.OptionParser() parser = optparse.OptionParser()

Loading…
Cancel
Save