Skip to content

Commit

Permalink
correct typing for load_graph
Browse files Browse the repository at this point in the history
handling of empty/nonexistent graph file
  • Loading branch information
ytausch committed Feb 8, 2024
1 parent 6f6ec07 commit 94fd022
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 16 deletions.
7 changes: 4 additions & 3 deletions conda_forge_tick/auto_tick.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
fold_log_lines,
frozen_to_json_friendly,
get_keys_default,
load_existing_graph,
load_graph,
parse_meta_yaml,
parse_munged_run_export,
Expand Down Expand Up @@ -982,7 +983,7 @@ def initialize_migrators(
dry_run: bool = False,
) -> Tuple[MigratorSessionContext, list, MutableSequence[Migrator]]:
temp = glob.glob("/tmp/*")
gx = load_graph()
gx = load_existing_graph()
smithy_version = eval_cmd("conda smithy --version").strip()
pinning_version = json.loads(eval_cmd("conda list conda-forge-pinning --json"))[0][
"version"
Expand Down Expand Up @@ -1409,7 +1410,7 @@ def _setup_limits():
resource.setrlimit(resource.RLIMIT_AS, (limit_int, limit_int))


def _update_nodes_with_bot_rerun(gx):
def _update_nodes_with_bot_rerun(gx: nx.DiGraph):
"""Go through all the open PRs and check if they are rerun"""

print("processing bot-rerun labels", flush=True)
Expand Down Expand Up @@ -1552,7 +1553,7 @@ def _remove_closed_pr_json():

def _update_graph_with_pr_info():
_remove_closed_pr_json()
gx = load_graph()
gx = load_existing_graph()
_update_nodes_with_bot_rerun(gx)
_update_nodes_with_new_versions(gx)
dump_graph(gx)
Expand Down
2 changes: 1 addition & 1 deletion conda_forge_tick/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Optional

import click
from click import IntRange, Context
from click import Context, IntRange

from conda_forge_tick import lazy_json_backends

Expand Down
4 changes: 2 additions & 2 deletions conda_forge_tick/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from . import sensitive_env
from .cli_context import CliContext
from .lazy_json_backends import CF_TICK_GRAPH_DATA_HASHMAPS, get_lazy_json_backends
from .utils import load_graph
from .utils import load_existing_graph

BUILD_URL_KEY = "CIRCLE_BUILD_URL"

Expand Down Expand Up @@ -93,7 +93,7 @@ def deploy(ctx: CliContext):

# make sure the graph can load, if not we will error
try:
gx = load_graph()
gx = load_existing_graph()
# TODO: be more selective about which json to check
for node, attrs in gx.nodes.items():
attrs["payload"]._load()
Expand Down
4 changes: 2 additions & 2 deletions conda_forge_tick/pypi_name_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from packaging.utils import canonicalize_name as canonicalize_pypi_name

from .lazy_json_backends import LazyJson, dump, get_all_keys_for_hashmap, loads
from .utils import as_iterable, load_graph
from .utils import as_iterable, load_existing_graph, load_graph


class Mapping(TypedDict):
Expand Down Expand Up @@ -298,7 +298,7 @@ def determine_best_matches_for_pypi_import(
map_by_conda_name[conda_name] = m

graph_file = str(pathlib.Path(".") / "graph.json")
gx = load_graph(graph_file)
gx = load_existing_graph(graph_file)
# TODO: filter out archived feedstocks?

try:
Expand Down
4 changes: 2 additions & 2 deletions conda_forge_tick/update_prs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from .executors import executor
from .make_graph import ghctx
from .utils import github_client, load_graph
from .utils import github_client, load_existing_graph

# from conda_forge_tick.profiler import profiling

Expand Down Expand Up @@ -164,7 +164,7 @@ def close_dirty_prs(

# @profiling
def main(ctx: CliContext, job: int = 1, n_jobs: int = 1) -> None:
gx = load_graph()
gx = load_existing_graph()

gx = close_labels(gx, ctx.dry_run, job=job, n_jobs=n_jobs)
gx = update_graph_pr_status(gx, ctx.dry_run, job=job, n_jobs=n_jobs)
Expand Down
4 changes: 2 additions & 2 deletions conda_forge_tick/update_upstream_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
RawURL,
ROSDistro,
)
from .utils import get_keys_default, load_graph
from .utils import get_keys_default, load_existing_graph

T = TypeVar("T")

Expand Down Expand Up @@ -393,7 +393,7 @@ def main(
"""
logger.info("Reading graph")
# Graph enabled for inspection
gx = load_graph()
gx = load_existing_graph()

# Check if 'versions' folder exists or create a new one;
os.makedirs("versions", exist_ok=True)
Expand Down
27 changes: 23 additions & 4 deletions conda_forge_tick/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def _munge_dict_repr(d):
datetime=datetime,
)

DEFAULT_GRAPH_FILENAME = "graph.json"


@contextlib.contextmanager
def fold_log_lines(title):
Expand Down Expand Up @@ -431,7 +433,6 @@ def dump_graph_json(gx: nx.DiGraph, filename: str = "graph.json") -> None:
links = nld["links"]
links2 = sorted(links, key=lambda x: f'{x["source"]}{x["target"]}')
nld["links"] = links2
from conda_forge_tick.lazy_json_backends import LazyJson

lzj = LazyJson(filename)
with lzj as attrs:
Expand All @@ -447,14 +448,32 @@ def dump_graph(
dump_graph_json(gx, filename)


def load_graph(filename: str = "graph.json") -> nx.DiGraph:
from conda_forge_tick.lazy_json_backends import LazyJson
def load_existing_graph(filename: str = DEFAULT_GRAPH_FILENAME) -> nx.DiGraph:
"""
Load the graph from a file using the lazy json backend.
If a non-existing or empty file is encountered, a FileNotFoundError is raised.
If you expect the graph to possibly not exist, use load_graph.
:return: the graph
"""
gx = load_graph(filename)
if gx is None:
raise FileNotFoundError(f"Graph file {filename} does not exist or is empty")
return gx


def load_graph(filename: str = DEFAULT_GRAPH_FILENAME) -> Optional[nx.DiGraph]:
"""
Load the graph from a file using the lazy json backend.
If you expect the graph to exist, use load_existing_graph.
:return: the graph, or None if the file does not exist or is empty JSON
"""
dta = copy.deepcopy(LazyJson(filename).data)
if dta:
return nx.node_link_graph(dta)
else:
raise FileNotFoundError("Graph file not found.")
return None


# TODO: This type does not support generics yet sadly
Expand Down

0 comments on commit 94fd022

Please sign in to comment.