mirror of
https://github.com/keymanapp/keyman.git
synced 2026-08-09 18:35:32 +00:00
install and uninstall kmp
unpack kmp parse metadata from kmp.json install main files to /usr/local/share/keyman/<id> install doc file to /usr/local/share/doc/keyman/<id> install fonts to /usr/local/share/fonts/keyman/<id>
This commit is contained in:
parent
f3dd382d4b
commit
bc95db6ecb
3 changed files with 261 additions and 0 deletions
76
linux/keyman-config/install_kmp.py
Executable file
76
linux/keyman-config/install_kmp.py
Executable file
|
|
@ -0,0 +1,76 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import tempfile
|
||||
import zipfile
|
||||
import sys
|
||||
import os.path
|
||||
from kmpmetadata import parsemetadata, determine_filetype
|
||||
from os import listdir, makedirs
|
||||
from shutil import copy2
|
||||
|
||||
def list_files(directory, extension):
|
||||
return (f for f in listdir(directory) if f.endswith('.' + extension))
|
||||
|
||||
def main(argv):
|
||||
if len(sys.argv) != 2:
|
||||
print("install_kmp.py <kmpfile>")
|
||||
sys.exit(2)
|
||||
inputfile = sys.argv[1]
|
||||
|
||||
if not os.path.isfile(inputfile):
|
||||
print("install_kmp.py Input file ", inputfile, " not found")
|
||||
print("install_kmp.py <kmpfile>")
|
||||
sys.exit(2)
|
||||
|
||||
name, ext = os.path.splitext(inputfile)
|
||||
if ext != ".kmp":
|
||||
print("install_kmp.py Input file ", inputfile, "is not a kmp file")
|
||||
print("install_kmp.py <kmpfile>")
|
||||
sys.exit(2)
|
||||
|
||||
# create a temporary directory using the context manager
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
print('created temporary directory', tmpdirname)
|
||||
with zipfile.ZipFile(inputfile,"r") as zip_ref:
|
||||
zip_ref.extractall(tmpdirname)
|
||||
# jsonfiles = list_files(tmpdirname, "json")
|
||||
# for f in jsonfiles:
|
||||
# print(f)
|
||||
kmpjson = os.path.join(tmpdirname, "kmp.json")
|
||||
if os.path.isfile(kmpjson):
|
||||
info, system, options, keyboards, files = parsemetadata(kmpjson, False)
|
||||
kbid = keyboards[0]['id']
|
||||
print("Installing", info['name']['description'])
|
||||
if not os.access('/usr/local/share/keyman', os.X_OK | os.W_OK)): # Check for write access of keyman dir
|
||||
print("You do not have permissions to install the keyboard files. You need to be a member of the keyman group. `sudo adduser <username> keyman`")
|
||||
exit(3)
|
||||
kbdir = os.path.join('/usr/local/share/keyman', kbid)
|
||||
if not os.access('/usr/local/share/doc/keyman', os.X_OK | os.W_OK): # Check for write access of keyman doc dir
|
||||
print("You do not have permissions to install the documentation. You need to be a member of the keyman group. `sudo adduser <username> keyman`")
|
||||
exit(3)
|
||||
kbdocdir = os.path.join('/usr/local/share/doc/keyman', kbid)
|
||||
if not os.access('/usr/local/share/fonts/keyman', os.X_OK | os.W_OK): # Check for write access of keyman fonts
|
||||
print("You do not have permissions to install the font files. You need to be a member of the keyman group. `sudo adduser <username> keyman`")
|
||||
exit(3)
|
||||
kbfontdir = os.path.join('/usr/local/share/fonts/keyman', kbid)
|
||||
for f in files:
|
||||
fpath = os.path.join(tmpdirname, f['name'])
|
||||
ftype = determine_filetype(f['name'])
|
||||
if ftype == "Documentation":
|
||||
print("Installing", f['name'], "as documentation")
|
||||
if not os.path.isdir(kbdocdir):
|
||||
os.makedirs(kbdocdir)
|
||||
copy2(fpath, kbdocdir)
|
||||
elif ftype == "Font":
|
||||
print("Installing", f['name'], "as font")
|
||||
if not os.path.isdir(kbfontdir):
|
||||
os.makedirs(kbfontdir)
|
||||
copy2(fpath, kbfontdir)
|
||||
elif ftype == "Metadata" or ftype == "Image" or ftype == "Keyboard icon" or ftype == "Keyboard icon" or ftype == "Compiled keyboard" or ftype == "Compiled on screen keyboard":
|
||||
print("Installing", f['name'], "as keyman file")
|
||||
if not os.path.isdir(kbdir):
|
||||
os.makedirs(kbdir)
|
||||
copy2(fpath, kbdir)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
136
linux/keyman-config/kmpmetadata.py
Executable file
136
linux/keyman-config/kmpmetadata.py
Executable file
|
|
@ -0,0 +1,136 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import json
|
||||
import sys
|
||||
import os.path
|
||||
import magic
|
||||
|
||||
def print_info(info):
|
||||
try:
|
||||
print("---- Info ----")
|
||||
print("Name: ", info['name']['description'])
|
||||
print("Copyright: ", info['copyright']['description'])
|
||||
print("Version: ", info['version']['description'])
|
||||
print("Author: ", info['author']['description'])
|
||||
print("URL: ", info['author']['url'])
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def print_system(system):
|
||||
try:
|
||||
print("---- System ----")
|
||||
print("File Version: ", system['fileVersion'])
|
||||
print("Keyman Developer Version: ", system['keymanDeveloperVersion'])
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def print_options(options):
|
||||
try:
|
||||
print("---- Options ----")
|
||||
print("Readme File: ", options['readmeFile'])
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def print_keyboards(keyboards):
|
||||
try:
|
||||
print("---- Keyboards ----")
|
||||
for kb in keyboards:
|
||||
print("Keyboard Name: ", kb['name'])
|
||||
print("Keyboard Id: ", kb['id'])
|
||||
print("Keyboard Version: ", kb['version'])
|
||||
print("Languages")
|
||||
for lang in kb['languages']:
|
||||
print(" Name: ", lang['name'], "Id: ", lang['id'])
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def determine_filetype(filename):
|
||||
name, ext = os.path.splitext(filename)
|
||||
if ext == ".ico":
|
||||
return "Keyboard icon"
|
||||
elif ext == ".kmn":
|
||||
return "Keyboard source"
|
||||
elif ext == ".kmx":
|
||||
return "Compiled keyboard"
|
||||
elif ext == ".kvk":
|
||||
return "Compiled on screen keyboard"
|
||||
elif ext == ".ttf" or ext == ".otf":
|
||||
return "Font"
|
||||
elif ext == ".txt" or ext == ".pdf" or ext == ".htm" or ext == ".html":
|
||||
return "Documentation"
|
||||
elif ext == ".inf" or ext == ".json":
|
||||
return "Metadata"
|
||||
elif ext == ".png" or ext == ".jpeg" or ext == ".jpg":
|
||||
return "Image"
|
||||
else:
|
||||
return "Unknown type"
|
||||
|
||||
def print_files(files, extracted_dir):
|
||||
try:
|
||||
print("---- Files ----")
|
||||
for kbfile in files:
|
||||
print("* File name: ", kbfile['name'])
|
||||
print(" Description: ", kbfile['description'])
|
||||
print(" Type: ", determine_filetype(kbfile['name']))
|
||||
file = os.path.join(extracted_dir, kbfile['name'])
|
||||
if os.path.isfile(file):
|
||||
print(" File", file, "exists")
|
||||
ms = magic.open(magic.MAGIC_NONE)
|
||||
ms.load()
|
||||
ftype = ms.file(file)
|
||||
print (" Type: ", ftype)
|
||||
ms.close()
|
||||
else:
|
||||
print(" File", file, "does not exist")
|
||||
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def parsemetadata(jsonfile, verbose):
|
||||
info = system = keyboards = files = options = nonexistent = None
|
||||
extracted_dir = os.path.dirname(jsonfile)
|
||||
|
||||
with open(jsonfile, "r") as read_file:
|
||||
data = json.load(read_file)
|
||||
for x in data:
|
||||
# print("%s: %s" % (x, data[x]))
|
||||
if x == 'info':
|
||||
info = data[x]
|
||||
elif x == 'system':
|
||||
system = data[x]
|
||||
elif x == 'keyboards':
|
||||
keyboards = data[x]
|
||||
elif x == 'files':
|
||||
files = data[x]
|
||||
elif x == 'options':
|
||||
options = data[x]
|
||||
elif x == 'nonexistent':
|
||||
nonexistent = data[x]
|
||||
if nonexistent != None:
|
||||
print("This should not happen")
|
||||
if verbose:
|
||||
print_info(info)
|
||||
print_system(system)
|
||||
if options:
|
||||
print_options(options)
|
||||
print_keyboards(keyboards)
|
||||
print_files(files, extracted_dir)
|
||||
return info, system, options, keyboards, files
|
||||
|
||||
def main(argv):
|
||||
if len(sys.argv) != 2:
|
||||
print("kmpmetadata.py <jsonfile>")
|
||||
sys.exit(2)
|
||||
inputfile = sys.argv[1]
|
||||
|
||||
if not os.path.isfile(inputfile):
|
||||
print("kmpmetadata.py Input file ", inputfile, " not found")
|
||||
print("kmpmetadata.py <jsonfile>")
|
||||
sys.exit(2)
|
||||
|
||||
parsemetadata(inputfile, True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
|
||||
49
linux/keyman-config/uninstall_kmp.py
Executable file
49
linux/keyman-config/uninstall_kmp.py
Executable file
|
|
@ -0,0 +1,49 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import sys
|
||||
import os.path
|
||||
from shutil import rmtree
|
||||
|
||||
def uninstall_kmp(keyboardid):
|
||||
kbdir = os.path.join('/usr/local/share/keyman', keyboardid)
|
||||
if not os.path.isdir(kbdir):
|
||||
print("Keyboard directory for", keyboardid, "does not exist. Aborting")
|
||||
exit(3)
|
||||
|
||||
kbdocdir = os.path.join('/usr/local/share/doc/keyman', keyboardid)
|
||||
kbfontdir = os.path.join('/usr/local/share/fonts/keyman', keyboardid)
|
||||
|
||||
print("Uninstalling keyboard:", keyboardid)
|
||||
if not os.access(kbdir, os.X_OK | os.W_OK): # Check for write access of keyman dir
|
||||
print("You do not have permissions to uninstall the keyboard files. You need to be a member of the keyman group. `sudo adduser <username> keyman`")
|
||||
exit(3)
|
||||
if os.path.isdir(kbdocdir):
|
||||
if not os.access(kbdocdir, os.X_OK | os.W_OK): # Check for write access of keyman doc dir
|
||||
print("You do not have permissions to uninstall the documentation. You need to be a member of the keyman group. `sudo adduser <username> keyman`")
|
||||
exit(3)
|
||||
rmtree(kbdocdir)
|
||||
print("Removed documentation directory:", kbdocdir)
|
||||
else:
|
||||
print("No documentation directory")
|
||||
if os.path.isdir(kbfontdir):
|
||||
if not os.access(kbfontdir, os.X_OK | os.W_OK): # Check for write access of keyman fonts
|
||||
print("You do not have permissions to uninstall the font files. You need to be a member of the keyman group. `sudo adduser <username> keyman`")
|
||||
exit(3)
|
||||
rmtree(kbfontdir)
|
||||
print("Removed font directory:", kbfontdir)
|
||||
else:
|
||||
print("No font directory")
|
||||
rmtree(kbdir)
|
||||
print("Removed keyman directory:", kbdir)
|
||||
print("Finished uninstalling keyboard:", keyboardid)
|
||||
|
||||
|
||||
def main(argv):
|
||||
if len(sys.argv) != 2:
|
||||
print("uninstall_kmp.py <keyboard id>")
|
||||
sys.exit(2)
|
||||
keyboardid = sys.argv[1]
|
||||
uninstall_kmp(keyboardid)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
Loading…
Add table
Reference in a new issue