From c50c68fef179d9306f1a3432f48985bf20555e38 Mon Sep 17 00:00:00 2001 From: Jonathan Abrahams Date: Tue, 27 Mar 2018 14:30:46 -0400 Subject: SERVER-23312 Python linting - Lint using pylint, pydocstyle & mypy --- buildscripts/mongosymb.py | 65 ++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 26 deletions(-) (limited to 'buildscripts/mongosymb.py') diff --git a/buildscripts/mongosymb.py b/buildscripts/mongosymb.py index a38c395978d..bf11ff872c5 100755 --- a/buildscripts/mongosymb.py +++ b/buildscripts/mongosymb.py @@ -24,9 +24,9 @@ import subprocess import sys -def symbolize_frames(trace_doc, dbg_path_resolver, symbolizer_path=None, dsym_hint=None): - """Given a trace_doc in MongoDB stack dump format, returns a list of symbolized stack frames. - """ +def symbolize_frames( # pylint: disable=too-many-locals + trace_doc, dbg_path_resolver, symbolizer_path=None, dsym_hint=None): + """Return a list of symbolized stack frames from a trace_doc in MongoDB stack dump format.""" if symbolizer_path is None: symbolizer_path = os.environ.get("MONGOSYMB_SYMBOLIZER_PATH", "llvm-symbolizer") @@ -34,8 +34,9 @@ def symbolize_frames(trace_doc, dbg_path_resolver, symbolizer_path=None, dsym_hi dsym_hint = [] def make_base_addr_map(somap_list): - """Makes a map from binary load address to description of library from the somap, which is - a list of dictionaries describing individual loaded libraries. + """Return map from binary load address to description of library from the somap_list. + + The somap_list is a list of dictionaries describing individual loaded libraries. """ return {so_entry["b"]: so_entry for so_entry in somap_list if so_entry.has_key("b")} @@ -71,9 +72,9 @@ def symbolize_frames(trace_doc, dbg_path_resolver, symbolizer_path=None, dsym_hi stderr=open("/dev/null")) def extract_symbols(stdin): - """Extracts symbol information from the output of llvm-symbolizer. + """Extract symbol information from the output of llvm-symbolizer. - Returns a list of dictionaries, each of which has fn, file, column and line entries. + Return a list of dictionaries, each of which has fn, file, column and line entries. The format of llvm-symbolizer output is that for every CODE line of input, it outputs zero or more pairs of lines, and then a blank line. This way, if @@ -109,47 +110,58 @@ def symbolize_frames(trace_doc, dbg_path_resolver, symbolizer_path=None, dsym_hi return frames -class path_dbg_file_resolver(object): +class PathDbgFileResolver(object): + """PathDbgFileResolver class.""" + def __init__(self, bin_path_guess): + """Initialize PathDbgFileResolver.""" self._bin_path_guess = bin_path_guess def get_dbg_file(self, soinfo): + """Return dbg file name.""" return soinfo.get("path", self._bin_path_guess) -class s3_buildid_dbg_file_resolver(object): +class S3BuildidDbgFileResolver(object): + """S3BuildidDbgFileResolver class.""" + def __init__(self, cache_dir, s3_bucket): + """Initialize S3BuildidDbgFileResolver.""" self._cache_dir = cache_dir self._s3_bucket = s3_bucket def get_dbg_file(self, soinfo): - buildId = soinfo.get("buildId", None) - if buildId is None: + """Return dbg file name.""" + build_id = soinfo.get("buildId", None) + if build_id is None: return None - buildId = buildId.lower() - buildIdPath = os.path.join(self._cache_dir, buildId + ".debug") - if not os.path.exists(buildIdPath): + build_id = build_id.lower() + build_id_path = os.path.join(self._cache_dir, build_id + ".debug") + if not os.path.exists(build_id_path): try: - self._get_from_s3(buildId) - except: + self._get_from_s3(build_id) + except Exception: # pylint: disable=broad-except ex = sys.exc_info()[0] - sys.stderr.write("Failed to find debug symbols for %s in s3: %s\n" % (buildId, ex)) + sys.stderr.write("Failed to find debug symbols for %s in s3: %s\n" % (build_id, ex)) return None - if not os.path.exists(buildIdPath): + if not os.path.exists(build_id_path): return None - return buildIdPath + return build_id_path - def _get_from_s3(self, buildId): + def _get_from_s3(self, build_id): + """Download debug symbols from S3.""" subprocess.check_call( - ['wget', 'https://s3.amazonaws.com/%s/%s.debug.gz' % - (self._s3_bucket, buildId)], cwd=self._cache_dir) - subprocess.check_call(['gunzip', buildId + ".debug.gz"], cwd=self._cache_dir) + ['wget', + 'https://s3.amazonaws.com/%s/%s.debug.gz' % + (self._s3_bucket, build_id)], cwd=self._cache_dir) + subprocess.check_call(['gunzip', build_id + ".debug.gz"], cwd=self._cache_dir) -def classic_output(frames, outfile, **kwargs): +def classic_output(frames, outfile, **kwargs): # pylint: disable=unused-argument + """Provide classic output.""" for frame in frames: symbinfo = frame["symbinfo"] - if len(symbinfo) > 0: + if symbinfo: for sframe in symbinfo: outfile.write(" %(file)s:%(line)s:%(column)s: %(fn)s\n" % sframe) else: @@ -157,13 +169,14 @@ def classic_output(frames, outfile, **kwargs): def main(argv): + """Execute Main program.""" parser = optparse.OptionParser() parser.add_option("--dsym-hint", action="append", dest="dsym_hint") parser.add_option("--symbolizer-path", dest="symbolizer_path", default=None) parser.add_option("--debug-file-resolver", dest="debug_file_resolver", default="path") parser.add_option("--output-format", dest="output_format", default="classic") (options, args) = parser.parse_args(argv) - resolver_constructor = dict(path=path_dbg_file_resolver, s3=s3_buildid_dbg_file_resolver).get( + resolver_constructor = dict(path=PathDbgFileResolver, s3=S3BuildidDbgFileResolver).get( options.debug_file_resolver, None) if resolver_constructor is None: sys.stderr.write("Invalid debug-file-resolver argument: %s\n" % options.debug_file_resolver) -- cgit v1.2.1