Debian Codegraph @

wxgeometrie/dependances.py

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 # -*- coding: utf-8 -*-
2
3 # WxGeometrie
4 # Dynamic geometry, graph plotter, and more for french mathematic teachers.
5 # Copyright (C) 2005-2013 Nicolas Pourcelot
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
21
22 # Ce module sert à tester que toutes les dépendances sont satisfaites,
23 # et à y remédier dans le cas contraire (si possible).
24
25 import sys, imp, platform, os, shutil, subprocess
26
27 # ------------------------------------------------------------------------------
28 # CONFIGURATION
29 # ------------------------------------------------------------------------------
30
31 from .version import NOMPROG
32
33 # Les valeurs sont les noms des paquets sous Debian/Ubuntu.
34 dependances = {'PyQt5': 'python3-pyqt5',
35 'matplotlib': 'python3-matplotlib',
36 'scipy': 'python3-scipy',
37 'numpy': 'python3-numpy',
38 'mpmath': 'python3-mpmath',
39 'PyQt5.Qsci': 'python3-pyqt5.qsci',
40 'PyQt5.QtSvg': 'python3-pyqt5.qtsvg',
41 }
42
43
44 # Most of the time, the name of the pip package is just the module name.
45 # However, those are exceptions:
46 pip_packages = {'PyQt5.Qsci': 'Qscintilla',
47 'PyQt5.QtSvg': '', # already included in PyQt5
48 }
49
50
51 python_version_min = (3, 5)
52 python_version_max = (3, 99)
53
54 plateforme = platform.system() #'Windows' ou 'Linux' par exemple.
55
56 # Paramètres de matplotlib
57 # Utiliser une installation LaTeX existante (meilleur rendu mais très lent !)
58 latex = False
59 latex_unicode = True
60 moteur_de_rendu = 'Qt5Agg'
61
62 # ------------------------------------------------------------------------------
63
64
65 def tester_dependances():
66 """
67 Cette procédure teste si la liste des dépendances est à jour. Dans le cas
68 contraire, elle émet comme message une suite de commandes que
69 l'utilisateur peut lancer (s'il est administrateur/sudoer) afin de
70 satisfaire la liste des dépendances.
71 """
72 if hasattr(sys, 'frozen'):
73 # Ne pas faire ces tests avec py2exe/py2app/CxFreeze (non seulement inutiles, mais en plus ils échouent).
74 return
75 # Make sure I have the right Python version.
76 version_python_supportee = True
77 if sys.version_info[:2] < python_version_min:
78 print(" ** Erreur fatale **")
79 print(NOMPROG + " nécessite Python %d.%d au minimum." % python_version_min)
80 version_python_supportee = False
81 elif sys.version_info[:2] > python_version_max:
82 print(" ** Erreur fatale **")
83 print(NOMPROG + " supporte Python %d.%d au maximum." % python_version_max)
84 version_python_supportee = False
85
86 if not version_python_supportee:
87 print("Python %d.%d détecté." % sys.version_info[:2])
88 sys.exit(-1)
89
90 # Test for dependencies:
91 modules_manquants = []
92 for module in dependances:
93 module_name = module
94 try:
95 # imp.find_module() doesn't support submodules.
96 path = None
97 while '.' in module:
98 module, submodule = module.split('.', 1)
99 f, filename, description = imp.find_module(module, path)
100 module = submodule
101 path = [filename]
102 imp.find_module(module, path)
103 except ImportError:
104 modules_manquants.append(module_name)
105
106
107
108 if modules_manquants:
109 try:
110 if not sys.stdin.isatty():
111 raise ImportError
112 if input(f"Certaines librairies manquent : {modules_manquants}.\n"
113 "Voulez-vous les installer automatiquement ? (o/N)"
114 ).lower() not in ('o', 'oui', 'yes', 'y'):
115 raise ImportError
116 # Try to install missing modules using pip.
117 pip_modules = [pip_packages.get(module, module) for module in modules_manquants]
118 try:
119 subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--user'] + pip_modules)
120 except subprocess.CalledProcessError:
121 # Maybe we are in a virtual environment, so --user is meaningless ?
122 try:
123 subprocess.check_call([sys.executable, '-m', 'pip', 'install'] + pip_modules)
124 except subprocess.CalledProcessError:
125 print("L'installation automatique des modules manquants a échouée.")
126 raise ImportError
127 print('Des modules indispensables ont été installés.')
128 print('Redémarrage de Géophar...')
129 os.execv(sys.executable, [sys.executable] + sys.argv)
130 sys.exit()
131 except ImportError:
132 if plateforme == 'Linux' and shutil.which('apt'):
133 paquets = ' '.join(dependances[m] for m in modules_manquants)
134 print(f"""
135 [\033[91mERREUR\033[39m] Certains paquets sont manquants : {paquets}.
136
137 Votre distribution semble être dérivée de Debian ou d'Ubuntu,
138 il est possible de remédier à cette situation en lançant la commande :
139 ----------------------------------------------------------------------
140 \033[1m\
141 sudo apt update; sudo apt install {paquets}\
142 \033[0m
143 ----------------------------------------------------------------------
144 Vous pourrez ensuite relancer Géophar.
145 """)
146 else:
147 print('** Erreur fatale **\nLes modules suivants sont introuvables !')
148 print('MODULE(S) MANQUANT(S): %s.' % ', '.join(modules_manquants))
149 sys.exit(-1)
150
151
152 def configurer_dependances():
153 import PyQt5
154
155 # ---------------------------
156 # Configuration de Matplotlib
157 # ---------------------------
158 try:
159 # Cx_Freeze version needs this (so that matplotlib can found pyparsing).
160 import pyparsing
161 except ImportError:
162 pass
163 import matplotlib
164 matplotlib.rcParams['backend'] = 'Qt5Agg'
165 try:
166 # For old versions of matplotlib (version < 3.0)
167 if matplotlib.__version__.split(".")[0] < '3':
168 matplotlib.rcParams['backend.qt5'] ='PyQt5'
169 matplotlib.rcParams["text.latex.unicode"] = latex_unicode
170 except KeyError:
171 pass
172 matplotlib.use(moteur_de_rendu)
173 matplotlib.rcParams['text.usetex'] = latex
174
175 # A changer *avant* d'importer pylab ?
176 matplotlib.rcParams['font.family'] ='serif'
177 #matplotlib.rcParams['font.sans-serif'] ='STIXGeneral'
178 matplotlib.rcParams['font.serif'] ='STIXGeneral'
179 #matplotlib.rcParams['font.monospace'] ='STIXGeneral'
180 matplotlib.rcParams['mathtext.fontset'] ='stix'
181