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

Conversation

AlbaHerrerias
Copy link

@AlbaHerrerias AlbaHerrerias commented Jun 10, 2024

Hello 👋!

As part of the STF work for Conda, this PR refactors the usage of os in favour of pathlib, in an effort to modernise Conda.

Thank you!

Checklist

  • Added a news entry

@AlbaHerrerias AlbaHerrerias requested a review from a team as a code owner June 10, 2024 12:30
@wolfv
Copy link
Member

wolfv commented Jul 23, 2024

Hey, we pushed some changes recently to conda-smithy that created merge conflicts. I'll get rid of them and then IMO this is good to go :)

Comment on lines 93 to 103
# 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(
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.

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).

"tokens",
project + ".json",
)
token_file = str(Path(tmpdir, "tokens", f"{project}.json"))
Copy link
Member

Choose a reason for hiding this comment

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

Why cast this to a string here?


# append the token if needed
if os.path.exists(token_file):
if Path(token_file).exists():
with open(token_file, "r") as fp:
Copy link
Member

Choose a reason for hiding this comment

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

I think we can use token_file.open() (instead of open(token_file)

Copy link
Member

Choose a reason for hiding this comment

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

or even json.loads(token_file.read_string())

Copy link
Member

Choose a reason for hiding this comment

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

I think you mean .read_text(), right?

if os.path.isfile(fname):
fname = Path(feedstock_directory, ".gitignore")
print("Updating ", fname.name)
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().

@@ -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()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants