utils.safe_copy() fails to copy *to* a location where leading directories do not exist
Summary
When writing a source plugin, I tried to copy files (on disk) to a similar location in BuildStream using utils.safe_copy('opt/baz/bin/foo', os.path.join(directory, 'opt/baz/bin/foo'))
Now, this function fails because the directories opt/baz/bin
do not exist. Note, we're utimately calling shutil.copy_file()
here: https://gitlab.com/BuildStream/buildstream/blob/master/buildstream/utils.py#L266
A simple fix for this is to execute:
os.makedirs(os.path.dirname(os.path.join(directory, 'opt/baz/bin/foo')), exist_ok=True)
before we call utils.safe_copy
, within the plugin.
It seems that this fix could also be implemented within the utils.safe_copy()
function itself. However, @juergbi mentioned that we should not expand the scope of safe_copy() as, strictly speaking, it would even be an API break
Perhaps, we could consider an additional helper method?
I've opened this issue for discussion