From b6acfacf0b5a3fef2899a2b7252d9a3cc954c90d Mon Sep 17 00:00:00 2001 From: Andrew Gaffney Date: Sat, 12 Sep 2009 13:07:36 -0500 Subject: [PATCH] Replace usage of types module and type() with isinstance() --- ChangeLog | 7 +++++ modules/catalyst/lock.py | 2 +- modules/catalyst/spawn.py | 8 ++--- modules/catalyst/target/generic_stage.py | 39 ++++++++++++------------ modules/catalyst/target/grp.py | 6 ++-- modules/catalyst/target/livecd_stage1.py | 4 +-- modules/catalyst/target/netboot.py | 12 ++++---- 7 files changed, 42 insertions(+), 36 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9bf50227..d1c4c322 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,13 @@ # Distributed under the GPL v2 # $Id$ + 12 Sep 2009; Andrew Gaffney + modules/catalyst/lock.py, modules/catalyst/spawn.py, + modules/catalyst/target/generic_stage.py, modules/catalyst/target/grp.py, + modules/catalyst/target/livecd_stage1.py, + modules/catalyst/target/netboot.py: + Replace usage of types module and type() with isinstance() + 12 Sep 2009; Andrew Gaffney modules/catalyst/target/generic_stage.py, modules/catalyst/target/livecd_stage1.py, diff --git a/modules/catalyst/lock.py b/modules/catalyst/lock.py index 63206a6f..81c7f90b 100644 --- a/modules/catalyst/lock.py +++ b/modules/catalyst/lock.py @@ -223,7 +223,7 @@ class LockDir: # Why test lockfilename? Because we may have been handed an # fd originally, and the caller might not like having their # open fd closed automatically on them. - #if type(lockfilename) == types.StringType: + #if isinstance(lockfilename, str): # os.close(myfd) if (self.myfd != None): diff --git a/modules/catalyst/spawn.py b/modules/catalyst/spawn.py index 6d87b892..78b614a4 100644 --- a/modules/catalyst/spawn.py +++ b/modules/catalyst/spawn.py @@ -2,7 +2,7 @@ This module contains all the functions related to spawning external commands """ -import sys, os, types, signal +import sys, os, signal import catalyst.util from catalyst.error import * from catalyst.output import * @@ -95,7 +95,7 @@ def spawn_get_output(mycommand,raw_exit_code=False,emulate_gso=True, \ mypid=spawn_bash(mycommand,fd_pipes=fd_pipes,**keywords) os.close(pw) - if type(mypid) != types.ListType: + if not isinstance(mypid, list): os.close(pr) return [mypid, "%s: No such file or directory" % mycommand.split()[0]] @@ -143,7 +143,7 @@ def spawn(mycommand,env={},raw_exit_code=False,opt_name=None,fd_pipes=None,retur myc='' if not func_call: - if type(mycommand)==types.StringType: + if isinstance(mycommand, str): mycommand=mycommand.split() myc = mycommand[0] if not os.access(myc, os.X_OK): @@ -186,7 +186,7 @@ def spawn(mycommand,env={},raw_exit_code=False,opt_name=None,fd_pipes=None,retur # this may look ugly, but basically it moves file descriptors around to ensure no # handles that are needed are accidentally closed during the final dup2 calls. trg_fd=[] - if type(fd_pipes)==types.DictType: + if isinstance(fd_pipes, dict): src_fd=[] k=fd_pipes.keys() k.sort() diff --git a/modules/catalyst/target/generic_stage.py b/modules/catalyst/target/generic_stage.py index 0f4a9fe8..e5be90ef 100644 --- a/modules/catalyst/target/generic_stage.py +++ b/modules/catalyst/target/generic_stage.py @@ -4,7 +4,7 @@ This class does all of the chroot setup, copying of files, etc. It is the driver class for pretty much everything that Catalyst does. """ -import os, types +import os import catalyst from catalyst.output import * from catalyst.spawn import cmd @@ -221,7 +221,7 @@ class generic_stage_target(generic_target): def set_install_mask(self): if "install_mask" in self.settings: - if type(self.settings["install_mask"])!=types.StringType: + if not isinstance(self.settings["install_mask"], str): self.settings["install_mask"]=\ " ".join(self.settings["install_mask"]) @@ -234,13 +234,13 @@ class generic_stage_target(generic_target): self.settings["version_stamp"] def set_source_subpath(self): - if type(self.settings["source_subpath"])!=types.StringType: + if not isinstance(self.settings["source_subpath"], str): raise CatalystError,\ "source_subpath should have been a string. Perhaps you have something wrong in your spec file?" def set_pkgcache_path(self): if "pkgcache_path" in self.settings: - if type(self.settings["pkgcache_path"])!=types.StringType: + if not isinstance(self.settings["pkgcache_path"], str): self.settings["pkgcache_path"]=\ catalyst.util.normpath(" ".join(self.settings["pkgcache_path"])) else: @@ -250,7 +250,7 @@ class generic_stage_target(generic_target): def set_kerncache_path(self): if "kerncache_path" in self.settings: - if type(self.settings["kerncache_path"])!=types.StringType: + if not isinstance(self.settings["kerncache_path"], str): self.settings["kerncache_path"]=\ catalyst.util.normpath(" ".join(self.settings["kerncache_path"])) else: @@ -391,7 +391,7 @@ class generic_stage_target(generic_target): def set_use(self): if "use" in self.settings: - if type(self.settings["use"])==types.StringType: + if isinstance(self.settings["use"], str): self.settings["use"]=self.settings["use"].split() self.settings["use"].append("bindist") @@ -406,13 +406,13 @@ class generic_stage_target(generic_target): def set_rm(self): if "rm" in self.settings: - if type(self.settings["rm"])==types.StringType: + if isinstance(self.settings["rm"], str): self.settings["rm"]=\ self.settings["rm"].split() def set_portage_overlay(self): if "portage_overlay" in self.settings: - if type(self.settings["portage_overlay"])==types.StringType: + if isinstance(self.settings["portage_overlay"], str): self.settings["portage_overlay"]=\ self.settings["portage_overlay"].split() msg("portage_overlay directories are set to: '" + \ @@ -420,13 +420,13 @@ class generic_stage_target(generic_target): def set_overlay(self): if "overlay" in self.settings: - if type(self.settings["overlay"])==types.StringType: + if isinstance(self.settings["overlay"], str): self.settings["overlay"]=\ self.settings["overlay"].split() def set_root_overlay(self): if "root_overlay" in self.settings: - if type(self.settings["root_overlay"])==types.StringType: + if isinstance(self.settings["root_overlay"], str): self.settings["root_overlay"]=\ self.settings["root_overlay"].split() @@ -436,7 +436,7 @@ class generic_stage_target(generic_target): def set_valid_build_kernel_vars(self): if "boot/kernel" in self.settings: - if type(self.settings["boot/kernel"])==types.StringType: + if isinstance(self.settings["boot/kernel"], str): loopy=[self.settings["boot/kernel"]] else: loopy=self.settings["boot/kernel"] @@ -455,8 +455,7 @@ class generic_stage_target(generic_target): self.valid_values.append("boot/kernel/"+x+"/use") self.valid_values.append("boot/kernel/"+x+"/packages") if "boot/kernel/"+x+"/packages" in self.settings: - if type(self.settings["boot/kernel/"+x+\ - "/packages"])==types.StringType: + if isinstance(self.settings["boot/kernel/" + x + "/packages"], str): self.settings["boot/kernel/"+x+"/packages"]=\ [self.settings["boot/kernel/"+x+"/packages"]] @@ -974,7 +973,7 @@ class generic_stage_target(generic_target): msg("Resume point detected, skipping empty operation...") else: if "empty" in self.settings: - if type(self.settings["empty"])==types.StringType: + if isinstance(self.settings["empty"], str): self.settings["empty"]=\ self.settings["empty"].split() for x in self.settings["empty"]: @@ -1064,14 +1063,14 @@ class generic_stage_target(generic_target): varname="clst_"+string.replace(x,"/","_") varname=string.replace(varname,"-","_") varname=string.replace(varname,".","_") - if type(self.settings[x])==types.StringType: + if isinstance(self.settings[x], str): """ Prefix to prevent namespace clashes """ #os.environ[varname]=self.settings[x] self.env[varname]=self.settings[x] - elif type(self.settings[x])==types.ListType: + elif isinstance(self.settings[x], list): #os.environ[varname] = " ".join(self.settings[x]) self.env[varname] = " ".join(self.settings[x]) - elif type(self.settings[x])==types.BooleanType: + elif isinstance(self.settings[x], bool): if self.settings[x]: self.env[varname]="true" else: @@ -1115,7 +1114,7 @@ class generic_stage_target(generic_target): msg("Resume point detected, skipping unmerge operation...") else: if "unmerge" in self.settings: - if type(self.settings["unmerge"])==types.StringType: + if isinstance(self.settings["unmerge"], str): self.settings["unmerge"]=\ [self.settings["unmerge"]] myunmerge=\ @@ -1194,7 +1193,7 @@ class generic_stage_target(generic_target): if "boot/kernel" in self.settings: try: mynames=self.settings["boot/kernel"] - if type(mynames)==types.StringType: + if isinstance(mynames, str): mynames=[mynames] """ Execute the script that sets up the kernel build environment @@ -1238,7 +1237,7 @@ class generic_stage_target(generic_target): myopts=self.settings["boot/kernel/"+kname+\ "/kernelopts"] - if type(myopts) != types.StringType: + if not isinstance(myopts, str): myopts = " ".join(myopts) self.env[kname+"_kernelopts"]=myopts diff --git a/modules/catalyst/target/grp.py b/modules/catalyst/target/grp.py index 2e52fda4..ccd22980 100644 --- a/modules/catalyst/target/grp.py +++ b/modules/catalyst/target/grp.py @@ -3,7 +3,7 @@ The builder class for GRP (Gentoo Reference Platform) builds. """ -import os,types,glob +import os, glob from generic_stage import * import catalyst from catalyst.error import * @@ -23,11 +23,11 @@ class grp_target(generic_stage_target): raise CatalystError,"Required value \"grp\" not specified in spec." self.required_values.extend(["grp"]) - if type(self.settings["grp"])==types.StringType: + if isinstance(self.settings["grp"], str): self.settings["grp"]=[self.settings["grp"]] if "grp/use" in self.settings: - if type(self.settings["grp/use"])==types.StringType: + if isinstance(self.settings["grp/use"], str): self.settings["grp/use"]=[self.settings["grp/use"]] for x in self.settings["grp"]: diff --git a/modules/catalyst/target/livecd_stage1.py b/modules/catalyst/target/livecd_stage1.py index 7c066d92..3c9d8b17 100644 --- a/modules/catalyst/target/livecd_stage1.py +++ b/modules/catalyst/target/livecd_stage1.py @@ -52,14 +52,14 @@ class livecd_stage1_target(generic_stage_target): def set_packages(self): generic_stage_target.set_packages(self) if "packages" in self.settings: - if type(self.settings["packages"]) == types.StringType: + if isinstance(self.settings["packages"], str): self.settings["packages"] = \ self.settings["packages"].split() self.settings["packages"].append("app-misc/livecd-tools") def set_pkgcache_path(self): if "pkgcache_path" in self.settings: - if type(self.settings["pkgcache_path"]) != types.StringType: + if not isinstance(self.settings["pkgcache_path"], str): self.settings["pkgcache_path"] = catalyst.util.normpath(" ".join(self.settings["pkgcache_path"])) else: generic_stage_target.set_pkgcache_path(self) diff --git a/modules/catalyst/target/netboot.py b/modules/catalyst/target/netboot.py index 81af14f6..593e3e95 100644 --- a/modules/catalyst/target/netboot.py +++ b/modules/catalyst/target/netboot.py @@ -3,7 +3,7 @@ Builder class for a netboot build, version 2 """ -import os,string,types +import os, string from generic_stage import * import catalyst.util from catalyst.error import * @@ -30,7 +30,7 @@ class netboot_target(generic_stage_target): try: if "netboot/packages" in self.settings: - if type(self.settings["netboot/packages"]) == types.StringType: + if isinstance(self.settings["netboot/packages"], str): loopy=[self.settings["netboot/packages"]] else: loopy=self.settings["netboot/packages"] @@ -66,20 +66,20 @@ class netboot_target(generic_stage_target): msg("Resume point detected, skipping target path setup operation...") else: if "netboot/packages" in self.settings: - if type(self.settings["netboot/packages"]) == types.StringType: + if isinstance(self.settings["netboot/packages"], str): loopy=[self.settings["netboot/packages"]] else: loopy=self.settings["netboot/packages"] for x in loopy: if "netboot/packages/"+x+"/files" in self.settings: - if type(self.settings["netboot/packages/"+x+"/files"]) == types.ListType: + if isinstance(self.settings["netboot/packages/"+x+"/files"], list): myfiles.extend(self.settings["netboot/packages/"+x+"/files"]) else: myfiles.append(self.settings["netboot/packages/"+x+"/files"]) if "netboot/extra_files" in self.settings: - if type(self.settings["netboot/extra_files"]) == types.ListType: + if isinstance(self.settings["netboot/extra_files"], list): myfiles.extend(self.settings["netboot/extra_files"]) else: myfiles.append(self.settings["netboot/extra_files"]) @@ -130,7 +130,7 @@ class netboot_target(generic_stage_target): msg("Resume point detected, skipping empty operation...") else: if "netboot/empty" in self.settings: - if type(self.settings["netboot/empty"])==types.StringType: + if isinstance(self.settings["netboot/empty"], str): self.settings["netboot/empty"]=self.settings["netboot/empty"].split() for x in self.settings["netboot/empty"]: myemp=self.settings["chroot_path"] + self.settings["merge_path"] + x -- 2.26.2