A4

Merge lp:~francesco-marella/a4/improve-setup into lp:a4

Proposed by Francesco Marella
Status: Merged
Merged at revision: 111
Proposed branch: lp:~francesco-marella/a4/improve-setup
Merge into: lp:a4
Diff against target: 223 lines (+197/-18)
1 file modified
setup.py (+197/-18)
To merge this branch: bzr merge lp:~francesco-marella/a4/improve-setup
Reviewer Review Type Date Requested Status
Andrea Gasparini Approve
Review via email: mp+50411@code.launchpad.net
To post a comment you must log in.
110. By Francesco Marella

setup.py: Fix the locale directory for the generated mo files

Thanks to Andrea Gasparini for reporting and testing.

111. By Francesco Marella

setup.py: fix all the pep8 warnings

Revision history for this message
Andrea Gasparini (gaspa) wrote :

just merged. I fixed a little pep8 error, the rest is really verbose but it's ok for now.

We should although keep in mind that:
* we should handle better the install path of the .po files.
* I just found this: http://www.glatzor.de/projects/python-distutils-extra
  perhaps we can use it to simplify the setup.py.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'setup.py' (properties changed: +x to -x)
2--- setup.py 2010-08-02 13:02:07 +0000
3+++ setup.py 2011-02-23 23:39:05 +0000
4@@ -1,22 +1,201 @@
5-#!/usr/bin/env python
6+#!/usr/bin/python
7+# -*- coding: utf-8 -*-
8
9 from distutils.core import setup
10+from distutils.dist import Distribution
11+from distutils.cmd import Command
12+from distutils.command.install_data import install_data
13+from distutils.command.build import build
14+from distutils.dep_util import newer
15+from distutils.log import warn, info, error
16+from distutils.errors import DistutilsFileError
17+import glob
18+import os
19+import sys
20+import subprocess
21+import platform
22+
23 from a4lib import __version__
24
25-setup(name='A4',
26- version=__version__,
27- description='A cool tool to create amazing presentations',
28- author='A4 Developers',
29- author_email='a4-dev@lists.launchpad.net',
30- url='https://launchpad.net/a4',
31- license='GNU GPL3',
32- packages=['a4lib'],
33- scripts=['a4'],
34- data_files=[('share/a4/data', ['data/window_main.glade']),
35- ('share/a4/tests/images',
36- ['tests/images/A4_nested_transforms.svg',
37- 'tests/images/rotations.svg', 'tests/images/roi.svg']),
38- ('share/man/man1', ['data/a4.1']),
39- ('share/applications', ['data/a4.desktop']),
40- ('share/pixmaps', ['data/a4.png'])]
41- )
42+PO_DIR = 'po'
43+MO_DIR = os.path.join('build', 'mo')
44+
45+
46+class CustomDist(Distribution):
47+
48+ global_options = Distribution.global_options + [('without-gettext',
49+ None, "Don't build/install gettext .mo files"),
50+ ('without-icon-cache', None,
51+ "Don't attempt to run gtk-update-icon-cache")]
52+
53+ def __init__(self, *args):
54+ self.without_gettext = False
55+ self.without_icon_cache = False
56+ Distribution.__init__(self, *args)
57+
58+
59+class BuildData(build):
60+
61+ def run(self):
62+ build.run(self)
63+
64+ if self.distribution.without_gettext:
65+ return
66+
67+ for po in glob.glob(os.path.join(PO_DIR, '*.po')):
68+ lang = os.path.basename(po[:-3])
69+ mo = os.path.join(MO_DIR, lang, 'LC_MESSAGES', 'a4.mo')
70+
71+ directory = os.path.dirname(mo)
72+ if not os.path.exists(directory):
73+ info('creating %s' % directory)
74+ os.makedirs(directory)
75+
76+ if newer(po, mo):
77+ info('compiling %s -> %s' % (po, mo))
78+ try:
79+ rc = subprocess.call(['msgfmt', '-o', mo, po])
80+ if rc != 0:
81+ raise Warning('msgfmt returned %d' % rc)
82+ except Exception, e:
83+ error('Building gettext files failed. \
84+Try setup.py --without-gettext [build|install]'
85+ )
86+ error('Error: %s' % str(e))
87+ sys.exit(1)
88+
89+ TOP_BUILDDIR = '.'
90+ INTLTOOL_MERGE = 'intltool-merge'
91+ desktop_in = 'data/a4.desktop.in'
92+ desktop_data = 'data/a4.desktop'
93+ os.system('C_ALL=C ' + INTLTOOL_MERGE + ' -d -u -c '
94+ + TOP_BUILDDIR + '/po/.intltool-merge-cache '
95+ + TOP_BUILDDIR + '/po ' + desktop_in + ' '
96+ + desktop_data)
97+
98+
99+class Uninstall(Command):
100+
101+ description = 'Attempt an uninstall from an install --record file'
102+
103+ user_options = [('manifest=', None, 'Installation record filename')]
104+
105+ def initialize_options(self):
106+ self.manifest = None
107+
108+ def finalize_options(self):
109+ pass
110+
111+ def get_command_name(self):
112+ return 'uninstall'
113+
114+ def run(self):
115+ f = None
116+ self.ensure_filename('manifest')
117+ try:
118+ try:
119+ if not self.manifest:
120+ raise DistutilsFileError('Pass manifest with \
121+--manifest=file')
122+ f = open(self.manifest)
123+ files = [file.strip() for file in f]
124+ except IOError, e:
125+ raise DistutilsFileError('unable to open install manifest: %s',
126+ str(e))
127+ finally:
128+ if f:
129+ f.close()
130+
131+ for file in files:
132+ if os.path.isfile(file) or os.path.islink(file):
133+ info('removing %s' % repr(file))
134+ if not self.dry_run:
135+ try:
136+ os.unlink(file)
137+ except OSError, e:
138+ warn('could not delete: %s' % repr(file))
139+ elif not os.path.isdir(file):
140+ info('skipping %s' % repr(file))
141+
142+ dirs = set()
143+ for file in reversed(sorted(files)):
144+ dir = os.path.dirname(file)
145+ if dir not in dirs and os.path.isdir(dir) \
146+ and len(os.listdir(dir)) == 0:
147+ dirs.add(dir)
148+
149+ # Only nuke empty Python library directories, else we could destroy
150+ # e.g. locale directories we're the only app with a .mo installed for
151+
152+ if dir.find('site-packages/') > 0:
153+ info('removing %s' % repr(dir))
154+ if not self.dry_run:
155+ try:
156+ os.rmdir(dir)
157+ except OSError, e:
158+ warn('could not remove directory: %s'
159+ % str(e))
160+ else:
161+ info('skipping empty directory %s' % repr(dir))
162+
163+
164+class InstallData(install_data):
165+
166+ def run(self):
167+ self.data_files.extend(self._find_mo_files())
168+ install_data.run(self)
169+ if not self.distribution.without_icon_cache:
170+ self._update_icon_cache()
171+
172+ # We should do this on uninstall too
173+
174+ def _update_icon_cache(self):
175+ info('running gtk-update-icon-cache')
176+ try:
177+ subprocess.call(['gtk-update-icon-cache', '-q', '-f', '-t',
178+ os.path.join(self.install_dir,
179+ 'share/icons/hicolor')])
180+ except Exception, e:
181+ warn('updating the GTK icon cache failed: %s' % str(e))
182+
183+ def _find_mo_files(self):
184+ data_files = []
185+
186+ if not self.distribution.without_gettext:
187+ for mo in glob.glob(os.path.join(MO_DIR, '*', 'LC_MESSAGES',
188+ 'a4.mo')):
189+ lang = \
190+ os.path.basename(os.path.dirname(os.path.dirname(mo)))
191+ dest = os.path.join('share', 'locale', lang,
192+ 'LC_MESSAGES')
193+ data_files.append((dest, [mo]))
194+
195+ return data_files
196+
197+
198+if platform.system() == 'FreeBSD':
199+ man_dir = 'man'
200+else:
201+ man_dir = 'share/man'
202+
203+setup(
204+ name='A4',
205+ version=__version__,
206+ description='A cool tool to create amazing presentations',
207+ author='A4 Developers',
208+ author_email='a4-dev@lists.launchpad.net',
209+ url='https://launchpad.net/a4',
210+ license='GNU GPL3',
211+ packages=['a4lib'],
212+ scripts=['a4'],
213+ data_files=[('share/a4/data', ['data/window_main.glade']),
214+ ('share/a4/tests/images',
215+ ['tests/images/A4_nested_transforms.svg',
216+ 'tests/images/rotations.svg', 'tests/images/roi.svg']),
217+ (os.path.join(man_dir, 'man1'), ['data/a4.1']),
218+ ('share/applications', ['data/a4.desktop']),
219+ ('share/pixmaps', ['data/a4.png'])],
220+ cmdclass={'build': BuildData, 'install_data': InstallData,
221+ 'uninstall': Uninstall},
222+ distclass=CustomDist,
223+ )

Subscribers

People subscribed via source and target branches