|
| 1 | +############################################################################### |
| 2 | +# Copyright (c) 2026 Obeo. |
| 3 | +# This program and the accompanying materials |
| 4 | +# are made available under the terms of the Eclipse Public License v2.0 |
| 5 | +# which accompanies this distribution, and is available at |
| 6 | +# https://www.eclipse.org/legal/epl-2.0/ |
| 7 | +# |
| 8 | +# SPDX-License-Identifier: EPL-2.0 |
| 9 | +# |
| 10 | +# Contributors: |
| 11 | +# Obeo - initial API and implementation |
| 12 | +############################################################################### |
| 13 | + |
| 14 | +import argparse |
| 15 | +import json |
| 16 | +import uuid |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +import requests # <1> |
| 20 | + |
| 21 | + |
| 22 | +GRAPHQL_ENDPOINT = "/api/graphql" |
| 23 | +GRAPHQL_UPLOAD_ENDPOINT = "/api/graphql/upload" |
| 24 | + |
| 25 | + |
| 26 | +fetch_editing_context_query = """ |
| 27 | +query FetchEditingContext($projectId: ID!) { |
| 28 | + viewer { |
| 29 | + project(projectId: $projectId) { |
| 30 | + currentEditingContext { |
| 31 | + id |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | +""" |
| 37 | + |
| 38 | + |
| 39 | +upload_document_mutation = """ |
| 40 | +mutation UploadDocument($input: UploadDocumentInput!) { |
| 41 | + uploadDocument(input: $input) { |
| 42 | + __typename |
| 43 | + ... on UploadDocumentSuccessPayload { |
| 44 | + id |
| 45 | + report |
| 46 | + } |
| 47 | + ... on ErrorPayload { |
| 48 | + messages { |
| 49 | + body |
| 50 | + level |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | +""" |
| 56 | + |
| 57 | + |
| 58 | +def build_headers(token): # <2> |
| 59 | + headers = {} |
| 60 | + if token: |
| 61 | + headers["Authorization"] = f"Bearer {token}" |
| 62 | + return headers |
| 63 | + |
| 64 | + |
| 65 | +def get_graphql_url(url): |
| 66 | + return f"{url.rstrip('/')}{GRAPHQL_ENDPOINT}" |
| 67 | + |
| 68 | + |
| 69 | +def get_graphql_upload_url(url): |
| 70 | + return f"{url.rstrip('/')}{GRAPHQL_UPLOAD_ENDPOINT}" |
| 71 | + |
| 72 | + |
| 73 | +def print_graphql_errors(data): |
| 74 | + errors = data.get("errors", []) |
| 75 | + for error in errors: |
| 76 | + print(f"GraphQL error: {error.get('message', error)}") |
| 77 | + |
| 78 | + |
| 79 | +def fetch_editing_context_id(url, project_id, token): # <3> |
| 80 | + response = requests.post( |
| 81 | + get_graphql_url(url), |
| 82 | + json={ |
| 83 | + "query": fetch_editing_context_query, |
| 84 | + "variables": {"projectId": project_id}, |
| 85 | + }, |
| 86 | + headers=build_headers(token), |
| 87 | + ) |
| 88 | + |
| 89 | + if response.status_code != 200: |
| 90 | + print(f"Error fetching editing context: {response.status_code} - {response.text}") |
| 91 | + return None |
| 92 | + |
| 93 | + data = response.json() |
| 94 | + if data.get("errors"): |
| 95 | + print_graphql_errors(data) |
| 96 | + return None |
| 97 | + |
| 98 | + project = data.get("data", {}).get("viewer", {}).get("project") |
| 99 | + if not project: |
| 100 | + print(f"Project not found: {project_id}") |
| 101 | + return None |
| 102 | + |
| 103 | + editing_context = project.get("currentEditingContext") |
| 104 | + if not editing_context: |
| 105 | + print(f"Editing context not found for project: {project_id}") |
| 106 | + return None |
| 107 | + |
| 108 | + return editing_context.get("id") |
| 109 | + |
| 110 | + |
| 111 | +def print_messages(messages): |
| 112 | + for message in messages: |
| 113 | + print(f"{message.get('level', 'INFO')}: {message.get('body', '')}") |
| 114 | + |
| 115 | + |
| 116 | +def import_sysml_file(url, file_path, editing_context_id, read_only, token): # <4> |
| 117 | + operation_id = str(uuid.uuid4()) |
| 118 | + operations = { |
| 119 | + "query": upload_document_mutation, |
| 120 | + "variables": { |
| 121 | + "input": { |
| 122 | + "id": operation_id, |
| 123 | + "editingContextId": editing_context_id, |
| 124 | + "file": None, |
| 125 | + "readOnly": read_only, |
| 126 | + } |
| 127 | + }, |
| 128 | + } |
| 129 | + file_map = { |
| 130 | + "0": "variables.file" |
| 131 | + } |
| 132 | + |
| 133 | + with open(file_path, "rb") as file: |
| 134 | + response = requests.post( # <5> |
| 135 | + get_graphql_upload_url(url), |
| 136 | + data={ |
| 137 | + "operations": json.dumps(operations), |
| 138 | + "map": json.dumps(file_map), |
| 139 | + }, |
| 140 | + files={ |
| 141 | + "0": (file_path.name, file, "text/plain"), |
| 142 | + }, |
| 143 | + headers=build_headers(token), |
| 144 | + ) |
| 145 | + |
| 146 | + if response.status_code not in (200, 201): |
| 147 | + print(f"Error importing SysML file: {response.status_code} - {response.text}") |
| 148 | + return False |
| 149 | + |
| 150 | + data = response.json() |
| 151 | + if data.get("errors"): |
| 152 | + print_graphql_errors(data) |
| 153 | + return False |
| 154 | + |
| 155 | + payload = data.get("data", {}).get("uploadDocument", {}) |
| 156 | + if payload.get("__typename") == "UploadDocumentSuccessPayload": |
| 157 | + print(f"SysML file imported successfully: {file_path}") |
| 158 | + report = payload.get("report") |
| 159 | + if report: |
| 160 | + print("Import report:") |
| 161 | + print(report) |
| 162 | + return True |
| 163 | + |
| 164 | + if payload.get("__typename") == "ErrorPayload": |
| 165 | + print_messages(payload.get("messages", [])) |
| 166 | + return False |
| 167 | + |
| 168 | + print(f"Unexpected response: {data}") |
| 169 | + return False |
| 170 | + |
| 171 | + |
| 172 | +def parse_arguments(): |
| 173 | + parser = argparse.ArgumentParser(description="Import a SysML textual file into a SysON project") |
| 174 | + parser.add_argument( |
| 175 | + "arguments", |
| 176 | + nargs="+", |
| 177 | + help="Either: file-path project-id, or: url file-path project-id", |
| 178 | + ) |
| 179 | + parser.add_argument( |
| 180 | + "--token", |
| 181 | + type=str, |
| 182 | + help="Bearer token used to authenticate on the SysON server", |
| 183 | + ) |
| 184 | + parser.add_argument( |
| 185 | + "--read-only", |
| 186 | + action="store_true", |
| 187 | + help="Import the uploaded document as read-only", |
| 188 | + ) |
| 189 | + args = parser.parse_args() |
| 190 | + |
| 191 | + if len(args.arguments) == 2: |
| 192 | + args.url = "http://localhost:8080" |
| 193 | + args.file_path = Path(args.arguments[0]) |
| 194 | + args.project_id = args.arguments[1] |
| 195 | + elif len(args.arguments) == 3: |
| 196 | + args.url = args.arguments[0] |
| 197 | + args.file_path = Path(args.arguments[1]) |
| 198 | + args.project_id = args.arguments[2] |
| 199 | + else: |
| 200 | + parser.error("expected either: file-path project-id, or: url file-path project-id") |
| 201 | + |
| 202 | + return args |
| 203 | + |
| 204 | + |
| 205 | +if __name__ == "__main__": |
| 206 | + args = parse_arguments() |
| 207 | + file_path = args.file_path.expanduser().resolve() |
| 208 | + |
| 209 | + if not file_path.is_file(): |
| 210 | + print(f"File not found: {file_path}") |
| 211 | + exit(1) |
| 212 | + |
| 213 | + editing_context_id = fetch_editing_context_id(args.url, args.project_id, args.token) # <6> |
| 214 | + if not editing_context_id: |
| 215 | + exit(1) |
| 216 | + |
| 217 | + if not import_sysml_file(args.url, file_path, editing_context_id, args.read_only, args.token): # <7> |
| 218 | + exit(1) |
0 commit comments