changeset 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 652cd93dfdb4
children 02c26e6630d0
files misc/README misc/org.goffi.SAT.service setup.py
diffstat 3 files changed, 44 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/misc/README	Mon Sep 01 15:45:35 2014 +0200
+++ b/misc/README	Wed Sep 03 11:46:06 2014 +0200
@@ -10,3 +10,6 @@
 ### end of .zshrc completion ###
 
     Then, you should be able to complete a jp command line by pressing [TAB].
+
+* org.goffi.SAT.service:
+	This file is used by D-Bus to know how to launch the backend, you have to put in in D-Bus services dir (usually /usr/share/dbus-1/services)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/misc/org.goffi.SAT.service	Wed Sep 03 11:46:06 2014 +0200
@@ -0,0 +1,3 @@
+[D-BUS Service]
+Name=org.goffi.SAT
+Exec=sat
--- a/setup.py	Mon Sep 01 15:45:35 2014 +0200
+++ b/setup.py	Wed Sep 03 11:46:06 2014 +0200
@@ -28,6 +28,7 @@
 import subprocess
 from stat import ST_MODE
 import shutil
+import re
 
 # seen here: http://stackoverflow.com/questions/7275295
 try:
@@ -60,6 +61,17 @@
 NO_X_OPT = 'nox'  # don't install X dependant packages
 CLEAN_OPT = 'clean'  # remove previous installation directories
 PURGE_OPT = 'purge'  # remove building and previous installation directories
+DBUS_DIR = 'dbus-1/services'
+DBUS_FILE = 'misc/org.goffi.SAT.service'
+
+# Following map describe file to adapt with installation path:
+# key is the self attribute to get (e.g.: sh_script_path will modify self.sh_script_path file)
+# value is a dict where key is the regex of the part to change, and value is either the string
+# to replace or a tuple with a template and values to replace (if value to replace is a string,
+# the attribute from self with that name will be used).
+FILE_ADJ = {'sh_script_path': {r'PYTHON *=.*': 'PYTHON="{}"'.format(sys.executable)},
+            'dbus_service_path': {r'Exec *=.*': ('Exec={}', 'sh_script_path')},
+           }
 
 
 class MercurialException(Exception):
@@ -79,20 +91,30 @@
 
 class CustomInstall(install):
 
-    def custom_auto_options(self):
-        """Change options for twistd in the shell script
-        Mainly change the paths"""
-        sh_buffer = ""
-        with open(self.sh_script_path, 'r') as sh_file:
-            for ori_line in sh_file:
-                if ori_line.startswith('PYTHON='):
-                    dest_line = 'PYTHON="%s"\n' % sys.executable
-                else:
-                    dest_line = ori_line
-                sh_buffer += dest_line
+    def adapt_files(self):
+        """Adapt files to installed environments
 
-        with open(self.sh_script_path, 'w') as sh_file:
-            sh_file.write(sh_buffer)
+        Mainly change the paths
+        """
+        def adapter(ordered_replace, match_obj):
+            """do file adjustment, getting self attribute when needed"""
+            idx = match_obj.lastindex - 1
+            repl_data = ordered_replace[idx][1]
+            if isinstance(repl_data, tuple):
+                template = repl_data[0]
+                args = [getattr(self, arg) if isinstance(arg, basestring) else arg for arg in repl_data[1:]]
+                return template.format(*args)
+            return repl_data
+
+        for file_attr, replace_data in FILE_ADJ.iteritems():
+            file_path = getattr(self, file_attr)
+            ordered_replace = [(regex, repl) for regex, repl in replace_data.iteritems()]
+            regex = '|'.join(('({})'.format(regex) for regex, dummy in ordered_replace))
+            with open(file_path, 'r') as f:
+                buff = f.read()
+            buff = re.sub(regex, lambda match_obj: adapter(ordered_replace, match_obj), buff)
+            with open(file_path, 'w') as f:
+                f.write(buff)
 
     def custom_create_links(self):
         """Create symbolic links to executables"""
@@ -109,6 +131,7 @@
     def run(self):
         self.sh_script_path = os.path.join(self.install_lib, NAME, 'sat.sh')
         self.sh_script_links = [(self.sh_script_path, os.path.join(self.install_scripts, LAUNCH_DAEMON_COMMAND))]
+        self.dbus_service_path = os.path.join(self.install_data, 'share', DBUS_DIR, os.path.basename(DBUS_FILE))
         sys.stdout.write('running pre installation stuff\n')
         sys.stdout.flush()
         if PURGE_OPT in install_opt:
@@ -118,7 +141,7 @@
         install.run(self)
         sys.stdout.write('running post installation stuff\n')
         sys.stdout.flush()
-        self.custom_auto_options()
+        self.adapt_files()
         self.custom_create_links()
 
     def confirm(self, message):
@@ -261,6 +284,7 @@
                     'sat_frontends': ['wix/COPYING']},
       data_files=[(os.path.join(sys.prefix, 'share/locale/fr/LC_MESSAGES'), ['i18n/fr/LC_MESSAGES/sat.mo']),
                   ('share/doc/%s' % NAME, ['CHANGELOG', 'COPYING', 'INSTALL', 'README', 'README4TRANSLATORS']),
+                  (os.path.join('share', DBUS_DIR), (DBUS_FILE,)),
                   ],
       scripts=['frontends/src/jp/jp', 'frontends/src/primitivus/primitivus', 'frontends/src/wix/wix'],
       zip_safe=False,