root/build-tools/stx/debsentry.py
Yue Tao 5c799e96fb debsentry.py: capture exceptions
The manipulation of file, and pickle load/dump doesn't run in try
block. An empty or an invalid pickle file can crash the build system.

The crash of an empty file is:

File "/localdisk/designer/ytao/stx/cgcs-root/build-tools/stx/build-pkgs", line 412, in upload_with_deb
  subdebs = debsentry.get_subdebs(debs_clue, package, logger)
File "/localdisk/designer/ytao/stx/cgcs-root/build-tools/stx/debsentry.py", line 24, in get_subdebs
  debs = pickle.load(fclue)
EOFError: Ran out of input

Test Plan:

Pass: build-pkgs -c -p package with an empty debs_entry.pkl file
Pass: build-pkgs -c -p package with an invalid debs_entry.pkl file

Story: 2008846
Task: 45205

Signed-off-by: Yue Tao <yue.tao@windriver.com>
Change-Id: Id9ee74c1f9be09e759926e3031a4a9a01861db37
2022-05-05 11:03:37 +08:00

56 lines
1.9 KiB
Python
Executable File

#!/usr/bin/python3
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Copyright (C) 2021 Wind River Systems,Inc
import os
import pickle
def get_subdebs(clue, package, logger):
try:
with open(clue, 'rb') as fclue:
try:
debs = pickle.load(fclue)
if package in debs.keys():
return debs[package]
except (EOFError, ValueError, AttributeError, ImportError, IndexError, pickle.UnpicklingError) as e:
logger.warn(f"debs_entry:failed to load {clue}, return None")
except IOError:
logger.warn(f"debs_entry:{clue} does not exist")
return None
def set_subdebs(clue, package, debs, logger):
debmap = {}
try:
with open(clue, 'rb') as fclue:
try:
debmap = pickle.load(fclue)
logger.debug(f"debs_entry:loaded the debs clue {clue}")
except (EOFError, ValueError, AttributeError, ImportError, IndexError, pickle.UnpicklingError) as e:
logger.warn(f"debs_entry:failed to load {clue}, recreate it")
os.remove(clue)
debmap = {}
except IOError:
logger.debug(f"debs_entry:{clue} does not exist")
debmap[package] = debs
try:
with open(clue, 'wb+') as fclue:
pickle.dump(debmap, fclue, pickle.HIGHEST_PROTOCOL)
except IOError:
raise Exception(f"debs_entry:failed to write {clue}")
return True