def build_binary(self):
        def is_src_rpm(path):
            if not path:
                return False
            if not sh.isfile(path):
                return False
            if not path.lower().endswith('.src.rpm'):
                return False
            return True

        def list_src_rpms(path):
            path_files = []
            restricted = set()
            if sh.isdir(path):
                path_files = sh.listdir(path, filter_func=is_src_rpm)
            try:
                # Leave other groups files alone...
                restricted = set(_get_lines(self.generated_srpms_filename))
            except __HOLE__ as e:
                if e.errno != errno.ENOENT:
                    raise
            filtered = []
            for path in path_files:
                if path in restricted:
                    filtered.append(path)
            path_files = filtered
            return sorted(path_files)

        def move_rpms(repo_name):
            repo_dir = sh.joinpths(self.anvil_repo_dir, repo_name)
            search_dirs = [
                sh.joinpths(self.rpmbuild_dir, "RPMS"),
            ]
            for sub_dir in sh.listdir(self.rpmbuild_dir, dirs_only=True):
                search_dirs.append(sh.joinpths(sub_dir, "RPMS"))
            moved = []
            for source_dir in search_dirs:
                moved.extend(self._move_rpm_files(source_dir, repo_dir))
            return moved

        def build(repo_dir, repo_name, header_tpl, group, built_files):
            repo_files = []
            for srpm in list_src_rpms(repo_dir):
                if srpm not in built_files:
                    repo_files.append(srpm)
            if not repo_files:
                return []
            utils.log_iterable(repo_files,
                               header=header_tpl % (len(repo_files),
                                                    self.SRC_REPOS[repo_name],
                                                    self.jobs),
                               logger=LOG)
            rpmbuild_flags = "--rebuild"
            if self.opts.get("usr_only", False):
                rpmbuild_flags += " --define 'usr_only 1'"
            if self.opts.get("overwrite_configs", False):
                rpmbuild_flags += " --define 'overwrite_configs 1'"
            with sh.remove_before(self.rpmbuild_dir):
                self._create_rpmbuild_subdirs()
                # This is needed so that make correctly identifies the right
                # files and the right *.mark files and so-on; instead of
                # grabbing all the files (including ones we don't want to
                # build just yet...)
                files_dirname = '%s-%s-build' % (repo_name, group)
                files_dir = sh.joinpths(self.deps_dir, files_dirname)
                sh.mkdirslist(files_dir)
                for srpm in repo_files:
                    sh.copy(srpm, sh.joinpths(files_dir, sh.basename(srpm)))
                try:
                    self.py2rpm_helper.build_all_binaries(repo_name,
                                                          files_dir,
                                                          rpmbuild_flags,
                                                          self.tracewriter,
                                                          self.jobs)
                finally:
                    # If we made any rpms (even if a failure happened, make
                    # sure that we move them to the right target repo).
                    moved_rpms = move_rpms(repo_name)
                    if len(moved_rpms) > 0:
                        self._create_repo(repo_name)
            return repo_files

        def pre_build():
            build_requirements = self.requirements.get("build-requires")
            if build_requirements:
                utils.log_iterable(build_requirements,
                                   header="Installing build requirements",
                                   logger=LOG)
                self.helper.transaction(install_pkgs=build_requirements,
                                        tracewriter=self.tracewriter)
            build_requirements = []
            try:
                build_requirements.extend(_get_lines(self.rpm_build_requires_filename))
            except IOError as e:
                if e.errno != errno.ENOENT:
                    raise
            built_files = []
            built_requirements = []
            for repo_name in self.REPOS:
                repo_dir = sh.joinpths(self.anvil_repo_dir, self.SRC_REPOS[repo_name])
                matched_paths = []
                available_paths = list_src_rpms(repo_dir)
                envra_path_details = self.envra_helper.explode(*available_paths)
                for (path, envra_detail) in zip(available_paths, envra_path_details):
                    package_name = envra_detail.get('name')
                    if package_name in build_requirements:
                        matched_paths.append(path)
                        built_requirements.append(package_name)
                if matched_paths:
                    with sh.remove_before(self.prebuild_dir) as prebuild_dir:
                        sh.mkdirslist(prebuild_dir, tracewriter=self.tracewriter)
                        for path in matched_paths:
                            sh.copy(path,
                                    sh.joinpths(prebuild_dir, sh.basename(path)))
                        built_files.extend(
                            build(prebuild_dir, repo_name,
                                  'Prebuilding %s RPM packages from their'
                                  ' SRPMs for repo %s using %s jobs',
                                  "%s-prebuild" % self.group, built_files))
            leftover_requirements = set()
            for req in build_requirements:
                if req not in built_requirements:
                    leftover_requirements.add(req)
            return (leftover_requirements, built_files)

        leftover_requirements, built_files = pre_build()
        if leftover_requirements:
            utils.log_iterable(sorted(leftover_requirements),
                               header="%s unsatisfied build requirements (these"
                                      " will need to be satisfied by existing"
                                      " repositories)" % len(leftover_requirements),
                               logger=LOG)
        for repo_name in self.REPOS:
            repo_dir = sh.joinpths(self.anvil_repo_dir, self.SRC_REPOS[repo_name])
            built_files.extend(
                build(repo_dir, repo_name,
                      'Building %s RPM packages from their SRPMs for repo %s'
                      ' using %s jobs', self.group, built_files))
dataset/ETHPy150Open openstack/anvil/anvil/packaging/yum.py/YumDependencyHandler.build_binary