Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions pyinfra/operations/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,34 @@ def template(
)


@operation()
def move(src: str, dest: str, overwrite=False):
"""
Move remote file/directory/link into remote directory

+ src: remote file/directory to move
+ dest: remote directory to move `src` into
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might not be smart but the argument is explicit that this is a directory, so I think that's OK.

+ overwrite: whether to overwrite dest, if present
"""

if host.get_fact(File, src) is None:
raise OperationError("src {0} does not exist".format(src))

if not host.get_fact(Directory, dest):
raise OperationError("dest {0} is not an existing directory".format(dest))

full_dest_path = os.path.join(dest, os.path.basename(src))
if host.get_fact(File, full_dest_path) is not None:
if overwrite:
yield StringCommand("rm", "-rf", QuoteString(full_dest_path))
else:
raise OperationError(
"dest {0} already exists and `overwrite` is unset".format(full_dest_path)
)

yield StringCommand("mv", QuoteString(src), QuoteString(dest))


def _validate_path(path):
try:
return os.fspath(path)
Expand Down
19 changes: 19 additions & 0 deletions tests/operations/files.move/directory.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"require_platform": ["Darwin", "Linux"],
"args": ["tmp/testfile", "tmp2"],
"facts": {
"files.File": {
"path=tmp/testfile": false,
"path=tmp2/testfile": null
},
"files.Directory": {
"path=tmp/testfile": {
"mode": 700
},
"path=tmp2": {
"mode": 700
}
}
},
"commands": ["mv tmp/testfile tmp2"]
}
18 changes: 18 additions & 0 deletions tests/operations/files.move/invalid_dest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"require_platform": ["Darwin", "Linux"],
"args": ["tmp/testfile", "tmp2"],
"facts": {
"files.File": {
"path=tmp/testfile": {
"mode": 600
}
},
"files.Directory": {
"path=tmp2": null
}
},
"exception": {
"name": "OperationError",
"message": "dest tmp2 is not an existing directory"
}
}
18 changes: 18 additions & 0 deletions tests/operations/files.move/invalid_src.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"require_platform": ["Darwin", "Linux"],
"args": ["tmp/testfile", "tmp2"],
"facts": {
"files.File": {
"path=tmp/testfile": null
},
"files.Directory": {
"path=tmp2": {
"mode": 700
}
}
},
"exception": {
"name": "OperationError",
"message": "src tmp/testfile does not exist"
}
}
21 changes: 21 additions & 0 deletions tests/operations/files.move/link.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"require_platform": ["Darwin", "Linux"],
"args": ["tmp/testfile", "tmp2"],
"facts": {
"files.File": {
"path=tmp/testfile": false,
"path=tmp2/testfile": null
},
"files.Link": {
"path=tmp/testfile": {
"mode": 600
}
},
"files.Directory": {
"path=tmp2": {
"mode": 700
}
}
},
"commands": ["mv tmp/testfile tmp2"]
}
18 changes: 18 additions & 0 deletions tests/operations/files.move/no_conflict.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"require_platform": ["Darwin", "Linux"],
"args": ["tmp/testfile", "tmp2"],
"facts": {
"files.File": {
"path=tmp/testfile": {
"mode": 600
},
"path=tmp2/testfile": null
},
"files.Directory": {
"path=tmp2": {
"mode": 700
}
}
},
"commands": ["mv tmp/testfile tmp2"]
}
23 changes: 23 additions & 0 deletions tests/operations/files.move/no_overwrite.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"require_platform": ["Darwin", "Linux"],
"args": ["tmp/testfile", "tmp2"],
"facts": {
"files.File": {
"path=tmp/testfile": {
"mode": 600
},
"path=tmp2/testfile": {
"mode": 600
}
},
"files.Directory": {
"path=tmp2": {
"mode": 700
}
}
},
"exception": {
"name": "OperationError",
"message": "dest tmp2/testfile already exists and `overwrite` is unset"
}
}
21 changes: 21 additions & 0 deletions tests/operations/files.move/overwrite.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"require_platform": ["Darwin", "Linux"],
"args": ["tmp/testfile", "tmp2"],
"kwargs": {"overwrite": true},
"facts": {
"files.File": {
"path=tmp/testfile": {
"mode": 600
},
"path=tmp2/testfile": {
"mode": 600
}
},
"files.Directory": {
"path=tmp2": {
"mode": 700
}
}
},
"commands": ["rm -rf tmp2/testfile", "mv tmp/testfile tmp2"]
}