Skip to content

Commit c143685

Browse files
authored
Merge pull request #373 from ExaWorks/patch_testing_url
Patch testing url
2 parents 3a15a55 + dab10ef commit c143685

File tree

2 files changed

+98
-15
lines changed

2 files changed

+98
-15
lines changed

testing.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ custom_attributes =
130130
#
131131
# server_url = <url>
132132

133-
server_url = https://psij.testing.exaworks.org
133+
server_url = https://testing.psij.io
134134

135135

136136
#

tests/ci_runner.py

+97-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from contextlib import contextmanager
99
from pathlib import Path
1010
from tempfile import TemporaryDirectory
11-
from typing import Dict, TextIO, List, Optional, Generator
11+
from typing import Dict, TextIO, List, Optional, Generator, Callable
1212

1313
import requests
1414

@@ -17,6 +17,7 @@
1717
FAKE_BRANCHES = ['main', 'feature_1', 'feature_x']
1818
GITHUB_API_ROOT = 'https://api.github.com'
1919
MODE = 'plain'
20+
TARGET_PATCH_LEVEL = 2
2021

2122

2223
def read_line(f: TextIO) -> Optional[str]:
@@ -170,46 +171,129 @@ def run_tests(conf: Dict[str, str], site_ids: List[str], branches: List[str], cl
170171
install_deps(branch, tmpp)
171172
with info('Testing branch "%s"' % branch):
172173
run_branch_tests(conf, tmpp, run_id, clone, site_id, branch)
174+
# Patching routines
173175

174176

175-
OLD_REPO = 'ExaWorks/psi-j-python'
176-
NEW_REPO = 'ExaWorks/psij-python'
177+
def write_patch_level(level: int) -> None:
178+
with open('.ci.patchlevel', 'w') as f:
179+
f.write(str(level))
177180

178181

179-
def patch_file(file_name: str) -> None:
180-
if os.path.exists(file_name + '.is_patched'):
182+
def current_patch_level() -> int:
183+
try:
184+
with open('.ci.patchlevel') as f:
185+
return int(f.read().strip())
186+
except OSError:
187+
for fn in ['testing.conf', 'psij-ci-run']:
188+
if not Path(fn + '.is_patched').exists():
189+
return 0
190+
write_patch_level(1)
191+
return 1
192+
193+
194+
def deploy_patch(level: int) -> None:
195+
if level == 1:
196+
l1_patch_repo()
197+
l1_update_origin()
198+
elif level == 2:
199+
l2_remove_patch_flag_files()
200+
l2_update_upload_url()
201+
else:
202+
raise Exception('Nothing to do for patch level %s' % level)
203+
write_patch_level(level)
204+
205+
206+
def try_patch(level: int) -> None:
207+
if level <= current_patch_level():
181208
return
209+
else:
210+
deploy_patch(level)
211+
212+
213+
def deploy_patches() -> None:
214+
for level in range(1, TARGET_PATCH_LEVEL + 1):
215+
try_patch(level)
216+
182217

218+
def line_patcher(file_name: str, matcher: Callable[[str], bool],
219+
mutator: Callable[[str], str]) -> None:
183220
with info('Patching %s' % file_name):
184221
with open(file_name) as inf:
185222
with open(file_name + '._new_', 'w') as outf:
186223
for line in inf:
187224
# strip new line
188-
if line.find(OLD_REPO) != -1:
225+
if matcher(line):
189226
# we're adding one space so that the line has the same length;
190227
# when invoking a subprocess, bash stores the location where
191228
# it's supposed to continue parsing from, so it's a good idea
192229
# to to not move things around
193-
line = line.rstrip('\n').replace(OLD_REPO, NEW_REPO) + ' \n'
230+
line = mutator(line)
194231
outf.write(line)
195232
os.chmod(file_name + '._new_', os.stat(file_name).st_mode)
196233
os.rename(file_name + '._new_', file_name)
197-
Path(file_name + '.is_patched').touch()
198234

199235

200-
def patch_repo() -> None:
201-
patch_file('testing.conf')
202-
patch_file('psij-ci-run')
236+
# Patch 1
237+
# Updates repositories in testing.conf and psij-ci-run. It also
238+
# updates the git origin to point to the new repo. This is done to
239+
# account for the fact that we renamed the repo from psi-j-python
240+
# to psij-python.
241+
242+
OLD_REPO = 'ExaWorks/psi-j-python'
243+
NEW_REPO = 'ExaWorks/psij-python'
244+
245+
246+
def l1_patch_file(file_name: str) -> None:
247+
if os.path.exists(file_name + '.is_patched'):
248+
return
249+
line_patcher(file_name,
250+
lambda line: line.find(OLD_REPO) != -1,
251+
# The extra space before the newline is to not shift the content
252+
# of psij-ci-run that follows this line. Bash continues reading
253+
# the file after a command completes, but, if the content has
254+
# shifted, it might end up reading a partial line.
255+
lambda line: line.rstrip('\n').replace(OLD_REPO, NEW_REPO) + ' \n')
203256

204257

205-
def update_origin() -> None:
258+
def l1_patch_repo() -> None:
259+
l1_patch_file('testing.conf')
260+
l1_patch_file('psij-ci-run')
261+
262+
263+
def l1_update_origin() -> None:
206264
old_url = run('git', 'config', '--get', 'remote.origin.url')
207265
new_url = old_url.strip().replace(OLD_REPO, NEW_REPO)
208266
if new_url != old_url:
209267
with info('Updating git url to %s' % new_url):
210268
run('git', 'remote', 'set-url', 'origin', new_url)
211269

212270

271+
# Patch 2
272+
# Updates the test upload url from either testing.exaworks.org or
273+
# psij.testing.exaworks.org to testing.psij.io
274+
275+
OLD_UPLOAD_URLS = ['https://psij.testing.exaworks.org', 'https://testing.exaworks.org']
276+
NEW_UPLOAD_URL = 'https://testing.psij.io'
277+
278+
279+
def l2_remove_patch_flag_files() -> None:
280+
# we're using a single patch level file now
281+
for fn in ['testing.conf', 'psij-ci-run']:
282+
f = Path(fn + '.is_patched')
283+
if f.exists():
284+
f.unlink()
285+
286+
287+
def l2_update_upload_url() -> None:
288+
for old_url in OLD_UPLOAD_URLS:
289+
line_patcher('testing.conf',
290+
lambda line: line.find('server_url') != -1
291+
and line.find(old_url) != -1, # noqa: E127
292+
lambda line: line.rstrip('\n').replace(old_url, NEW_UPLOAD_URL) + '\n')
293+
294+
# End of patches
295+
296+
213297
@contextmanager
214298
def info(msg: str) -> Generator[bool, None, None]:
215299
print(msg + '... ', end='')
@@ -246,6 +330,5 @@ def info(msg: str) -> Generator[bool, None, None]:
246330
else:
247331
raise ValueError('Unrecognized value for scope: "%s"' % scope)
248332

249-
patch_repo()
250-
update_origin()
333+
deploy_patches()
251334
run_tests(conf, site_ids, branches, clone)

0 commit comments

Comments
 (0)