Repository navigation
Expand file tree
/
Copy pathhsync-rsync.py
More file actions
executable file
·76 lines (60 loc) · 2.39 KB
/
Copy pathhsync-rsync.py
File metadata and controls
executable file
·76 lines (60 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
# Load modules
from argparse import ArgumentParser
from configparser import ConfigParser
from os.path import expanduser,isfile,sep
from socket import gethostname
from subprocess import call
from sys import exit
# Version.
version=\
"""
Hsync version 0.1
"""
# Load hsync configuration.
ini = expanduser("~") + sep + '.hsync.ini'
conf = ConfigParser()
if isfile(ini):
conf.read(ini)
else:
print('Error: cannot find configuration file')
print('Expected to find ' + ini)
# Create an argument parser object.
parser = ArgumentParser(description='Hsync, an easy directory synchronisation tool.')
parser.add_argument('command', action='store', help='the main command to run', choices=['to','from'])
parser.add_argument('host', action='store', help='the host to sync to|from', choices=conf.sections())
parser.add_argument('-a', '--asif', action='store_true', help='act as if and pretend to do something')
parser.add_argument('-v', '--version', action='version', version=version)
args = parser.parse_args()
def createWorklist():
"Will create a worklist, given to|from and a host"
workList=[]
if args.host == gethostname():
print('Error: you cannot sync something to itself')
print('Specify another host instead of ' + args.host)
exit(1)
local=conf.items(gethostname())
remote=conf.items(args.host)
for (localKey, localValue) in local:
source=localValue.split(',')[0] + '/'
for (remoteKey, remoteValue) in remote:
target=args.host + ':' + remoteValue.split(',')[0] + '/'
if localKey == remoteKey:
workItem=[]
if args.asif == True:
workItem.extend(['echo'])
workItem.extend(['rsync', '-av', '--delete', '-PHS'])
if args.command == 'to':
workItem.extend([source, target.translate(str.maketrans({" ": r"\ "}))])
elif args.command == 'from':
workItem.extend([target.translate(str.maketrans({" ": r"\ "})), source])
else:
print('args.command was not to or from.. should be impossible!')
exit(1)
workList.append(workItem)
return workList
def sync(workList):
"Will take a worklist and execute it."
for work in workList:
call(work)
sync(createWorklist())