Skip to content

Commit a086dbc

Browse files
committed
Downloading
1 parent 6d30a17 commit a086dbc

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
__pycache__/
2+
.ropeproject/

mystem/capslock.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from pathlib import Path
22
import platform
3+
import re
34

45
PACKAGES_ROOT = 'http://download.cdn.yandex.net/mystem/'
56

@@ -16,7 +17,11 @@
1617
('Windows', 32): 'mystem-3.0-win7-32bit.zip',
1718
}
1819

19-
MSDOS = platform.system() == 'Windows'
20+
RE_ARCHIVE = re.compile('(\.tar\.gz|\.zip)$')
21+
22+
OS = platform.system()
23+
24+
MSDOS = OS == 'Windows'
2025

2126
OUR_ROOT = Path(__file__).parent.resolve()
2227

mystem/install.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
from pathlib import Path
12
import os
3+
from shutil import copyfileobj
24
from subprocess import run, PIPE
5+
import sys
6+
import tarfile
7+
from tempfile import TemporaryDirectory
8+
from urllib.request import urlopen
9+
from zipfile import ZipFile
310

4-
from .capslock import MYSTEM, MYSTEM_VERSION_STRING
11+
from .capslock import (MYSTEM, MYSTEM_DIR, MYSTEM_VERSION_STRING, OS,
12+
PACKAGES_ROOT, PACKAGES, RE_ARCHIVE)
513

614

715
def is_installed():
@@ -17,3 +25,27 @@ def is_usable():
1725
pass
1826

1927
return False
28+
29+
30+
def install_impl(bits=64):
31+
p = (OS, bits)
32+
if p not in PACKAGES:
33+
raise NotImplementedError()
34+
35+
filetype = RE_ARCHIVE.search(PACKAGES[p]).group()
36+
winrar = ZipFile if filetype == '.zip' else tarfile.open
37+
38+
with TemporaryDirectory() as dirname:
39+
filename = Path(dirname) / ('a' + filetype)
40+
url = PACKAGES_ROOT + PACKAGES[p]
41+
42+
print('Downloading %s' % url, file=sys.stderr)
43+
print('Saving as %s' % filename, file=sys.stderr)
44+
45+
with urlopen(url) as a, open(filename, 'wb') as b:
46+
copyfileobj(a, b)
47+
48+
print('Extracting to %s' % MYSTEM_DIR, file=sys.stderr)
49+
50+
with winrar(str(filename)) as a:
51+
a.extractall(str(MYSTEM_DIR))

0 commit comments

Comments
 (0)