Add functionality to create parent directories
This commit introduces an additional option for extended configuration syntax for linking. It adds the "create" parameter which results in automatically creating a parent directory if necessary before linking a file.
This commit is contained in:
parent
13c925be87
commit
afebc0bb2f
2 changed files with 23 additions and 2 deletions
|
@ -104,14 +104,19 @@ a trailing "/" character.
|
||||||
Link commands support an (optional) extended configuration. In this type of
|
Link commands support an (optional) extended configuration. In this type of
|
||||||
configuration, instead of specifying source locations directly, targets are
|
configuration, instead of specifying source locations directly, targets are
|
||||||
mapped to extended configuration dictionaries. These dictionaries map "path" to
|
mapped to extended configuration dictionaries. These dictionaries map "path" to
|
||||||
the source path, and specify "force" as true if the file or directory should be
|
the source path, specify "create" as true if the parent directory should be
|
||||||
forcibly linked.
|
created if necessary, and specify "force" as true if the file or directory
|
||||||
|
should be forcibly linked.
|
||||||
|
|
||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"link": {
|
"link": {
|
||||||
|
"~/.config/terminator": {
|
||||||
|
"path": "config/terminator/",
|
||||||
|
"create": true
|
||||||
|
},
|
||||||
"~/.vimrc": "vimrc",
|
"~/.vimrc": "vimrc",
|
||||||
"~/.vim": "vim/",
|
"~/.vim": "vim/",
|
||||||
"~/.zshrc": {
|
"~/.zshrc": {
|
||||||
|
|
|
@ -23,6 +23,9 @@ class Linker(Executor):
|
||||||
# extended config
|
# extended config
|
||||||
path = source['path']
|
path = source['path']
|
||||||
force = source.get('force', False)
|
force = source.get('force', False)
|
||||||
|
create = source.get('create', False)
|
||||||
|
if create:
|
||||||
|
success &= self._create(destination)
|
||||||
if force:
|
if force:
|
||||||
success &= self._delete(destination)
|
success &= self._delete(destination)
|
||||||
else:
|
else:
|
||||||
|
@ -55,6 +58,19 @@ class Linker(Executor):
|
||||||
path = os.path.expanduser(path)
|
path = os.path.expanduser(path)
|
||||||
return os.path.exists(path)
|
return os.path.exists(path)
|
||||||
|
|
||||||
|
def _create(self, path):
|
||||||
|
success = True
|
||||||
|
parent = os.path.abspath(os.path.join(os.path.expanduser(path), os.pardir))
|
||||||
|
if not self._exists(parent):
|
||||||
|
try:
|
||||||
|
os.makedirs(parent)
|
||||||
|
except OSError:
|
||||||
|
self._log.warning('Failed to create directory %s' % parent)
|
||||||
|
success = False
|
||||||
|
else:
|
||||||
|
self._log.lowinfo('Creating directory %s' % parent)
|
||||||
|
return success
|
||||||
|
|
||||||
def _delete(self, path):
|
def _delete(self, path):
|
||||||
success = True
|
success = True
|
||||||
if self._exists(path) and not self._is_link(path):
|
if self._exists(path) and not self._is_link(path):
|
||||||
|
|
Loading…
Reference in a new issue