diff setup.py @ 449:981ed669d3b3

/!\ reorganize all the file hierarchy, move the code and launching script to src: - browser_side --> src/browser - public --> src/browser_side/public - libervia.py --> src/browser/libervia_main.py - libervia_server --> src/server - libervia_server/libervia.sh --> src/libervia.sh - twisted --> src/twisted - new module src/common - split constants.py in 3 files: - src/common/constants.py - src/browser/constants.py - src/server/constants.py - output --> html (generated by pyjsbuild during the installation) - new option/parameter "data_dir" (-d) to indicates the directory containing html and server_css - setup.py installs libervia to the following paths: - src/common --> <LIB>/libervia/common - src/server --> <LIB>/libervia/server - src/twisted --> <LIB>/twisted - html --> <SHARE>/libervia/html - server_side --> <SHARE>libervia/server_side - LIBERVIA_INSTALL environment variable takes 2 new options with prompt confirmation: - clean: remove previous installation directories - purge: remove building and previous installation directories You may need to update your sat.conf and/or launching script to update the following options/parameters: - ssl_certificate - data_dir
author souliane <souliane@mailoo.org>
date Tue, 20 May 2014 06:41:16 +0200
parents f94c3caea2ba
children 1a0cec9b0f1e
line wrap: on
line diff
--- a/setup.py	Fri May 16 11:51:10 2014 +0200
+++ b/setup.py	Tue May 20 06:41:16 2014 +0200
@@ -1,8 +1,9 @@
-#!/usr/bin/python
+#!/usr/bin/env python2
 # -*- coding: utf-8 -*-
 
 # Libervia: a Salut à Toi frontend
-# Copyright (C) 2011, 2012, 2013, 2014 Jérôme Poisson <goffi@goffi.org>
+# Copyright (C) 2011, 2012, 2013, 2014  Jérôme Poisson (goffi@goffi.org)
+# Copyright (C) 2013, 2014 Adrien Cossa (souliane@mailoo.org)
 
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published by
@@ -26,6 +27,8 @@
 import sys
 import subprocess
 from stat import ST_MODE
+import shutil
+from src.server.constants import Const as C
 
 # seen here: http://stackoverflow.com/questions/7275295
 try:
@@ -52,6 +55,12 @@
 NAME = 'libervia'
 LAUNCH_DAEMON_COMMAND = 'libervia'
 
+ENV_LIBERVIA_INSTALL = "LIBERVIA_INSTALL"  # environment variable to customise installation
+NO_PREINSTALL_OPT = 'nopreinstall'  # skip all preinstallation checks
+AUTO_DEB_OPT = 'autodeb'  # automaticaly install debs
+CLEAN_OPT = 'clean'  # remove previous installation directories
+PURGE_OPT = 'purge'  # remove building and previous installation directories
+
 
 class MercurialException(Exception):
     pass
@@ -78,9 +87,9 @@
         with open(self.sh_script_path, 'r') as sh_file:
             for ori_line in sh_file:
                 if ori_line.startswith('DAEMON='):
-                    dest_line = 'DAEMON=""\n'  # we want to launch sat as a daemon
-                elif ori_line.startswith('TAP_PATH='):
-                    dest_line = 'TAP_PATH="%s/"\n' % run_dir
+                    dest_line = 'DAEMON=""\n'  # we want to launch libervia as a daemon
+                elif ori_line.startswith('DATA_DIR='):
+                    dest_line = 'DATA_DIR="%s"\n' % self.install_data_dir
                 elif ori_line.startswith('PYTHON='):
                     dest_line = 'PYTHON="%s"\n' % sys.executable
                 else:
@@ -93,36 +102,117 @@
     def custom_create_links(self):
         """Create symbolic links to executables"""
         # the script which launch the daemon
-        links = [(self.sh_script_path, LAUNCH_DAEMON_COMMAND)]
-        for source, dest in links:
-            dest_name, copied = copy_file(source, os.path.join(self.install_scripts, dest), link='sym')
+        for source, dest in self.sh_script_links:
+            dest = os.path.join(self.install_scripts, dest)
+            if os.path.islink(dest) and os.readlink(dest) != source:
+                os.remove(dest)  # copy_file doesn't force the link update
+            dest_name, copied = copy_file(source, dest, link='sym')
             assert (copied)
             # we change the perm in the same way as in the original install_scripts
             mode = ((os.stat(dest_name)[ST_MODE]) | 0555) & 07777
             os.chmod(dest_name, mode)
 
     def pyjs_build(self):
-        return subprocess.call('pyjsbuild libervia --no-compile-inplace -m -I %s' % self.install_lib, shell=True)
+        """Build the browser side JS files from Python source."""
+        cwd = os.getcwd()
+        os.chdir(os.path.join('src', 'browser'))
+        result = subprocess.call('pyjsbuild libervia_main --no-compile-inplace -I %s -o %s' %
+                                 (self.install_lib, self.pyjamas_output_dir), shell=True)
+        os.chdir(cwd)
+        return result
+
+    def copy_data_files(self):
+        """To copy the JS files couldn't be done with the data_files parameter
+        of setuptools.setup because all the files to be copied must exist before
+        the call. Also, we need the value of self.install_lib to build the JS
+        files (it's not easily predictable as it may vary from one system to
+        another), so we can't call pyjsbuild before setuptools.setup.
+        """
+        html = os.path.join(self.install_data_dir, C.HTML_DIR)
+        if os.path.isdir(html):
+            shutil.rmtree(html, ignore_errors=True)
+        shutil.copytree(self.pyjamas_output_dir, html)
 
     def run(self):
+        self.sh_script_path = os.path.join(self.install_lib, NAME, 'libervia.sh')
+        self.sh_script_links = [(self.sh_script_path, LAUNCH_DAEMON_COMMAND)]
+        self.install_data_dir = os.path.join(self.install_data, 'share', NAME)
+        self.pyjamas_output_dir = os.path.join(os.getcwd(), 'html')
         sys.stdout.write('running pre installation stuff\n')
         sys.stdout.flush()
-        build_result = self.pyjs_build()
+        if PURGE_OPT in install_opt:
+            self.purge()
+        elif CLEAN_OPT in install_opt:
+            self.clean()
+        install.run(self)
+        sys.stdout.write('running post installation stuff\n')
+        sys.stdout.flush()
+        build_result = self.pyjs_build()  # build after libervia.common is accessible
         if build_result == 127:  # TODO: remove magic string
             print "pyjsbuild is not installed or not accessible from the PATH of user '%s'" % os.getenv('USERNAME')
             return
         if build_result != 0:
             print "pyjsbuild failed to build libervia"
             return
-        install.run(self)
-        sys.stdout.write('running post installation stuff\n')
-        sys.stdout.flush()
-        self.sh_script_path = os.path.join(self.install_lib, 'libervia_server', 'libervia.sh')
+        self.copy_data_files()
         self.custom_auto_options()
         self.custom_create_links()
 
+    def confirm(self, message):
+        """Ask the user for a confirmation"""
+        message += 'Proceed'
+        while True:
+            res = raw_input("%s (y/n)? " % message)
+            if res not in ['y', 'Y', 'n', 'N']:
+                print "Your response ('%s') was not one of the expected responses: y, n" % res
+                message = 'Proceed'
+                continue
+            if res in ('y', 'Y'):
+                return True
+            return False
 
-def preinstall_check():
+    def clean(self, message=None, to_remove=None):
+        """Clean previous installation directories
+
+        @param message (str): to use a non-default confirmation message
+        @param to_remove (str): extra files/directories to remove
+        """
+        if message is None:
+            message = "Cleaning previous installation directories"
+        if to_remove is None:
+            to_remove = []
+        to_remove.extend([os.path.join(self.install_lib, NAME),
+                          self.install_data_dir,
+                          os.path.join(self.install_data, 'share', 'doc', NAME),
+                          os.path.join(self.install_lib, "%s-py%s.egg-info" % (self.config_vars['dist_fullname'], self.config_vars['py_version_short'])),
+                          ])
+        for source, dest in self.sh_script_links:
+            dest = os.path.join(self.install_scripts, dest)
+            if os.path.islink(dest):
+                to_remove.append(dest)
+        plugin_file = os.path.join(self.install_lib, 'twisted', 'plugins', NAME)
+        if os.path.isfile(plugin_file):
+            to_remove.append(plugin_file)
+
+        message = "%s:\n%s\n" % (message, "\n".join(["    %s" % path for path in to_remove]))
+        if not self.confirm(message):
+            return
+        sys.stdout.write('cleaning previous installation directories...\n')
+        sys.stdout.flush()
+        for path in to_remove:
+            if os.path.isdir(path):
+                shutil.rmtree(path, ignore_errors=True)
+            else:
+                os.remove(path)
+
+    def purge(self):
+        """Clean building and previous installation directories"""
+        message = "Cleaning building and previous installation directories"
+        to_remove = [os.path.join(os.getcwd(), 'build'), self.pyjamas_output_dir]
+        self.clean(message, to_remove)
+
+
+def preinstall_check(install_opt):
     """Check presence of problematic dependencies, and try to install them with package manager
     This ugly stuff is necessary as distributions are not installed correctly with setuptools/distribute
     Hope to remove this at some point"""
@@ -138,10 +228,18 @@
 
     # which modules are not installed ?
     modules_toinstall = [mod for mod in modules_tocheck if not module_installed(mod)]
+    """# is mercurial available ?
+    hg_installed = subprocess.call('which hg', stdout=open('/dev/null', 'w'), shell=True) == 0
+    if not hg_installed:
+        modules_toinstall.append('mercurial')"""  # hg can be installed from pypi
 
     if modules_toinstall:
-        # are we on a distribution using apt ?
-        apt_path = subprocess.Popen('which apt-get', stdout=subprocess.PIPE, shell=True).communicate()[0][:-1]
+        if AUTO_DEB_OPT in install_opt: # auto debian installation is requested
+            # are we on a distribution using apt ?
+            apt_path = subprocess.Popen('which apt-get', stdout=subprocess.PIPE, shell=True).communicate()[0][:-1]
+        else:
+            apt_path = None
+
         not_installed = set()
         if apt_path:
             # we have apt, we'll try to use it
@@ -164,9 +262,9 @@
 
 if sys.argv[1].lower() in ['egg_info', 'install']:
     # we only check dependencies if egg_info or install is used
-    install_opt = os.environ.get("LIBERVIA_INSTALL", "")
-    if not "nopreinstall" in install_opt:  # user can force preinstall skipping
-        preinstall_check()
+    install_opt = os.environ.get(ENV_LIBERVIA_INSTALL, "").split()
+    if not NO_PREINSTALL_OPT in install_opt:  # user can force preinstall skipping
+        preinstall_check(install_opt)
 
 setup(name=NAME,
       version='0.4.0',
@@ -181,14 +279,15 @@
                    'License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)',
                    'Operating System :: POSIX :: Linux',
                    'Topic :: Communications :: Chat'],
-      package_dir={'libervia': '.', 'libervia_server': 'libervia_server'},
-      packages=['libervia', 'libervia.output', 'libervia_server', 'twisted.plugins'],
-      package_data={'libervia': ['libervia.py'], 'libervia.output': ['**/*.*'], 'libervia_server': ['libervia.sh']},
-      data_files=[('share/doc/%s' % NAME, ['COPYING', 'README'])],
+      package_dir={'libervia': 'src', 'twisted.plugins': 'src/twisted/plugins'},
+      packages=['libervia', 'libervia.common', 'libervia.server', 'twisted.plugins'],
+      package_data={'libervia': ['libervia.sh']},
+      data_files=[(os.path.join('share', 'doc', NAME), ['COPYING', 'README', 'INSTALL']),
+                  (os.path.join('share', NAME, C.SERVER_CSS_DIR), [os.path.join(C.SERVER_CSS_DIR, filename) for filename in os.listdir(C.SERVER_CSS_DIR)]),
+                  ],
       scripts=[],
       zip_safe=False,
       dependency_links=['http://www.blarg.net/%7Esteveha/pyfeed-0.7.4.tar.gz', 'http://www.blarg.net/%7Esteveha/xe-0.7.4.tar.gz'],
       install_requires=['sat', 'twisted', 'pyfeed', 'xe', 'txJSON-RPC', 'zope.interface', 'pyopenssl'],
       cmdclass={'install': CustomInstall},
       )
-