comparison setup.py @ 360:9834136b15ed

added setup.py for the installation with setuptools
author souliane <souliane@mailoo.org>
date Tue, 18 Feb 2014 00:15:43 +0100
parents
children f2c380af7304
comparison
equal deleted inserted replaced
359:151bf1afd97e 360:9834136b15ed
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Libervia: a Salut à Toi frontend
5 # Copyright (C) 2011, 2012, 2013, 2014 Jérôme Poisson <goffi@goffi.org>
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 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 Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from distribute_setup import use_setuptools
21 use_setuptools()
22 from setuptools.command.install import install
23 from setuptools import setup
24 from distutils.file_util import copy_file
25 import os
26 import sys
27 import subprocess
28 from stat import ST_MODE
29
30 # seen here: http://stackoverflow.com/questions/7275295
31 try:
32 from setuptools.command import egg_info
33 egg_info.write_toplevel_names
34 except (ImportError, AttributeError):
35 pass
36 else:
37 def _top_level_package(name):
38 return name.split('.', 1)[0]
39
40 def _hacked_write_toplevel_names(cmd, basename, filename):
41 pkgs = dict.fromkeys(
42 [_top_level_package(k)
43 for k in cmd.distribution.iter_distribution_names()
44 if _top_level_package(k) != "twisted"
45 ]
46 )
47 cmd.write_file("top-level names", filename, '\n'.join(pkgs) + '\n')
48
49 egg_info.write_toplevel_names = _hacked_write_toplevel_names
50
51
52 NAME = 'libervia'
53 LAUNCH_DAEMON_COMMAND = 'libervia'
54
55
56 class MercurialException(Exception):
57 pass
58
59
60 def module_installed(module_name):
61 """Try to import module_name, and return False if it failed
62 @param module_name: name of the module to test
63 @return: True if successful"""
64 try:
65 __import__(module_name)
66 except ImportError:
67 return False
68 return True
69
70
71 class CustomInstall(install):
72
73 def custom_auto_options(self):
74 """Change options for twistd in the shell script
75 Mainly change the paths"""
76 sh_buffer = ""
77 run_dir = os.path.dirname(self.sh_script_path)
78 with open(self.sh_script_path, 'r') as sh_file:
79 for ori_line in sh_file:
80 if ori_line.startswith('DAEMON='):
81 dest_line = 'DAEMON=""\n' # we want to launch sat as a daemon
82 elif ori_line.startswith('TAP_PATH='):
83 dest_line = 'TAP_PATH="%s/"\n' % run_dir
84 elif ori_line.startswith('PYTHON='):
85 dest_line = 'PYTHON="%s"\n' % sys.executable
86 else:
87 dest_line = ori_line
88 sh_buffer += dest_line
89
90 with open(self.sh_script_path, 'w') as sh_file:
91 sh_file.write(sh_buffer)
92
93 def custom_create_links(self):
94 """Create symbolic links to executables"""
95 # the script which launch the daemon
96 links = [(self.sh_script_path, LAUNCH_DAEMON_COMMAND)]
97 for source, dest in links:
98 dest_name, copied = copy_file(source, os.path.join(self.install_scripts, dest), link='sym')
99 assert (copied)
100 # we change the perm in the same way as in the original install_scripts
101 mode = ((os.stat(dest_name)[ST_MODE]) | 0555) & 07777
102 os.chmod(dest_name, mode)
103
104 def pyjs_build(self):
105 return subprocess.call('pyjsbuild libervia --no-compile-inplace -m -I %s' % self.install_lib, shell=True)
106
107 def run(self):
108 sys.stdout.write('running pre installation stuff\n')
109 sys.stdout.flush()
110 build_result = self.pyjs_build()
111 if build_result == 127: # TODO: remove magic string
112 print "pyjsbuild is not installed or not accessible from the PATH of user '%s'" % os.getenv('USERNAME')
113 return
114 if build_result != 0:
115 print "pyjsbuild failed to build libervia"
116 return
117 install.run(self)
118 sys.stdout.write('running post installation stuff\n')
119 sys.stdout.flush()
120 self.sh_script_path = os.path.join(self.install_lib, 'libervia_server', 'libervia.sh')
121 self.custom_auto_options()
122 self.custom_create_links()
123
124
125 def preinstall_check():
126 """Check presence of problematic dependencies, and try to install them with package manager
127 This ugly stuff is necessary as distributions are not installed correctly with setuptools/distribute
128 Hope to remove this at some point"""
129
130 modules_tocheck = [] # if empty this method is dummy
131
132 package = {'twisted': 'python-twisted-core',
133 'twisted.words': 'python-twisted-words',
134 'twisted.web': 'python-twisted-web',
135 'mercurial': 'mercurial'} # this dict map dependencies to packages names for debian distributions
136
137 sys.stdout.write("Running pre-installation dependencies check\n")
138
139 # which modules are not installed ?
140 modules_toinstall = [mod for mod in modules_tocheck if not module_installed(mod)]
141
142 if modules_toinstall:
143 # are we on a distribution using apt ?
144 apt_path = subprocess.Popen('which apt-get', stdout=subprocess.PIPE, shell=True).communicate()[0][:-1]
145 not_installed = set()
146 if apt_path:
147 # we have apt, we'll try to use it
148 for module_name in modules_toinstall:
149 package_name = package[module_name]
150 sys.stdout.write("Installing %s\n" % package_name)
151 success = subprocess.call('%s -qy install %s' % (apt_path, package_name), shell=True) == 0
152 if not success:
153 not_installed.add(module_name)
154 else:
155 not_installed = set(modules_toinstall)
156
157 if not_installed:
158 # some packages can't be automatically installed, we print their name for manual installation
159 sys.stdout.write("You should install the following dependencies with your distribution recommanded tool before installing %s:\n" % NAME)
160 for module_name in not_installed:
161 sys.stdout.write("- %s (Debian name: %s)\n" % (module_name, package[module_name]))
162 sys.exit(2)
163
164
165 if sys.argv[1].lower() in ['egg_info', 'install']:
166 # we only check dependencies if egg_info or install is used
167 install_opt = os.environ.get("LIBERVIA_INSTALL", "")
168 if not "nopreinstall" in install_opt: # user can force preinstall skipping
169 preinstall_check()
170
171 setup(name=NAME,
172 version='0.3.0',
173 description=u'Web frontend for Salut à Toi',
174 long_description=u'Libervia is a web frontend for Salut à Toi (SàT), a multi-frontends and multi-purposes XMPP client.',
175 author='Goffi (Jérôme Poisson)',
176 author_email='goffi@goffi.org',
177 url='http://sat.goffi.org',
178 classifiers=['Development Status :: 3 - Alpha',
179 'Environment :: Web Environment',
180 'Framework :: Twisted',
181 'Framework :: Pyjamas',
182 'License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)',
183 'Operating System :: POSIX :: Linux',
184 'Topic :: Communications :: Chat'],
185 package_dir={'libervia': '.', 'libervia_server': 'libervia_server'},
186 packages=['libervia', 'libervia.output', 'libervia_server', 'twisted.plugins'],
187 package_data={'libervia': ['libervia.py'], 'libervia.output': ['**/*.*'], 'libervia_server': ['libervia.sh']},
188 data_files=[('share/doc/%s' % NAME, ['COPYING', 'README'])],
189 scripts=[],
190 zip_safe=False,
191 dependency_links=['http://www.blarg.net/%7Esteveha/pyfeed-0.7.4.tar.gz', 'http://www.blarg.net/%7Esteveha/xe-0.7.4.tar.gz'],
192 install_requires=['sat', 'twisted', 'pyfeed', 'xe', 'txjsonrpc', 'zope'],
193 cmdclass={'install': CustomInstall},
194 )
195