Debian Codegraph @

wxgeometrie/dependances.py

raw symbols ◀ ▶ SCIP
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, importlib, 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 importlib.import_module(module)
96 except ModuleNotFoundError:
97 modules_manquants.append(module_name)
98
99
100
101 if modules_manquants:
102 try:
103 if not sys.stdin.isatty():
104 raise ImportError
105 if input(f"Certaines librairies manquent : {modules_manquants}.\n"
106 "Voulez-vous les installer automatiquement ? (o/N)"
107 ).lower() not in ('o', 'oui', 'yes', 'y'):
108 raise ImportError
109 # Try to install missing modules using pip.
110 pip_modules = [pip_packages.get(module, module) for module in modules_manquants]
111 try:
112 subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--user'] + pip_modules)
113 except subprocess.CalledProcessError:
114 # Maybe we are in a virtual environment, so --user is meaningless ?
115 try:
116 subprocess.check_call([sys.executable, '-m', 'pip', 'install'] + pip_modules)
117 except subprocess.CalledProcessError:
118 print("L'installation automatique des modules manquants a échouée.")
119 raise ImportError
120 print('Des modules indispensables ont été installés.')
121 print('Redémarrage de Géophar...')
122 os.execv(sys.executable, [sys.executable] + sys.argv)
123 sys.exit()
124 except ImportError:
125 if plateforme == 'Linux' and shutil.which('apt'):
126 paquets = ' '.join(dependances[m] for m in modules_manquants)
127 print(f"""
128 [\033[91mERREUR\033[39m] Certains paquets sont manquants : {paquets}.
129
130 Votre distribution semble être dérivée de Debian ou d'Ubuntu,
131 il est possible de remédier à cette situation en lançant la commande :
132 ----------------------------------------------------------------------
133 \033[1m\
134 sudo apt update; sudo apt install {paquets}\
135 \033[0m
136 ----------------------------------------------------------------------
137 Vous pourrez ensuite relancer Géophar.
138 """)
139 else:
140 print('** Erreur fatale **\nLes modules suivants sont introuvables !')
141 print('MODULE(S) MANQUANT(S): %s.' % ', '.join(modules_manquants))
142 sys.exit(-1)
143
144
145 def configurer_dependances():
146 import PyQt5
147
148 # ---------------------------
149 # Configuration de Matplotlib
150 # ---------------------------
151 try:
152 # Cx_Freeze version needs this (so that matplotlib can found pyparsing).
153 import pyparsing
154 except ImportError:
155 pass
156 import matplotlib
157 matplotlib.rcParams['backend'] = 'Qt5Agg'
158 try:
159 # For old versions of matplotlib (version < 3.0)
160 if matplotlib.__version__.split(".")[0] < '3':
161 matplotlib.rcParams['backend.qt5'] ='PyQt5'
162 matplotlib.rcParams["text.latex.unicode"] = latex_unicode
163 except KeyError:
164 pass
165 matplotlib.use(moteur_de_rendu)
166 matplotlib.rcParams['text.usetex'] = latex
167
168 # A changer *avant* d'importer pylab ?
169 matplotlib.rcParams['font.family'] ='serif'
170 #matplotlib.rcParams['font.sans-serif'] ='STIXGeneral'
171 matplotlib.rcParams['font.serif'] ='STIXGeneral'
172 #matplotlib.rcParams['font.monospace'] ='STIXGeneral'
173 matplotlib.rcParams['mathtext.fontset'] ='stix'
174