Skip to content

Commit a16c665

Browse files
committed
sync codemeta with Codemeta class
1 parent 8fe9dc4 commit a16c665

2 files changed

Lines changed: 21 additions & 75 deletions

File tree

src/somesy/codemeta/__init__.py

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,4 @@
11
"""Integration with codemetapy (to re-generate codemeta as part of somesy sync)."""
2-
import contextlib
3-
import logging
4-
from pathlib import Path
2+
from .create import Codemeta
53

6-
from ..core.models import SomesyConfig
7-
from .exec import gen_codemeta
8-
from .utils import cff_codemeta_tempfile, update_codemeta_file
9-
10-
log = logging.getLogger("somesy")
11-
12-
13-
def collect_cm_sources(conf: SomesyConfig):
14-
"""Assemble list of inputs for codemetapy based on somesy config.
15-
16-
Returns files that are supported by both somesy and codemetapy and are enabled for somesy.
17-
"""
18-
cm_sources = []
19-
if (
20-
not conf.no_sync_pyproject
21-
and conf.pyproject_file is not None
22-
and conf.pyproject_file.is_file()
23-
):
24-
cm_sources.append(conf.pyproject_file)
25-
# NOTE: we don't add CFF directly, because it must be handled separately
26-
# NOTE: add other suitable somesy targets / codemeta sources (except CFF and codemeta) here
27-
if (
28-
conf.no_sync_package_json
29-
and conf.package_json_file is not None
30-
and conf.package_json_file.is_file()
31-
):
32-
cm_sources.append(conf.package_json_file)
33-
return cm_sources
34-
35-
36-
def update_codemeta(conf: SomesyConfig):
37-
"""Generate or update codemeta file based on sources that somesy supports.
38-
39-
Returns True if file has been written, False if it was up to date.
40-
"""
41-
cm_sources = collect_cm_sources(conf)
42-
43-
# if cff file is given, convert it to codemeta tempfile and pass as extra input
44-
temp_cff_cm = contextlib.nullcontext(None)
45-
if not conf.no_sync_cff and conf.cff_file is not None:
46-
temp_cff_cm = cff_codemeta_tempfile(conf.cff_file)
47-
cm_sources.append(Path(temp_cff_cm.name))
48-
49-
# run codemetapy
50-
with temp_cff_cm:
51-
cm_harvest = gen_codemeta(cm_sources)
52-
53-
# NOTE: once codemetapy is fixed (still broken with 2.5.1), we should not need
54-
# update_codemeta_file + codemeta context dump + most of utils.py + their tests anymore
55-
# we'll first disable the workaround and see for a while if everything works
56-
# as expected, and if it does, the cleanup can be completed.
57-
#
58-
# save to file with same settings as used by codemetapy
59-
# with open(conf.codemeta_file, "w") as f:
60-
# f.write(json.dumps(cm_harvest, indent=4, ensure_ascii=False, sort_keys=True))
61-
62-
# check output and write file if needed (with workaround checking graph equivalence)
63-
return update_codemeta_file(conf.codemeta_file, cm_harvest)
64-
65-
66-
__all__ = ["update_codemeta"]
4+
__all__ = ["Codemeta"]

src/somesy/commands/sync.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from rich.pretty import pretty_repr
66

77
from somesy.cff.writer import CFF
8-
from somesy.codemeta import update_codemeta
9-
from somesy.core.models import ProjectMetadata, SomesyConfig, SomesyInput
8+
from somesy.codemeta import Codemeta
9+
from somesy.core.models import ProjectMetadata, SomesyInput
1010
from somesy.package_json.writer import PackageJSON
1111
from somesy.pyproject.writer import Pyproject
1212

@@ -30,13 +30,11 @@ def sync(somesy_input: SomesyInput):
3030
_sync_package_json(metadata, conf.package_json_file)
3131

3232
# create these by default if they are missing:
33-
3433
if not conf.no_sync_cff:
3534
_sync_cff(metadata, conf.cff_file)
3635

37-
# NOTE: codemeta should always be last, it uses (some of) the other targets
3836
if not conf.no_sync_codemeta:
39-
_sync_codemeta(conf)
37+
_sync_codemeta(metadata, conf.codemeta_file)
4038

4139

4240
def _sync_python(
@@ -93,9 +91,19 @@ def _sync_package_json(
9391
logger.verbose("Saved synced package.json file.\n")
9492

9593

96-
def _sync_codemeta(conf: SomesyConfig):
97-
logger.verbose("Updating codemeta.json file.")
98-
if update_codemeta(conf):
99-
logger.verbose(f"New codemeta graph written to {conf.codemeta_file}.")
100-
else:
101-
logger.verbose(f"Codemeta graph unchanged, keeping old {conf.codemeta_file}.")
94+
def _sync_codemeta(
95+
metadata: ProjectMetadata,
96+
codemeta_file: Path,
97+
):
98+
"""Sync package.json file using project metadata.
99+
100+
Args:
101+
metadata (ProjectMetadata): project metadata to sync pyproject.toml file.
102+
codemeta_file (Path, optional): codemeta.json file path if wanted to be synced. Defaults to None.
103+
"""
104+
logger.verbose("Creating codemeta.json file.")
105+
cm = Codemeta(codemeta_file)
106+
logger.verbose("Syncing codemeta.json file.")
107+
cm.sync(metadata)
108+
cm.save()
109+
logger.verbose(f"New codemeta graph written to {codemeta_file}.")

0 commit comments

Comments
 (0)