|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import platform |
| 4 | +import shutil |
| 5 | +import tempfile |
| 6 | +from distutils.dir_util import copy_tree |
| 7 | +from urllib.request import urlretrieve |
| 8 | +from zipfile import ZipFile |
| 9 | + |
| 10 | +class Installer: |
| 11 | + base_link = "http://apertium.projectjj.com/{}" |
| 12 | + |
| 13 | + def __init__(self, languages): # type: (Installer, list) -> None |
| 14 | + self._install_path = os.getenv("LOCALAPPDATA") |
| 15 | + self._apertium_path = os.path.join(self._install_path, "apertium-all-dev") |
| 16 | + self._download_path = tempfile.mkdtemp() |
| 17 | + self._languages = languages |
| 18 | + logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG) |
| 19 | + self._logger = logging.getLogger() |
| 20 | + self._logger.setLevel(logging.DEBUG) |
| 21 | + |
| 22 | + def _download_zips(self, download_files, extract_path): # type: (Installer, dict, str) -> None |
| 23 | + for zip_name, zip_link in download_files.items(): |
| 24 | + zip_download_path = os.path.join(self._download_path, zip_name) |
| 25 | + urlretrieve(Installer.base_link.format(zip_link), filename=zip_download_path) |
| 26 | + self._logger.info("%s download completed", zip_name) |
| 27 | + |
| 28 | + # Extract the zip |
| 29 | + with ZipFile(zip_download_path) as zip_file: |
| 30 | + zip_file.extractall(path=extract_path) |
| 31 | + self._logger.info("%s Extraction completed", zip_name) |
| 32 | + os.remove(zip_download_path) |
| 33 | + self._logger.info("%s removed", zip_name) |
| 34 | + |
| 35 | + def _download_apertium_windows(self): # type: (Installer) -> None |
| 36 | + """Installs Apertium-all-dev to %localappdata%""" |
| 37 | + |
| 38 | + apertium_windows = { |
| 39 | + "apertium-all-dev.zip": "/win64/nightly/apertium-all-dev.zip", |
| 40 | + } |
| 41 | + |
| 42 | + self._download_zips(apertium_windows, self._install_path) |
| 43 | + |
| 44 | + def _download_package(self): # type: (Installer) -> None |
| 45 | + """Installs Language Data to Apertium""" |
| 46 | + |
| 47 | + if platform.system() == "Windows": |
| 48 | + zip_path = "win32/nightly/data.php?zip=" |
| 49 | + else: |
| 50 | + raise ValueError("Installation for {} is not supported".format(platform.system())) |
| 51 | + language_zip = {} |
| 52 | + for curr_lang in self._languages: |
| 53 | + language_zip[curr_lang] = zip_path + curr_lang |
| 54 | + |
| 55 | + self._download_zips(language_zip, self._download_path) |
| 56 | + |
| 57 | + # move the extracted files to desired location |
| 58 | + lang_data_path = os.path.join(self._download_path, "usr", "share", "apertium") |
| 59 | + |
| 60 | + self._logger.info("Copying Language Data to Apertium") |
| 61 | + for directory in os.listdir(lang_data_path): |
| 62 | + source = os.path.join(lang_data_path, directory) |
| 63 | + destination = os.path.join(self._apertium_path, "share", "apertium", directory) |
| 64 | + copy_tree(source, destination) |
| 65 | + self._logger.info("%s -> %s", source, destination) |
| 66 | + |
| 67 | + shutil.rmtree(os.path.join(self._download_path, "usr")) |
| 68 | + |
| 69 | + def _edit_modes(self): # type: (Installer) -> None |
| 70 | + """The mode files need to be modified before being used on Windows System |
| 71 | +
|
| 72 | + 1. Replace /usr/share with %localappdata%\apertium-all-dev\share |
| 73 | + 2. Replace "/" with "\" to make path compatible with Windows System |
| 74 | + """ |
| 75 | + |
| 76 | + # List of Mode Files |
| 77 | + mode_path = os.path.join(self._apertium_path, "share", "apertium", "modes") |
| 78 | + for f in os.listdir(mode_path): |
| 79 | + if os.path.isfile(os.path.join(mode_path, f)) and f.endswith(".mode"): |
| 80 | + self._logger.info("Editing mode %s ", f) |
| 81 | + with open(os.path.join(mode_path, f)) as infile: |
| 82 | + line = infile.read() |
| 83 | + |
| 84 | + contents = line.split(" ") |
| 85 | + # Editing mode file to be compatible with windows platform |
| 86 | + for i, t in enumerate(contents): |
| 87 | + if len(t) > 2 and t[0] == "'" and t[1] == "/": |
| 88 | + t = t.replace("/", os.sep) |
| 89 | + t = t.replace(r"\usr", self._apertium_path) |
| 90 | + contents[i] = t |
| 91 | + line = " ".join(contents) |
| 92 | + with open(os.path.join(mode_path, f), "w") as outfile: |
| 93 | + outfile.write(line) |
| 94 | + outfile.close() |
| 95 | + |
| 96 | + def install_windows(self): |
| 97 | + self._download_apertium_windows() |
| 98 | + self._download_package() |
| 99 | + self._edit_modes() |
| 100 | + |
| 101 | +def install_apertium_windows(): |
| 102 | + """Download ApertiumWin64 and Move to %localappdata%""" |
| 103 | + |
| 104 | + if platform.system() == "Windows": |
| 105 | + p = Installer(["apertium-eng", "apertium-en-es"]) |
| 106 | + p.install_windows() |
0 commit comments