Dear Fortran enthusiasts, I have been proposing cross-platform system interaction APIs for stdlib: mainly subprocessing and filesystem operations.
I would like to gather community feedback on the proposed API, for example:
- should we prefer a functional style, oo style, both?
- what names should be using? (i.e.
is_directory
,is_dir
,isdir
, etc.)? - What functionality should be encapsulated? I.e., for
delete
, should we have separate functionsdelete
anddelete_directory
, or both with the same name, etc.?
For sub-processing functionality, stdlib_system
now provides an interface for managing external processes which has a similar design as the Python subprocess
module. It allows running processes synchronously or asynchronously, checking their status, retrieving outputs, and handling process termination.
Current API proposal
The current design uses Fortran interfaces for consistency with stdlib
. Error handling uses integer exit codes but could be integrated with state_type
if/when standardized for the whole stdlib
.
-
type(process_type)
: Represents an external process, storing process state,stdin
,stdout
, andstderr
. -
Run a process (synchronous or asynchronous):
process = run(cmd [, wait=.true.,] [, stdin=string] [, want_stdout=.false.] [, want_stderr=.false.]) process = run(args(:) [, wait=.true.,] [, stdin=string] [, want_stdout=.false.] [, want_stderr=.false.])
-
Run a synchronous command:
call run(cmd [, exit_state] [, command_state] [, stdout] [, stderr])
-
Update process state:
call update(process)
-
Check if a process is running:
status = is_running(process)
-
Check if a process has completed:
status = is_completed(process)
-
Wait for process completion (optional timeout):
call wait(process [, max_wait_time=10.0])
-
Get elapsed time since process start or total duration:
duration = elapsed(process)
-
Kill a running process:
call kill(process, success)
-
Retrieve process output:
print *, process%stdout print *, process%stderr print *, 'success = ',process%exit_code == 0
-
Check if a path is a directory:
is_directory(path) → logical
-
Delete a file:
call delete_file(path [, err])
-
Retrieve OS type:
function os_type() → cached OS flag
- cached to static storage
-
Get OS name:
function OS_NAME() → string
-
Get null device path:
function null_device() → string
-
Load a whole ASCII file into a
string_type
variable:type(string_type) function getfile(fileName [, err] [, delete])
- Optionally delete the file after reading (
delete=.true.
).
- Optionally delete the file after reading (
-
Library-wide error handling:
- extend
state_type
error handling, that was well accepted for linear algebra, to the whole library (without API changes: just via a polymorphicstate_type->linalg_state_type
inheritance)
- extend
Let’s use this thread to collect the best ideas for implementation.
Looking forward to your feedback!