Skip to content

Commit 3571bb4

Browse files
committed
rimport can now accept positional args (files to process).
To match relink behavior.
1 parent 5f0a3ca commit 3571bb4

3 files changed

Lines changed: 116 additions & 15 deletions

File tree

rimport

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ def build_parser() -> argparse.ArgumentParser:
7171
),
7272
)
7373

74+
parser.add_argument(
75+
"items_to_process",
76+
nargs="*",
77+
help="One or more files to process. (Optional; can use --file instead to process just one.)"
78+
)
79+
7480
# Add inputdata_root option flags
7581
shared.add_inputdata_root(parser)
7682

@@ -305,14 +311,15 @@ def print_can_file_be_downloaded(file_can_be_downloaded: bool):
305311
logger.info("%sFile is not (yet) available for download.", INDENT)
306312

307313

308-
def get_files_to_process(file: str, filelist: str):
314+
def get_files_to_process(file: str, filelist: str, items_to_process: list):
309315
"""Get list of files to process.
310316
311-
Uses --file and/or --filelist arguments.
317+
Uses --file and/or --filelist arguments, as well as positional items_to_process if given.
312318
313319
Args:
314320
file (str): Single file to process.
315321
filelist (str): File containing list of files to process.
322+
items_to_process (list): List of files to process.
316323
317324
Returns:
318325
list: List of files to process
@@ -334,6 +341,9 @@ def get_files_to_process(file: str, filelist: str):
334341
return None, 2
335342
files_to_process.extend(files_in_list)
336343

344+
if items_to_process:
345+
files_to_process.extend(items_to_process)
346+
337347
if not files_to_process:
338348
logger.error("rimport: At least one of --file or --filelist is required")
339349
return None, 2
@@ -382,7 +392,7 @@ def main(argv: List[str] | None = None) -> int:
382392
return 2
383393

384394
# Determine the list of relative filenames to handle
385-
files_to_process, status = get_files_to_process(args.file, args.filelist)
395+
files_to_process, status = get_files_to_process(args.file, args.filelist, args.items_to_process)
386396
if status:
387397
return status
388398

tests/rimport/test_build_parser.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,27 @@ def test_file_and_list_not_mutually_exclusive(self, capsys):
6868
args = parser.parse_args(["-file", file, "-list", filelist])
6969
assert args.file == file
7070
assert args.filelist == filelist
71+
assert args.items_to_process == []
72+
73+
def test_positional_items_to_process(self):
74+
"""Test that positional items_to_process are accepted"""
75+
parser = rimport.build_parser()
76+
items_to_process = ["abc123", "def456"]
77+
args = parser.parse_args(items_to_process)
78+
assert args.file is None
79+
assert args.filelist is None
80+
assert args.items_to_process == items_to_process
81+
82+
def test_file_and_list_ok_with_items_to_process(self):
83+
"""Test that -file and -list can be used together with items_to_process"""
84+
parser = rimport.build_parser()
85+
file = "test.txt"
86+
filelist = "files.txt"
87+
items_to_process = ["abc123", "def456"]
88+
args = parser.parse_args(["-file", file, "-list", filelist, *items_to_process])
89+
assert args.file == file
90+
assert args.filelist == filelist
91+
assert args.items_to_process == items_to_process
7192

7293
def test_inputdata_default(self):
7394
"""Test that -inputdata has correct default value."""

tests/rimport/test_get_files_to_process.py

Lines changed: 82 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_single_file_relpath(self, tmp_path):
3939

4040
# Run
4141
files_to_process, result = rimport.get_files_to_process(
42-
file=filename, filelist=None
42+
file=filename, filelist=None, items_to_process=None,
4343
)
4444

4545
# Verify
@@ -60,7 +60,7 @@ def test_single_file_abspath(self, tmp_path):
6060

6161
# Run
6262
files_to_process, result = rimport.get_files_to_process(
63-
file=test_file, filelist=None
63+
file=test_file, filelist=None, items_to_process=None,
6464
)
6565

6666
# Verify
@@ -87,7 +87,7 @@ def test_filelist_relpath_with_relpaths(self, tmp_path):
8787

8888
# Run
8989
files_to_process, result = rimport.get_files_to_process(
90-
file=None, filelist=filelist_relpath
90+
file=None, filelist=filelist_relpath, items_to_process=None,
9191
)
9292

9393
# Verify
@@ -113,7 +113,7 @@ def test_filelist_abspath_with_relpaths(self, tmp_path):
113113

114114
# Run
115115
files_to_process, result = rimport.get_files_to_process(
116-
file=None, filelist=filelist
116+
file=None, filelist=filelist, items_to_process=None,
117117
)
118118

119119
# Verify
@@ -140,7 +140,7 @@ def test_filelist_relpath_with_abspaths(self, tmp_path):
140140

141141
# Run
142142
files_to_process, result = rimport.get_files_to_process(
143-
file=None, filelist=filelist_relpath
143+
file=None, filelist=filelist_relpath, items_to_process=None,
144144
)
145145

146146
# Verify
@@ -166,7 +166,7 @@ def test_filelist_abspath_with_abspaths(self, tmp_path):
166166

167167
# Run
168168
files_to_process, result = rimport.get_files_to_process(
169-
file=None, filelist=filelist
169+
file=None, filelist=filelist, items_to_process=None,
170170
)
171171

172172
# Verify
@@ -178,7 +178,7 @@ def test_filelist_not_found(self):
178178
filelist = "bsfearirn"
179179
assert not os.path.exists(filelist)
180180
files_to_process, result = rimport.get_files_to_process(
181-
file=None, filelist=filelist
181+
file=None, filelist=filelist, items_to_process=None,
182182
)
183183
assert result == 2
184184
assert files_to_process is None
@@ -188,11 +188,81 @@ def test_filelist_empty(self, tmp_path):
188188
filelist = tmp_path / "bsfearirn"
189189
filelist.write_text("")
190190
files_to_process, result = rimport.get_files_to_process(
191-
file=None, filelist=filelist
191+
file=None, filelist=filelist, items_to_process=[],
192192
)
193193
assert result == 2
194194
assert files_to_process is None
195195

196+
def test_items_to_process_abspaths(self, tmp_path):
197+
"""Test giving it a list of absolute paths in items_to_process"""
198+
# Setup
199+
inputdata_root = tmp_path / "inputdata"
200+
inputdata_root.mkdir()
201+
staging_root = tmp_path / "staging"
202+
staging_root.mkdir()
203+
204+
filenames = []
205+
for i in range(2):
206+
filename = inputdata_root / f"test{i}.txt"
207+
filenames.append(str(filename))
208+
filename.write_text("def567")
209+
210+
# Run
211+
files_to_process, result = rimport.get_files_to_process(
212+
file=None, filelist=None, items_to_process=filenames,
213+
)
214+
215+
# Verify
216+
assert result == 0
217+
assert files_to_process == filenames
218+
219+
def test_items_to_process_relpaths(self, tmp_path):
220+
"""Test giving it a list of relative paths in items_to_process"""
221+
# Setup
222+
inputdata_root = tmp_path / "inputdata"
223+
inputdata_root.mkdir()
224+
225+
filenames = []
226+
for i in range(2):
227+
filename = inputdata_root / f"test{i}.txt"
228+
filenames.append(os.path.basename(filename))
229+
filename.write_text("def567")
230+
231+
# Run
232+
files_to_process, result = rimport.get_files_to_process(
233+
file=None, filelist=None, items_to_process=filenames,
234+
)
235+
236+
# Verify
237+
assert result == 0
238+
assert files_to_process == filenames
239+
240+
def test_items_to_process_mixpaths(self, tmp_path):
241+
"""Test giving it a list of absolute and relative paths in items_to_process"""
242+
# Setup
243+
inputdata_root = tmp_path / "inputdata"
244+
inputdata_root.mkdir()
245+
246+
filenames = []
247+
for i in range(2):
248+
filename = inputdata_root / f"test{i}.txt"
249+
filenames.append(os.path.basename(filename))
250+
filename.write_text("def567")
251+
for i in range(2):
252+
filename = inputdata_root / f"test{2*i}.txt"
253+
filenames.append(str(filename))
254+
filename.write_text("def567")
255+
assert len(filenames) == 4
256+
257+
# Run
258+
files_to_process, result = rimport.get_files_to_process(
259+
file=None, filelist=None, items_to_process=filenames,
260+
)
261+
262+
# Verify
263+
assert result == 0
264+
assert files_to_process == filenames
265+
196266
def test_single_file_and_list(self, tmp_path):
197267
"""Test giving it a single file by its relative path"""
198268
# Setup
@@ -216,18 +286,18 @@ def test_single_file_and_list(self, tmp_path):
216286

217287
# Run
218288
files_to_process, result = rimport.get_files_to_process(
219-
file=filename, filelist=filelist
289+
file=filename, filelist=filelist, items_to_process=None,
220290
)
221291

222292
# Verify
223293
assert result == 0
224294
assert files_to_process == [filename] + filenames
225295

226-
def test_single_or_list_required(self):
227-
"""Test that either file or filelist is required"""
296+
def test_single_or_filelist_or_list_required(self):
297+
"""Test that at least one of file, filelist, items_to_process is required"""
228298
# Run
229299
files_to_process, result = rimport.get_files_to_process(
230-
file=None, filelist=None
300+
file=None, filelist=None, items_to_process=None,
231301
)
232302

233303
# Verify

0 commit comments

Comments
 (0)