2
2
# SPDX-License-Identifier: MPL-2.0
3
3
4
4
import json
5
- from base64 import b64encode
5
+ import os
6
+ import sys
6
7
from dataclasses import dataclass
7
8
from typing import Dict , Optional , Union
8
9
from urllib import request
9
- from typegraph . graph . shared_types import BasicAuth
10
- from typegraph .wit import ArtifactResolutionConfig
10
+
11
+ from typegraph .gen . exports . utils import QueryDeployParams
11
12
from typegraph .gen .types import Err
13
+ from typegraph .graph .shared_types import BasicAuth
12
14
from typegraph .graph .typegraph import TypegraphOutput
13
- from typegraph .gen .exports .utils import QueryDeployParams
14
- from typegraph .wit import store , wit_utils
15
+ from typegraph .wit import ArtifactResolutionConfig , store , wit_utils
15
16
16
17
17
18
@dataclass
@@ -55,23 +56,58 @@ def tg_deploy(tg: TypegraphOutput, params: TypegraphDeployParams) -> DeployResul
55
56
headers ["Authorization" ] = params .auth .as_header_value ()
56
57
serialized = tg .serialize (params .artifacts_config )
57
58
59
+ res = wit_utils .gql_deploy_query (
60
+ store ,
61
+ params = QueryDeployParams (
62
+ tg = serialized ,
63
+ secrets = [(k , v ) for k , v in (params .secrets or {}).items ()],
64
+ ),
65
+ )
66
+
67
+ if isinstance (res , Err ):
68
+ raise Exception (res .value )
69
+
70
+ req = request .Request (
71
+ url = url ,
72
+ method = "POST" ,
73
+ headers = headers ,
74
+ data = res .value .encode (),
75
+ )
76
+
77
+ result = DeployResult (
78
+ serialized = serialized ,
79
+ typegate = handle_response (request .urlopen (req ).read ().decode ()),
80
+ )
81
+
58
82
# upload the referred files
59
- ref_files = core .get_ref_files (store )
60
- if isinstance (ref_files , Err ):
61
- raise Exception (ref_files .value )
83
+ # print("***************A")
84
+ ref_files = {
85
+ "662307922d7b879a17ce206d57db63b27630a7b4a8de2455a5730fb4bfe07a5c" : "file:rust.wasm"
86
+ }
87
+ # if isinstance(ref_files, Err):
88
+ # raise Exception(ref_files.value)
62
89
63
90
# TODO: fetch all the upload by one request
64
- get_upload_url = params .base_url + sep + "get-upload-url"
65
- for file_hash , file_path in ref_files .value :
91
+ get_upload_url = params .base_url + sep + tg .name + "/get-upload-url"
92
+ for file_hash , file_path in ref_files .items ():
93
+ prefix = "file:"
94
+ if not file_path .startswith (prefix ):
95
+ raise Exception (f"file path { file_path } should start with { prefix } " )
96
+
97
+ curr_dir = os .path .dirname (os .path .abspath (sys .argv [0 ]))
98
+ file_path = os .path .join (curr_dir , file_path [len (prefix ) :])
99
+
66
100
with open (file_path , "rb" ) as file :
67
101
file_content = file .read ()
68
102
artifact = UploadArtifactMeta (
69
- name = file . name ,
103
+ name = os . path . basename ( file_path ) ,
70
104
file_hash = file_hash ,
71
105
file_size_in_bytes = len (file_content ),
72
106
)
107
+
108
+ artifact_json = json .dumps (artifact .__dict__ ).encode ()
73
109
req = request .Request (
74
- url = get_upload_url , method = "GET " , headers = headers , data = artifact
110
+ url = get_upload_url , method = "PUT " , headers = headers , data = artifact_json
75
111
)
76
112
77
113
response = handle_response (request .urlopen (req ).read ().decode ())
@@ -84,27 +120,7 @@ def tg_deploy(tg: TypegraphOutput, params: TypegraphDeployParams) -> DeployResul
84
120
)
85
121
response = request .urlopen (upload_req )
86
122
87
- res = wit_utils .gql_deploy_query (
88
- store ,
89
- params = QueryDeployParams (
90
- tg = serialized ,
91
- secrets = [(k , v ) for k , v in (params .secrets or {}).items ()],
92
- ),
93
- )
94
-
95
- if isinstance (res , Err ):
96
- raise Exception (res .value )
97
-
98
- req = request .Request (
99
- url = url ,
100
- method = "POST" ,
101
- headers = headers ,
102
- data = res .value .encode (),
103
- )
104
- return DeployResult (
105
- serialized = serialized ,
106
- typegate = handle_response (request .urlopen (req ).read ().decode ()),
107
- )
123
+ return result
108
124
109
125
110
126
def tg_remove (tg : TypegraphOutput , params : TypegraphRemoveParams ):
0 commit comments