/
syncClippings-setup.sh
executable file
·461 lines (388 loc) · 12.6 KB
/
syncClippings-setup.sh
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/bin/bash
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
os=`uname`
defaultInstallPath=/usr/local/opt/sync-clippings
installPath=$defaultInstallPath
exeFilename=syncClippings.py
confFilename=syncClippings.ini
nativeManifestFilename=syncClippings.json
setupFailed=0
noTkinter=0
# ANSI colors
bold='\033[1m'
blue='\033[94m'
red='\033[91m'
reset='\033[0m'
checkNotSudo() {
if [ $EUID -eq 0 ]; then
echo
echo "Sync Clippings Helper Setup should not be run using sudo, as this can cause"
echo "improper installation. Rerun Sync Clippings Helper Setup without sudo."
echo "Setup will now exit."
echo
exit 0
fi
}
checkPython() {
if [ $os = "Darwin" ]; then
local isPython3=`which -s python3; echo $?`
test $isPython3 -eq 0
else
local isPython3=`which python3`
test -n "$isPython3"
fi
if [ $? -eq 0 ]; then
# Python 3 is installed
echo "Press CTRL+C at any time to cancel."
else
echo
echo "It looks like Python 3 is not installed."
echo "Python 3 is needed to run the Sync Clippings Helper app. You can download it"
echo -e "from the Python website at ${blue}https://www.python.org/downloads${reset}."
echo "Alternatively, you can install it using your favorite package manager such"
echo "as Homebrew. Make sure to also install the Tkinter package (python3-tk)."
echo
echo "After Python 3 is installed, rerun Sync Clippings Helper Setup."
echo "Setup will now exit."
echo
exit 0
fi
}
checkTkinter() {
python3 -c "import tkinter" 2> /dev/null
if [ $? -ne 0 ]; then
echo
echo "The Python 3 Tkinter package (python3-tk) is not installed."
echo "You may choose to continue with setup and install it afterwards,"
echo "or cancel setup now, install it, and then rerun Sync Clippings Helper setup."
echo -en "${bold}Continue with setup? [Y/n]: ${reset}"
read answer
while true; do
[[ $answer == "y" || $answer == "Y" || $answer == "" ]] && break
[[ $answer == "n" || $answer == "N" ]] && exit 0
echo -n "Please answer 'y' or 'n': "
read answer
done
noTkinter=1
fi
}
promptInstallPath() {
echo
echo "Setup will install Sync Clippings Helper in \"${defaultInstallPath}\"."
echo "Press ENTER to accept, or type in the location of a different folder"
echo "and then press ENTER."
echo -en "${bold}Destination folder: ${reset}"
read installPath
pathHasSpaces=`echo "$installPath" | grep "\s"`
while [[ -n $pathHasSpaces ]]; do
echo -e "${red}Folder names should not contain spaces.${reset}"
echo -en "${bold}Destination folder: ${reset}"
read installPath
pathHasSpaces=`echo "$installPath" | grep "\s"`
done
[[ -z $installPath ]] && installPath=$defaultInstallPath
}
writeExecFile() {
echo "Creating installation path ${installPath}"
echo
echo "Elevated permissions required to create the installation folder."
echo "If prompted, enter password for sudo."
sudo mkdir -pv "${installPath}"
local exeFile="${installPath}/${exeFilename}"
echo "Writing Python script ${exeFile}"
sudo bash -c "cat > $exeFile" << EOF
#!/usr/bin/env python3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import platform
import sys
import json
import struct
import configparser
import copy
from pathlib import Path
import tkinter as tk
from tkinter import filedialog
DEBUG = False
APP_NAME = "Sync Clippings"
APP_VER = "1.2b2"
CONF_FILENAME = "syncClippings.ini"
SYNC_FILENAME = "clippings-sync.json"
gDefaultClippingsData = {
"version": "6.1",
"createdBy": APP_NAME,
"userClippingsRoot": []
}
def getAppName():
return APP_NAME
def getAppVer():
return APP_VER
def getConfigFilePath():
rv = None
osName = platform.system()
homeDir = os.path.expanduser("~")
if osName == "Windows":
rv = homeDir + "\\\\AppData\\\\Local\\\\Sync Clippings\\\\" + CONF_FILENAME
elif osName == "Darwin": # macOS
rv = homeDir + "/Library/Preferences/" + CONF_FILENAME
else:
rv = homeDir + "/.config/sync-clippings/" + CONF_FILENAME
return rv
def getSyncDir():
rv = ""
conf = configparser.ConfigParser()
confFilePath = getConfigFilePath()
try:
conf.read(confFilePath)
rv = conf["Sync File"]["Path"]
except:
# Create config file if it doesn't exist.
file = open(confFilePath, "w", encoding="utf-8")
file.close()
setSyncDir("")
return rv
def setSyncDir(aPath):
conf = configparser.ConfigParser()
confFilePath = getConfigFilePath()
conf["Sync File"] = { "Path": aPath }
with open(confFilePath, "w") as configFile:
conf.write(configFile)
def getSyncFileInfo(aSyncFileDir):
rv = {
"fileName": "",
"fileSizeKB": ""
}
if not Path(aSyncFileDir).exists():
log("getSyncFileInfo(): Directory does not exist: %s" % aSyncFileDir)
return rv
syncFilePath = Path(aSyncFileDir) / SYNC_FILENAME
if not syncFilePath.exists():
log("getSyncFileInfo(): Sync file does not exist at directory %s" % aSyncFileDir)
return rv
fileInfo = os.stat(syncFilePath)
fileSizeBytes = fileInfo.st_size
rv["fileName"] = SYNC_FILENAME
# Convert file size to kilobytes, and show 1 decimal place if < 10 KB.
numDigits = None
fileSizeKB = int(fileSizeBytes) / 1024
if fileSizeKB < 10:
numDigits = 1
rv["fileSizeKB"] = round(fileSizeKB, numDigits)
return rv
def getSyncedClippingsData(aSyncFileDir):
rv = ""
if not Path(aSyncFileDir).exists():
log("getSyncedClippingsData(): Directory does not exist: %s" % aSyncFileDir)
syncDirPath = Path(aSyncFileDir)
syncDirPath.mkdir(parents=True)
log("getSyncedClippingsData(): aSyncFileDir: %s" % aSyncFileDir)
syncFilePath = Path(aSyncFileDir) / SYNC_FILENAME
if syncFilePath.exists():
log("getSyncedClippingsData(): Reading sync file '%s'" % syncFilePath)
file = open(syncFilePath, "r", encoding="utf-8")
rv = file.read()
else:
log("getSyncedClippingsData(): Sync file '%s' not found.\nGenerating new file from template." % syncFilePath)
fileData = json.dumps(gDefaultClippingsData)
file = open(syncFilePath, "w", encoding="utf-8")
file.write(fileData)
rv = fileData
if file is not None:
file.close()
return rv
def updateSyncedClippingsData(aSyncFileDir, aSyncedClippingsData):
syncData = copy.deepcopy(gDefaultClippingsData)
syncData["userClippingsRoot"] = aSyncedClippingsData
syncFileData = json.dumps(syncData)
syncFilePath = Path(aSyncFileDir) / SYNC_FILENAME
try:
file = open(syncFilePath, "w", encoding="utf-8")
file.write(syncFileData)
except Exception as e:
log("updateSyncedClippingsData(): Error writing to sync file '{0}': {1}".format(aSyncFilePath), e.message)
finally:
if file is not None:
file.close()
def promptSyncFldrPath():
rv = ""
root = tk.Tk()
root.withdraw()
root.overrideredirect(True)
root.geometry('0x0+0+0')
root.deiconify()
root.lift()
root.focus_force()
# Additional hack for macOS.
if platform.system() == "Darwin":
os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')
homeDir = os.path.expanduser("~")
rv = filedialog.askdirectory(initialdir=homeDir)
# Get rid of the top-level instance once to make it invisible.
root.destroy()
return rv
def log(aMsg):
if DEBUG:
with open("debug.txt", "a") as file:
file.write(aMsg)
file.write("\n")
def getResponseOK():
rv = { "status": "ok" }
return rv
def getResponseErr(aErr):
template = "An exception of type {0} has occurred. Arguments: {1!r}"
rv = {
"status": "failure",
"details": template.format(type(aErr).__name__, aErr.args)
}
return rv
def getMessage():
rawLength = sys.stdin.buffer.read(4)
if len(rawLength) == 0:
sys.exit(0)
messageLength = struct.unpack('@I', rawLength)[0]
message = sys.stdin.buffer.read(messageLength).decode('utf-8')
rv = json.loads(message)
return rv
def encodeMessage(aMsgContent):
encodedContent = json.dumps(aMsgContent).encode('utf-8')
encodedLength = struct.pack('@I', len(encodedContent))
return {'length': encodedLength, 'content': encodedContent}
def sendMessage(aEncodedMsg):
sys.stdout.buffer.write(aEncodedMsg['length'])
sys.stdout.buffer.write(aEncodedMsg['content'])
sys.stdout.buffer.flush()
while True:
msg = getMessage()
resp = None
if "msgID" not in msg:
err = "Error: expected key 'msgID' does not exist!"
log(err)
sys.stderr.buffer.write("%s: %s" % (APP_NAME, err))
sys.stderr.buffer.flush()
sys.exit(1)
log("Value of key 'msgID' from 'msg' dictionary: %s" % msg["msgID"])
if msg["msgID"] == "get-app-version":
resp = {
"appName": getAppName(),
"appVersion": getAppVer()
}
elif msg["msgID"] == "get-sync-dir":
resp = {
"syncFilePath": getSyncDir()
}
elif msg["msgID"] == "set-sync-dir":
path = msg["filePath"]
log("Message 'set-sync-dir': filePath = {0}".format(msg['filePath']))
if path.startswith("~/"):
path = os.path.expanduser("~") + path[1:len(path)]
try:
setSyncDir(path)
resp = getResponseOK()
except Exception as e:
resp = getResponseErr(e)
elif msg["msgID"] == "get-sync-file-info":
syncDir = getSyncDir()
resp = getSyncFileInfo(syncDir)
elif msg["msgID"] == "get-synced-clippings":
syncDir = getSyncDir()
resp = getSyncedClippingsData(syncDir)
elif msg["msgID"] == "set-synced-clippings":
syncDir = getSyncDir()
syncData = msg["syncData"]
try:
updateSyncedClippingsData(syncDir, syncData)
resp = getResponseOK()
except Exception as e:
resp = getResponseErr(e)
elif msg["msgID"] == "sync-dir-folder-picker":
resp = {
"syncFilePath": promptSyncFldrPath()
}
if resp is not None:
sendMessage(encodeMessage(resp))
EOF
if [ $? -ne 0 ]; then
setupFailed=1
else
echo "Successfully wrote Python script"
fi
echo "Setting file mode"
sudo chmod -v 755 $exeFile
[[ $? -ne 0 ]] && setupFailed=1
}
writeConfFile() {
if [ $os = "Darwin" ]; then
configFileDir=~/Library/Preferences
else
configFileDir=~/.config/sync-clippings
mkdir -pv $configFileDir
fi
local configFile=${configFileDir}/${confFilename}
# Don't overwrite config file if it already exists.
if [[ -f $configFile ]]; then
echo -e "Config file exists at ${configFile}"
return
fi
echo "Writing config file $configFile"
cat << EOF > $configFile
[Sync File]
Path =
EOF
[[ $? -ne 0 ]] && setupFailed=1
}
writeNativeManifest() {
if [ $os = "Darwin" ]; then
nativeManifestDir=/Library/Application\ Support/Mozilla/NativeMessagingHosts
else
nativeManifestDir=/usr/lib/mozilla/native-messaging-hosts
fi
local nativeManifestFile="${nativeManifestDir}/${nativeManifestFilename}"
local exePath="$installPath/$exeFilename"
echo "Writing native messaging manifest $nativeManifestFile"
# Check if the native manifest directory exists; if not, then create it.
test -d "$nativeManifestDir"
if [ $? -ne 0 ]; then
sudo mkdir -pv "$nativeManifestDir"
fi
sudo bash -c "cat > \"$nativeManifestFile\"" << EOF
{
"name": "syncClippings",
"description": "Sync Clippings",
"path": "$exePath",
"type": "stdio",
"allowed_extensions": [
"{91aa5abe-9de4-4347-b7b5-322c38dd9271}",
"clippings-tb@aecreations.github.io"
]
}
EOF
[[ $? -ne 0 ]] && setupFailed=1
}
main() {
echo
echo -e "${bold}Welcome to Sync Clippings Helper Setup${reset}"
checkNotSudo
checkPython
checkTkinter
promptInstallPath
echo
echo "Starting installation."
writeExecFile
writeNativeManifest
writeConfFile
echo
if [ $setupFailed -ne 0 ]; then
echo -e "${red}An error occurred during setup. Check the above messages for error details.${reset}"
else
echo "Setup successfully completed."
if [ $noTkinter -eq 1 ]; then
echo "Remember to install the Python 3 Tkinter package."
fi
fi
echo
}
main