-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncthing_findconflicts.py
executable file
·142 lines (120 loc) · 4.63 KB
/
syncthing_findconflicts.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
# vim: set fileencoding=utf-8 :
""" syncthing_findconflicts.py
Scans all folders of the local Syncthing instance for conflict files"""
# The MIT License (MIT)
#
# Copyright (c) 2017-2024 Georg Lutz
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Standard library imports:
import argparse
import os
import xml.parsers.expat
def get_args():
'''Configures command line parser and returns parsed parameters'''
parser = argparse.ArgumentParser(
description="Scans all folders of the local Syncthing instance for conflict files")
parser.add_argument(
"-q", "--quiet",
help="Generates output only when conflicts are found",
action="store_true")
return parser.parse_args()
def get_config():
'''Returns an array with the relevant part of syncthing config:
[
{ "folder_label": "label_of_first_folder",
"path": "local/path/of/first/folder"
},
{ "folder_label": "label_of_second_folder",
"path": "local/path/of/second/folder"
}
]
'''
filepath_candidates = [
os.path.expanduser("~/.local/state/syncthing/config.xml"),
os.path.expanduser("~/.config/syncthing/config.xml")
]
filepath = ""
for file_ in filepath_candidates:
if os.path.exists(file_):
filepath = file_
break
if not filepath:
print("Could not find syncthing config file")
sys.exit(1)
result = []
# Workaround to allow write access to variables from inner functions
in_configuration = [False]
depth_counter = [0]
def cb_start_element(name, attrs):
'''expat callback'''
depth_counter[0] = depth_counter[0] + 1
if name == "configuration":
in_configuration[0] = True
if in_configuration[0] and name == "folder" and depth_counter[0] == 2:
result.append(
{"folder_label" : attrs["label"], "path": attrs["path"]})
def cb_end_element(name):
'''expat callback'''
depth_counter[0] = depth_counter[0] - 1
if name == "configuration" and in_configuration[0]:
in_configuration[0] = False
with open(filepath, "rb") as filehandle:
parser = xml.parsers.expat.ParserCreate()
parser.StartElementHandler = cb_start_element
parser.EndElementHandler = cb_end_element
parser.ParseFile(filehandle)
return result
def find_conflict_files(folder_path):
'''Returns an array of Syncthing conflict files for the given folder path'''
result = []
pattern = ".sync-conflict-"
for root, dirs, files in os.walk(folder_path):
for dir_ in dirs:
if dir_.find(pattern) >= 0:
result.append(os.path.join(root, dir_))
for file_ in files:
if file_.find(pattern) >= 0:
result.append(os.path.join(root, file_))
return result
def main():
'''main function, called when script file is executed directly'''
args = get_args()
config = get_config()
if args.quiet:
for entry in config:
conflicts = find_conflict_files(entry["path"])
for conflict in conflicts:
print(conflict)
else:
first = True
for entry in config:
if not first:
print("")
print("Checking folder " + entry["folder_label"] + ":")
conflicts = find_conflict_files(entry["path"])
if not conflicts:
print("No conflicts found")
else:
for conflict in conflicts:
print(" " + conflict)
first = False
if __name__ == "__main__":
main()