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

A template engine for skeletons that enables multiple styles. #3176

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 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
3 changes: 3 additions & 0 deletions conda_build/cli/main_skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import conda_build.api as api
from conda_build.config import Config
from conda_build.skeletons import STYLES

thisdir = os.path.dirname(os.path.abspath(__file__))
logging.basicConfig(level=logging.INFO)
Expand All @@ -32,6 +33,8 @@ def parse_args(args):
options available.
""",
)
p.add_argument("--style", default="jinja", choices=STYLES,
help="Desired skeleton style.")

repos = p.add_subparsers(
dest="repo"
Expand Down
22 changes: 22 additions & 0 deletions conda_build/skeletons/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os

from jinja2 import Environment, FileSystemLoader, select_autoescape

TEMPLATES_DIR = os.path.join(os.path.dirname(__file__), "templates")
STYLES = os.listdir(os.path.join(TEMPLATES_DIR, "styles"))


TEMPLATES = Environment(
loader=FileSystemLoader(TEMPLATES_DIR),
autoescape=select_autoescape(['yaml']),
trim_blocks=True,
lstrip_blocks=True
)


def get_template(style, source):
if style is None:
style = "jinja"
return TEMPLATES.get_template(os.path.join('styles',
style,
'{}.yaml'.format(source)))
84 changes: 24 additions & 60 deletions conda_build/skeletons/cpan.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,51 +25,10 @@
from conda_build.config import get_or_merge_config
from conda_build.utils import on_win, check_call_env
from conda_build.variants import get_default_variant
from conda_build.skeletons import get_template

import requests

CPAN_META = """\
{{% set name = "{packagename}" %}}
{{% set version = "{version}" %}}
{{% set sha256 = "{sha256}" %}}

package:
name: {{{{ name }}}}
version: {{{{ version }}}}

{source_comment}source:
{useurl}fn: {filename}
{useurl}url: {cpanurl}
{usesha256}sha256: {{{{ sha256 }}}}

# If this is a new build for the same version, increment the build
# number. If you do not include this key, it defaults to 0.
build:
number: 0

requirements:
build:
- perl{build_depends}

run:
- perl{run_depends}

{import_comment}test:
# Perl 'use' tests
{import_comment}imports:{import_tests}

# You can also put a file called run_test.pl (or run_test.py) in the recipe
# that will be run at test time.

about:
home: {homeurl}
license: {license}
summary: {summary}

# See
# http://docs.continuum.io/conda/build.html for
# more information about meta.yaml
"""

CPAN_BUILD_SH = """\
#!/bin/bash
Expand Down Expand Up @@ -218,7 +177,8 @@ def package_exists(package_name):
# meta_cpan_url="http://api.metacpan.org",
def skeletonize(packages, output_dir=".", version=None,
meta_cpan_url="http://fastapi.metacpan.org/v1",
recursive=False, force=False, config=None, write_core=False):
recursive=False, force=False, config=None, write_core=False,
style=None):
'''
Loops over packages, outputting conda recipes converted from CPAN metata.
'''
Expand All @@ -230,8 +190,6 @@ def skeletonize(packages, output_dir=".", version=None,
# wildcards are not valid for perl
perl_version = perl_version.replace(".*", "")
package_dicts = {}
indent = '\n - '
indent_core = '\n #- '
processed_packages = set()
orig_version = version
while packages:
Expand Down Expand Up @@ -277,15 +235,16 @@ def skeletonize(packages, output_dir=".", version=None,
continue

d = package_dicts.setdefault(package, {'packagename': packagename,
'run_depends': '',
'build_depends': '',
'run_depends': [],
'host_depends': [],
'build_comment': '# ',
'build_number': 0,
'test_commands': '',
'usesha256': '',
'useurl': '',
'source_comment': '',
'summary': "''",
'import_tests': ''})
'import_tests': []})

# Fetch all metadata from CPAN
if version is None:
Expand Down Expand Up @@ -321,13 +280,11 @@ def skeletonize(packages, output_dir=".", version=None,

# Get which deps are in perl_core

d['build_depends'] += indent.join([''] + list(build_deps |
run_deps))
d['build_depends'] += indent_core.join([''] + list(build_core_deps |
run_core_deps))
d['host_depends'] += ['perl'] + list(build_deps | run_deps)
d['host_depends'] += list(build_core_deps | run_core_deps)

d['run_depends'] += indent.join([''] + list(run_deps))
d['run_depends'] += indent_core.join([''] + list(run_core_deps))
d['run_depends'] += ['perl'] + list(run_deps)
d['run_depends'] += list(run_core_deps)
# Make sure we append any packages before continuing
packages.extend(packages_to_append)
empty_recipe = False
Expand Down Expand Up @@ -361,9 +318,9 @@ def skeletonize(packages, output_dir=".", version=None,
d['source_comment'] = '#'

try:
d['homeurl'] = release_data['resources']['homepage']
d['home'] = release_data['resources']['homepage']
except KeyError:
d['homeurl'] = 'http://metacpan.org/pod/' + package
d['home'] = 'http://metacpan.org/pod/' + package
if 'abstract' in release_data:
# TODO this does not escape quotes in a YAML friendly manner
summary = repr(release_data['abstract']).lstrip('u')
Expand All @@ -386,7 +343,7 @@ def skeletonize(packages, output_dir=".", version=None,
# Filter out weird modules that don't belong
if (provided_mod.startswith(module_prefix) and
'::_' not in provided_mod):
d['import_tests'] += indent + provided_mod
d['import_tests'].append(provided_mod)
if d['import_tests']:
d['import_comment'] = ''
else:
Expand All @@ -398,8 +355,15 @@ def skeletonize(packages, output_dir=".", version=None,
# Write recipe files to a directory
# TODO def write_recipe
print("Writing recipe for %s-%s" % (packagename, d['version']))

# obtain template according to given style and render recipe
template = get_template(style, 'cpan')
rendered_recipe = template.render(**d)

# write meta.yaml
with open(join(dir_path, 'meta.yaml'), 'w') as f:
f.write(CPAN_META.format(**d))
f.write(rendered_recipe)

with open(join(dir_path, 'build.sh'), 'w') as f:
if empty_recipe:
f.write('#!/bin/bash\necho "Nothing to do."\n')
Expand Down Expand Up @@ -649,9 +613,9 @@ def core_module_dict(cpan_url, module):
mod_dict = get_cpan_api_url(
'{0}/module/{1}'.format(cpan_url, module), colons=True)
# If there was an error, report it
except CondaHTTPError:
except CondaHTTPError as e:
sys.exit(('Error: Could not find module or distribution named'
' %s on MetaCPAN. Error was: %s') % (module))
' %s on MetaCPAN. Error was: %s') % (module, e))
else:
mod_dict = {}
mod_dict['distribution'] = 'perl'
Expand Down
Loading