def build(self, package): self.clean_cache_dir(package) LOG.info("%s: Starting build process" % package.name) self._build_srpm(package) self._install_external_dependencies(package) cmd = (self.common_mock_args + " --rebuild %s --no-clean --resultdir=%s" % (self.build_dir + "/*.rpm", self.build_dir)) if package.rpmmacro: cmd = cmd + " --macro-file=%s" % package.rpmmacro LOG.info("%s: Building RPM" % package.name) try: utils.run_command(cmd) # On success save rpms and destroy build directory unless told # otherwise. except exception.SubprocessError: LOG.info("%s: Failed to build RPMs, build artifacts are kept at " "%s" % (package.name, self.build_dir)) raise msg = "%s: Success! RPMs built!" % (package.name) self._copy_rpms(self.build_dir, package.build_cache_dir) LOG.info(msg) if not CONF.get('build_packages').get('keep_build_dir'): self._destroy_build_directory()
def build(self, package): print("%s: Starting build process" % package.name) self._prepare(package) self._build_srpm(package) self._install_external_dependencies(package) cmd = "mock -r %s --rebuild %s --no-clean --resultdir=%s" % ( self.mock_config, self.build_dir + "/*.rpm", self.build_dir) if package.rpmmacro: cmd = cmd + " --macro-file=%s" % package.rpmmacro print("%s: Building RPM" % package.name) try: utils.run_command(cmd) # On success save rpms and destroy build directory unless told # otherwise. except exception.SubprocessError: print("%s: Failed to build RPMs, build artifacts are kept at " "%s" % (package.name, self.build_dir)) raise msg = "%s: Success! RPMs built!" % (package.name) self._save_rpm(package) print(msg) LOG.info(msg) if (CONF.get('keep_builddir', None) or not CONF.get('keep_builddir')): self._destroy_build_directory()
def _build_srpm(self, package): LOG.info("%s: Building SRPM" % package.name) cmd = ( self.common_mock_args + " --buildsrpm --no-clean --spec %s --sources %s --resultdir=%s" % (package.spec_file.path, self.archive, self.build_dir)) utils.run_command(cmd)
def create_repository(dir_path): """ Create yum repository in a directory containing RPM packages. Args: directory (str): path to directory containing RPM packages """ utils.run_command("createrepo %s" % dir_path)
def initialize(self): """ Initializes the configured chroot by installing the essential packages. This setup is common for all packages that are built and needs to be done only once. """ cmd = self.common_mock_args + " --init" utils.run_command(cmd)
def create_repository(dir_path): """ Create yum repository in a directory containing RPM packages. Args: directory (str): path to directory containing RPM packages """ utils.run_command("createrepo %s" % dir_path)
def _refresh_stats(self): self.tmpfile = "/tmp/zems-dhcpd-unparsed" command = "%s -c %s -l %s -f c -o %s" % ( self.config.get("dhcpd_pools_command", "/usr/bin/dhcpd-pools"), self.config.get("dhcpd_config_file", "/etc/dhcpd/dhcpd.conf"), self.config.get("dhcpd_leases_file", "/var/lib/dhcpd/dhcpd.leases"), self.tmpfile) run_command(command)
def _build_srpm(self, package): print("%s: Building SRPM" % package.name) cmd = ("mock -r %s --buildsrpm --no-clean --spec %s --source %s " "--resultdir=%s" % (self.mock_config, package.specfile, self.archive, self.build_dir)) utils.run_command(cmd)
def bump_release(self, change_log_lines, user_name, user_email): comment = "\n".join(['- ' + l for l in change_log_lines]) user_string = "%(user_name)s <%(user_email)s>" % locals() cmd = "rpmdev-bumpspec -c '%s' -u '%s' %s" % (comment, user_string, self.path) utils.run_command(cmd) # Flush the cache to force re-reading the file self._content = None
def bump_release(self, change_log_lines, user_name, user_email): comment = "\n".join(['- ' + l for l in change_log_lines]) user_string = "%(user_name)s <%(user_email)s>" % locals() cmd = "rpmdev-bumpspec -c '%s' -u '%s' %s" % ( comment, user_string, self.path) utils.run_command(cmd) # Flush the cache to force re-reading the file self._content = None
def _download_source(self, build_dir): """ An alternative to just execute a given command to obtain sources. """ utils.run_command(self.download_source, cwd=build_dir) # automatically append tar.gz if expects_source has no extension if self.expects_source == self.name: self.expects_source = "%s.tar.gz" % self.name return os.path.join(build_dir, self.expects_source)
def _download_source(self, build_dir): """ An alternative to just execute a given command to obtain sources. """ utils.run_command(self.download_source, cwd=build_dir) # automatically append tar.gz if expects_source has no extension if self.expects_source == self.name: self.expects_source = "%s.tar.gz" % self.name return os.path.join(build_dir, self.expects_source)
def _svn_archive(source, directory): archive_name = source['svn']['archive'] archive_file = os.path.join(directory, archive_name + ".tar.gz") cmd = "tar --transform 's,^\.,{0},' -cvzf {1} . --exclude='*/.svn'".format( archive_name, archive_file) utils.run_command(cmd, cwd=source['svn']['dest']) source['svn']['archive'] = archive_file return source
def _install_external_dependencies(self, package): if package.build_dependencies: cmd = self.common_mock_args install = " --install" for dep in package.build_dependencies: install = " ".join([install, " ".join(dep.cached_build_results)]) cmd = cmd + install LOG.info("%s: Installing dependencies on chroot" % package.name) utils.run_command(cmd)
def _prepare_chroot(self, package): cmd = "mock --init -r %s " % self.mock_config if package.build_files: files = [] for f in os.listdir(package.build_files): files.append(os.path.join(package.build_files, f)) cmd = cmd + " --copyin %s %s" % (" ".join(files), MOCK_CHROOT_BUILD_DIR) utils.run_command(cmd)
def _install_external_dependencies(self, package): cmd = "mock -r %s " % self.mock_config if package.build_dependencies or package.dependencies: install = " --install" for dep in package.build_dependencies: install = " ".join([install, " ".join(dep.result_packages)]) for dep in package.dependencies: install = " ".join([install, " ".join(dep.result_packages)]) cmd = cmd + install print("%s: Installing dependencies on chroot" % package.name) utils.run_command(cmd)
def _run_mock_command(self, cmd): distro = distro_utils.get_distro( self.common_config.get('distro_name'), self.common_config.get('distro_version'), self.common_config.get('arch_and_endianness')) mock_config_file = self.config.get('mock_config').get( distro.lsb_name).get(distro.version) try: utils.run_command( "%s -r %s %s %s" % (self.mock_binary, mock_config_file, self.mock_args, cmd)) except exception.SubprocessError: LOG.error("Failed to build ISO") raise
def _hg_archive(source, directory): """ Creates a tar.gz archive for mercurial [source] an places it in [directory]. Returns [source] with updated [archive] pointing to the created file. """ archive_name = source['hg']['archive'] archive_file = os.path.join(directory, archive_name + ".tar.gz") cmd = 'hg archive -t tgz "{}"'.format(archive_file) utils.run_command(cmd, cwd=source['hg']['dest']) source['hg']['archive'] = archive_file return source
def query_tag(self, tag_name, extra_args=""): """ Queries the spec file for a tag's value. Cached content not yet written to the file is not considered. Args: tag_name (str): name of the tag to query extra_args (str): extra arguments to append to the command Returns: str: tag value """ if tag_name in self._cached_tags: tag_value = self._cached_tags[tag_name] else: command = "rpmspec --srpm -q --qf '%%{%s}' %s %s 2>/dev/null" % ( tag_name.upper(), self.path, extra_args) tag_value = utils.run_command(command).strip() if tag_value == "(none)": tag_value = None if not extra_args: # Extra arguments may define macros that alter the tags self._cached_tags[tag_name] = tag_value return tag_value
def create_repository(self): """ Create yum repository in build results directory. """ result_dir = CONF.get('common').get('result_dir') build_results_dir = os.path.join( result_dir, 'packages', self.timestamp) utils.run_command("createrepo %s" % build_results_dir) repo_config_dir = os.path.join(result_dir, "repository_config") utils.create_directory(repo_config_dir) repo_config_path = os.path.join( repo_config_dir, self.timestamp + ".repo") with open(repo_config_path, "w") as repo_config: repo_config.write(YUM_REPO_CONFIG_TEMPLATE.format( timestamp=self.timestamp, repo_path=os.path.abspath(build_results_dir)))
def validate_yaml(yaml_file_path): """ Validate yaml file Args: yaml_file_path (str): yaml file path Returns: bool: if YAML file is valid """ try: run_command("yamllint %s" % yaml_file_path) except exception.SubprocessError as e: #pylint: disable=no-member print("validation of yaml file %s failed, output: %s" % (yaml_file_path, e.stdout)) return False return True
def validate_rpm_spec(spec_file_path): """ Validate RPM specification file Args: spec_file_path (str): RPM specification file path Returns: bool: if RPM specification file is valid """ try: LOG.info("Verifying file %s" % spec_file_path) run_command("rpmlint -f .rpmlint -v %s" % spec_file_path) except exception.SubprocessError as e: #pylint: disable=no-member LOG.exception("validation of RPM specification file %s failed, output: %s" % (spec_file_path, e.stdout)) return False return True
def validate_rpm_spec(spec_file_path): """ Validate RPM specification file Args: spec_file_path (str): RPM specification file path Returns: bool: if RPM specification file is valid """ try: run_command("rpmlint -f .rpmlint -v %s" % spec_file_path) except exception.SubprocessError as e: #pylint: disable=no-member print("validation of RPM specification file %s failed, output: %s" % (spec_file_path, e.stdout)) return False return True
def validate_yaml(yaml_file_path): """ Validate yaml file Args: yaml_file_path (str): yaml file path Returns: bool: if YAML file is valid """ try: LOG.info("Verifying file %s" % yaml_file_path) run_command("yamllint %s" % yaml_file_path) except exception.SubprocessError as e: #pylint: disable=no-member LOG.exception("validation of yaml file %s failed, output: %s" % (yaml_file_path, e.stdout)) return False return True
def run_command(self, cmd): """ Run mock command using arguments passed to the constructor. Args: cmd (str): mock command to execute Returns: mock command standard output """ cmd = " ".join(self.common_mock_args + [cmd]) return utils.run_command(cmd)
def query_tag(self, tag): """ Queries the spec file for a tag's value. Cached content not yet written to the file is not considered. """ if tag not in self._cached_tags: tag = utils.run_command( "rpmspec --srpm -q --qf '%%{%s}' %s 2>/dev/null" % (tag.upper(), self.path)).strip() if tag == "(none)": tag = None self._cached_tags[tag] = tag return self._cached_tags[tag]
def query_tag(self, tag_name, extra_args="", unexpanded_macros=[]): """ Queries the spec file for a tag's value. Cached content not yet written to the file is not considered. Args: tag_name (str): name of the tag to query extra_args (str): extra arguments to append to the command unexpanded_macros ([str]): macros to be returned without expansion in the query result. Note: --define doesn't override macros defined in the spec file so, for these macros, expansion will happen reagarless of this parameter's value Returns: str: tag value """ if tag_name in self._cached_tags: tag_value = self._cached_tags[tag_name] else: # if a macro happens to not be defined, we can't find its # location in the string after expansion, so we define a # dummy value here that can be searched for later dummy_macros = {m : "dummy_" + m for m in unexpanded_macros} extra_args += get_define_line(dummy_macros) command = "rpmspec --srpm -q --qf '%%{%s}' %s %s 2>/dev/null" % ( tag_name.upper(), self.path, extra_args) tag_value = utils.run_command(command).strip() for macro in unexpanded_macros: tag_value = tag_value.replace('dummy_%s' % macro, '%%{%s}' % macro) if tag_value == "(none)": tag_value = None if not extra_args: # Extra arguments may define macros that alter the tags self._cached_tags[tag_name] = tag_value return tag_value
def _clone_repository(timeout): cmd = command.format(timeout) utils.run_command(cmd)
def clean(self): utils.run_command(self.common_mock_args + " --clean")
def clean(self): utils.run_command("mock --clean")