Debian Codegraph @

tools/linux-install.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # WxGeometrie
5 # Dynamic geometry, graph plotter, and more for french mathematic teachers.
6 # Copyright (C) 2005-2013 Nicolas Pourcelot
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
22
23 import os, sys, stat
24 from os.path import join, expanduser, isdir
25 from .scriptlib import command, cd, cp
26
27 _module_path = os.path.split(os.path.realpath(sys._getframe().f_code.co_filename))[0]
28
29 print('Repertoire courant : ' + _module_path)
30
31 paths = {
32 'root': '/usr/local', # os.environ['XDG_DATA_DIRS'].split(':')[-1],
33 'local': expanduser('~/.local'),
34 }
35
36 locations = {
37 'desktop_path': 'applications',
38 'mime_root_path': 'mime',
39 'mime_path': 'mime/packages',
40 'svg_icon_path': 'icons/hicolor/scalable/apps',
41 'png_icon_path': 'icons/hicolor/48x48/apps',
42 }
43
44 # Choix du mode d'installation (local ou global).
45
46 while True:
47 choice = input('Installer pour tous les utilisateurs (o/N) ?')
48 if choice in ('n', 'N', ''):
49 choice = 'local'
50 break
51 elif choice in ('o', 'O'):
52 if not os.access(paths['root'], os.W_OK):
53 print("\n\033[1;31m*** ERREUR ***\033[0m")
54 print("Vous n'avez pas la permission d'installer pour "
55 "tous les utilisateurs.")
56 print("Tapez '\033[0;32msudo python tools/linux-install.py\033[0m' pour une "
57 "installation multi-utilisateurs.")
58 sys.exit(1)
59 choice = 'root'
60 break
61 else:
62 print("Réponse incorrecte (tapez 'o' ou 'N').")
63
64
65 for loc in locations:
66 locations[loc] = join(paths[choice], 'share', locations[loc])
67 if not isdir(locations[loc]):
68 os.makedirs(locations[loc])
69
70 # Copie des fichiers.
71
72 cd(join(_module_path, 'resources'))
73
74 cp('geophar.desktop', locations['desktop_path'])
75 cp('geophar.svg', locations['svg_icon_path'])
76 cp('geophar.png', locations['png_icon_path'])
77 cp('x-geophar.xml', locations['mime_path'])
78
79 cd('../..')
80
81 # Création du fichier sh.
82 if choice == 'root':
83 exec_path = '/usr/local/bin'
84 else:
85 exec_path = expanduser('~/bin')
86 if not isdir(exec_path):
87 os.mkdir(exec_path)
88
89 script_name = join(exec_path, 'geophar')
90
91 with open(script_name, 'w') as f:
92 f.write('#!/bin/sh\n')
93 f.write('exec %s/geophar.pyw $*' % os.getcwd())
94
95 if choice == 'root':
96 os.chmod(script_name, stat.S_IRWXU|stat.S_IXOTH)
97 else:
98 os.chmod(script_name, stat.S_IRWXU)
99
100 # Mise à jour des bases de données.
101
102 update_desktop = 'update-desktop-database %s' % locations['desktop_path']
103 update_mime = 'update-mime-database %s' % locations['mime_root_path']
104 command(update_desktop)
105 command(update_mime)
106
107 # Création du script de désinstallation.
108
109 cd(_module_path)
110
111 with open('linux-uninstall.py', 'w') as f:
112 f.write('#!/usr/bin/env python3\n')
113 f.write('# -*- coding: utf-8 -*-\n')
114 f.write('from scriptlib import *\n')
115 f.write('rm(%s)\n' % repr(join(locations['desktop_path'], 'geophar.desktop')))
116 f.write('rm(%s)\n' % repr(join(locations['mime_path'], 'x-geophar.xml')))
117 f.write('rm(%s)\n' % repr(join(locations['svg_icon_path'], 'geophar.svg')))
118 f.write('rm(%s)\n' % repr(join(locations['png_icon_path'], 'geophar.png')))
119 f.write('rm(%s)\n' % repr(script_name))
120 f.write('command(%s)\n' % repr(update_desktop))
121 f.write('command(%s)\n' % repr(update_mime))
122 f.write('print("=== Desinstallation terminee. ===")')
123
124 os.chmod('linux-uninstall.py', stat.S_IRWXU)
125
126 print("=== L'installation est terminée. ===")
127 print("Un fichier linux-uninstall.py a été généré.")