Skip to content

Commit 7b3d120

Browse files
chore: fix command execution when job_path is not cwd
1 parent c91c839 commit 7b3d120

9 files changed

Lines changed: 2963 additions & 2949 deletions

pixi.lock

Lines changed: 2914 additions & 2918 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dirac_cwl/commands/create_failover_request.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import logging
88
import os
9+
from pathlib import Path
910

1011
from DIRAC.AccountingSystem.Client.DataStoreClient import DataStoreClient
1112
from DIRAC.Core.Utilities.ReturnValues import SErrorException, returnValueOrRaise
@@ -76,7 +77,7 @@ def _execute(self, job_path: os.PathLike[str], workflow_commons: WorkflowCommons
7677
if workflow_commons.step_status == StepStatus.Done:
7778
self.job_report.setApplicationStatus("Job Finished Successfully", True)
7879

79-
self.generate_failover_file(workflow_commons)
80+
self.generate_failover_file(job_path, workflow_commons)
8081

8182
def _resolve_clients(self, workflow_commons: WorkflowCommons):
8283
super()._resolve_clients(workflow_commons)
@@ -87,7 +88,7 @@ def _resolve_clients(self, workflow_commons: WorkflowCommons):
8788
if not self.dsc:
8889
self.dsc = DataStoreClient()
8990

90-
def generate_failover_file(self, workflow_commons: WorkflowCommons):
91+
def generate_failover_file(self, job_path: os.PathLike[str], workflow_commons: WorkflowCommons):
9192
"""Create a request.json file."""
9293
try:
9394
diset_op = returnValueOrRaise(self.job_report.generateForwardDISET())
@@ -114,7 +115,9 @@ def generate_failover_file(self, workflow_commons: WorkflowCommons):
114115
request_json_content = returnValueOrRaise(self.request.toJSON())
115116

116117
# Write it
117-
fname = f"{workflow_commons.production_id}_{workflow_commons.prod_job_id}_request.json"
118+
fname = Path(job_path).joinpath(
119+
f"{workflow_commons.production_id}_{workflow_commons.prod_job_id}_request.json"
120+
)
118121
with open(fname, "w", encoding="utf-8") as f:
119122
json.dump(request_json_content, f)
120123

src/dirac_cwl/commands/report_bookkeeping.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import copy
44
import logging
55
import os
6+
from pathlib import Path
67
from typing import Any, Dict
78

89
from DIRAC.Core.Utilities.ReturnValues import SErrorException, returnValueOrRaise
@@ -102,6 +103,7 @@ def _execute_for_step(
102103
workflow_commons.prod_job_id,
103104
step_commons.number,
104105
step_commons.id,
106+
path=job_path,
105107
)
106108

107109
info_dict = {
@@ -145,9 +147,10 @@ def _execute_for_step(
145147
step_commons.xf_o,
146148
step_commons.outputs,
147149
step_commons.inputs,
148-
step_commons.size,
149-
step_commons.md5,
150-
step_commons.guid,
150+
path=job_path,
151+
size_map=step_commons.size,
152+
md5_map=step_commons.md5,
153+
guid_map=step_commons.guid,
151154
)
152155

153156
# Generate SimulationConditions
@@ -158,7 +161,7 @@ def _execute_for_step(
158161
doc = job_info.to_xml()
159162

160163
# Write to file
161-
bfilename = f"bookkeeping_{step_commons.id}.xml"
164+
bfilename = Path(job_path).joinpath(f"bookkeeping_{step_commons.id}.xml")
162165
with open(bfilename, "wb") as bfile:
163166
bfile.write(doc)
164167

src/dirac_cwl/commands/upload_log_file.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import os
55
import shlex
6+
from pathlib import Path
67

78
from DIRAC.ConfigurationSystem.Client.Helpers.Operations import Operations
89
from DIRAC.Core.Utilities.ReturnValues import SErrorException, returnSingleResult, returnValueOrRaise
@@ -75,7 +76,7 @@ def _execute(self, job_path: os.PathLike[str], workflow_commons: WorkflowCommons
7576
_prepareRequest(self.request, workflow_commons.job_id)
7677

7778
try:
78-
file_list = returnValueOrRaise(systemCall(0, shlex.split("ls -al")))
79+
file_list = returnValueOrRaise(systemCall(0, shlex.split(f"ls -al {str(job_path)}")))
7980
if file_list:
8081
logger.info("The contents of the working directory...")
8182
logger.info(str(file_list[1]))
@@ -85,7 +86,7 @@ def _execute(self, job_path: os.PathLike[str], workflow_commons: WorkflowCommons
8586
logger.error("Failed to list the log directory\n%s", e)
8687

8788
workflow_commons.log_dir = os.path.realpath(
88-
os.path.join(job_path, f"./job/log/{workflow_commons.production_id}/{workflow_commons.prod_job_id}")
89+
Path(job_path).joinpath("job", "log", workflow_commons.production_id, workflow_commons.production_id)
8990
)
9091
logger.info("Selected log files will be temporarily stored in %s", workflow_commons.log_dir)
9192

@@ -94,7 +95,7 @@ def _execute(self, job_path: os.PathLike[str], workflow_commons: WorkflowCommons
9495
logger.info("Determining the files to be saved in the logs.")
9596

9697
try:
97-
selected_files = returnValueOrRaise(_determineRelevantFiles(log_extensions))
98+
selected_files = returnValueOrRaise(_determineRelevantFiles(log_extensions, path=job_path))
9899
except SErrorException as e:
99100
logger.error("Completely failed to select relevant log files.", exc_info=e)
100101
return # Does not fail
@@ -123,7 +124,7 @@ def _execute(self, job_path: os.PathLike[str], workflow_commons: WorkflowCommons
123124

124125
# zip all files
125126
try:
126-
zip_file_name = returnValueOrRaise(_zip_files(workflow_commons.prod_job_id, selected_files))
127+
zip_file_name = returnValueOrRaise(_zip_files(workflow_commons.prod_job_id, selected_files, path=job_path))
127128
except SErrorException as e:
128129
logger.error("Failed to create zip of log files %s", e)
129130
self.job_report.setApplicationStatus("Failed to create zip of log files")
@@ -133,7 +134,7 @@ def _execute(self, job_path: os.PathLike[str], workflow_commons: WorkflowCommons
133134

134135
# logFilePath is something like /lhcb/MC/2016/LOG/00095376/0000/
135136
# the zipFileName should have the same name, e.g. 00000381.zip
136-
zip_path = os.path.join(workflow_commons.log_file_path, zip_file_name)
137+
zip_path = Path(job_path).joinpath(workflow_commons.log_file_path, zip_file_name)
137138
log_https_url = _get_log_url(log_se, zip_path)
138139

139140
logger.info("putFile %s to %s", zip_file_name, log_se)
@@ -150,10 +151,7 @@ def _execute(self, job_path: os.PathLike[str], workflow_commons: WorkflowCommons
150151
try:
151152
upload_result_dict = returnValueOrRaise(
152153
_uploadLogToFailoverSE(
153-
self.failover_transfer,
154-
zip_file_name,
155-
log_lfn_path,
156-
workflow_commons.site_name,
154+
self.failover_transfer, zip_file_name, log_lfn_path, workflow_commons.site_name, path=job_path
157155
)
158156
)
159157

src/dirac_cwl/commands/upload_output_data.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import random
66
import time
7+
from pathlib import Path
78

89
from DIRAC.Core.Utilities.ReturnValues import SErrorException, returnValueOrRaise
910
from DIRAC.DataManagementSystem.Client.DataManager import DataManager
@@ -75,6 +76,7 @@ def _execute(self, job_path: os.PathLike[str], workflow_commons: WorkflowCommons
7576
workflow_commons.output_data_file_mask,
7677
workflow_commons.output_data_step,
7778
workflow_commons.output_SEs,
79+
path=job_path,
7880
)
7981

8082
if not file_metadata:
@@ -118,7 +120,7 @@ def _execute(self, job_path: os.PathLike[str], workflow_commons: WorkflowCommons
118120
self.file_report.setFileStatus(int(workflow_commons.production_id), lfns_with_descendents, "Processed")
119121
raise WorkflowProcessingException("Input Data Already Processed")
120122

121-
bk_files = _getBKFiles()
123+
bk_files = _getBKFiles(path=job_path)
122124
logger.info("The following BK records will be sent\n%s", ", ".join(bk_files))
123125

124126
for bk_file in bk_files:
@@ -134,7 +136,7 @@ def _execute(self, job_path: os.PathLike[str], workflow_commons: WorkflowCommons
134136
logger.info("Preparing DISET request for %s", bk_file)
135137

136138
logger.info("Creating DISABLE_WATCHDOG_CPU_WALLCLOCK_CHECK in order to disable the Watchdog")
137-
with open("DISABLE_WATCHDOG_CPU_WALLCLOCK_CHECK", "w") as f:
139+
with open(Path(job_path).joinpath("DISABLE_WATCHDOG_CPU_WALLCLOCK_CHECK"), "w") as f:
138140
f.write(f"{time.asctime()}")
139141

140142
perform_bk_registration = []
@@ -143,7 +145,9 @@ def _execute(self, job_path: os.PathLike[str], workflow_commons: WorkflowCommons
143145
for file_name, metadata in final.items():
144146
target_se = metadata["resolvedSE"]
145147

146-
logger.info("Attempting to store file to SE %s to the following SE(s):\n%s", file_name, ", ".join(target_se))
148+
logger.info(
149+
"Attempting to store file to SE %s to the following SE(s):\n%s", file_name, ", ".join(target_se)
150+
)
147151

148152
file_meta_dict = _createMetaDict(metadata)
149153

test/conftest.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Common pytest fixture used by test modules."""
22

3+
import shutil
34
from pathlib import Path
45

56
import pytest
@@ -56,7 +57,16 @@ def sample_job(sample_command_line_tool):
5657
@pytest.fixture
5758
def job_path():
5859
"""Job Path Fixture."""
59-
return Path(".")
60+
path = Path(".").joinpath("examplepath")
61+
62+
if path.exists():
63+
raise Exception("NON EMPTY DIRECTORY !!!")
64+
65+
path.mkdir()
66+
67+
yield path
68+
69+
shutil.rmtree(path)
6070

6171

6272
@pytest.fixture

test/test_command_bookkeeping_report.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_report_bk_prod_mcsimulation_success(
8282
wf_commons["steps"][0]["outputs"] = [
8383
{"outputDataName": "00209455_00001537_1.sim", "outputDataType": "sim"},
8484
]
85-
Path(wf_commons["steps"][0]["outputs"][0]["outputDataName"]).touch()
85+
Path(job_path).joinpath(wf_commons["steps"][0]["outputs"][0]["outputDataName"]).touch()
8686

8787
# Mock the XMLSummary object
8888
xml_content = dedent("""\
@@ -327,8 +327,8 @@ def test_report_bk_prod_mcreconstruction_success(
327327
{"outputDataName": "00209455_00001537_1", "outputDataType": "digi"},
328328
]
329329
wf_commons["steps"][0]["application_log"] = "application.log"
330-
Path(wf_commons["steps"][0]["application_log"]).touch()
331-
Path(wf_commons["steps"][0]["outputs"][0]["outputDataName"]).touch()
330+
Path(job_path).joinpath(wf_commons["steps"][0]["application_log"]).touch()
331+
Path(job_path).joinpath(wf_commons["steps"][0]["outputs"][0]["outputDataName"]).touch()
332332

333333
# Mock the XMLSummary object
334334
xml_content = dedent("""\

test/test_command_upload_log_file.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def uplogfile(self, mocker: MockerFixture, wf_commons, job_path):
3535
Path(job_path).joinpath(f"{wf_commons['prod_job_id']}.zip").unlink(missing_ok=True)
3636
Path(job_path).joinpath("workflow_commons.json").unlink(missing_ok=True)
3737

38-
shutil.rmtree(Path(job_path).joinpath("unzipped"), ignore_errors=True)
38+
shutil.rmtree(Path("unzipped"), ignore_errors=True)
3939

4040
@pytest.fixture
4141
def prodconf_json(self, job_path):
@@ -45,7 +45,7 @@ def prodconf_json(self, job_path):
4545
with open(file, "w") as f:
4646
f.write('{"foo": "bar"}')
4747

48-
yield file
48+
yield file.name
4949

5050
file.unlink(missing_ok=True)
5151

@@ -57,7 +57,7 @@ def prodconf_py(self, job_path):
5757
with open(file, "w") as f:
5858
f.write('foo = "bar"')
5959

60-
yield file
60+
yield file.name
6161

6262
file.unlink(missing_ok=True)
6363

@@ -96,7 +96,7 @@ def test_uploadLogFile_success(
9696
assert zipFile.exists()
9797

9898
zipfile.ZipFile(zipFile, "r").extractall("unzipped")
99-
unzipped = Path("unzipped").joinpath(updated_wf_commons.prod_job_id)
99+
unzipped = Path("unzipped").joinpath(updated_wf_commons.prod_job_id, job_path)
100100
assert unzipped.joinpath(prodconf_json).exists()
101101
assert unzipped.joinpath(prodconf_py).exists()
102102
assert unzipped.joinpath(prodconf_json).read_text() == '{"foo": "bar"}'
@@ -288,7 +288,7 @@ def test_uploadLogFile_SEError(
288288
assert zipFile.exists()
289289

290290
zipfile.ZipFile(zipFile, "r").extractall("unzipped")
291-
unzipped = Path("unzipped").joinpath(updated_wf_commons.prod_job_id)
291+
unzipped = Path("unzipped").joinpath(updated_wf_commons.prod_job_id, job_path)
292292
assert unzipped.joinpath(prodconf_json).exists()
293293
assert unzipped.joinpath(prodconf_py).exists()
294294
assert unzipped.joinpath(prodconf_json).read_text() == '{"foo": "bar"}'
@@ -355,7 +355,7 @@ def test_uploadLogFile_transferError(
355355
assert zipFile.exists()
356356

357357
zipfile.ZipFile(zipFile, "r").extractall("unzipped")
358-
unzipped = Path("unzipped").joinpath(updated_wf_commons.prod_job_id)
358+
unzipped = Path("unzipped").joinpath(updated_wf_commons.prod_job_id, job_path)
359359
assert unzipped.joinpath(prodconf_json).exists()
360360
assert unzipped.joinpath(prodconf_py).exists()
361361
assert unzipped.joinpath(prodconf_json).read_text() == '{"foo": "bar"}'

test/test_command_upload_output_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def sim_file(self, wf_commons, job_path):
3434
with open(path, "w") as f:
3535
f.write("Bookkeeping file content")
3636

37-
yield str(path)
37+
yield str(path.name)
3838

3939
path.unlink(missing_ok=True)
4040

@@ -555,7 +555,7 @@ def test_uploadOutputData_noOutput(self, mocker: MockerFixture, upload_output, j
555555
wf_commons["output_data_step"] = self.OUTPUT_DATA_STEP
556556

557557
# Remove the output
558-
Path(sim_file).unlink(missing_ok=True)
558+
Path(job_path).joinpath(sim_file).unlink(missing_ok=True)
559559

560560
WorkflowCommons(**wf_commons).save(job_path)
561561

0 commit comments

Comments
 (0)