-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: port
util.copy_task
from gecko_taskgraph
And use it in `util.templates.merge`. This is a port of the following patch in `gecko_taskgraph`: https://hg.mozilla.org/mozilla-central/rev/fa2cdac6989ac78d606d8f8adb10d826a6e50429
- Loading branch information
Showing
3 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from typing import Any | ||
|
||
from taskgraph.task import Task | ||
from taskgraph.util.readonlydict import ReadOnlyDict | ||
|
||
immutable_types = {int, float, bool, str, type(None), ReadOnlyDict} | ||
|
||
|
||
def deepcopy(obj: Any) -> Any: | ||
"""Perform a deep copy of an object with a tree like structure. | ||
This is a re-implementation of Python's `copy.deepcopy` function with a few key differences: | ||
1. Unlike the stdlib, this does *not* support copying graph-like structure, | ||
which allows it to be more efficient than deepcopy on tree-like structures | ||
(such as Tasks). | ||
2. This special cases support for `taskgraph.task.Task` objects. | ||
Args: | ||
obj: The object to deep copy. | ||
Returns: | ||
A deep copy of the object. | ||
""" | ||
ty = type(obj) | ||
if ty in immutable_types: | ||
return obj | ||
if ty is dict: | ||
return {k: deepcopy(v) for k, v in obj.items()} | ||
if ty is list: | ||
return [deepcopy(elt) for elt in obj] | ||
if ty is Task: | ||
task = Task( | ||
kind=deepcopy(obj.kind), | ||
label=deepcopy(obj.label), | ||
attributes=deepcopy(obj.attributes), | ||
task=deepcopy(obj.task), | ||
description=deepcopy(obj.description), | ||
optimization=deepcopy(obj.optimization), | ||
dependencies=deepcopy(obj.dependencies), | ||
soft_dependencies=deepcopy(obj.soft_dependencies), | ||
if_dependencies=deepcopy(obj.if_dependencies), | ||
) | ||
if obj.task_id: | ||
task.task_id = obj.task_id | ||
return task | ||
raise NotImplementedError(f"copying '{ty}' from '{obj}'") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import pytest | ||
|
||
from taskgraph.task import Task | ||
from taskgraph.util.copy import deepcopy, immutable_types | ||
from taskgraph.util.readonlydict import ReadOnlyDict | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input", | ||
( | ||
1, | ||
False, | ||
"foo", | ||
ReadOnlyDict(a=1, b="foo"), | ||
["foo", "bar"], | ||
{ | ||
"foo": Task( | ||
label="abc", | ||
kind="kind", | ||
attributes={"bar": "baz"}, | ||
dependencies={"dep": "bar"}, | ||
task={"payload": {"command": ["echo hello"]}}, | ||
) | ||
}, | ||
), | ||
) | ||
def test_deepcopy(input): | ||
result = deepcopy(input) | ||
assert result == input | ||
|
||
if type(result) in immutable_types: | ||
assert id(result) == id(input) | ||
else: | ||
assert id(result) != id(input) |