Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable working with Jinja2 spec templates #844

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions autospec/autospec.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,13 @@ def package(args, url, name, archives, workingdir):
pkg_integrity.check(url, conf, interactive=interactive_mode)
pkg_integrity.load_specfile(specfile)

specfile.write_spec()
spec_type = specfile.write_spec()

while 1:
package.package(filemanager, args.mock_config, args.mock_opts, conf, requirements, content, args.cleanup)
if spec_type == "template":
# specfile template is assumed "correct" and any failures need to be manually addressed
break
filemanager.load_specfile(specfile)
specfile.write_spec()
filemanager.newfiles_printed = 0
Expand Down Expand Up @@ -304,7 +308,8 @@ def package(args, url, name, archives, workingdir):
except Exception:
pass

check.check_regression(conf.download_path, conf.config_opts['skip_tests'], package.round - 1)
if spec_type == "generate":
check.check_regression(conf.download_path, conf.config_opts['skip_tests'], package.round - 1)

examine_abi(conf.download_path, content.name)
if os.path.exists("/var/lib/rpm"):
Expand Down
43 changes: 37 additions & 6 deletions autospec/specfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from collections import OrderedDict

import git
from jinja2 import Environment
from jinja2.loaders import DictLoader
from util import _file_write, open_auto

AVX2_CFLAGS = "-march=x86-64-v3"
Expand Down Expand Up @@ -73,12 +75,42 @@ def __init__(self, url, version, name, release, config, requirements, content):

def write_spec(self):
"""Write spec file."""
self.specfile = open_auto("{}/{}.spec".format(self.config.download_path, self.name), "w")
spec_path = f"{os.path.join(self.config.download_path, self.name)}.spec"
self.specfile = open_auto(spec_path, "w")
self.specfile.write_strip = types.MethodType(_file_write, self.specfile)

# last chance to sanitize url for template and build types
if self.config.urlban:
clean_url = re.sub(self.config.urlban, "localhost", self.url)
# Duplicate prefixes entry before we change the url
self.content.prefixes[clean_url] = self.content.prefixes.get(self.url)
self.url = clean_url

template_path = f"{spec_path}.template"

if os.path.isfile(template_path):
# make templates have a template build pattern
self.config.default_pattern = "template"
# spec file comment header
self.write_comment_header()

if os.path.isfile(template_path):
with open_auto(template_path) as tfile:
template_content = tfile.read()
template = Environment(loader=DictLoader({'spec': template_content})).get_template('spec')
kw = {
'package_name': self.name,
'package_version': self.version,
'package_url': self.url,
'package_release': self.release,
}
self.specfile.write(template.render(**kw))
self.specfile.write_strip('\n')
self.specfile.close()
# return specfile type built so autospec knows how to
# handle build results (template should only builds once)
return "template"

if self.config.config_opts.get('keepstatic'):
self._write("%define keepstatic 1\n")

Expand Down Expand Up @@ -110,6 +142,10 @@ def write_spec(self):

self.specfile.close()

# return specfile type built so autospec knows how to
# handle build results (generate has multiple builds)
return "generate"

def write_comment_header(self):
"""Write comment header to spec file."""
self._write("#\n")
Expand All @@ -132,11 +168,6 @@ def write_comment_header(self):

def write_nvr(self):
"""Write name, version, and release information."""
if self.config.urlban:
clean_url = re.sub(self.config.urlban, "localhost", self.url)
# Duplicate prefixes entry before we change the url
self.content.prefixes[clean_url] = self.content.prefixes.get(self.url)
self.url = clean_url
self._write("Name : {}\n".format(self.name))
self._write("Version : {}\n".format(self.version))
self._write("Release : {}\n".format(str(self.release)))
Expand Down
15 changes: 1 addition & 14 deletions tests/test_specfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_write_comment_header(self):
self.WRITES = self.WRITES[:4] + self.WRITES[6:]
self.assertEqual(expect, self.WRITES)

def test_write_nvr_no_urlban(self):
def test_write_nvr(self):
"""
test Specfile.write_nvr with no urlban set
"""
Expand All @@ -59,19 +59,6 @@ def test_write_nvr_no_urlban(self):
"Source0 : http://www.testpkg.com/testpkg/pkg-1.0.tar.gz\n"]
self.assertEqual(expect, self.WRITES)

def test_write_nvr_urlban(self):
"""
test Specfile.write_nvr with urlban set
"""
self.specfile.config.urlban = "www.testpkg.com"
self.specfile.write_nvr()
expect = ["Name : pkg\n",
"Version : 1.0\n",
"Release : 2\n",
"URL : http://localhost/testpkg/pkg-1.0.tar.gz\n",
"Source0 : http://localhost/testpkg/pkg-1.0.tar.gz\n"]
self.assertEqual(expect, self.WRITES)

def test_write_sources(self):
"""
test write_sources with all Specfile.sources set.
Expand Down
Loading