comparison setup.py @ 1124:28e3eb3bb217

files reorganisation and installation rework: - files have been reorganised to follow other SàT projects and usual Python organisation (no more "/src" directory) - VERSION file is now used, as for other SàT projects - replace the overcomplicated setup.py be a more sane one. Pyjamas part is not compiled anymore by setup.py, it must be done separatly - removed check for data_dir if it's empty - installation tested working in virtual env - libervia launching script is now in bin/libervia
author Goffi <goffi@goffi.org>
date Sat, 25 Aug 2018 17:59:48 +0200
parents 6d6c0694f72a
children b251c8bb6776
comparison
equal deleted inserted replaced
1123:63a4b8fe9782 1124:28e3eb3bb217
1 #!/usr/bin/env python2 1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # Libervia: a Salut à Toi frontend 4 # Libervia: a Salut à Toi frontend
5 # Copyright (C) 2011-2016 Jérôme Poisson (goffi@goffi.org) 5 # Copyright (C) 2011-2018 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org) 6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
7 7
8 # This program is free software: you can redistribute it and/or modify 8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by 9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or 10 # the Free Software Foundation, either version 3 of the License, or
16 # GNU Affero General Public License for more details. 16 # GNU Affero General Public License for more details.
17 17
18 # You should have received a copy of the GNU Affero General Public License 18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. 19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 20
21 from ez_setup import use_setuptools
22 use_setuptools()
23 from setuptools.command.install import install
24 from setuptools import setup 21 from setuptools import setup
25 from distutils.file_util import copy_file
26 import os 22 import os
27 import sys
28 import subprocess
29 from stat import ST_MODE
30 import shutil
31 from src.server.constants import Const as C
32 import tempfile
33 23
34 # seen here: http://stackoverflow.com/questions/7275295 24 NAME = "libervia"
35 try:
36 from setuptools.command import egg_info
37 egg_info.write_toplevel_names
38 except (ImportError, AttributeError):
39 pass
40 else:
41 def _top_level_package(name):
42 return name.split('.', 1)[0]
43 25
44 def _hacked_write_toplevel_names(cmd, basename, filename): 26 install_requires = [
45 pkgs = dict.fromkeys( 27 "sat",
46 [_top_level_package(k) 28 "sat-templates",
47 for k in cmd.distribution.iter_distribution_names() 29 "twisted",
48 if _top_level_package(k) != "twisted" 30 "txJSON-RPC==0.3.1",
49 ] 31 "zope.interface",
50 ) 32 "pyopenssl",
51 cmd.write_file("top-level names", filename, '\n'.join(pkgs) + '\n') 33 "jinja2>=2.9",
34 "shortuuid",
35 "autobahn",
36 ]
37 long_description = u"""\
38 Libervia is a web frontend for Salut à Toi (SàT), a multi-frontends and multi-purposes XMPP client.
39 It features chat, blog, forums, events, tickets, merge requests, file sharing, photo albums, etc.
40 It is also a decentralized, XMPP based web framework.
41 """
52 42
53 egg_info.write_toplevel_names = _hacked_write_toplevel_names 43 with open(os.path.join(NAME, "VERSION")) as v:
44 VERSION = v.read().strip()
45 is_dev_version = VERSION.endswith("D")
54 46
55 47
56 NAME = 'libervia' 48 def libervia_dev_version():
57 LAUNCH_DAEMON_COMMAND = 'libervia' 49 """Use mercurial data to compute version"""
58 50
59 ENV_LIBERVIA_INSTALL = "LIBERVIA_INSTALL" # environment variable to customise installation 51 def version_scheme(version):
60 JS_DEBUG = "jsdebug" # use debug mode with pyjsbuild 52 return VERSION.replace("D", ".dev0")
61 NO_PREINSTALL_OPT = 'nopreinstall' # skip all preinstallation checks 53
62 AUTO_DEB_OPT = 'autodeb' # automaticaly install debs 54 def local_scheme(version):
63 CLEAN_OPT = 'clean' # remove previous installation directories 55 return "+{rev}.{distance}".format(rev=version.node[1:], distance=version.distance)
64 PURGE_OPT = 'purge' # remove building and previous installation directories 56
57 return {"version_scheme": version_scheme, "local_scheme": local_scheme}
65 58
66 59
67 class MercurialException(Exception): 60 setup(
68 pass 61 name=NAME,
69 62 version=VERSION,
70 63 description=u"Web frontend for Salut à Toi",
71 def module_installed(module_name): 64 long_description=long_description,
72 """Try to import module_name, and return False if it failed 65 author="Association « Salut à Toi »",
73 @param module_name: name of the module to test 66 author_email="contact@goffi.org",
74 @return: True if successful""" 67 url="https://www.salut-a-toi.org",
75 try: 68 classifiers=[
76 __import__(module_name) 69 "Development Status :: 3 - Alpha",
77 except ImportError: 70 "Environment :: Web Environment",
78 return False 71 "Framework :: Twisted",
79 return True 72 "License :: OSI Approved :: GNU Affero General Public License v3 "
80 73 "or later (AGPLv3+)",
81 74 "Operating System :: POSIX :: Linux",
82 class CustomInstall(install): 75 "Topic :: Communications :: Chat",
83 76 ],
84 def custom_auto_options(self): 77 packages=["libervia", "libervia.common", "libervia.server", "twisted.plugins"],
85 """Change options for twistd in the shell script 78 include_package_data=True,
86 Mainly change the paths""" 79 data_files=[(os.path.join("share", "doc", NAME), ["COPYING", "README", "INSTALL"])]
87 sh_buffer = "" 80 + [
88 with open(self.sh_script_path, 'r') as sh_file: 81 (os.path.join("share", NAME, root), [os.path.join(root, f) for f in files])
89 for ori_line in sh_file: 82 for root, dirs, files in os.walk(u"themes")
90 if ori_line.startswith('PLUGIN_OPTIONS='): 83 ],
91 dest_line = 'PLUGIN_OPTIONS="-d %s"\n' % self.install_data_dir 84 scripts=["bin/libervia"],
92 elif ori_line.startswith('PYTHON='): 85 zip_safe=False,
93 dest_line = 'PYTHON="%s"\n' % sys.executable 86 setup_requires=["setuptools_scm"] if is_dev_version else [],
94 else: 87 use_scm_version=libervia_dev_version if is_dev_version else False,
95 dest_line = ori_line 88 install_requires=install_requires,
96 sh_buffer += dest_line 89 package_data={"libervia": ["VERSION"]},
97 90 python_requires="~=2.7",
98 with open(self.sh_script_path, 'w') as sh_file: 91 )
99 sh_file.write(sh_buffer)
100
101 def custom_create_links(self):
102 """Create symbolic links to executables"""
103 # the script which launch the daemon
104 for source, dest in self.sh_script_links:
105 if os.path.islink(dest) and os.readlink(dest) != source:
106 os.remove(dest) # copy_file doesn't force the link update
107 dest_name, copied = copy_file(source, dest, link='sym')
108 assert (copied)
109 # we change the perm in the same way as in the original install_scripts
110 mode = ((os.stat(dest_name)[ST_MODE]) | 0555) & 07777
111 os.chmod(dest_name, mode)
112
113 def pyjs_build(self):
114 """Build the browser side JS files from Python source."""
115 cwd = os.getcwd()
116 os.chdir(os.path.join('src', 'browser'))
117 # we must have only certain package in the path, so we create a tmp dir to link only what we need
118 tmp_dir = tempfile.mkdtemp()
119 import sat, sat_frontends
120 os.symlink(os.path.dirname(sat.__file__), os.path.join(tmp_dir,"sat")) # FIXME: only work on unixes
121 os.symlink(os.path.dirname(sat_frontends.__file__), os.path.join(tmp_dir,"sat_frontends")) # FIXME: only work on unixes
122 libervia_files = os.path.abspath("../../src")
123 os.symlink(libervia_files, os.path.join(tmp_dir,"libervia")) # FIXME: only work on unixes
124 for module in ('libervia_main', 'libervia_test'):
125 build_args = ['pyjsbuild', module] + (['-d'] if JS_DEBUG in install_opt else []) + ['--no-compile-inplace', '-I', tmp_dir, '-o', self.pyjamas_output_dir]
126 result = subprocess.call(build_args)
127 if result != 0:
128 continue
129 shutil.rmtree(tmp_dir)
130 os.chdir(cwd)
131 return result
132
133 def copy_data_files(self):
134 # XXX: To copy the JS files couldn't be done with the data_files parameter
135 # of setuptools.setup because all the files to be copied must exist before
136 # the call. Also, we need the value of self.install_lib to build the JS
137 # files (it's not easily predictable as it may vary from one system to
138 # another), so we can't call pyjsbuild before setuptools.setup.
139
140 html = os.path.join(self.install_data_dir, C.HTML_DIR)
141 if os.path.isdir(html):
142 shutil.rmtree(html, ignore_errors=True)
143 shutil.copytree(self.pyjamas_output_dir, html)
144
145 def run(self):
146 self.sh_script_path = os.path.join(self.install_lib, NAME, 'libervia.sh')
147 self.sh_script_links = [(self.sh_script_path, os.path.join(self.install_scripts, LAUNCH_DAEMON_COMMAND))]
148 self.install_data_dir = os.path.join(self.install_data, 'share', NAME)
149 self.pyjamas_output_dir = os.path.join(os.getcwd(), 'html')
150 sys.stdout.write('running pre installation stuff\n')
151 sys.stdout.flush()
152 if PURGE_OPT in install_opt:
153 self.purge()
154 elif CLEAN_OPT in install_opt:
155 self.clean()
156 install.run(self)
157 sys.stdout.write('running post installation stuff\n')
158 sys.stdout.flush()
159 try:
160 build_result = self.pyjs_build() # build after libervia.common is accessible
161 except OSError as e:
162 print "can't run pyjsbuild, are you sure pyjamas is installed?\nexception: {}".format(e)
163 return
164 if build_result == 127: # TODO: remove magic string # FIXME: seems useless as an OSError is raised if pyjsbuild is not accessible
165 print "pyjsbuild is not installed or not accessible from the PATH of user '%s'" % os.getenv('USERNAME')
166 return
167 if build_result != 0:
168 print "pyjsbuild failed to build libervia"
169 return
170 self.copy_data_files()
171 self.custom_auto_options()
172 if not "arch" in install_opt:
173 self.custom_create_links()
174
175 def confirm(self, message):
176 """Ask the user for a confirmation"""
177 message += 'Proceed'
178 while True:
179 res = raw_input("%s (y/n)? " % message)
180 if res not in ['y', 'Y', 'n', 'N']:
181 print "Your response ('%s') was not one of the expected responses: y, n" % res
182 message = 'Proceed'
183 continue
184 if res in ('y', 'Y'):
185 return True
186 return False
187
188 def clean(self, message=None, to_remove=None):
189 """Clean previous installation directories
190
191 @param message (str): to use a non-default confirmation message
192 @param to_remove (str): extra files/directories to remove
193 """
194 if message is None:
195 message = "Cleaning previous installation directories"
196 if to_remove is None:
197 to_remove = []
198 for path in [os.path.join(self.install_lib, NAME),
199 self.install_data_dir,
200 os.path.join(self.install_data, 'share', 'doc', NAME),
201 os.path.join(self.install_lib, "%s.egg-info" % self.config_vars['dist_fullname']),
202 os.path.join(self.install_lib, "%s-py%s.egg-info" % (self.config_vars['dist_fullname'], self.config_vars['py_version_short'])),
203 ]:
204 if os.path.isdir(path):
205 to_remove.append(path)
206 for source, dest in self.sh_script_links:
207 if os.path.islink(dest):
208 to_remove.append(dest)
209 plugin_file = os.path.join(self.install_lib, 'twisted', 'plugins', NAME)
210 if os.path.isfile(plugin_file):
211 to_remove.append(plugin_file)
212
213 message = "%s:\n%s\n" % (message, "\n".join([" %s" % path for path in to_remove]))
214 if not self.confirm(message):
215 return
216 sys.stdout.write('cleaning previous installation directories...\n')
217 sys.stdout.flush()
218 for path in to_remove:
219 if os.path.isdir(path):
220 shutil.rmtree(path, ignore_errors=True)
221 else:
222 os.remove(path)
223
224 def purge(self):
225 """Clean building and previous installation directories"""
226 message = "Cleaning building and previous installation directories"
227 to_remove = [os.path.join(os.getcwd(), 'build'), self.pyjamas_output_dir]
228 self.clean(message, to_remove)
229
230
231 def preinstall_check(install_opt):
232 """Check presence of problematic dependencies, and try to install them with package manager
233 This ugly stuff is necessary as distributions are not installed correctly with setuptools/distribute
234 Hope to remove this at some point"""
235
236 modules_tocheck = [] # if empty this method is dummy
237
238 package = {'twisted': 'python-twisted-core',
239 'twisted.words': 'python-twisted-words',
240 'twisted.web': 'python-twisted-web',
241 'mercurial': 'mercurial'} # this dict map dependencies to packages names for debian distributions
242
243 sys.stdout.write("Running pre-installation dependencies check\n")
244
245 # which modules are not installed ?
246 modules_toinstall = [mod for mod in modules_tocheck if not module_installed(mod)]
247 """# is mercurial available ?
248 hg_installed = subprocess.call('which hg', stdout=open('/dev/null', 'w'), shell=True) == 0
249 if not hg_installed:
250 modules_toinstall.append('mercurial')""" # hg can be installed from pypi
251
252 if modules_toinstall:
253 if AUTO_DEB_OPT in install_opt: # auto debian installation is requested
254 # are we on a distribution using apt ?
255 apt_path = subprocess.Popen('which apt-get', stdout=subprocess.PIPE, shell=True).communicate()[0][:-1]
256 else:
257 apt_path = None
258
259 not_installed = set()
260 if apt_path:
261 # we have apt, we'll try to use it
262 for module_name in modules_toinstall:
263 package_name = package[module_name]
264 sys.stdout.write("Installing %s\n" % package_name)
265 success = subprocess.call('%s -qy install %s' % (apt_path, package_name), shell=True) == 0
266 if not success:
267 not_installed.add(module_name)
268 else:
269 not_installed = set(modules_toinstall)
270
271 if not_installed:
272 # some packages can't be automatically installed, we print their name for manual installation
273 sys.stdout.write("You should install the following dependencies with your distribution recommanded tool before installing %s:\n" % NAME)
274 for module_name in not_installed:
275 sys.stdout.write("- %s (Debian name: %s)\n" % (module_name, package[module_name]))
276 sys.exit(2)
277
278
279 if sys.argv[1].lower() in ['egg_info', 'install']:
280 # we only check dependencies if egg_info or install is used
281 install_opt = os.environ.get(ENV_LIBERVIA_INSTALL, "").split()
282 if not NO_PREINSTALL_OPT in install_opt: # user can force preinstall skipping
283 preinstall_check(install_opt)
284
285 setup(name=NAME,
286 version='0.7.0a2.post1',
287 description=u'Web frontend for Salut à Toi',
288 long_description=u'Libervia is a web frontend for Salut à Toi (SàT), a multi-frontends and multi-purposes XMPP client.',
289 author='Association « Salut à Toi »',
290 author_email='contact@goffi.org',
291 url='http://www.salut-a-toi.org',
292 classifiers=['Development Status :: 3 - Alpha',
293 'Environment :: Web Environment',
294 'Framework :: Twisted',
295 'License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)',
296 'Operating System :: POSIX :: Linux',
297 'Topic :: Communications :: Chat'],
298 package_dir={'libervia': 'src', 'twisted.plugins': 'src/twisted/plugins'},
299 packages=['libervia', 'libervia.common', 'libervia.server', 'twisted.plugins'],
300 package_data={'libervia': ['libervia.sh']},
301 include_package_data=True,
302 data_files=[(os.path.join('share', 'doc', NAME), ['COPYING', 'README', 'INSTALL']), ] +
303 [(os.path.join('share', NAME, root),
304 [os.path.join(root, f) for f in files])
305 for root, dirs, files in os.walk(C.THEMES_DIR)],
306 scripts=[],
307 zip_safe=False,
308 install_requires=['sat', 'twisted', 'txJSON-RPC==0.3.1', 'zope.interface', 'pyopenssl', 'jinja2>=2.9', 'shortuuid', 'autobahn'],
309 cmdclass={'install': CustomInstall},
310 )