From c06650c47545e91c002e97bc8fa287e3d655047e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20Jedlicska?= <57442769+gjedlicska@users.noreply.github.com> Date: Wed, 2 Nov 2022 12:57:46 +0100 Subject: [PATCH] Update to new specklepy (#1173) * Publish images for all branches but limit tagging * only tag 'latest' and '2' when 'SHOULD_PUBLISH' variable is 'true' * Publishing helm chart should check for `SHOULD_PUBLISH` * Move blocking step to publish-helm chart, and allow images to be published * Pin python requirements and bump to latest versions * Fix EOL whitespace * use valid version for psycopg2-binary (the clue is in the 2!) * fix(fileimports): add exception printing to file imports * fix(fileimports): bump specklepy version move to a specklepy version that contains a fix for send without writing to disk Co-authored-by: Iain Sproat <68657+iainsproat@users.noreply.github.com> --- .../fileimport-service/obj/import_file.py | 102 ++++++++++-------- packages/fileimport-service/requirements.txt | 2 +- .../fileimport-service/stl/import_file.py | 52 +++++---- 3 files changed, 89 insertions(+), 67 deletions(-) diff --git a/packages/fileimport-service/obj/import_file.py b/packages/fileimport-service/obj/import_file.py index 853440538..a63aa4f80 100644 --- a/packages/fileimport-service/obj/import_file.py +++ b/packages/fileimport-service/obj/import_file.py @@ -1,53 +1,58 @@ - +import sys, os import json from specklepy.objects import Base from specklepy.objects.other import RenderMaterial -from specklepy.objects.geometry import Mesh, Point, Line +from specklepy.objects.geometry import Mesh from specklepy.transports.server import ServerTransport from specklepy.api.client import SpeckleClient -from specklepy.api.credentials import get_default_account from specklepy.api import operations - -import sys, os - from obj_file import ObjFile -TMP_RESULTS_PATH = '/tmp/import_result.json' -DEFAULT_BRANCH = 'uploads' + +TMP_RESULTS_PATH = "/tmp/import_result.json" +DEFAULT_BRANCH = "uploads" + def convert_material(obj_mat): speckle_mat = RenderMaterial() - speckle_mat.name = obj_mat['name'] - if 'diffuse' in obj_mat: - argb = [1,] + obj_mat['diffuse'] - speckle_mat.diffuse = int.from_bytes([int(val * 255) for val in argb], byteorder="big", signed=True) - if 'dissolved' in obj_mat: - speckle_mat.opacity = obj_mat['dissolved'] + speckle_mat.name = obj_mat["name"] + if "diffuse" in obj_mat: + argb = [ + 1, + ] + obj_mat["diffuse"] + speckle_mat.diffuse = int.from_bytes( + [int(val * 255) for val in argb], byteorder="big", signed=True + ) + if "dissolved" in obj_mat: + speckle_mat.opacity = obj_mat["dissolved"] return speckle_mat + def import_obj(): - file_path, user_id, stream_id, branch_name, commit_message = sys.argv[1:] - print(f'ImportOBJ argv[1:]: {sys.argv[1:]}') + file_path, _, stream_id, branch_name, commit_message = sys.argv[1:] + print(f"ImportOBJ argv[1:]: {sys.argv[1:]}") # Parse input obj = ObjFile(file_path) - print(f'Parsed obj with {len(obj.faces)} faces ({len(obj.vertices) * 3} vertices)') + print(f"Parsed obj with {len(obj.faces)} faces ({len(obj.vertices) * 3} vertices)") speckle_root = Base() - speckle_root['@objects'] = [] + speckle_root["@objects"] = [] for objname in obj.objects: - print(f' Converting {objname}...') + print(f" Converting {objname}...") speckle_obj = Base() speckle_obj.name = objname - speckle_obj['@displayValue'] = [] - speckle_root['@objects'].append(speckle_obj) - + speckle_obj["@displayValue"] = [] + speckle_root["@objects"].append(speckle_obj) + for obj_mesh in obj.objects[objname]: - speckle_vertices = [coord for point in obj_mesh['vertices'] for coord in point] + speckle_vertices = [ + coord for point in obj_mesh["vertices"] for coord in point + ] speckle_faces = [] - for obj_face in obj_mesh['faces']: + for obj_face in obj_mesh["faces"]: if len(obj_face) == 3: speckle_faces.append(0) elif len(obj_face) == 4: @@ -57,64 +62,75 @@ def import_obj(): speckle_faces.extend(obj_face) has_vertex_colors = False - for vc in obj_mesh['vertex_colors']: + for vc in obj_mesh["vertex_colors"]: if vc is not None: has_vertex_colors = True colors = [] if has_vertex_colors: - for vc in obj_mesh['vertex_colors']: + for vc in obj_mesh["vertex_colors"]: if vc is None: r, g, b = (1.0, 1.0, 1.0) else: r, g, b = vc argb = (1.0, r, g, b) - color = int.from_bytes([int(val * 255) for val in argb], byteorder="big", signed=True) + color = int.from_bytes( + [int(val * 255) for val in argb], byteorder="big", signed=True + ) colors.append(color) speckle_mesh = Mesh( vertices=speckle_vertices, faces=speckle_faces, colors=colors, - textureCoordinates=[] + textureCoordinates=[], ) - obj_material = obj_mesh['material'] + obj_material = obj_mesh["material"] if obj_material: - speckle_mesh['renderMaterial'] = convert_material(obj_material) + speckle_mesh["renderMaterial"] = convert_material(obj_material) - speckle_obj['@displayValue'].append(speckle_mesh) + speckle_obj["@displayValue"].append(speckle_mesh) # Commit - client = SpeckleClient(host=os.getenv('SPECKLE_SERVER_URL', 'localhost:3000'), use_ssl=False) - client.authenticate(os.environ['USER_TOKEN']) + client = SpeckleClient( + host=os.getenv("SPECKLE_SERVER_URL", "localhost:3000"), use_ssl=False + ) + client.authenticate_with_token(os.environ["USER_TOKEN"]) if not client.branch.get(stream_id, branch_name): - client.branch.create(stream_id, branch_name, 'File upload branch' if branch_name == 'uploads' else '') + client.branch.create( + stream_id, + branch_name, + "File upload branch" if branch_name == "uploads" else "", + ) transport = ServerTransport(client=client, stream_id=stream_id) - id = operations.send(base=speckle_root, transports=[transport], use_default_cache=False) + id = operations.send( + base=speckle_root, transports=[transport], use_default_cache=False + ) commit_id = client.commit.create( stream_id=stream_id, object_id=id, branch_name=(branch_name or DEFAULT_BRANCH), - message=(commit_message or 'OBJ file upload'), - source_application='OBJ' + message=(commit_message or "OBJ file upload"), + source_application="OBJ", ) return commit_id -if __name__ == '__main__': +if __name__ == "__main__": + from pathlib import Path + try: commit_id = import_obj() if not commit_id: raise Exception("Can't create commit") - results = {'success': True, 'commitId': commit_id} + results = {"success": True, "commitId": commit_id} except Exception as ex: - print('ERROR: ' + str(ex)) - results = {'success': False, 'error': str(ex)} + print("ERROR: " + str(ex)) + results = {"success": False, "error": str(ex)} - with open(TMP_RESULTS_PATH, 'w') as f: - json.dump(results, f) + Path(TMP_RESULTS_PATH).write_text(json.dumps(results)) diff --git a/packages/fileimport-service/requirements.txt b/packages/fileimport-service/requirements.txt index 7b3349251..f7e1614c8 100644 --- a/packages/fileimport-service/requirements.txt +++ b/packages/fileimport-service/requirements.txt @@ -1,2 +1,2 @@ numpy-stl==2.17.1 -specklepy==2.9.0 +specklepy==2.9.1 diff --git a/packages/fileimport-service/stl/import_file.py b/packages/fileimport-service/stl/import_file.py index 8de5c4f95..ac7d8740c 100644 --- a/packages/fileimport-service/stl/import_file.py +++ b/packages/fileimport-service/stl/import_file.py @@ -1,46 +1,50 @@ - import json import stl -from specklepy.objects.geometry import Mesh, Point +from specklepy.objects.geometry import Mesh from specklepy.transports.server import ServerTransport from specklepy.api.client import SpeckleClient -from specklepy.api.credentials import get_default_account from specklepy.api import operations import sys, os -TMP_RESULTS_PATH = '/tmp/import_result.json' -DEFAULT_BRANCH = 'uploads' +TMP_RESULTS_PATH = "/tmp/import_result.json" +DEFAULT_BRANCH = "uploads" + def import_stl(): - file_path, user_id, stream_id, branch_name, commit_message = sys.argv[1:] - print(f'ImportSTL argv[1:]: {sys.argv[1:]}') + file_path, _, stream_id, branch_name, commit_message = sys.argv[1:] + print(f"ImportSTL argv[1:]: {sys.argv[1:]}") # Parse input stl_mesh = stl.mesh.Mesh.from_file(file_path) - print(f'Parsed mesh with {stl_mesh.points.shape[0]} faces ({stl_mesh.points.shape[0] * 3} vertices)') + print( + f"Parsed mesh with {stl_mesh.points.shape[0]} faces ({stl_mesh.points.shape[0] * 3} vertices)" + ) # Construct speckle obj vertices = stl_mesh.points.flatten().tolist() faces = [] for i in range(stl_mesh.points.shape[0]): - faces.extend([0, 3*i, 3*i+1, 3*i+2]) + faces.extend([0, 3 * i, 3 * i + 1, 3 * i + 2]) speckle_mesh = Mesh( - vertices=vertices, - faces=faces, - colors=[], - textureCoordinates=[] + vertices=vertices, faces=faces, colors=[], textureCoordinates=[] ) - print('Constructed Speckle Mesh object') + print("Constructed Speckle Mesh object") # Commit - client = SpeckleClient(host=os.getenv('SPECKLE_SERVER_URL', 'localhost:3000'), use_ssl=False) - client.authenticate(os.environ['USER_TOKEN']) + client = SpeckleClient( + host=os.getenv("SPECKLE_SERVER_URL", "localhost:3000"), use_ssl=False + ) + client.authenticate_with_token(os.environ["USER_TOKEN"]) if not client.branch.get(stream_id, branch_name): - client.branch.create(stream_id, branch_name, 'File upload branch' if branch_name == 'uploads' else '') + client.branch.create( + stream_id, + branch_name, + "File upload branch" if branch_name == "uploads" else "", + ) transport = ServerTransport(client=client, stream_id=stream_id) id = operations.send( @@ -53,20 +57,22 @@ def import_stl(): stream_id=stream_id, object_id=id, branch_name=(branch_name or DEFAULT_BRANCH), - message=(commit_message or 'STL file upload'), - source_application='STL' + message=(commit_message or "STL file upload"), + source_application="STL", ) return commit_id -if __name__ == '__main__': +if __name__ == "__main__": + from pathlib import Path + try: commit_id = import_stl() - results = {'success': True, 'commitId': commit_id} + results = {"success": True, "commitId": commit_id} except Exception as ex: results = {'success': False, 'error': str(ex)} print(ex) - with open(TMP_RESULTS_PATH, 'w') as f: - json.dump(results, f) + print(results) + Path(TMP_RESULTS_PATH).write_text(json.dumps(results))