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

Make temporary folders temporary #1566

Merged
merged 5 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
50 changes: 26 additions & 24 deletions nf_core/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
"""Lists available nf-core pipelines and versions."""

from __future__ import print_function
from collections import OrderedDict

import datetime
from datetime import datetime
import git
import json
import logging
Expand Down Expand Up @@ -175,7 +174,7 @@ def filtered_workflows(self):
for k in self.keyword_filters:
in_name = k in wf.name if wf.name else False
in_desc = k in wf.description if wf.description else False
in_topics = any([k in t for t in wf.topics])
in_topics = any(k in t for t in wf.topics)
if not in_name and not in_desc and not in_topics:
break
else:
Expand Down Expand Up @@ -302,10 +301,10 @@ def __init__(self, data):
# Beautify date
for release in self.releases:
release["published_at_pretty"] = pretty_date(
datetime.datetime.strptime(release.get("published_at"), "%Y-%m-%dT%H:%M:%SZ")
datetime.strptime(release.get("published_at"), "%Y-%m-%dT%H:%M:%SZ")
)
release["published_at_timestamp"] = int(
datetime.datetime.strptime(release.get("published_at"), "%Y-%m-%dT%H:%M:%SZ").strftime("%s")
datetime.strptime(release.get("published_at"), "%Y-%m-%dT%H:%M:%SZ").strftime("%s")
)


Expand Down Expand Up @@ -358,7 +357,7 @@ def get_local_nf_workflow_details(self):
self.commit_sha = str(repo.head.commit.hexsha)
self.remote_url = str(repo.remotes.origin.url)
self.last_pull = os.stat(os.path.join(self.local_path, ".git", "FETCH_HEAD")).st_mtime
self.last_pull_date = datetime.datetime.fromtimestamp(self.last_pull).strftime("%Y-%m-%d %H:%M:%S")
self.last_pull_date = datetime.fromtimestamp(self.last_pull).strftime("%Y-%m-%d %H:%M:%S")
self.last_pull_pretty = pretty_date(self.last_pull)

# Get the checked out branch if we can
Expand Down Expand Up @@ -392,7 +391,6 @@ def pretty_date(time):
Based on https://stackoverflow.com/a/1551394/713980
Adapted by sven1103
"""
from datetime import datetime

now = datetime.now()
if isinstance(time, datetime):
Expand All @@ -402,23 +400,27 @@ def pretty_date(time):
second_diff = diff.seconds
day_diff = diff.days

pretty_msg = OrderedDict()
pretty_msg[0] = [(float("inf"), 1, "from the future")]
pretty_msg[1] = [
(10, 1, "just now"),
(60, 1, "{sec:.0f} seconds ago"),
(120, 1, "a minute ago"),
(3600, 60, "{sec:.0f} minutes ago"),
(7200, 1, "an hour ago"),
(86400, 3600, "{sec:.0f} hours ago"),
]
pretty_msg[2] = [(float("inf"), 1, "yesterday")]
pretty_msg[7] = [(float("inf"), 1, "{days:.0f} day{day_s} ago")]
pretty_msg[31] = [(float("inf"), 7, "{days:.0f} week{day_s} ago")]
pretty_msg[365] = [(float("inf"), 30, "{days:.0f} months ago")]
pretty_msg[float("inf")] = [(float("inf"), 365, "{days:.0f} year{day_s} ago")]

for days, seconds in pretty_msg.items():
pretty_msg = (
(0, ((float("inf"), 1, "from the future"),)),
(
1,
(
(10, 1, "just now"),
(60, 1, "{sec:.0f} seconds ago"),
(120, 1, "a minute ago"),
(3600, 60, "{sec:.0f} minutes ago"),
(7200, 1, "an hour ago"),
(86400, 3600, "{sec:.0f} hours ago"),
),
),
(2, ((float("inf"), 1, "yesterday"),)),
(7, ((float("inf"), 1, "{days:.0f} day{day_s} ago"),)),
(31, ((float("inf"), 7, "{days:.0f} week{day_s} ago"),)),
(365, ((float("inf"), 30, "{days:.0f} months ago"),)),
(float("inf"), ((float("inf"), 365, "{days:.0f} year{day_s} ago"),)),
)

for days, seconds in pretty_msg:
if day_diff < days:
for sec in seconds:
if second_diff < sec[0]:
Expand Down
3 changes: 2 additions & 1 deletion nf_core/modules/list.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import json
import logging
from os import pipe

import rich
import logging

import nf_core.modules.module_utils

Expand Down
34 changes: 20 additions & 14 deletions tests/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@
""" Tests covering the workflow listing code.
"""

import nf_core.list

import json
import mock
import os
import pytest
import tempfile
import time
import unittest
from datetime import datetime
from pathlib import Path

import mock
import pytest
from rich.console import Console

from datetime import datetime
import nf_core.list

# create a temporary directory that can be used by the tests in this file
tmp_nxf = Path(tempfile.mkdtemp()) / "nxf"
tmp_nxf_str = str(tmp_nxf)


class TestLint(unittest.TestCase):
Expand Down Expand Up @@ -100,28 +106,28 @@ def test_local_workflows_compare_and_fail_silently(self):

rwf_ex.releases = None

@mock.patch.dict(os.environ, {"NXF_ASSETS": "/tmp/nxf"})
@mock.patch.dict(os.environ, {"NXF_ASSETS": tmp_nxf_str})
@mock.patch("nf_core.list.LocalWorkflow")
def test_parse_local_workflow_and_succeed(self, mock_local_wf):
test_path = "/tmp/nxf/nf-core"
test_path = tmp_nxf / "nf-core"
if not os.path.isdir(test_path):
os.makedirs(test_path)
assert os.environ["NXF_ASSETS"] == "/tmp/nxf"
with open("/tmp/nxf/nf-core/dummy-wf", "w") as f:
assert os.environ["NXF_ASSETS"] == tmp_nxf_str
with open(tmp_nxf / "nf-core/dummy-wf", "w") as f:
f.write("dummy")
workflows_obj = nf_core.list.Workflows()
workflows_obj.get_local_nf_workflows()
assert len(workflows_obj.local_workflows) == 1

@mock.patch.dict(os.environ, {"NXF_ASSETS": "/tmp/nxf"})
@mock.patch.dict(os.environ, {"NXF_ASSETS": tmp_nxf_str})
@mock.patch("nf_core.list.LocalWorkflow")
@mock.patch("subprocess.check_output")
def test_parse_local_workflow_home(self, mock_local_wf, mock_subprocess):
test_path = "/tmp/nxf/nf-core"
test_path = tmp_nxf / "nf-core"
if not os.path.isdir(test_path):
os.makedirs(test_path)
assert os.environ["NXF_ASSETS"] == "/tmp/nxf"
with open("/tmp/nxf/nf-core/dummy-wf", "w") as f:
assert os.environ["NXF_ASSETS"] == tmp_nxf_str
with open(tmp_nxf / "nf-core/dummy-wf", "w") as f:
f.write("dummy")
workflows_obj = nf_core.list.Workflows()
workflows_obj.get_local_nf_workflows()
Expand All @@ -130,7 +136,7 @@ def test_parse_local_workflow_home(self, mock_local_wf, mock_subprocess):
@mock.patch("git.Repo")
def test_local_workflow_investigation(self, mock_repo, mock_stat):
local_wf = nf_core.list.LocalWorkflow("dummy")
local_wf.local_path = "/tmp"
local_wf.local_path = tmp_nxf
fabianegli marked this conversation as resolved.
Show resolved Hide resolved
mock_repo.head.commit.hexsha = "h00r4y"
mock_stat.st_mode = 1
local_wf.get_local_nf_workflow_details()
Expand Down