Skip to content

Commit 63345de

Browse files
committedMar 12, 2025
files.move: new operation
Proposing a fairly simple operation to use `mv` among remote files. Notice: it's not as smart. `mv t1 t2/t1` will try to create `t2/t1/t1`.
1 parent efb7cb3 commit 63345de

File tree

8 files changed

+160
-0
lines changed

8 files changed

+160
-0
lines changed
 

‎pyinfra/operations/files.py

+29
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,35 @@ def template(
10691069
)
10701070

10711071

1072+
@operation()
1073+
def move(src: str, dest: str, overwrite=False):
1074+
"""
1075+
Move remote file/directory/link into remote directory
1076+
1077+
+ src: remote file/directory to move
1078+
+ dest: remote directory to move `src` into
1079+
+ overwrite: whether to overwrite dest, if present
1080+
"""
1081+
1082+
if host.get_fact(File, src) is None:
1083+
raise OperationError("src {0} does not exist".format(src))
1084+
1085+
if not host.get_fact(Directory, dest):
1086+
raise OperationError("dest {0} is not an existing directory".format(dest))
1087+
1088+
full_dest_path = os.path.join(dest, os.path.basename(src))
1089+
if host.get_fact(File, full_dest_path) is not None:
1090+
if overwrite:
1091+
yield StringCommand("rm", "-rf", QuoteString(full_dest_path))
1092+
else:
1093+
raise OperationError(
1094+
"dest {0} already exists and `overwrite` is unset".format(full_dest_path)
1095+
)
1096+
1097+
yield StringCommand("mv", QuoteString(src), QuoteString(dest))
1098+
changed = True
1099+
1100+
10721101
def _validate_path(path):
10731102
try:
10741103
return os.fspath(path)
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"args": ["tmp/testfile", "tmp2"],
3+
"facts": {
4+
"files.File": {
5+
"path=tmp/testfile": false,
6+
"path=tmp2/testfile": null
7+
},
8+
"files.Directory": {
9+
"path=tmp/testfile": {
10+
"mode": 700
11+
},
12+
"path=tmp2": {
13+
"mode": 700
14+
}
15+
}
16+
},
17+
"commands": ["mv tmp/testfile tmp2"]
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"args": ["tmp/testfile", "tmp2"],
3+
"facts": {
4+
"files.File": {
5+
"path=tmp/testfile": {
6+
"mode": 600
7+
}
8+
},
9+
"files.Directory": {
10+
"path=tmp2": null
11+
}
12+
},
13+
"exception": {
14+
"name": "OperationError",
15+
"message": "dest tmp2 is not an existing directory"
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"args": ["tmp/testfile", "tmp2"],
3+
"facts": {
4+
"files.File": {
5+
"path=tmp/testfile": null
6+
},
7+
"files.Directory": {
8+
"path=tmp2": {
9+
"mode": 700
10+
}
11+
}
12+
},
13+
"exception": {
14+
"name": "OperationError",
15+
"message": "src tmp/testfile does not exist"
16+
}
17+
}

‎tests/operations/files.move/link.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"args": ["tmp/testfile", "tmp2"],
3+
"facts": {
4+
"files.File": {
5+
"path=tmp/testfile": false,
6+
"path=tmp2/testfile": null
7+
},
8+
"files.Link": {
9+
"path=tmp/testfile": {
10+
"mode": 600
11+
}
12+
},
13+
"files.Directory": {
14+
"path=tmp2": {
15+
"mode": 700
16+
}
17+
}
18+
},
19+
"commands": ["mv tmp/testfile tmp2"]
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"args": ["tmp/testfile", "tmp2"],
3+
"facts": {
4+
"files.File": {
5+
"path=tmp/testfile": {
6+
"mode": 600
7+
},
8+
"path=tmp2/testfile": null
9+
},
10+
"files.Directory": {
11+
"path=tmp2": {
12+
"mode": 700
13+
}
14+
}
15+
},
16+
"commands": ["mv tmp/testfile tmp2"]
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"args": ["tmp/testfile", "tmp2"],
3+
"facts": {
4+
"files.File": {
5+
"path=tmp/testfile": {
6+
"mode": 600
7+
},
8+
"path=tmp2/testfile": {
9+
"mode": 600
10+
}
11+
},
12+
"files.Directory": {
13+
"path=tmp2": {
14+
"mode": 700
15+
}
16+
}
17+
},
18+
"exception": {
19+
"name": "OperationError",
20+
"message": "dest tmp2/testfile already exists and `overwrite` is unset"
21+
}
22+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"args": ["tmp/testfile", "tmp2"],
3+
"kwargs": {"overwrite": true},
4+
"facts": {
5+
"files.File": {
6+
"path=tmp/testfile": {
7+
"mode": 600
8+
},
9+
"path=tmp2/testfile": {
10+
"mode": 600
11+
}
12+
},
13+
"files.Directory": {
14+
"path=tmp2": {
15+
"mode": 700
16+
}
17+
}
18+
},
19+
"commands": ["rm -rf tmp2/testfile", "mv tmp/testfile tmp2"]
20+
}

0 commit comments

Comments
 (0)
Failed to load comments.