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

refactor: use pathlib #1948

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
30 changes: 13 additions & 17 deletions conda_smithy/ci_skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
added to conda-forge's queue.
"""

import os
from pathlib import Path
import sys

from .configure_feedstock import make_jinja_env


def _render_template(template_file, env, forge_dir, config):
"""Renders the template"""
template = env.get_template(
os.path.basename(template_file) + ".ci-skel.tmpl"
)
target_fname = os.path.join(forge_dir, template_file)
print("Generating " + target_fname, file=sys.stderr)
template_file_name = Path(template_file).name
template = env.get_template(template_file_name + ".ci-skel.tmpl")
target_fname = Path(forge_dir, template_file)
print("Generating ", target_fname, file=sys.stderr)
new_file_contents = template.render(**config)
os.makedirs(os.path.dirname(target_fname), exist_ok=True)
target_fname.parent.mkdir(parents=True, exist_ok=True)
with open(target_fname, "w") as fh:
fh.write(new_file_contents)

Expand All @@ -37,18 +36,16 @@ def _insert_into_gitignore(
):
"""Places gitignore contents into gitignore."""
# get current contents
fname = os.path.join(feedstock_directory, ".gitignore")
print("Updating " + fname)
if os.path.isfile(fname):
fname = Path(feedstock_directory, ".gitignore")
print("Updating ", fname.name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print("Updating ", fname.name)
print("Updating", fname.name)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure, isn't .name going to give you only the file name (not the whole path as previously)?

I think it should be print("Updating", fname).

if fname.is_file():
with open(fname, "r") as f:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be written as fname.read_text() or fname.read_binary().

s = f.read()
before, _, s = s.partition(prefix)
_, _, after = s.partition(suffix)
else:
before = after = ""
dname = os.path.dirname(fname)
if dname:
os.makedirs(dname, exist_ok=True)
fname.parent.mkdir(parents=True, exist_ok=True)
new = prefix + GITIGNORE_ADDITIONAL + suffix
# write out the file
with open(fname, "w") as f:
Expand All @@ -60,7 +57,7 @@ def generate(
package_name="pkg", feedstock_directory=".", recipe_directory="recipe"
):
"""Generates the CI skeleton."""
forge_dir = os.path.abspath(feedstock_directory)
forge_dir = Path(feedstock_directory).resolve()
env = make_jinja_env(forge_dir)
config = dict(
package_name=package_name,
Expand All @@ -69,8 +66,7 @@ def generate(
)
# render templates
_render_template("conda-forge.yml", env, forge_dir, config)
_render_template(
os.path.join(recipe_directory, "meta.yaml"), env, forge_dir, config
)
recipe_file_name = str(Path(recipe_directory, "meta.yaml"))
_render_template(recipe_file_name, env, forge_dir, config)
# update files which may exist with other content
_insert_into_gitignore(feedstock_directory=feedstock_directory)