diff -r a00cd6b55728 -r 31a7decd3393 eric7/PipInterface/Pip.py --- a/eric7/PipInterface/Pip.py Fri Mar 25 19:16:09 2022 +0100 +++ b/eric7/PipInterface/Pip.py Sun Mar 27 19:56:41 2022 +0200 @@ -970,3 +970,64 @@ dependencies = json.loads(output) return dependencies + + ####################################################################### + ## License handling methods below + ####################################################################### + + def getLicenses(self, envName, summary=False): + """ + Public method to get the licenses per package for a given environment. + + @param envName name of the environment to get the licenses for + @type str + @param summary flag indicating to get a summary listing (defaults to + False) + @type bool (optional) + @return list of dictionaries containing the license and version per + package + @rtype dict + """ + licenses = [] + + if envName: + interpreter = self.getVirtualenvInterpreter(envName) + if interpreter: + from . import piplicenses + with open(piplicenses.__file__, "r") as f: + content = f.read() + args = [ + "-c", + content, + "--format", + "json", + "--from", + "mixed", + "--with-system", + ] + if summary: + args.append("--summary") + + proc = QProcess() + proc.start(interpreter, args) + if proc.waitForStarted(15000) and proc.waitForFinished(30000): + output = str(proc.readAllStandardOutput(), + Preferences.getSystem("IOEncoding"), + 'replace').strip() + with contextlib.suppress(json.JSONDecodeError): + licenses = json.loads(output) + + return licenses + + def getLicensesSummary(self, envName): + """ + Public method to get a summary of licenses found in a given + environment. + + @param envName name of the environment to get the licenses summary for + @type str + @return list of dictionaries containing the license and the count of + packages + @rtype dict + """ + return self.getLicenses(envName, summary=True)