From bb57566f61e4215b6d4edd5b07492760881a033d Mon Sep 17 00:00:00 2001 From: Joe George Date: Mon, 22 Jan 2024 13:45:25 -0500 Subject: [PATCH] Finish formatting and linting of scripts --- .github/workflows/ruff.yml | 2 +- scripts/Component.py | 114 +++++++++++++++++++---------------- scripts/Controller.py | 20 +++++-- scripts/Expect.py | 4 +- scripts/Glacier2Util.py | 12 +++- scripts/IceBoxUtil.py | 18 +++++- scripts/IceBridgeUtil.py | 3 +- scripts/IceGridUtil.py | 30 +++++++--- scripts/IcePatch2Util.py | 3 +- scripts/IceStormUtil.py | 20 +++++-- scripts/LocalDriver.py | 17 +++--- scripts/NetworkProxy.py | 8 +-- scripts/Util.py | 118 +++++++++++++++++++------------------ scripts/icehashpassword.py | 4 +- 14 files changed, 223 insertions(+), 150 deletions(-) diff --git a/.github/workflows/ruff.yml b/.github/workflows/ruff.yml index 35f88c1011c..3af6590eafd 100644 --- a/.github/workflows/ruff.yml +++ b/.github/workflows/ruff.yml @@ -20,4 +20,4 @@ jobs: run: pip install ruff - name: Run Ruff - run: ruff check . --exclude "scripts" --exclude "python" --ignore E402 + run: ruff check . --exclude "python" --ignore E402 diff --git a/scripts/Component.py b/scripts/Component.py index 47f144689ef..438fb8a4cd4 100644 --- a/scripts/Component.py +++ b/scripts/Component.py @@ -3,11 +3,14 @@ # import os +import re -from Util import * +# Import as modules to allow for circular imports +import Util +import IceGridUtil -class Ice(Component): +class Ice(Util.Component): # Options for all transports (ran only with Ice client/server tests defined for cross testing) transportOptions = { "protocol": ["tcp", "ssl", "wss", "ws"], @@ -36,19 +39,20 @@ class Ice(Component): } def useBinDist(self, mapping, current): - return Component._useBinDist(self, mapping, current, "ICE_BIN_DIST") + return Util.Component._useBinDist(self, mapping, current, "ICE_BIN_DIST") def getInstallDir(self, mapping, current): # On Windows, the Ice MSI installation can only be used for C++ envHomeName = ( None - if isinstance(platform, Windows) and not isinstance(mapping, CppMapping) + if isinstance(Util.platform, Util.Windows) + and not isinstance(mapping, Util.CppMapping) else "ICE_HOME" ) - return Component._getInstallDir(self, mapping, current, envHomeName) + return Util.Component._getInstallDir(self, mapping, current, envHomeName) def getPhpExtension(self, mapping, current): - if isinstance(platform, Windows): + if isinstance(Util.platform, Util.Windows): return ( "php_ice.dll" if current.driver.configs[mapping].buildConfig in ["Debug", "Release"] @@ -58,14 +62,16 @@ def getPhpExtension(self, mapping, current): return "ice.so" def getNugetPackageVersionFile(self, mapping): - if isinstance(mapping, CSharpMapping): - return os.path.join(toplevel, "csharp", "msbuild", "zeroc.ice.net.nuspec") + if isinstance(mapping, Util.CSharpMapping): + return os.path.join( + Util.toplevel, "csharp", "msbuild", "zeroc.ice.net.nuspec" + ) else: return os.path.join( - toplevel, + Util.toplevel, "cpp", "msbuild", - "zeroc.ice.{0}.nuspec".format(platform.getPlatformToolset()), + "zeroc.ice.{0}.nuspec".format(Util.platform.getPlatformToolset()), ) def getFilters(self, mapping, config): @@ -96,7 +102,7 @@ def getFilters(self, mapping, config): ], ["Ice/library", "Ice/plugin"], ) - elif isinstance(mapping, JavaMapping) and config.android: + elif isinstance(mapping, Util.JavaMapping) and config.android: return ( ["Ice/.*"], [ @@ -110,9 +116,9 @@ def getFilters(self, mapping, config): "Ice/properties", ], ) - elif isinstance(mapping, JavaScriptMapping): + elif isinstance(mapping, Util.JavaScriptMapping): return ([], ["typescript/.*"]) - elif isinstance(mapping, SwiftMapping) and config.buildPlatform in [ + elif isinstance(mapping, Util.SwiftMapping) and config.buildPlatform in [ "iphonesimulator", "iphoneos", ]: @@ -124,9 +130,9 @@ def getFilters(self, mapping, config): def canRun(self, testId, mapping, current): parent = re.match(r"^([\w]*).*", testId).group(1) - if isinstance(platform, Linux): + if isinstance(Util.platform, Util.Linux): if ( - platform.getLinuxId() in ["centos", "rhel", "fedora"] + Util.platform.getLinuxId() in ["centos", "rhel", "fedora"] and current.config.buildPlatform == "x86" ): # @@ -135,7 +141,7 @@ def canRun(self, testId, mapping, current): # if parent in ["Glacier2", "IceStorm", "IceGrid"]: return False - elif isinstance(platform, Windows): + elif isinstance(Util.platform, Util.Windows): # # On Windows, if testing with a binary distribution, don't test Glacier2/IceBridge services # with the Debug configurations since we don't provide binaries for them. @@ -146,7 +152,7 @@ def canRun(self, testId, mapping, current): and current.config.buildConfig.find("Debug") >= 0 ): return False - elif isinstance(platform, AIX): + elif isinstance(Util.platform, Util.AIX): if current.config.buildPlatform == "ppc" and self.useBinDist( mapping, current ): @@ -161,7 +167,7 @@ def canRun(self, testId, mapping, current): # No C++98 tests for Glacier2, IceGrid, IceStorm, IceBridge if ( - isinstance(mapping, CppMapping) + isinstance(mapping, Util.CppMapping) and not current.config.cpp11 and parent in ["Glacier2", "IceBridge", "IceGrid", "IceStorm"] ): @@ -174,12 +180,12 @@ def isMainThreadOnly(self, testId): def getDefaultProcesses(self, mapping, processType, testId): if testId.startswith("IceUtil") or testId.startswith("Slice"): - return [SimpleClient()] + return [Util.SimpleClient()] elif testId.startswith("IceGrid"): if processType in ["client", "collocated"]: - return [IceGridClient()] + return [IceGridUtil.IceGridClient()] if processType in ["server", "serveramd"]: - return [IceGridServer()] + return [IceGridUtil.IceGridServer()] def getOptions(self, testcase, current): parent = re.match(r"^([\w]*).*", testcase.getTestSuite().getId()).group(1) @@ -194,7 +200,7 @@ def getOptions(self, testcase, current): ]: return None - if isinstance(testcase, CollocatedTestCase): + if isinstance(testcase, Util.CollocatedTestCase): return None # Define here Ice tests which are slow to execute and for which it's not useful to test different options @@ -206,7 +212,7 @@ def getOptions(self, testcase, current): return self.serviceOptions # We only run the client/server tests defined for cross testing with all transports - if isinstance(testcase, ClientServerTestCase) and self.isCross( + if isinstance(testcase, Util.ClientServerTestCase) and self.isCross( testcase.getTestSuite().getId() ): return self.transportOptions @@ -245,7 +251,7 @@ def isCross(self, testId): def getSoVersion(self): with open( - os.path.join(toplevel, "cpp", "include", "IceUtil", "Config.h"), "r" + os.path.join(Util.toplevel, "cpp", "include", "IceUtil", "Config.h"), "r" ) as config: intVersion = int( re.search("ICE_INT_VERSION ([0-9]*)", config.read()).group(1) @@ -263,60 +269,66 @@ def getSoVersion(self): component = Ice() -from Glacier2Util import * -from IceBoxUtil import * -from IceBridgeUtil import * -from IcePatch2Util import * -from IceGridUtil import * -from IceStormUtil import * - # # Supported mappings # for m in filter( - lambda x: os.path.isdir(os.path.join(toplevel, x)), os.listdir(toplevel) + lambda x: os.path.isdir(os.path.join(Util.toplevel, x)), os.listdir(Util.toplevel) ): if m == "cpp" or re.match("cpp-.*", m): - Mapping.add(m, CppMapping(), component) + Util.Mapping.add(m, Util.CppMapping(), component) elif m == "java" or re.match("java-.*", m): - Mapping.add(m, JavaMapping(), component) + Util.Mapping.add(m, Util.JavaMapping(), component) elif m == "python" or re.match("python-.*", m): - Mapping.add(m, PythonMapping(), component) + Util.Mapping.add(m, Util.PythonMapping(), component) elif m == "ruby" or re.match("ruby-.*", m): - Mapping.add( - m, RubyMapping(), component, enable=not isinstance(platform, Windows) + Util.Mapping.add( + m, + Util.RubyMapping(), + component, + enable=not isinstance(Util.platform, Util.Windows), ) elif m == "php" or re.match("php-.*", m): - Mapping.add( - m, PhpMapping(), component, enable=not isinstance(platform, Windows) + Util.Mapping.add( + m, + Util.PhpMapping(), + component, + enable=not isinstance(Util.platform, Util.Windows), ) elif m == "js" or re.match("js-.*", m): - Mapping.add(m, JavaScriptMapping(), component, enable=platform.hasNodeJS()) - Mapping.add( + Util.Mapping.add( + m, Util.JavaScriptMapping(), component, enable=Util.platform.hasNodeJS() + ) + Util.Mapping.add( "typescript", - TypeScriptMapping(), + Util.TypeScriptMapping(), component, "js", - enable=platform.hasNodeJS(), + enable=Util.platform.hasNodeJS(), ) elif m == "swift" or re.match("swift-.*", m): # Swift mapping requires Swift 5.0 or greater - Mapping.add( - "swift", SwiftMapping(), component, enable=platform.hasSwift((5, 0)) + Util.Mapping.add( + "swift", + Util.SwiftMapping(), + component, + enable=Util.platform.hasSwift((5, 0)), ) elif m == "csharp" or re.match("charp-.*", m): - Mapping.add( + Util.Mapping.add( "csharp", - CSharpMapping(), + Util.CSharpMapping(), component, - enable=isinstance(platform, Windows) or platform.hasDotNet(), + enable=isinstance(Util.platform, Util.Windows) or Util.platform.hasDotNet(), ) # # Check if Matlab is installed and eventually add the Matlab mapping # try: - run("where matlab" if isinstance(platform, Windows) else "which matlab") - Mapping.add("matlab", MatlabMapping(), component) -except: + Util.run( + "where matlab" if isinstance(Util.platform, Util.Windows) else "which matlab" + ) + Util.Mapping.add("matlab", Util.MatlabMapping(), component) +except Exception: pass diff --git a/scripts/Controller.py b/scripts/Controller.py index 61ec8aed15e..459f3ca78e7 100755 --- a/scripts/Controller.py +++ b/scripts/Controller.py @@ -5,7 +5,19 @@ import os import sys -from Util import * + +from Util import ( + Darwin, + Driver, + IceProcess, + Mapping, + Result, + parseOptions, + toplevel, + platform, + runTests, + traceback, +) class ControllerDriver(Driver): @@ -109,7 +121,7 @@ def startServerSide(self, config, c): try: self.serverSideRunning = True return self.current.serverTestCase._startServerSide(self.current) - except Exception as ex: + except Exception: self.serverSideRunning = False raise Test.Common.TestCaseFailedException( self.current.result.getOutput() + "\n" + traceback.format_exc() @@ -141,7 +153,7 @@ def destroy(self, c): if self.serverSideRunning: try: self.current.serverTestCase._stopServerSide(self.current, False) - except: + except Exception: pass c.adapter.remove(c.id) @@ -171,7 +183,7 @@ def runTestCase(self, mapping, testsuite, testcase, cross, c): if self.testcase: try: self.testcase.destroy() - except: + except Exception: pass self.testcase = None diff --git a/scripts/Expect.py b/scripts/Expect.py index 5a3c60955d1..c95e0a8a848 100755 --- a/scripts/Expect.py +++ b/scripts/Expect.py @@ -634,11 +634,13 @@ def kill(): return killProcess(self.p) self.wait() + break except KeyboardInterrupt as e: ex = e raise - except e: + except Exception as e: ex = e + if ex: print(ex) raise ex diff --git a/scripts/Glacier2Util.py b/scripts/Glacier2Util.py index d74e9bb0ea0..403c2f5f5a2 100644 --- a/scripts/Glacier2Util.py +++ b/scripts/Glacier2Util.py @@ -4,7 +4,17 @@ import sys import os -from Util import * + +from Util import ( + ClientServerTestCase, + Mapping, + ProcessFromBinDir, + ProcessIsReleaseOnly, + Server, + TestSuite, + run, + toplevel, +) class Glacier2Router(ProcessFromBinDir, ProcessIsReleaseOnly, Server): diff --git a/scripts/IceBoxUtil.py b/scripts/IceBoxUtil.py index e9f7a36f321..1822da32ae4 100644 --- a/scripts/IceBoxUtil.py +++ b/scripts/IceBoxUtil.py @@ -2,8 +2,20 @@ # Copyright (c) ZeroC, Inc. All rights reserved. # -from Util import * -from Component import component +import Component +from Util import ( + AIX, + CSharpMapping, + Client, + CppMapping, + JavaMapping, + Linux, + Mapping, + ProcessFromBinDir, + ProcessIsReleaseOnly, + Server, + platform, +) class IceBox(ProcessFromBinDir, Server): @@ -62,7 +74,7 @@ def getExe(self, current): elif ( isinstance(platform, AIX) and current.config.buildPlatform == "ppc" - and not component.useBinDist(mapping, current) + and not Component.component.useBinDist(mapping, current) ): return "iceboxadmin_32" else: diff --git a/scripts/IceBridgeUtil.py b/scripts/IceBridgeUtil.py index 15de3277b7e..1df1f0f77ac 100644 --- a/scripts/IceBridgeUtil.py +++ b/scripts/IceBridgeUtil.py @@ -2,7 +2,8 @@ # Copyright (c) ZeroC, Inc. All rights reserved. # -from Util import * + +from Util import Mapping, ProcessFromBinDir, ProcessIsReleaseOnly, Server class IceBridge(ProcessFromBinDir, ProcessIsReleaseOnly, Server): diff --git a/scripts/IceGridUtil.py b/scripts/IceGridUtil.py index a544222d1d1..e232502c9b8 100644 --- a/scripts/IceGridUtil.py +++ b/scripts/IceGridUtil.py @@ -2,12 +2,25 @@ # Copyright (c) ZeroC, Inc. All rights reserved. # -import sys import os -from Util import * -from IceBoxUtil import * -from Glacier2Util import * -from IcePatch2Util import * +import shutil +from Glacier2Util import Glacier2Router +from IceBoxUtil import IceBox +from IcePatch2Util import IcePatch2Server + +from Util import ( + CSharpMapping, + Client, + Linux, + Mapping, + ProcessFromBinDir, + ProcessIsReleaseOnly, + Server, + TestCase, + toplevel, + platform, + val, +) class IceGridProcess: @@ -106,7 +119,7 @@ def teardown(self, current, success): if success or current.driver.isInterrupted(): try: shutil.rmtree(self.dbdir) - except: + except Exception: pass def getProps(self, current): @@ -176,7 +189,7 @@ def teardown(self, current, success): if success or current.driver.isInterrupted(): try: shutil.rmtree(self.dbdir) - except: + except Exception: pass def getProps(self, current): @@ -295,11 +308,10 @@ def setupClientSide(self, current): if self.application: try: self.runadmin(current, "application remove Test", quiet=True) - except: + except Exception: pass javaHome = os.environ.get("JAVA_HOME", None) - serverProps = Server().getProps(current) variables = { "test.dir": self.getPath(current), "java.exe": os.path.join(javaHome, "bin", "java") diff --git a/scripts/IcePatch2Util.py b/scripts/IcePatch2Util.py index 436502895d3..bcb1d8964fc 100644 --- a/scripts/IcePatch2Util.py +++ b/scripts/IcePatch2Util.py @@ -2,7 +2,8 @@ # Copyright (c) ZeroC, Inc. All rights reserved. # -from Util import * + +from Util import Mapping, Process, ProcessFromBinDir, ProcessIsReleaseOnly class IcePatch2Calc(ProcessFromBinDir, ProcessIsReleaseOnly, Process): diff --git a/scripts/IceStormUtil.py b/scripts/IceStormUtil.py index 036c7435ed6..b841f48ecf0 100644 --- a/scripts/IceStormUtil.py +++ b/scripts/IceStormUtil.py @@ -3,9 +3,17 @@ # import os -from Util import * -from Component import component -from IceBoxUtil import * +import shutil +import Component +from IceBoxUtil import IceBoxAdmin +from Util import ( + Client, + Mapping, + ProcessFromBinDir, + ProcessIsReleaseOnly, + Server, + TestCase, +) class IceStorm(ProcessFromBinDir, Server): @@ -61,7 +69,7 @@ def teardown(self, current, success): # Remove the database directory tree try: shutil.rmtree(self.dbdir) - except: + except Exception: pass def getProps(self, current): @@ -71,7 +79,7 @@ def getProps(self, current): props.update( { "IceBox.Service.IceStorm": "IceStormService," - + component.getSoVersion() + + Component.component.getSoVersion() + ":createIceStorm", "IceBox.PrintServicesReady": "IceStorm", "IceBox.InheritProperties": 1, @@ -223,7 +231,7 @@ def __init__(self, instanceName=None, instance=None, *args, **kargs): IceStormProcess.__init__(self, instanceName, instance) def getExe(self, current): - if current.config.buildPlatform == "ppc" and not component.useBinDist( + if current.config.buildPlatform == "ppc" and not Component.component.useBinDist( self.mapping, current ): return self.exe + "_32" diff --git a/scripts/LocalDriver.py b/scripts/LocalDriver.py index 32de5d1bdc4..c90227814ba 100644 --- a/scripts/LocalDriver.py +++ b/scripts/LocalDriver.py @@ -7,7 +7,9 @@ import time import threading import queue -from Util import * +import traceback + +from Util import Driver, IceProcess, Mapping, Result, parseOptions, Expect # # The Executor class runs testsuites on multiple worker threads. @@ -82,7 +84,7 @@ def runTestSuites(self, driver, total, results, mainThread=False): except KeyboardInterrupt: if mainThread: raise - except: + except Exception: pass finally: current.destroy() @@ -285,7 +287,7 @@ def startServerSide(self, testcase, current): except Test.Common.TestCaseFailedException as ex: current.result.writeln(ex.output) raise RuntimeError("test failed:\n" + str(ex)) - except: + except Exception: current.serverTestCase.destroy() current.serverTestCase = None raise @@ -588,7 +590,7 @@ def runTestSuite(self, current): current.result.started("setup") current.testsuite.setup(current) current.result.succeeded("setup") - except Exception as ex: + except Exception: current.result.failed("setup", traceback.format_exc()) raise @@ -602,7 +604,7 @@ def runTestSuite(self, current): ): current.config = conf testcase.run(current) - except: + except Exception: if current.driver.debug: current.result.writeln(traceback.format_exc()) raise @@ -614,7 +616,7 @@ def runTestSuite(self, current): current.result.started("teardown") current.testsuite.teardown(current, success) current.result.succeeded("teardown") - except Exception as ex: + except Exception: current.result.failed("teardown", traceback.format_exc()) raise @@ -785,6 +787,3 @@ def getMappings(self): def filterOptions(self, options): return self.runner.filterOptions(options) - - -Driver.add("local", LocalDriver) diff --git a/scripts/NetworkProxy.py b/scripts/NetworkProxy.py index 24c1b18a821..0b7a575d4a9 100644 --- a/scripts/NetworkProxy.py +++ b/scripts/NetworkProxy.py @@ -38,7 +38,7 @@ def close(self): if self.remoteSocket: self.remoteSocket.close() self.remoteSocket = None - except: + except Exception: pass def run(self): @@ -48,7 +48,7 @@ def run(self): try: self.remoteSocket.connect(remoteAddr) self.socket.send(self.response(True)) - except: + except Exception: self.socket.send(self.response(False)) return @@ -106,7 +106,7 @@ def run(self): self.socket.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEPORT, 1 ) - except: + except Exception: # Ignore, this can throw on some platforms if not supported (e.g: ARMHF/Qemu) pass self.socket.bind(("127.0.0.1", self.port)) @@ -126,7 +126,7 @@ def run(self): connection.start() with self.cond: self.connections.append(connection) - except: + except Exception: pass finally: self.socket.close() diff --git a/scripts/Util.py b/scripts/Util.py index 60ba5ef694b..d34c4c60c2d 100644 --- a/scripts/Util.py +++ b/scripts/Util.py @@ -45,7 +45,7 @@ def run(cmd, cwd=None, err=False, stdout=False, stdin=None, stdinRepeat=True): if not stdinRepeat: break time.sleep(1) - except: + except Exception: pass out = (p.stderr if stdout else p.stdout).read().decode("UTF-8").strip() @@ -66,9 +66,9 @@ def run(cmd, cwd=None, err=False, stdout=False, stdin=None, stdinRepeat=True): def val(v, quoteValue=True): - if type(v) == bool: + if isinstance(v, bool): return "1" if v else "0" - elif type(v) == str: + elif isinstance(v, str): if not quoteValue or v.find(" ") < 0: return v v = v.replace("\\", "\\\\").replace('"', '\\"') @@ -110,7 +110,7 @@ def useBinDist(self, mapping, current): """ def getInstallDir(self, mapping, current): - raise Error("must be overridden") + raise Exception("must be overridden") def getSourceDir(self): return toplevel @@ -139,7 +139,7 @@ def getNugetPackage(self, mapping): ) def getNugetPackageVersion(self, mapping): - if not mapping in self.nugetVersion: + if mapping not in self.nugetVersion: file = self.getNugetPackageVersionFile(mapping) if file.endswith(".nuspec"): expr = "(.*)" @@ -152,7 +152,7 @@ def getNugetPackageVersion(self, mapping): m = re.search(expr, config.read()) if m: self.nugetVersion[mapping] = m.group(1) - if not mapping in self.nugetVersion: + if mapping not in self.nugetVersion: raise RuntimeError( "couldn't figure out the nuget version from `{0}'".format(file) ) @@ -255,7 +255,7 @@ def __init__(self): "global-packages: (.*)", run("dotnet nuget locals --list global-packages"), ).groups(1)[0] - except: + except Exception: self.nugetPackageCache = None self._hasNodeJS = None @@ -271,14 +271,14 @@ def init(self, component): ) def hasDotNet(self): - return self.nugetPackageCache != None + return self.nugetPackageCache is not None def hasNodeJS(self): if self._hasNodeJS is None: try: run("node --version") self._hasNodeJS = True - except: + except Exception: self._hasNodeJS = False return self._hasNodeJS @@ -294,7 +294,7 @@ def hasSwift(self, version): ) else: self.hasSwift = False - except: + except Exception: self._hasSwift = False return self._hasSwift @@ -308,8 +308,8 @@ def parseBuildVariables(self, component, variables): cwd = Mapping.getByName("cpp").getPath() output = run('make print V="{0}"'.format(" ".join(variables.keys())), cwd=cwd) - for l in output.split("\n"): - match = re.match(r"^.*:.*: (.*) = (.*)", l) + for line in output.split("\n"): + match = re.match(r"^.*:.*: (.*) = (.*)", line) if match and match.group(1): if match.group(1) in variables: (varname, valuefn) = variables[match.group(1).strip()] @@ -497,7 +497,7 @@ def getDefaultBuildConfig(self): return "Release" def getCompiler(self): - if self.compiler != None: + if self.compiler is not None: return self.compiler if os.environ.get("CPP_COMPILER", "") != "": self.compiler = os.environ["CPP_COMPILER"] @@ -520,7 +520,7 @@ def getCompiler(self): self.compiler = "v143" else: raise RuntimeError("Unknown compiler version:\n{0}".format(out)) - except: + except Exception: self.compiler = "" return self.compiler @@ -632,7 +632,7 @@ def getNugetPackageDir(self, component, mapping, current): def getDotNetExe(self): try: return run("where dotnet").strip().splitlines()[0] - except: + except Exception: return None @@ -658,13 +658,13 @@ def parseOptions(obj, options, mapped={}): if isinstance(getattr(obj, o), bool): setattr(obj, o, True if not a else (a.lower() in ["yes", "true", "1"])) elif isinstance(getattr(obj, o), list): - l = getattr(obj, o) - l.append(a) + argList = getattr(obj, o) + argList.append(a) else: if not a and not isinstance(a, str): a = "0" setattr(obj, o, type(getattr(obj, o))(a)) - if not o in obj.parsedOptions: + if o not in obj.parsedOptions: obj.parsedOptions.append(o) else: remaining.append((o, a)) @@ -770,7 +770,7 @@ def __str__(self): for o in self.parsedOptions: v = getattr(self, o) if v: - s.append(o if type(v) == bool else str(v)) + s.append(o if isinstance(v, bool) else str(v)) return ",".join(s) def getAll(self, current, testcase, rand=False): @@ -812,7 +812,7 @@ def gen(supportedOptions): for k, v in supportedOptions.items(): v = next(v) if v: - if type(v) == bool: + if isinstance(v, bool): if v: options.append(("--{0}".format(k), None)) else: @@ -821,10 +821,10 @@ def gen(supportedOptions): # Add parsed options for o in self.parsedOptions: v = getattr(self, o) - if type(v) == bool: + if isinstance(v, bool): if v: options.append(("--{0}".format(o), None)) - elif type(v) == list: + elif isinstance(v, list): options += [("--{0}".format(o), e) for e in v] else: options.append(("--{0}".format(o), v)) @@ -853,10 +853,10 @@ def canRun(self, testId, current): for k, v in options.items(): if hasattr(self, k): - if not getattr(self, k) in v: + if getattr(self, k) not in v: return False elif hasattr(current.driver, k): - if not getattr(current.driver, k) in v: + if getattr(current.driver, k) not in v: return False else: return True @@ -950,7 +950,7 @@ def getProps(self, process, current): @classmethod def getByName(self, name): - if not name in self.mappings: + if name not in self.mappings: raise RuntimeError( "unknown mapping: `{0}', known mappings: `{1}'".format( name, list(self.mappings) @@ -1524,7 +1524,7 @@ def start(self, current, args=[], props={}, watchDog=None): ) try: self.waitForStart(current) - except: + except Exception: self.stop(current) raise @@ -1985,7 +1985,7 @@ def _startServerSide(self, current): try: self.startServerSide(current) return current.host - except: + except Exception: self._stopServerSide(current, False) raise finally: @@ -2013,7 +2013,7 @@ def _startServer(self, current, server): def _stopServer(self, current, server, success): try: server.stop(current, success) - except: + except Exception: success = False raise finally: @@ -2415,7 +2415,7 @@ def __init__(self, current): pass def start(self, process, current, args, props, envs, watchDog): - raise NotImplemented() + raise NotImplementedError() def destroy(self, driver): pass @@ -2612,7 +2612,7 @@ def getHost(self, current): def getController(self, current): ident = self.getControllerIdentity(current) - if type(ident) == str: + if isinstance(ident, str): ident = current.driver.getCommunicator().stringToIdentity(ident) import Ice @@ -2654,7 +2654,7 @@ def callback(future): try: future.result() with self.cond: - if not ident in self.processControllerProxies: + if ident not in self.processControllerProxies: self.processControllerProxies[proxy.ice_getIdentity()] = proxy self.cond.notifyAll() except Exception: @@ -2668,7 +2668,7 @@ def callback(future): proxy.ice_pingAsync().add_done_callback(callback) with self.cond: - if not ident in self.processControllerProxies: + if ident not in self.processControllerProxies: self.cond.wait(5) if ident in self.processControllerProxies: return self.processControllerProxies[ident] @@ -2811,7 +2811,7 @@ def startEmulator(self, avd): port = -1 out = run("adb devices -l") for p in range(5554, 5586, 2): - if not "emulator-{}".format(p) in out: + if "emulator-{}".format(p) not in out: port = p if port == -1: @@ -2865,7 +2865,7 @@ def startControllerApp(self, current, ident): print("creating virtual device ({0})... ".format(sdk)) try: run("avdmanager -v delete avd -n IceTests") # Delete the created device - except: + except Exception: pass # The SDK is downloaded by test VMs instead of here. # run("sdkmanager \"{0}\"".format(sdk), stdout=True, stdin="yes", stdinRepeat=True) # yes to accept licenses @@ -2879,7 +2879,7 @@ def startControllerApp(self, current, ident): # First try uninstall in case the controller was left behind from a previous run try: run("{} shell pm uninstall com.zeroc.testcontroller".format(self.adb())) - except: + except Exception: pass run( "{} install -t -r {}".format( @@ -2895,13 +2895,13 @@ def startControllerApp(self, current, ident): def stopControllerApp(self, ident): try: run("{} shell pm uninstall com.zeroc.testcontroller".format(self.adb())) - except: + except Exception: pass if self.avd: try: run("{} emu kill".format(self.adb())) - except: + except Exception: pass if self.avd == "IceTests": @@ -2909,7 +2909,7 @@ def stopControllerApp(self, ident): run( "avdmanager -v delete avd -n IceTests" ) # Delete the created device - except: + except Exception: pass # @@ -2919,7 +2919,7 @@ def stopControllerApp(self, ident): sys.stdout.write("Waiting for the emulator to shutdown..") sys.stdout.flush() while True: - if self.emulator.poll() != None: + if self.emulator.poll() is not None: print(" ok") break sys.stdout.write(".") @@ -2928,7 +2928,7 @@ def stopControllerApp(self, ident): try: run("adb kill-server") - except: + except Exception: pass @@ -2946,7 +2946,7 @@ def __init__(self, current): m = re.search("iOS .* \\(.*\\) - (.*)", r) if m: self.runtimeID = m.group(1) - except: + except Exception: pass if not self.runtimeID: self.runtimeID = ( @@ -3035,7 +3035,7 @@ def restartControllerApp(self, current, ident): def stopControllerApp(self, ident): try: run('xcrun simctl uninstall "{0}" {1}'.format(self.device, ident.name)) - except: + except Exception: pass def destroy(self, driver): @@ -3045,7 +3045,7 @@ def destroy(self, driver): sys.stdout.flush() try: run('xcrun simctl shutdown "{0}"'.format(self.simulatorID)) - except: + except Exception: pass print("ok") @@ -3054,7 +3054,7 @@ def destroy(self, driver): sys.stdout.flush() try: run('xcrun simctl delete "{0}"'.format(self.simulatorID)) - except: + except Exception: pass print("ok") @@ -3145,7 +3145,7 @@ def __init__(self, current): self.driver = webdriver.Safari(service=service) else: self.driver = getattr(webdriver, driver)() - except: + except Exception: self.destroy(current.driver) raise @@ -3212,7 +3212,7 @@ def getControllerIdentity(self, current): Test.Common.BrowserProcessControllerPrx.uncheckedCast(prx).redirect( url ) - except: + except Exception: pass finally: self.clearProcessController(prx, prx.ice_getCachedConnection()) @@ -3242,7 +3242,7 @@ def getController(self, current): # Print out the browser log try: print("browser log:\n{0}".format(self.driver.get_log("browser"))) - except: + except Exception: pass # Not all browsers support retrieving the browser console log raise ex @@ -3254,7 +3254,7 @@ def destroy(self, driver): try: self.driver.quit() - except: + except Exception: pass @@ -3315,8 +3315,8 @@ def createFile(self, path, lines, encoding=None): with open(path, "w", encoding=encoding) if encoding else open( path, "w" ) as file: - for l in lines: - file.write("%s\n" % l) + for line in lines: + file.write("%s\n" % line) self.files.append(path) def mkdirs(self, dirs): @@ -3441,12 +3441,14 @@ def __init__(self, options, component): if self.languages: self.languages = [ - i for sublist in [l.split(",") for l in self.languages] for i in sublist + i + for sublist in [lang.split(",") for lang in self.languages] + for i in sublist ] if self.rlanguages: self.rlanguages = [ i - for sublist in [l.split(",") for l in self.rlanguages] + for sublist in [lang.split(",") for lang in self.rlanguages] for i in sublist ] @@ -4109,7 +4111,7 @@ def getPythonVersion(self): 'import sys; print("{0}.{1}".format(sys.version_info[0], sys.version_info[1]))', ] ) - if type(version) != str: + if not isinstance(version, str): version = version.decode("utf-8") self.pythonVersion = tuple(int(num) for num in version.split(".")) return self.pythonVersion @@ -4583,7 +4585,7 @@ def getXcodeProject(self, current): # # Import component classes and instantiate the default component # -from Component import * +from Component import component # noqa: F401, E402 # # Initialize the platform with component @@ -4591,9 +4593,11 @@ def getXcodeProject(self, current): platform.init(component) # -# Import local driver +# Import local driver (this adds the local driver to the list of supported drivers) # -from LocalDriver import * +import LocalDriver # noqa: F401, E402 + +Driver.add("local", LocalDriver.LocalDriver) def runTestsWithPath(path): @@ -4686,6 +4690,6 @@ def usage(): finally: driver.destroy() - except Exception as e: + except Exception: print(sys.argv[0] + ": unexpected exception raised:\n" + traceback.format_exc()) sys.exit(1) diff --git a/scripts/icehashpassword.py b/scripts/icehashpassword.py index e7978fdaf28..afa2f2670f6 100755 --- a/scripts/icehashpassword.py +++ b/scripts/icehashpassword.py @@ -83,14 +83,14 @@ def main(): elif o in ("-s", "--salt"): try: salt = int(a) - except ValueError as err: + except ValueError: print("Invalid salt size. Value must be an integer") usage() return 2 elif o in ("-r", "--rounds"): try: rounds = int(a) - except ValueError as err: + except ValueError: print("Invalid number of rounds. Value must be an integer") usage() return 2