comparison setup.py @ 1150:beaf8d4475e4

misc (D-Bus, installation): added a .service file for D-Bus auto-launch feature + installation from setup.py (path adaptation now use regex).
author Goffi <goffi@goffi.org>
date Wed, 03 Sep 2014 11:46:06 +0200
parents a7cdf03c00e9
children 25a16df15ae2
comparison
equal deleted inserted replaced
1149:652cd93dfdb4 1150:beaf8d4475e4
26 import os 26 import os
27 import sys 27 import sys
28 import subprocess 28 import subprocess
29 from stat import ST_MODE 29 from stat import ST_MODE
30 import shutil 30 import shutil
31 import re
31 32
32 # seen here: http://stackoverflow.com/questions/7275295 33 # seen here: http://stackoverflow.com/questions/7275295
33 try: 34 try:
34 from setuptools.command import egg_info 35 from setuptools.command import egg_info
35 egg_info.write_toplevel_names 36 egg_info.write_toplevel_names
58 NO_PREINSTALL_OPT = 'nopreinstall' # skip all preinstallation checks 59 NO_PREINSTALL_OPT = 'nopreinstall' # skip all preinstallation checks
59 AUTO_DEB_OPT = 'autodeb' # automaticaly install debs 60 AUTO_DEB_OPT = 'autodeb' # automaticaly install debs
60 NO_X_OPT = 'nox' # don't install X dependant packages 61 NO_X_OPT = 'nox' # don't install X dependant packages
61 CLEAN_OPT = 'clean' # remove previous installation directories 62 CLEAN_OPT = 'clean' # remove previous installation directories
62 PURGE_OPT = 'purge' # remove building and previous installation directories 63 PURGE_OPT = 'purge' # remove building and previous installation directories
64 DBUS_DIR = 'dbus-1/services'
65 DBUS_FILE = 'misc/org.goffi.SAT.service'
66
67 # Following map describe file to adapt with installation path:
68 # key is the self attribute to get (e.g.: sh_script_path will modify self.sh_script_path file)
69 # value is a dict where key is the regex of the part to change, and value is either the string
70 # to replace or a tuple with a template and values to replace (if value to replace is a string,
71 # the attribute from self with that name will be used).
72 FILE_ADJ = {'sh_script_path': {r'PYTHON *=.*': 'PYTHON="{}"'.format(sys.executable)},
73 'dbus_service_path': {r'Exec *=.*': ('Exec={}', 'sh_script_path')},
74 }
63 75
64 76
65 class MercurialException(Exception): 77 class MercurialException(Exception):
66 pass 78 pass
67 79
77 return True 89 return True
78 90
79 91
80 class CustomInstall(install): 92 class CustomInstall(install):
81 93
82 def custom_auto_options(self): 94 def adapt_files(self):
83 """Change options for twistd in the shell script 95 """Adapt files to installed environments
84 Mainly change the paths""" 96
85 sh_buffer = "" 97 Mainly change the paths
86 with open(self.sh_script_path, 'r') as sh_file: 98 """
87 for ori_line in sh_file: 99 def adapter(ordered_replace, match_obj):
88 if ori_line.startswith('PYTHON='): 100 """do file adjustment, getting self attribute when needed"""
89 dest_line = 'PYTHON="%s"\n' % sys.executable 101 idx = match_obj.lastindex - 1
90 else: 102 repl_data = ordered_replace[idx][1]
91 dest_line = ori_line 103 if isinstance(repl_data, tuple):
92 sh_buffer += dest_line 104 template = repl_data[0]
93 105 args = [getattr(self, arg) if isinstance(arg, basestring) else arg for arg in repl_data[1:]]
94 with open(self.sh_script_path, 'w') as sh_file: 106 return template.format(*args)
95 sh_file.write(sh_buffer) 107 return repl_data
108
109 for file_attr, replace_data in FILE_ADJ.iteritems():
110 file_path = getattr(self, file_attr)
111 ordered_replace = [(regex, repl) for regex, repl in replace_data.iteritems()]
112 regex = '|'.join(('({})'.format(regex) for regex, dummy in ordered_replace))
113 with open(file_path, 'r') as f:
114 buff = f.read()
115 buff = re.sub(regex, lambda match_obj: adapter(ordered_replace, match_obj), buff)
116 with open(file_path, 'w') as f:
117 f.write(buff)
96 118
97 def custom_create_links(self): 119 def custom_create_links(self):
98 """Create symbolic links to executables""" 120 """Create symbolic links to executables"""
99 # the script which launch the daemon 121 # the script which launch the daemon
100 for source, dest in self.sh_script_links: 122 for source, dest in self.sh_script_links:
107 os.chmod(dest_name, mode) 129 os.chmod(dest_name, mode)
108 130
109 def run(self): 131 def run(self):
110 self.sh_script_path = os.path.join(self.install_lib, NAME, 'sat.sh') 132 self.sh_script_path = os.path.join(self.install_lib, NAME, 'sat.sh')
111 self.sh_script_links = [(self.sh_script_path, os.path.join(self.install_scripts, LAUNCH_DAEMON_COMMAND))] 133 self.sh_script_links = [(self.sh_script_path, os.path.join(self.install_scripts, LAUNCH_DAEMON_COMMAND))]
134 self.dbus_service_path = os.path.join(self.install_data, 'share', DBUS_DIR, os.path.basename(DBUS_FILE))
112 sys.stdout.write('running pre installation stuff\n') 135 sys.stdout.write('running pre installation stuff\n')
113 sys.stdout.flush() 136 sys.stdout.flush()
114 if PURGE_OPT in install_opt: 137 if PURGE_OPT in install_opt:
115 self.purge() 138 self.purge()
116 elif CLEAN_OPT in install_opt: 139 elif CLEAN_OPT in install_opt:
117 self.clean() 140 self.clean()
118 install.run(self) 141 install.run(self)
119 sys.stdout.write('running post installation stuff\n') 142 sys.stdout.write('running post installation stuff\n')
120 sys.stdout.flush() 143 sys.stdout.flush()
121 self.custom_auto_options() 144 self.adapt_files()
122 self.custom_create_links() 145 self.custom_create_links()
123 146
124 def confirm(self, message): 147 def confirm(self, message):
125 """Ask the user for a confirmation""" 148 """Ask the user for a confirmation"""
126 message += 'Proceed' 149 message += 'Proceed'
259 'sat_frontends.primitivus', 'sat_frontends.wix', 'sat_frontends.tools', 'sat.stdui', 'twisted.plugins'], 282 'sat_frontends.primitivus', 'sat_frontends.wix', 'sat_frontends.tools', 'sat.stdui', 'twisted.plugins'],
260 package_data={'sat': ['sat.sh'], 283 package_data={'sat': ['sat.sh'],
261 'sat_frontends': ['wix/COPYING']}, 284 'sat_frontends': ['wix/COPYING']},
262 data_files=[(os.path.join(sys.prefix, 'share/locale/fr/LC_MESSAGES'), ['i18n/fr/LC_MESSAGES/sat.mo']), 285 data_files=[(os.path.join(sys.prefix, 'share/locale/fr/LC_MESSAGES'), ['i18n/fr/LC_MESSAGES/sat.mo']),
263 ('share/doc/%s' % NAME, ['CHANGELOG', 'COPYING', 'INSTALL', 'README', 'README4TRANSLATORS']), 286 ('share/doc/%s' % NAME, ['CHANGELOG', 'COPYING', 'INSTALL', 'README', 'README4TRANSLATORS']),
287 (os.path.join('share', DBUS_DIR), (DBUS_FILE,)),
264 ], 288 ],
265 scripts=['frontends/src/jp/jp', 'frontends/src/primitivus/primitivus', 'frontends/src/wix/wix'], 289 scripts=['frontends/src/jp/jp', 'frontends/src/primitivus/primitivus', 'frontends/src/wix/wix'],
266 zip_safe=False, 290 zip_safe=False,
267 dependency_links=['http://www.blarg.net/%7Esteveha/pyfeed-0.7.4.tar.gz', 'http://www.blarg.net/%7Esteveha/xe-0.7.4.tar.gz'], 291 dependency_links=['http://www.blarg.net/%7Esteveha/pyfeed-0.7.4.tar.gz', 'http://www.blarg.net/%7Esteveha/xe-0.7.4.tar.gz'],
268 install_requires=['twisted', 'wokkel >= 0.7.1', 'progressbar', 'urwid >= 1.1.0', 'urwid-satext >= 0.3.0', 'pyfeed', 'xe', 'mutagen', 'pillow', 'lxml', 'pyxdg', 'markdown', 'html2text', 'pycrypto >= 2.6.1', 'python-potr'], 292 install_requires=['twisted', 'wokkel >= 0.7.1', 'progressbar', 'urwid >= 1.1.0', 'urwid-satext >= 0.3.0', 'pyfeed', 'xe', 'mutagen', 'pillow', 'lxml', 'pyxdg', 'markdown', 'html2text', 'pycrypto >= 2.6.1', 'python-potr'],