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
Show file tree
Hide file tree
Changes from 4 commits
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
11 changes: 5 additions & 6 deletions bootstrap-obvious-ci-and-miniconda.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

"""
import argparse
import os
from pathlib import Path
import platform
import subprocess
import sys

try:
from urllib.request import urlretrieve
Expand Down Expand Up @@ -85,22 +84,22 @@ def main(
else:
raise ValueError("Unsupported operating system.")

if not os.path.exists(basename):
if not Path(basename).exists():
print("Downloading from {}".format(URL))
urlretrieve(URL, basename)
else:
print("Using cached version of {}".format(URL))

# Install with powershell.
if os.path.exists(target_dir):
if Path(target_dir).exists():
raise IOError("Installation directory already exists")
subprocess.check_call(cmd)

if not os.path.isdir(target_dir):
if not Path(target_dir).is_dir():
raise RuntimeError("Failed to install miniconda :(")

if install_obvci:
conda_path = os.path.join(target_dir, bin_dir, "conda")
conda_path = Path(target_dir, bin_dir, "conda")
subprocess.check_call(
Comment on lines 93 to 103
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why we are using several Path instantiations here when it's all target_dir. We should have something like:

target_dir = Path(target_dir)
if target_dir.exists():
  ...
if not target_dir.is_dir():
  ...
if install_obvci:
  conda_path = target_dir / bin_dir / "conda"

and so on. I'm afraid this happens in more areas of the PR, so a careful review is advised imo.

[
conda_path,
Expand Down
3 changes: 2 additions & 1 deletion conda_smithy/azure_ci_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from pathlib import Path
import typing
import warnings

Expand Down Expand Up @@ -41,7 +42,7 @@ def __init__(

try:
with open(
os.path.expanduser("~/.conda-smithy/azure.token"), "r"
Copy link
Member

@wolfv wolfv Jul 23, 2024

Choose a reason for hiding this comment

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

How about Path("~/.conda-smithy/azure.token").expanduser().read_text().strip()?

Path("~/.conda-smithy/azure.token").expanduser(), "r"
) as fh:
self.token = fh.read().strip()
if not self.token:
Expand Down
11 changes: 6 additions & 5 deletions conda_smithy/ci_register.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
import os
from pathlib import Path
import requests
import time
import sys
Expand All @@ -14,7 +15,7 @@
# https://circleci.com/api/v1/project/:username/:project/envvar?circle-token=:token

try:
with open(os.path.expanduser("~/.conda-smithy/circle.token"), "r") as fh:
with open(Path("~/.conda-smithy/circle.token").expanduser(), "r") as fh:
circle_token = fh.read().strip()
if not circle_token:
raise ValueError()
Expand All @@ -25,7 +26,7 @@
)

try:
with open(os.path.expanduser("~/.conda-smithy/appveyor.token"), "r") as fh:
with open(Path("~/.conda-smithy/appveyor.token").expanduser(), "r") as fh:
appveyor_token = fh.read().strip()
if not appveyor_token:
raise ValueError()
Expand All @@ -36,7 +37,7 @@
)

try:
with open(os.path.expanduser("~/.conda-smithy/drone.token"), "r") as fh:
with open(Path("~/.conda-smithy/drone.token").expanduser(), "r") as fh:
drone_token = fh.read().strip()
if not drone_token:
raise ValueError()
Expand All @@ -51,7 +52,7 @@
except KeyError:
try:
with open(
os.path.expanduser("~/.conda-smithy/anaconda.token"), "r"
Path("~/.conda-smithy/anaconda.token").expanduser(), "r"
) as fh:
anaconda_token = fh.read().strip()
if not anaconda_token:
Expand Down Expand Up @@ -91,7 +92,7 @@ def travis_headers():
"Content-Type": "application/json",
"Travis-API-Version": "3",
}
travis_token = os.path.expanduser("~/.conda-smithy/travis.token")
travis_token = Path("~/.conda-smithy/travis.token").expanduser()
try:
with open(travis_token, "r") as fh:
token = fh.read().strip()
Expand Down
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)