Skip to content

Commit 1ed3109

Browse files
committed
Migrate Windows operations, connector & tests to use explicit fact imports.
1 parent 7d4232e commit 1ed3109

File tree

16 files changed

+72
-54
lines changed

16 files changed

+72
-54
lines changed

pyinfra/api/connectors/winrm.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,12 @@ def put_file(
228228
Upload file by chunking and sending base64 encoded via winrm
229229
'''
230230

231+
# TODO: fix this? Workaround for circular import
232+
from pyinfra.facts.windows_files import WindowsTempDir
233+
231234
# Always use temp file here in case of failure
232235
temp_file = ntpath.join(
233-
host.fact.windows_temp_dir(),
236+
host.get_fact(WindowsTempDir),
234237
'pyinfra-{0}'.format(sha1_hash(remote_filename)),
235238
)
236239

pyinfra/operations/windows_files.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
OperationTypeError,
1919
)
2020
from pyinfra.api.util import get_file_sha1
21+
from pyinfra.facts.windows import WindowsDate
22+
from pyinfra.facts.windows_files import (
23+
WindowsDirectory,
24+
WindowsFile,
25+
WindowsLink,
26+
WindowsMd5File,
27+
WindowsSha1File,
28+
WindowsSha256File,
29+
)
2130

2231
from .util.compat import fspath
2332
from .util.files import ensure_mode_int
@@ -57,7 +66,7 @@ def download(
5766
)
5867
'''
5968

60-
info = host.fact.windows_file(dest)
69+
info = host.get_fact(WindowsFile, name=dest)
6170
# Destination is a directory?
6271
if info is False:
6372
raise OperationError(
@@ -76,21 +85,23 @@ def download(
7685
else:
7786
if cache_time:
7887
# Time on files is not tz-aware, and will be the same tz as the server's time,
79-
# so we can safely remove the tzinfo from host.fact.date before comparison.
80-
cache_time = host.fact.windows_date.replace(tzinfo=None) - timedelta(seconds=cache_time)
88+
# so we can safely remove the tzinfo from WindowsDate before comparison.
89+
cache_time = (
90+
host.get_fact(WindowsDate).replace(tzinfo=None) - timedelta(seconds=cache_time)
91+
)
8192
if info['mtime'] and info['mtime'] > cache_time:
8293
download = True
8394

8495
if sha1sum:
85-
if sha1sum != host.fact.windows_sha1_file(dest):
96+
if sha1sum != host.get_fact(WindowsSha1File, name=dest):
8697
download = True
8798

8899
if sha256sum:
89-
if sha256sum != host.fact.windows_sha256_file(dest):
100+
if sha256sum != host.get_fact(WindowsSha256File, name=dest):
90101
download = True
91102

92103
if md5sum:
93-
if md5sum != host.fact.windows_md5_file(dest):
104+
if md5sum != host.get_fact(WindowsMd5File, name=dest):
94105
download = True
95106

96107
# If we download, always do user/group/mode as SSH user may be different
@@ -191,7 +202,7 @@ def put(
191202
raise IOError('No such file: {0}'.format(local_file))
192203

193204
mode = ensure_mode_int(mode)
194-
remote_file = host.fact.windows_file(dest)
205+
remote_file = host.get_fact(WindowsFile, name=dest)
195206

196207
if create_remote_dir:
197208
yield _create_remote_dir(state, host, dest, user, group)
@@ -209,7 +220,7 @@ def put(
209220
# File exists, check sum and check user/group/mode if supplied
210221
else:
211222
local_sum = get_file_sha1(src)
212-
remote_sum = host.fact.windows_sha1_file(dest)
223+
remote_sum = host.get_fact(WindowsSha1File, name=dest)
213224

214225
# Check sha1sum, upload if needed
215226
if local_sum != remote_sum:
@@ -283,7 +294,7 @@ def file(
283294
raise OperationTypeError('Name must be a string')
284295

285296
# mode = ensure_mode_int(mode)
286-
info = host.fact.windows_file(path)
297+
info = host.get_fact(WindowsFile, name=path)
287298

288299
# Not a file?!
289300
if info is False:
@@ -395,7 +406,7 @@ def directory(
395406
if not isinstance(path, six.string_types):
396407
raise OperationTypeError('Name must be a string')
397408

398-
info = host.fact.windows_directory(path)
409+
info = host.get_fact(WindowsDirectory, name=path)
399410

400411
# Not a directory?!
401412
if info is False:
@@ -410,9 +421,9 @@ def directory(
410421
# yield chown(path, user, group, recursive=recursive)
411422
#
412423
# Somewhat bare fact, should flesh out more
413-
host.fact._create(
414-
'windows_directory',
415-
args=(path,),
424+
host.create_fact(
425+
WindowsDate,
426+
kwargs={'name': path},
416427
data={'type': 'directory'},
417428
)
418429

@@ -504,7 +515,7 @@ def link(
504515
if present and not target:
505516
raise OperationError('If present is True target must be provided')
506517

507-
info = host.fact.windows_link(path)
518+
info = host.get_fact(WindowsLink, name=path)
508519

509520
# Not a link?
510521
if info is not None and not info:
@@ -531,16 +542,16 @@ def link(
531542
# if user or group:
532543
# yield chown(path, user, group, dereference=False)
533544

534-
# host.fact._create(
535-
# 'windows_link',
536-
# args=(path,),
545+
# host.create_fact(
546+
# WindowsLink,
547+
# kwargs={'name': path},
537548
# data={'link_target': target, 'group': group, 'user': user},
538549
# )
539550

540551
# It exists and we don't want it
541552
elif (assume_present or info) and not present:
542553
yield remove_cmd
543-
# host.fact._delete('windows_link', args=(path,))
554+
# host.delete_fact(WindowsLink, kwargs={'name': path})
544555

545556
else:
546557
host.noop('link {0} already exists and force=False'.format(path))

tests/operations/windows_files.directory/add.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"recursive": true
77
},
88
"facts": {
9-
"windows_directory": {
10-
"testdir": null
9+
"windows_files.WindowsDirectory": {
10+
"name=testdir": null
1111
}
1212
},
1313
"commands": [

tests/operations/windows_files.directory/delete.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"present": false
55
},
66
"facts": {
7-
"windows_directory": {
8-
"testdir": {
7+
"windows_files.WindowsDirectory": {
8+
"name=testdir": {
99
"type": "directory"
1010
}
1111
}

tests/operations/windows_files.directory/delete_assume_present.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"assume_present": true
66
},
77
"facts": {
8-
"windows_directory": {
9-
"testdir": null
8+
"windows_files.WindowsDirectory": {
9+
"name=testdir": null
1010
}
1111
},
1212
"commands": [

tests/operations/windows_files.download/download.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"dest": "c:\\myfile"
77
},
88
"facts": {
9-
"windows_file": {
10-
"c:\\myfile": null
9+
"windows_files.WindowsFile": {
10+
"name=c:\\myfile": null
1111
}
1212
},
1313
"commands": [

tests/operations/windows_files.download/download_cache_time.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"cache_time": 3600
88
},
99
"facts": {
10-
"windows_date": "datetime:2015-01-01T00:00:00",
11-
"windows_file": {
12-
"c:\\myfile": {
10+
"windows.WindowsDate": "datetime:2015-01-01T00:00:00",
11+
"windows_files.WindowsFile": {
12+
"name=c:\\myfile": {
1313
"mtime": "datetime:2015-01-01T00:00:00"
1414
}
1515
}

tests/operations/windows_files.download/download_directory.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"dest": "c:\\"
77
},
88
"facts": {
9-
"windows_file": {
10-
"c:\\": false
9+
"windows_files.WindowsFile": {
10+
"name=c:\\": false
1111
}
1212
},
1313
"exception": {

tests/operations/windows_files.download/download_existing_checksum.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
"md5sum": "md5-sum"
1010
},
1111
"facts": {
12-
"windows_file": {
13-
"c:\\myfile": {}
12+
"windows_files.WindowsFile": {
13+
"name=c:\\myfile": {}
1414
},
15-
"windows_sha1_file": {
16-
"c:\\myfile": "not-a-match"
15+
"windows_files.WindowsSha1File": {
16+
"name=c:\\myfile": "not-a-match"
1717
},
18-
"windows_sha256_file": {
19-
"c:\\myfile": "not-a-match"
18+
"windows_files.WindowsSha256File": {
19+
"name=c:\\myfile": "not-a-match"
2020
},
21-
"windows_md5_file": {
22-
"c:\\myfile": "not-a-match"
21+
"windows_files.WindowsMd5File": {
22+
"name=c:\\myfile": "not-a-match"
2323
}
2424
},
2525
"commands": [

tests/operations/windows_files.download/skip_existing.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"dest": "c:\\myfile"
77
},
88
"facts": {
9-
"windows_file": {
10-
"c:\\myfile": {}
9+
"windows_files.WindowsFile": {
10+
"name=c:\\myfile": {}
1111
}
1212
},
1313
"commands": [],

0 commit comments

Comments
 (0)