Added support for --command argument
[zeroinstall/solver.git] / setup.py
blob044e573cd7db47be486fb40599861c34faec4f09
1 from distutils.core import setup
2 from distutils.util import convert_path
3 from distutils.core import Command
4 from distutils.command.build_py import build_py
5 from distutils.command.install import install
6 from distutils.command.install_lib import install_lib
7 from distutils.command.install_data import install_data
8 import os, subprocess, sys
9 import glob
10 import zeroinstall
12 class adjust_scripts_for_home(Command):
13 """setup.py install --home puts libraries in ~/lib/python, but Python doesn't look there.
14 If we're installing with --home, modify the scripts to add this to sys.path.
15 Don't do this otherwise; the system copy mustn't conflict with the copy in $HOME.
16 """
17 description = "(used internally when using --home)"
19 user_options = [
20 ('scripts-dir=', 'd', "directory to install scripts to"),
21 ('lib-dir=', 'd', "directory libraries install to"),
24 def initialize_options (self):
25 self.scripts_dir = None
26 self.lib_dir = None
28 def finalize_options (self):
29 self.set_undefined_options('install',
30 ('install_scripts', 'scripts_dir'),
31 ('install_lib', 'lib_dir'),
34 def run(self):
35 for script in self.distribution.scripts:
36 outfile = os.path.join(self.scripts_dir, os.path.basename(script))
38 stream = open(outfile)
39 code = stream.read()
40 stream.close()
42 code = code.replace('## PATH ##', '''
43 import os, sys
44 sys.path.insert(0, %s)''' % repr(self.lib_dir))
45 stream = open(outfile, 'w')
46 stream.write(code)
47 stream.close()
49 class build_with_data(build_py):
50 """Python < 2.4 doesn't support package_data_files, so add it manually."""
51 package_data_files = []
52 def run(self):
53 # Copy .py files and build, as usual
54 build_py.run(self)
55 # Copy data files
56 for data_file in self.package_data_files:
57 outfile = os.path.join(self.build_lib, data_file)
58 self.copy_file(data_file, outfile, preserve_mode=0)
59 executable = (os.stat(data_file).st_mode & 0111) != 0
60 if executable:
61 os.chmod(outfile, os.stat(outfile).st_mode | 0111)
63 class install_lib_exec(install_lib):
64 def run(self):
65 install_lib.run(self) # super.run()
67 class install_data_locale(install_data):
68 def run(self):
69 self.data_files.extend(self._compile_po_files())
70 install_data.run(self) # super.run()
72 def _compile_po_files(self):
73 i18nfiles = []
74 mo_files = glob.glob("share/locale/*/LC_MESSAGES/zero-install.mo")
75 assert mo_files
76 for mo in mo_files:
77 dest = os.path.dirname(mo)
78 i18nfiles.append((dest, [mo]))
79 return i18nfiles
81 # distutils doesn't seem to have any support for adding configuration files.
82 # Unfortunately, the freedesktop.org menu spec strangely defines part of the
83 # menu definitions as configuration.
84 class my_install(install):
85 def finalize_options(self):
86 install.finalize_options(self) # super.finalize_options()
87 if self.home:
88 self.__config_dir = os.path.join(self.home, '.config')
89 elif self.prefix == '/usr':
90 self.__config_dir = os.path.join(self.root or '/', 'etc/xdg')
91 else:
92 self.__config_dir = os.path.join(self.root or '/', self.prefix[1:], 'etc/xdg')
94 def run(self):
95 install.run(self) # super.run()
96 menus_dir = os.path.join(self.__config_dir, 'menus/applications-merged')
97 self.mkpath(menus_dir)
98 menu = convert_path('share/applications/zeroinstall.menu')
99 self.copy_file(menu, menus_dir)
101 setup(name="zeroinstall-solver",
102 version=zeroinstall.version,
103 description="The Zero Install Solver (0solve)",
104 author="Thomas Leonard",
105 author_email="zero-install-devel@lists.sourceforge.net",
106 url="http://0install.net",
107 scripts=['0solve'],
108 data_files = [],
109 license='LGPL',
110 cmdclass={
111 'build_py': build_with_data,
112 'install_lib': install_lib_exec,
113 'install_data': install_data_locale,
114 'adjust_scripts_for_home': adjust_scripts_for_home,
115 'install': my_install,
117 long_description="""\
118 A running process is created by combining many different libraries (and other
119 components). In the Zero Install world, we have all versions of each library
120 available at all times. The problem then is how to choose which versions to
121 use.
123 The injector solves this problem by selecting components to meet a program's
124 requirements, according to a policy you give it. The injector finds out which
125 versions are available, and downloads and runs the ones you choose.""",
126 packages=["zeroinstall", "zeroinstall.support", "zeroinstall.zerostore", "zeroinstall.injector"])