Skip to content

Commit

Permalink
improve tool
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilian Huber <[email protected]>
  • Loading branch information
maxhbr committed May 3, 2024
1 parent 106c9db commit cbd9734
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
12 changes: 11 additions & 1 deletion entr_puml.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@ set -euo pipefail
cd "$( dirname "${BASH_SOURCE[0]}" )"

spdx="$1"
shift
puml="$spdx.puml"
png="$spdx.png"

export spdx
export puml
entr_task() (
set -ex

nix develop --command poetry run python -m spdx3_to_graph -- -v "$spdx"
plantuml "$puml"
PLANTUML_LIMIT_SIZE=$(( 8192 * 10 )) plantuml "$puml"
)

if [[ "$#" -ge 1 ]]; then
if [[ "$1" == "--once" ]]; then
entr_task
exit 0
fi
fi

export -f entr_task

if [[ ! -f "$puml" || ! -f "$png" ]]; then
Expand Down
2 changes: 1 addition & 1 deletion spdx3_to_graph/spdx30.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ def _make_object(cls, typ):
def decode(cls, decoder, *, objectset=None):
typ, obj_d = decoder.read_object()
if typ is None:
raise TypeError("Unable to determine type for object")
raise TypeError(f"Unable to determine type for object {decoder.data}")

obj = cls._make_object(typ)
for key in (obj.ID_ALIAS, obj._OBJ_IRIS["_id"]):
Expand Down
37 changes: 30 additions & 7 deletions spdx3_to_graph/spdx3_to_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
import argparse
from pathlib import Path
import collections.abc
import re

from . import spdx30 as spdx

def escape_string(s):
# escapes newlines and quotes
return s.replace("\n", "\\n").replace("\"", "\\\"")

class SPDXPumlGraph():

def _get_idx(self, _id):
Expand All @@ -32,7 +37,6 @@ def _get_idx(self, _id):

def _get_idx_of_shaclobject(self, o):
if o is None:
logging.error(f"None object")
raise ValueError("None object")
_id = o._id
if o._id is None:
Expand All @@ -41,14 +45,26 @@ def _get_idx_of_shaclobject(self, o):
logging.warn(f"None id for {type(o)}, use {_id}")
return self._get_idx(_id)

def _create_node(self, o):
idx = self._get_idx_of_shaclobject(o)
if idx in self.inserted:
def _get_idx_of_str(self, o):
if o is None:
raise ValueError("None str")
if o not in self.id_to_idx:
idx = hashlib.md5(o.encode()).hexdigest()
self.id_to_idx[o] = idx
self.idx_to_id[idx] = o
return idx
self.inserted.add(idx)
else:
return self.id_to_idx[o]

self.logging.debug(f"Create node: {o._id} as {idx}: {o}")

def _create_node(self, o):
if isinstance(o, spdx.SHACLObject):
idx = self._get_idx_of_shaclobject(o)
if idx in self.inserted:
return idx
self.inserted.add(idx)
self.logging.debug(f"Create node: {o._id} as {idx}: {o}")

if isinstance(o, spdx.Element) and o.name is not None:
self.lines_defs.append(f"object \"<b>{o.name}</b>\\n{o.__class__.__name__}\" as {idx}")
elif o._id is None:
Expand Down Expand Up @@ -83,9 +99,16 @@ def _create_node(self, o):
continue

if isinstance(value, str):
self.lines.append(f"{idx} : {compact} = \"{value}\"")
self.lines.append(f"{idx} : {compact} = \"{escape_string(value)}\"")
else:
self.lines.append(f"{idx} : {compact} = {value}")
elif isinstance(o, str):
idx = self._get_idx_of_str(o)
self.logging.debug(f"Create str node: {idx}")
self.lines_defs.append(f"object \"{escape_string(o)}\" as {idx}")
else:
raise ValueError(f"Unknown type {type(o)}")

return idx


Expand Down

0 comments on commit cbd9734

Please sign in to comment.