def _get_build_results(self, instance_key, job_dir):
        job_name = os.path.basename(job_dir)
        try:
            dirs = glob(os.path.join(job_dir, 'builds', '*_*'))
            # Before Jenkins v1.597 the build folders were named with a timestamp (eg: 2015-03-10_19-59-29)
            # Starting from Jenkins v1.597 they are named after the build ID (1, 2, 3...)
            # So we need try both format when trying to find the latest build and parsing build.xml
            if len(dirs) == 0:
                dirs = glob(os.path.join(job_dir, 'builds', '[0-9]*'))
            if len(dirs) > 0:
                # versions of Jenkins > 1.597 need to be sorted by build number (integer)
                try:
                    dirs = sorted(dirs, key=lambda x: int(x.split('/')[-1]), reverse=True)
                except __HOLE__:
                    dirs = sorted(dirs, reverse=True)
                # We try to get the last valid build
                for dir_name in dirs:
                    watermark = self.high_watermarks[instance_key][job_name]
                    try:
                        build_metadata = self._get_build_metadata(dir_name, watermark)
                    except Exception:
                        build_metadata = None
                    if build_metadata is not None:
                        build_result = build_metadata.get('result')
                        if build_result is None:
                            break

                        output = {
                            'job_name':     job_name,
                            'event_type':   'build result'
                        }

                        output.update(build_metadata)
                        if 'number' not in output:
                            output['number'] = dir_name.split('/')[-1]
                        self.high_watermarks[instance_key][job_name] = output.get('timestamp')
                        self.log.debug("Processing %s results '%s'" % (job_name, output))
                        yield output

                    # If it not a new build, stop here
                    else:
                        break
        except Exception, e:
            self.log.error("Error while working on job %s, exception: %s" % (job_name, e))
dataset/ETHPy150Open serverdensity/sd-agent/checks.d/jenkins.py/Jenkins._get_build_results