|
4 | 4 | from distutils import version
|
5 | 5 | from distutils.core import Extension
|
6 | 6 | import glob
|
7 |
| -import io |
8 | 7 | import multiprocessing
|
9 | 8 | import os
|
10 | 9 | import re
|
|
13 | 12 | import sys
|
14 | 13 | import warnings
|
15 | 14 | from textwrap import fill
|
16 |
| - |
| 15 | +import shutil |
17 | 16 | import versioneer
|
18 | 17 |
|
19 | 18 |
|
20 | 19 | PY3min = (sys.version_info[0] >= 3)
|
21 | 20 | PY32min = (PY3min and sys.version_info[1] >= 2 or sys.version_info[0] > 3)
|
22 | 21 |
|
23 | 22 |
|
| 23 | +def _get_home(): |
| 24 | + """Find user's home directory if possible. |
| 25 | + Otherwise, returns None. |
| 26 | +
|
| 27 | + :see: |
| 28 | + http://mail.python.org/pipermail/python-list/2005-February/325395.html |
| 29 | + """ |
| 30 | + try: |
| 31 | + if not PY3min and sys.platform == 'win32': |
| 32 | + path = os.path.expanduser(b"~").decode(sys.getfilesystemencoding()) |
| 33 | + else: |
| 34 | + path = os.path.expanduser("~") |
| 35 | + except ImportError: |
| 36 | + # This happens on Google App Engine (pwd module is not present). |
| 37 | + pass |
| 38 | + else: |
| 39 | + if os.path.isdir(path): |
| 40 | + return path |
| 41 | + for evar in ('HOME', 'USERPROFILE', 'TMP'): |
| 42 | + path = os.environ.get(evar) |
| 43 | + if path is not None and os.path.isdir(path): |
| 44 | + return path |
| 45 | + return None |
| 46 | + |
| 47 | + |
| 48 | +def _get_xdg_cache_dir(): |
| 49 | + """ |
| 50 | + Returns the XDG cache directory, according to the `XDG |
| 51 | + base directory spec |
| 52 | + <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>`_. |
| 53 | + """ |
| 54 | + path = os.environ.get('XDG_CACHE_HOME') |
| 55 | + if path is None: |
| 56 | + path = _get_home() |
| 57 | + if path is not None: |
| 58 | + path = os.path.join(path, '.cache', 'matplotlib') |
| 59 | + return path |
| 60 | + |
| 61 | + |
24 | 62 | # This is the version of FreeType to use when building a local
|
25 | 63 | # version. It must match the value in
|
26 | 64 | # lib/matplotlib.__init__.py
|
|
70 | 108 | config.get("directories", "basedirlist").split(',')]
|
71 | 109 |
|
72 | 110 | if config.has_option('test', 'local_freetype'):
|
73 |
| - options['local_freetype'] = config.get("test", "local_freetype") |
| 111 | + options['local_freetype'] = config.getboolean("test", "local_freetype") |
74 | 112 | else:
|
75 | 113 | config = None
|
76 | 114 |
|
| 115 | +lft = bool(os.environ.get('MPLLOCALFREETYPE', False)) |
| 116 | +options['local_freetype'] = lft or options.get('local_freetype', False) |
| 117 | + |
77 | 118 |
|
78 | 119 | def get_win32_compiler():
|
79 | 120 | """
|
@@ -978,26 +1019,62 @@ def do_custom_build(self):
|
978 | 1019 | 'build', 'freetype-{0}'.format(LOCAL_FREETYPE_VERSION))
|
979 | 1020 |
|
980 | 1021 | # We've already built freetype
|
981 |
| - if os.path.isfile(os.path.join(src_path, 'objs', '.libs', 'libfreetype.a')): |
| 1022 | + if os.path.isfile( |
| 1023 | + os.path.join(src_path, 'objs', '.libs', 'libfreetype.a')): |
982 | 1024 | return
|
983 | 1025 |
|
984 | 1026 | tarball = 'freetype-{0}.tar.gz'.format(LOCAL_FREETYPE_VERSION)
|
985 | 1027 | tarball_path = os.path.join('build', tarball)
|
| 1028 | + try: |
| 1029 | + tarball_cache_dir = _get_xdg_cache_dir() |
| 1030 | + tarball_cache_path = os.path.join(tarball_cache_dir, tarball) |
| 1031 | + except: |
| 1032 | + # again, do not really care if this fails |
| 1033 | + tarball_cache_dir = None |
| 1034 | + tarball_cache_path = None |
986 | 1035 | if not os.path.isfile(tarball_path):
|
987 |
| - tarball_url = 'http://download.savannah.gnu.org/releases/freetype/{0}'.format(tarball) |
988 |
| - |
989 |
| - print("Downloading {0}".format(tarball_url)) |
990 |
| - if sys.version_info[0] == 2: |
991 |
| - from urllib import urlretrieve |
992 |
| - else: |
993 |
| - from urllib.request import urlretrieve |
994 |
| - |
995 |
| - if not os.path.exists('build'): |
996 |
| - os.makedirs('build') |
997 |
| - urlretrieve(tarball_url, tarball_path) |
| 1036 | + if (tarball_cache_path is not None and |
| 1037 | + os.path.isfile(tarball_cache_path)): |
| 1038 | + if get_file_hash(tarball_cache_path) == LOCAL_FREETYPE_HASH: |
| 1039 | + try: |
| 1040 | + # fail on Lpy, oh well |
| 1041 | + os.makedirs('build', exist_ok=True) |
| 1042 | + shutil.copy(tarball_cache_path, tarball_path) |
| 1043 | + print('Using cached tarball: {}' |
| 1044 | + .format(tarball_cache_path)) |
| 1045 | + except: |
| 1046 | + # If this fails, oh well just re-download |
| 1047 | + pass |
| 1048 | + |
| 1049 | + if not os.path.isfile(tarball_path): |
| 1050 | + url_fmt = ( |
| 1051 | + 'http://download.savannah.gnu.org/releases/freetype/{0}') |
| 1052 | + tarball_url = url_fmt.format(tarball) |
| 1053 | + |
| 1054 | + print("Downloading {0}".format(tarball_url)) |
| 1055 | + if sys.version_info[0] == 2: |
| 1056 | + from urllib import urlretrieve |
| 1057 | + else: |
| 1058 | + from urllib.request import urlretrieve |
| 1059 | + |
| 1060 | + if not os.path.exists('build'): |
| 1061 | + os.makedirs('build') |
| 1062 | + urlretrieve(tarball_url, tarball_path) |
| 1063 | + if get_file_hash(tarball_path) == LOCAL_FREETYPE_HASH: |
| 1064 | + try: |
| 1065 | + # this will fail on LPy, oh well |
| 1066 | + os.makedirs(tarball_cache_dir, exist_ok=True) |
| 1067 | + shutil.copy(tarball_cache_path, tarball_path) |
| 1068 | + print('Cached tarball at: {}' |
| 1069 | + .format(tarball_cache_path)) |
| 1070 | + except: |
| 1071 | + # again, we do not care if this fails, can |
| 1072 | + # always re download |
| 1073 | + pass |
998 | 1074 |
|
999 | 1075 | if get_file_hash(tarball_path) != LOCAL_FREETYPE_HASH:
|
1000 |
| - raise IOError("{0} does not match expected hash.".format(tarball)) |
| 1076 | + raise IOError( |
| 1077 | + "{0} does not match expected hash.".format(tarball)) |
1001 | 1078 |
|
1002 | 1079 | print("Building {0}".format(tarball))
|
1003 | 1080 | cflags = 'CFLAGS="{0} -fPIC" '.format(os.environ.get('CFLAGS', ''))
|
|
0 commit comments