jax-pyjit 0.1.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
jax/pyjit/__init__.py ADDED
@@ -0,0 +1,98 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one
2
+ # or more contributor license agreements. See the NOTICE file
3
+ # distributed with this work for additional information
4
+ # regarding copyright ownership. The ASF licenses this file
5
+ # to you under the Apache License, Version 2.0 (the
6
+ # "License"); you may not use this file except in compliance
7
+ # with the License. You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+ #
18
+ # Copyright [2025] The Jackson Laboratory
19
+ try:
20
+ import pkg_resources
21
+
22
+ pkg_resources.declare_namespace(__name__)
23
+ except ImportError:
24
+ import pkgutil
25
+
26
+ __path__ = pkgutil.extend_path(__path__, __name__)
27
+
28
+ import os
29
+
30
+ from typing import List
31
+
32
+ class SharedConfig:
33
+ """Config and Settings Configuration."""
34
+
35
+ DEBUG: bool = bool(os.getenv("DEBUG", None))
36
+ TESTING: bool = bool(os.getenv("TESTING", None))
37
+
38
+ USE_NIO_STORAGE: bool = bool(os.getenv("USE_NIO_STORAGE", None))
39
+ NIO_STORAGE_ROOT: str = os.getenv("NIO_STORAGE_ROOT", None)
40
+
41
+ LOG_LEVEL: str = os.getenv("LOG_LEVEL", "WARNING")
42
+ TEMPORAL_NAMESPACE: str = os.getenv("TEMPORAL_NAMESPACE", "default")
43
+ TEMPORAL_URI: str = os.getenv(
44
+ "TEMPORAL_URI", "localhost:7233"
45
+ ) # Deploy should set to CS cluster location.
46
+ CACHE_DIR: str = os.getenv("CACHE_DIR", "cache")
47
+
48
+ TEST_BUCKET_NAME: str = os.getenv("TEST_BUCKET_NAME", None)
49
+
50
+ DOWNLOAD_TIMEOUT: int = int(os.getenv("DOWNLOAD_TIMEOUT", "1200"))
51
+ DOWNLOAD_ATTEMPTS: int = int(os.getenv("DOWNLOAD_ATTEMPTS", "4"))
52
+
53
+ UPLOAD_TIMEOUT: int = int(os.getenv("UPLOAD_TIMEOUT", "1200"))
54
+ UPLOAD_ATTEMPTS: int = int(os.getenv("UPLOAD_ATTEMPTS", "4"))
55
+
56
+ CLEAN_TIMEOUT: int = int(os.getenv("CLEAN_TIMEOUT", "300"))
57
+ CLEAN_ATTEMPTS: int = int(os.getenv("CLEAN_ATTEMPTS", "4"))
58
+
59
+ # IO operations which come from other workers.
60
+ IO_STORE_QUEUE = os.getenv("STORE_QUEUE", "STORE_TASK_QUEUE")
61
+ IO_DOWNLOAD_OBJECT = os.getenv("IO_DOWNLOAD_OBJECT", "io-temporal-download-object")
62
+ IO_UPLOAD = os.getenv("IO_UPLOAD", "io-temporal-upload")
63
+ IO_CLEAN = os.getenv("IO_CLEAN", "io-temporal-clean")
64
+
65
+
66
+ shared_config = SharedConfig()
67
+
68
+
69
+ def set_test_defaults():
70
+ import os
71
+
72
+ os.environ["STORE_QUEUE"] = "TEST"
73
+ os.environ["DOWNLOAD_TIMEOUT"] = "30"
74
+ os.environ["DOWNLOAD_ATTEMPTS"] = "1"
75
+ os.environ["UPLOAD_TIMEOUT"] = "30"
76
+ os.environ["UPLOAD_ATTEMPTS"] = "1"
77
+ os.environ["CLEAN_TIMEOUT"] = "30"
78
+ os.environ["CLEAN_ATTEMPTS"] = "1"
79
+
80
+ shared_config.STORE_QUEUE = "TEST"
81
+ shared_config.DOWNLOAD_TIMEOUT = 30
82
+ shared_config.DOWNLOAD_ATTEMPTS = 1
83
+ shared_config.UPLOAD_TIMEOUT = 30
84
+ shared_config.UPLOAD_ATTEMPTS = 1
85
+ shared_config.CLEAN_TIMEOUT = 30
86
+ shared_config.CLEAN_ATTEMPTS = 1
87
+
88
+
89
+ class Settings:
90
+ """Settings class for Auth Client."""
91
+
92
+ AUTH_DOMAIN: str = "thejacksonlaboratory.auth0.com"
93
+ AUTH_CLIENT_ID: str = "f8QZPcIrPIG6DIeWR2Rr3C8X5bzx8zBz"
94
+ AUTH_ALGORITHMS: List[str] = ["RS256"]
95
+ AUTH_SCOPES: List[str] = ["openid", "profile", "email"]
96
+ AUTH_AUDIENCE: str = "https://cube.jax.org"
97
+
98
+ auth_settings = Settings()
@@ -0,0 +1,101 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one
2
+ # or more contributor license agreements. See the NOTICE file
3
+ # distributed with this work for additional information
4
+ # regarding copyright ownership. The ASF licenses this file
5
+ # to you under the Apache License, Version 2.0 (the
6
+ # "License"); you may not use this file except in compliance
7
+ # with the License. You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+ #
18
+ # Copyright [2025] The Jackson Laboratory
19
+ import os
20
+
21
+ from temporalio import activity
22
+ from temporalio.activity import logger
23
+
24
+ from jax.cs.storage import StorageObject
25
+ from jax.pyjit.dao import StorageKey
26
+
27
+
28
+ from pathlib import Path
29
+ import shutil
30
+
31
+ ###############################################################################
32
+ # These activities are provided by universal activities when deployed.
33
+ # We have test versions here to make sure they are implemented in test mode.
34
+ # We use the term 'dummy' to indicate these are not the real activities.
35
+ ###############################################################################
36
+
37
+
38
+ @activity.defn(name="io-temporal-download-object")
39
+ async def dummy_download_object(
40
+ key: StorageKey, run_id: str, overwrite: bool, ignoreNotExisting: bool
41
+ ) -> str:
42
+
43
+ try:
44
+ dir: Path = get_dir(run_id)
45
+ file: Path = Path(dir, key.object)
46
+ os.makedirs(file.parent, exist_ok=True)
47
+ file.touch()
48
+
49
+ uri: str = key.to_uri()
50
+ wrapped = StorageObject(uri)
51
+
52
+ with open(file, "wb") as download_file:
53
+ data = wrapped.download_as_bytes()
54
+ download_file.write(data)
55
+
56
+ return str(file)
57
+ except Exception as e:
58
+ logger.error(e)
59
+ raise e
60
+
61
+
62
+ @activity.defn(name="io-temporal-upload")
63
+ async def dummy_upload(key: StorageKey, file: str, replace: bool) -> str:
64
+
65
+ try:
66
+ if not os.path.exists(file):
67
+ return
68
+
69
+ local_wrapped: StorageObject = StorageObject(file)
70
+ uri: str = key.to_uri(
71
+ create=True
72
+ ) # Test uri will create local file, otherwise, create does nothing.
73
+ remote_wrapped: StorageObject = StorageObject(uri)
74
+ remote_wrapped.replace(local_wrapped)
75
+ return uri
76
+
77
+ except Exception as e:
78
+ logger.error(e)
79
+ raise e
80
+
81
+
82
+ @activity.defn(name="io-temporal-clean")
83
+ async def dummy_clean(run_id: str) -> None:
84
+
85
+ try:
86
+ dir: Path = get_dir(run_id)
87
+ shutil.rmtree(dir)
88
+
89
+ except Exception as e:
90
+ logger.error(e)
91
+ raise e
92
+
93
+
94
+ def get_dir(run_id: str) -> Path:
95
+
96
+ CACHE_DIR: str = os.getenv("CACHE_DIR", "cache")
97
+ cache: Path = Path(CACHE_DIR)
98
+ dir: Path = Path(cache, run_id)
99
+ # Make dirs if not existing
100
+ os.makedirs(dir, exist_ok=True)
101
+ return dir
jax/pyjit/auth.py ADDED
@@ -0,0 +1,271 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one
2
+ # or more contributor license agreements. See the NOTICE file
3
+ # distributed with this work for additional information
4
+ # regarding copyright ownership. The ASF licenses this file
5
+ # to you under the Apache License, Version 2.0 (the
6
+ # "License"); you may not use this file except in compliance
7
+ # with the License. You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+ #
18
+ # Copyright [2025] The Jackson Laboratory
19
+
20
+ # @see https://github.com/TheJacksonLaboratory/geneweaver-client/blob/main/src/geneweaver/client/auth.py
21
+
22
+ import time
23
+ from typing import Any, Dict, Optional
24
+
25
+ import jwt
26
+ import requests
27
+ from auth0.authentication.token_verifier import (
28
+ AsymmetricSignatureVerifier,
29
+ TokenVerifier,
30
+ )
31
+ from jax.pyjit import auth_settings
32
+
33
+ import json
34
+ from pathlib import Path
35
+
36
+ import typer
37
+
38
+ class AuthenticationError(Exception):
39
+ """Raised when authentication fails."""
40
+
41
+ def check_auth()-> Optional[str]:
42
+ """Check if the user is authenticated.
43
+
44
+ :returns: The access token if the user is authenticated, None otherwise.
45
+ """
46
+ try:
47
+ access_token = get_access_token()
48
+ if access_token is None:
49
+ login()
50
+ return get_access_token()
51
+
52
+ if (access_token_expired(access_token)):
53
+ login()
54
+ return get_access_token()
55
+
56
+ return access_token
57
+
58
+ except Exception as e:
59
+ print("Authentication error: ", str(e))
60
+ login()
61
+ return get_access_token()
62
+
63
+ def login() -> Optional[Dict[str, any]]:
64
+
65
+
66
+ """Run the device authorization flow."""
67
+ device_code_data = _get_device_code_data()
68
+ _print_device_code_instructions(device_code_data)
69
+ token_data: Optional[Dict[str, any]] = _poll_for_flow_completion(device_code_data)
70
+ save_auth_token(token_data)
71
+ return token_data
72
+
73
+ def get_id_token() -> Optional[str]:
74
+ """Get the ID token from the authentication token file.
75
+
76
+ :returns: The ID token.
77
+ """
78
+ return _get_token_data_value_or_none("id_token")
79
+
80
+
81
+ def get_access_token() -> Optional[str]:
82
+ """Get the Access token from the authentication token file.
83
+
84
+ :returns: The ID token.
85
+ """
86
+ token = _get_token_data_value_or_none("access_token")
87
+ if access_token_expired(token):
88
+ refresh_token()
89
+ token = _get_token_data_value_or_none("access_token")
90
+ return token
91
+
92
+
93
+ def _get_token_data_value_or_none(token_data_key: str) -> Optional[str]:
94
+ token_data = get_auth_token()
95
+
96
+ if token_data is None:
97
+ return None
98
+
99
+ return token_data.get(token_data_key)
100
+
101
+
102
+ def validate_token(token: str) -> None:
103
+ """Verify the token and its precedence.
104
+
105
+ :param token:
106
+ """
107
+ jwks_url = "https://{}/.well-known/jwks.json".format(auth_settings.AUTH_DOMAIN)
108
+ issuer = "https://{}/".format(auth_settings.AUTH_DOMAIN)
109
+ sv = AsymmetricSignatureVerifier(jwks_url)
110
+ tv = TokenVerifier(
111
+ signature_verifier=sv, issuer=issuer, audience=auth_settings.AUTH_CLIENT_ID
112
+ )
113
+ tv.verify(token)
114
+
115
+
116
+ def access_token_expired(access_token: str) -> bool:
117
+ """Check if the access token is unexpired."""
118
+ token_data = get_auth_token()
119
+ try:
120
+ jwt.decode(
121
+ token_data["access_token"],
122
+ algorithms=auth_settings.AUTH_ALGORITHMS,
123
+ options={
124
+ "verify_signature": False,
125
+ "verify_exp": True,
126
+ },
127
+ )
128
+ return False
129
+ except jwt.ExpiredSignatureError:
130
+ return True
131
+
132
+
133
+ def refresh_token() -> None:
134
+ """Refresh the access token."""
135
+ token_data = get_auth_token()
136
+ refresh_token = token_data["refresh_token"]
137
+ payload = {
138
+ "grant_type": "refresh_token",
139
+ "client_id": auth_settings.AUTH_CLIENT_ID,
140
+ "refresh_token": refresh_token,
141
+ }
142
+ response = requests.post(
143
+ "https://{}/oauth/token".format(auth_settings.AUTH_DOMAIN), data=payload
144
+ )
145
+ token_data = response.json()
146
+ token_data["refresh_token"] = get_auth_token()["refresh_token"]
147
+ save_auth_token(token_data)
148
+
149
+
150
+ def current_user(id_token: str) -> Dict[str, str]:
151
+ """Get the current user from the ID token."""
152
+ return jwt.decode(
153
+ id_token,
154
+ algorithms=auth_settings.AUTH_ALGORITHMS,
155
+ options={"verify_signature": False},
156
+ )
157
+
158
+
159
+ def _device_code_payload() -> Dict[str, str]:
160
+ return {
161
+ "client_id": auth_settings.AUTH_CLIENT_ID,
162
+ "audience": auth_settings.AUTH_AUDIENCE,
163
+ "scope": " ".join(auth_settings.AUTH_SCOPES),
164
+ }
165
+
166
+
167
+ def _get_device_code_data() -> dict:
168
+ device_code_response = requests.post(
169
+ "https://{}/oauth/device/code".format(auth_settings.AUTH_DOMAIN),
170
+ data=_device_code_payload(),
171
+ )
172
+
173
+ if device_code_response.status_code != 200:
174
+ raise AuthenticationError("Error generating the device code")
175
+
176
+ return device_code_response.json()
177
+
178
+
179
+ def _print_device_code_instructions(device_code_data: Dict[str, Any]) -> None:
180
+ print(
181
+ "1. On your computer or mobile device navigate to: ",
182
+ device_code_data["verification_uri_complete"],
183
+ )
184
+ print("2. Enter the following code: ", device_code_data["user_code"])
185
+
186
+
187
+ def _token_payload(device_code: str) -> Dict[str, str]:
188
+ return {
189
+ "grant_type": "urn:ietf:params:oauth:grant-type:device_code",
190
+ "device_code": device_code,
191
+ "scope": "offline_access",
192
+ "client_id": auth_settings.AUTH_CLIENT_ID,
193
+ }
194
+
195
+
196
+ def _poll_for_flow_completion(
197
+ device_code_data: Dict[str, Any]
198
+ ) -> Optional[Dict[str, any]]:
199
+ authenticated = False
200
+ token_data = None
201
+ while not authenticated:
202
+ token_response = requests.post(
203
+ "https://{}/oauth/token".format(auth_settings.AUTH_DOMAIN),
204
+ data=_token_payload(device_code_data["device_code"]),
205
+ )
206
+
207
+ token_data = token_response.json()
208
+ if token_response.status_code == 200:
209
+ print("Authenticated!")
210
+ print("- Id Token: {}...".format(token_data["id_token"][:10]))
211
+ validate_token(token_data["id_token"])
212
+ authenticated = True
213
+ elif token_data["error"] not in ("authorization_pending", "slow_down"):
214
+ raise AuthenticationError(token_data["error_description"])
215
+ else:
216
+ time.sleep(device_code_data["interval"])
217
+
218
+ return token_data
219
+
220
+
221
+
222
+ def get_config_dir() -> Path:
223
+ """Get the path to the configuration directory.
224
+
225
+ :returns: The path to the configuration directory.
226
+ """
227
+ return Path(typer.get_app_dir("pyjit"))
228
+
229
+
230
+ def get_config_file() -> Path:
231
+ """Get the path to the configuration file.
232
+
233
+ :returns: The path to the configuration file.
234
+ """
235
+ return get_config_dir() / "config.json"
236
+
237
+
238
+ def get_auth_token_file() -> Path:
239
+ """Get the path to the authentication token file.
240
+
241
+ :returns: The path to the authentication token file.
242
+ """
243
+ return get_config_dir() / "auth_token.json"
244
+
245
+
246
+ def get_auth_token() -> Optional[dict]:
247
+ """Get the authentication token data from the authentication token file.
248
+
249
+ :returns: The authentication token.
250
+ """
251
+ auth_token_file = get_auth_token_file()
252
+
253
+ if not auth_token_file.is_file():
254
+ return None
255
+
256
+ with open(auth_token_file, "r") as f:
257
+ token = json.load(f)
258
+
259
+ return token
260
+
261
+
262
+ def save_auth_token(token: dict) -> None:
263
+ """Save the authentication token to the authentication token file.
264
+
265
+ :param token: The authentication token.
266
+ """
267
+ auth_token_file = get_auth_token_file()
268
+ auth_token_file.parent.mkdir(parents=True, exist_ok=True)
269
+
270
+ with open(auth_token_file, "w") as f:
271
+ json.dump(token, f)