bitbake: layerindexlib: Fix various type checking errors

In the list_obj function, we can't check if the requested object is 'in',
the index data -- as it's actually an attribute of the object.  Move to hasattr.

The remaining items were incorrect usages of 'type' for class type comparison.
Instead move to 'isinstance'.  Remaing 'type' comparisons are still valid.  The
code was also reordered slightly to avoid a lot of:

if not isinstance(x, y):
   ...
else:
   ...

reordering it removes the not and makes the code slightly easier to read.

(Bitbake rev: cddea4282820ef10ad4863d87327891ea9383916)

Signed-off-by: Mark Hatle <mark.hatle@windriver.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Mark Hatle 2018-10-11 17:18:33 -04:00 committed by Richard Purdie
parent 64fe513327
commit b33c179aa9
2 changed files with 36 additions and 34 deletions

View File

@ -448,7 +448,7 @@ layerBranches set. If not, they are effectively blank.'''
This function is used to implement debugging and provide the user info. This function is used to implement debugging and provide the user info.
''' '''
for lix in self.indexes: for lix in self.indexes:
if object not in lix: if not hasattr(lix, object):
continue continue
logger.plain ('') logger.plain ('')
@ -1046,15 +1046,15 @@ class LayerBranch(LayerIndexItemObj):
self.id = id self.id = id
self.collection = collection self.collection = collection
self.version = version self.version = version
if type(layer) != type(LayerItem): if isinstance(layer, LayerItem):
self.layer_id = layer
else:
self.layer = layer self.layer = layer
if type(branch) != type(Branch):
self.branch_id = branch
else: else:
self.layer_id = layer
if isinstance(branch, Branch):
self.branch = branch self.branch = branch
else:
self.branch_id = branch
self.vcs_subdir = vcs_subdir self.vcs_subdir = vcs_subdir
self.vcs_last_fetch = vcs_last_fetch self.vcs_last_fetch = vcs_last_fetch
@ -1088,7 +1088,7 @@ class LayerBranch(LayerIndexItemObj):
@layer.setter @layer.setter
def layer(self, value): def layer(self, value):
if type(value) != type(LayerItem): if not isinstance(value, LayerItem):
raise TypeError('value is not a LayerItem') raise TypeError('value is not a LayerItem')
if self.index != value.index: if self.index != value.index:
raise AttributeError('Object and value do not share the same index and thus key set.') raise AttributeError('Object and value do not share the same index and thus key set.')
@ -1122,7 +1122,7 @@ class LayerBranch(LayerIndexItemObj):
@branch.setter @branch.setter
def branch(self, value): def branch(self, value):
if type(value) != type(LayerItem): if not isinstance(value, LayerItem):
raise TypeError('value is not a LayerItem') raise TypeError('value is not a LayerItem')
if self.index != value.index: if self.index != value.index:
raise AttributeError('Object and value do not share the same index and thus key set.') raise AttributeError('Object and value do not share the same index and thus key set.')
@ -1181,7 +1181,7 @@ class LayerIndexItemObj_LayerBranch(LayerIndexItemObj):
@layerbranch.setter @layerbranch.setter
def layerbranch(self, value): def layerbranch(self, value):
if type(value) != type(LayerBranch): if not isinstance(value, LayerBranch):
raise TypeError('value (%s) is not a layerBranch' % type(value)) raise TypeError('value (%s) is not a layerBranch' % type(value))
if self.index != value.index: if self.index != value.index:
raise AttributeError('Object and value do not share the same index and thus key set.') raise AttributeError('Object and value do not share the same index and thus key set.')
@ -1207,14 +1207,14 @@ class LayerIndexItemObj_LayerBranch(LayerIndexItemObj):
class LayerDependency(LayerIndexItemObj_LayerBranch): class LayerDependency(LayerIndexItemObj_LayerBranch):
def define_data(self, id, layerbranch, dependency, required=True): def define_data(self, id, layerbranch, dependency, required=True):
self.id = id self.id = id
if type(layerbranch) != type(LayerBranch): if isinstance(layerbranch, LayerBranch):
self.layerbranch_id = layerbranch
else:
self.layerbranch = layerbranch self.layerbranch = layerbranch
if type(dependency) != type(LayerDependency):
self.dependency_id = dependency
else: else:
self.layerbranch_id = layerbranch
if isinstance(dependency, LayerDependency):
self.dependency = dependency self.dependency = dependency
else:
self.dependency_id = dependency
self.required = required self.required = required
@property @property
@ -1240,7 +1240,7 @@ class LayerDependency(LayerIndexItemObj_LayerBranch):
@dependency.setter @dependency.setter
def dependency(self, value): def dependency(self, value):
if type(value) != type(LayerDependency): if not isinstance(value, LayerDependency):
raise TypeError('value (%s) is not a dependency' % type(value)) raise TypeError('value (%s) is not a dependency' % type(value))
if self.index != value.index: if self.index != value.index:
raise AttributeError('Object and value do not share the same index and thus key set.') raise AttributeError('Object and value do not share the same index and thus key set.')
@ -1288,10 +1288,10 @@ class Recipe(LayerIndexItemObj_LayerBranch):
self.inherits = inherits self.inherits = inherits
self.updated = updated or datetime.datetime.today().isoformat() self.updated = updated or datetime.datetime.today().isoformat()
self.blacklisted = blacklisted self.blacklisted = blacklisted
if type(layerbranch) != type(LayerBranch): if isinstance(layerbranch, LayerBranch):
self.layerbranch_id = layerbranch
else:
self.layerbranch = layerbranch self.layerbranch = layerbranch
else:
self.layerbranch_id = layerbranch
@property @property
def fullpath(self): def fullpath(self):
@ -1324,10 +1324,10 @@ class Machine(LayerIndexItemObj_LayerBranch):
self.id = id self.id = id
self.name = name self.name = name
self.description = description self.description = description
if type(layerbranch) != type(LayerBranch): if isinstance(layerbranch, LayerBranch):
self.layerbranch_id = layerbranch
else:
self.layerbranch = layerbranch self.layerbranch = layerbranch
else:
self.layerbranch_id = layerbranch
self.updated = updated or datetime.datetime.today().isoformat() self.updated = updated or datetime.datetime.today().isoformat()
class Distro(LayerIndexItemObj_LayerBranch): class Distro(LayerIndexItemObj_LayerBranch):
@ -1337,13 +1337,12 @@ class Distro(LayerIndexItemObj_LayerBranch):
self.id = id self.id = id
self.name = name self.name = name
self.description = description self.description = description
if type(layerbranch) != type(LayerBranch): if isinstance(layerbranch, LayerBranch):
self.layerbranch_id = layerbranch
else:
self.layerbranch = layerbranch self.layerbranch = layerbranch
else:
self.layerbranch_id = layerbranch
self.updated = updated or datetime.datetime.today().isoformat() self.updated = updated or datetime.datetime.today().isoformat()
# When performing certain actions, we may need to sort the data. # When performing certain actions, we may need to sort the data.
# This will allow us to keep it consistent from run to run. # This will allow us to keep it consistent from run to run.
def sort_entry(item): def sort_entry(item):

View File

@ -136,10 +136,13 @@ class CookerPlugin(layerindexlib.plugin.IndexPlugin):
layerrev = self._run_command('git rev-parse HEAD', layerpath, default="<unknown>") layerrev = self._run_command('git rev-parse HEAD', layerpath, default="<unknown>")
for remotes in self._run_command('git remote -v', layerpath, default="").split("\n"): for remotes in self._run_command('git remote -v', layerpath, default="").split("\n"):
remote = remotes.split("\t")[1].split(" ")[0] if not remotes:
if "(fetch)" == remotes.split("\t")[1].split(" ")[1]: layerurl = self._handle_git_remote(layerpath)
layerurl = self._handle_git_remote(remote) else:
break remote = remotes.split("\t")[1].split(" ")[0]
if "(fetch)" == remotes.split("\t")[1].split(" ")[1]:
layerurl = self._handle_git_remote(remote)
break
layerItemId += 1 layerItemId += 1
index.layerItems[layerItemId] = layerindexlib.LayerItem(index, None) index.layerItems[layerItemId] = layerindexlib.LayerItem(index, None)
@ -297,7 +300,7 @@ class CookerPlugin(layerindexlib.plugin.IndexPlugin):
for layerBranchId in index.layerBranches: for layerBranchId in index.layerBranches:
# load_bblayers uses the description to cache the actual path... # load_bblayers uses the description to cache the actual path...
machine_path = index.layerBranches[layerBranchId].getDescription() machine_path = index.layerBranches[layerBranchId].layer.description
machine_path = os.path.join(machine_path, 'conf/machine') machine_path = os.path.join(machine_path, 'conf/machine')
if os.path.isdir(machine_path): if os.path.isdir(machine_path):
for (dirpath, _, filenames) in os.walk(machine_path): for (dirpath, _, filenames) in os.walk(machine_path):
@ -310,7 +313,7 @@ class CookerPlugin(layerindexlib.plugin.IndexPlugin):
machine = layerindexlib.Machine(index, None) machine = layerindexlib.Machine(index, None)
machine.define_data(id=machineId, name=fname[:-5], machine.define_data(id=machineId, name=fname[:-5],
description=fname[:-5], description=fname[:-5],
layerbranch=collection_layerbranch[entry]) layerbranch=index.layerBranches[layerBranchId])
index.add_element("machines", [machine]) index.add_element("machines", [machine])
@ -321,7 +324,7 @@ class CookerPlugin(layerindexlib.plugin.IndexPlugin):
for layerBranchId in index.layerBranches: for layerBranchId in index.layerBranches:
# load_bblayers uses the description to cache the actual path... # load_bblayers uses the description to cache the actual path...
distro_path = index.layerBranches[layerBranchId].getDescription() distro_path = index.layerBranches[layerBranchId].layer.description
distro_path = os.path.join(distro_path, 'conf/distro') distro_path = os.path.join(distro_path, 'conf/distro')
if os.path.isdir(distro_path): if os.path.isdir(distro_path):
for (dirpath, _, filenames) in os.walk(distro_path): for (dirpath, _, filenames) in os.walk(distro_path):
@ -334,7 +337,7 @@ class CookerPlugin(layerindexlib.plugin.IndexPlugin):
distro = layerindexlib.Distro(index, None) distro = layerindexlib.Distro(index, None)
distro.define_data(id=distroId, name=fname[:-5], distro.define_data(id=distroId, name=fname[:-5],
description=fname[:-5], description=fname[:-5],
layerbranch=collection_layerbranch[entry]) layerbranch=index.layerBranches[layerBranchId])
index.add_element("distros", [distro]) index.add_element("distros", [distro])