~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/setup/GSetup.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# GNU Enterprise Common Library - Installation Helper For Packages
 
2
#
 
3
# This file is part of GNU Enterprise.
 
4
#
 
5
# GNU Enterprise is free software; you can redistribute it
 
6
# and/or modify it under the terms of the GNU General Public
 
7
# License as published by the Free Software Foundation; either
 
8
# version 2, or (at your option) any later version.
 
9
#
 
10
# GNU Enterprise is distributed in the hope that it will be
 
11
# useful, but WITHOUT ANY WARRANTY; without even the implied
 
12
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
13
# PURPOSE. See the GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public
 
16
# License along with program; see the file COPYING. If not,
 
17
# write to the Free Software Foundation, Inc., 59 Temple Place
 
18
# - Suite 330, Boston, MA 02111-1307, USA.
 
19
#
 
20
# Copyright 2001-2005 Free Software Foundation
 
21
#
 
22
# $Id: GSetup.py,v 1.4 2003/10/06 18:33:16 reinhard Exp $
 
23
 
 
24
import sys
 
25
import string
 
26
import os
 
27
from distutils.core import setup
 
28
import distutils.command.sdist
 
29
import distutils.command.build
 
30
import distutils.command.install
 
31
import gnue.paths
 
32
from gnue.common.setup import ChangeLog
 
33
 
 
34
# -----------------------------------------------------------------------------
 
35
# Check Python version
 
36
# -----------------------------------------------------------------------------
 
37
 
 
38
try:
 
39
  if sys.hexversion < 0x02000000:
 
40
    raise AttributeError
 
41
except AttributeError:
 
42
  print "-" * 70
 
43
  print """
 
44
  You are running Python %s.
 
45
 
 
46
  GNU Enterprise requires at least Python 2.0 (recommended: 2.1+).
 
47
  If you have a later version installed, you should run setup.py
 
48
  against that version. For example, if you have Python 2.2
 
49
  installed, you may need to run:
 
50
 
 
51
       python2.2 setup.py
 
52
""" % string.split(sys.version)[0]
 
53
  print "-" * 70
 
54
  sys.exit (1)
 
55
 
 
56
# -----------------------------------------------------------------------------
 
57
# Global GSetup instance
 
58
# -----------------------------------------------------------------------------
 
59
 
 
60
_setup = None
 
61
 
 
62
# =============================================================================
 
63
# sdist: build files to be distributed first
 
64
# =============================================================================
 
65
 
 
66
class sdist (distutils.command.sdist.sdist):
 
67
 
 
68
  def run (self):
 
69
    global _setup
 
70
 
 
71
    _setup.do_build_files ('sdist')
 
72
    distutils.command.sdist.sdist.run (self)
 
73
 
 
74
# =============================================================================
 
75
# build: if done from SVN, build files to be installed first
 
76
# =============================================================================
 
77
 
 
78
class build (distutils.command.build.build):
 
79
 
 
80
  def run (self):
 
81
    global _setup
 
82
 
 
83
    if not os.path.isfile ("PKG-INFO"):         # downloaded from SVN?
 
84
      _setup.do_build_files ('build')
 
85
    distutils.command.build.build.run (self)
 
86
 
 
87
# =============================================================================
 
88
# install: Some user_options are no longer allowed
 
89
# =============================================================================
 
90
 
 
91
class install (distutils.command.install.install):
 
92
 
 
93
  user_options = distutils.command.install.install.user_options
 
94
 
 
95
  allowed_options = ["root=", "compile", "no-compile", "optimize=", "force",
 
96
                     "skip-build", "record="]
 
97
 
 
98
  user_options = [opt for opt in user_options if opt [0] in allowed_options]
 
99
 
 
100
  # ---------------------------------------------------------------------------
 
101
  # Finalize options - set all install targets
 
102
  # ---------------------------------------------------------------------------
 
103
 
 
104
  def finalize_options (self):
 
105
    self.install_lib     = gnue.paths.lib
 
106
    self.install_scripts = gnue.paths.scripts
 
107
    self.install_data    = gnue.paths.data
 
108
    self.install_config  = gnue.paths.config
 
109
 
 
110
    distutils.command.install.install.finalize_options (self)
 
111
 
 
112
  # ---------------------------------------------------------------------------
 
113
  # install.run: generate and install path dependent files afterwards
 
114
  # ---------------------------------------------------------------------------
 
115
 
 
116
  def run (self):
 
117
    global _setup
 
118
 
 
119
    _setup.check_dependencies ()
 
120
 
 
121
    self.__outputs = []
 
122
 
 
123
    # install translations
 
124
    if os.path.isdir ('po'):
 
125
 
 
126
      # find out domain
 
127
      domain = None
 
128
      for f in os.listdir ('po'):
 
129
        if f [-4:] == '.pot':
 
130
          domain = f [:-4]
 
131
 
 
132
      if not domain:
 
133
        print "warning: cannot determine domain, not installing translations"
 
134
      else:
 
135
        # copy files
 
136
        for f in os.listdir ('po'):
 
137
          if f [-4:] == '.gmo':
 
138
            src = os.path.join ('po', f)
 
139
            dst = os.path.join (self.install_data, 'share', 'locale', f [:-4],
 
140
                                'LC_MESSAGES')
 
141
            self.mkpath (dst)
 
142
            dst = os.path.join (dst, domain + '.mo')
 
143
            self.copy_file (src, dst)
 
144
            self.__outputs.append (dst)
 
145
 
 
146
    distutils.command.install.install.run (self)
 
147
 
 
148
  # ---------------------------------------------------------------------------
 
149
  # install.get_outputs: list all installed files
 
150
  # ---------------------------------------------------------------------------
 
151
 
 
152
  def get_outputs (self):
 
153
    return distutils.command.install.install.get_outputs (self) \
 
154
           + self.__outputs
 
155
 
 
156
# =============================================================================
 
157
# GSetup: Basic class for setup scripts of GNUe packages
 
158
# =============================================================================
 
159
 
 
160
class GSetup:
 
161
    
 
162
  # ---------------------------------------------------------------------------
 
163
  # Abstract methods: setup.py scripts generally override these
 
164
  # ---------------------------------------------------------------------------
 
165
 
 
166
  def set_params (self, params):
 
167
    pass
 
168
  
 
169
  def build_files (self, action):
 
170
    pass
 
171
 
 
172
  def check_dependencies (self):
 
173
    pass
 
174
 
 
175
  # ---------------------------------------------------------------------------
 
176
  # Build files if called from SVN
 
177
  # ---------------------------------------------------------------------------
 
178
 
 
179
  def do_build_files (self, action):
 
180
 
 
181
    if os.name == 'posix':
 
182
 
 
183
      # First check if we have everything installed we need to build the
 
184
      # distribution
 
185
 
 
186
      if os.path.isdir ('po'):
 
187
        # xgettext
 
188
        if os.system ("pygettext --version > /dev/null") != 0:
 
189
          print "Could not find 'pygettext'. Strange."
 
190
          print "It should be included in the Python distribution."
 
191
          sys.exit (1)
 
192
 
 
193
        # msgmerge
 
194
        if os.system ("msgmerge --version > /dev/null") != 0:
 
195
          print "Could not find 'msgmerge'. Please install Gettext."
 
196
          sys.exit (1)
 
197
 
 
198
        # msgfmt
 
199
        if os.system ("msgfmt --version > /dev/null") != 0:
 
200
          print "Could not find 'msgfmt'. Please install Gettext."
 
201
          sys.exit (1)
 
202
 
 
203
      # -----------------------------------------------------------------------
 
204
 
 
205
      if action == 'sdist':
 
206
        # build ChangeLog file
 
207
        print 'building ChangeLog'
 
208
        ChangeLog.build ()
 
209
 
 
210
      # build translations
 
211
      if os.path.isdir ('po'):
 
212
        print "building translations"
 
213
        if os.system ("cd po && make gmo") != 0:
 
214
          sys.exit (1)
 
215
 
 
216
    else:
 
217
      # on non posix systems just run msgfmt on existing .po files
 
218
      if os.path.isdir ('po'):
 
219
        # msgfmt.py
 
220
        argv0_path = os.path.dirname(os.path.abspath(sys.executable))
 
221
        sys.path.append(argv0_path + "\\tools\\i18n")
 
222
      
 
223
        msgfmtOK = 0
 
224
        try:
 
225
          import msgfmt
 
226
          msgfmtOK = 1
 
227
        except:
 
228
          pass
 
229
      
 
230
        if msgfmtOK == 1:
 
231
          # pygettext.py exist in Python, but no msgmerge, so
 
232
          # just create a placeholder...
 
233
          potfile = open('po/'+ self.package +'.pot', 'w')
 
234
          potfile.write("#placeholder")
 
235
          potfile.close()
 
236
 
 
237
          # build translations
 
238
          print "building translations"
 
239
          for f in os.listdir('po'):
 
240
            if f[-3:] == '.po':
 
241
              print f
 
242
              msgfmt.make ('po/'+f, 'po/'+f[:-3]+'.gmo')
 
243
              msgfmt.MESSAGES = {}
 
244
 
 
245
    # -------------------------------------------------------------------------
 
246
 
 
247
    # do package specific stuff
 
248
    self.build_files (action)
 
249
 
 
250
  # ---------------------------------------------------------------------------
 
251
  # Helper methods for descendants
 
252
  # ---------------------------------------------------------------------------
 
253
 
 
254
  def allfiles (self, directory):
 
255
    directory = os.path.normpath (directory)
 
256
    exclude = [".svn", "CVS", "README.cvs", ".cvsignore", "Makefile"]
 
257
    return [directory + "/" + file for file in os.listdir (directory) \
 
258
            if not file in exclude and
 
259
               not os.path.isdir (os.path.join (directory, file))]
 
260
 
 
261
  # ---------------------------------------------------------------------------
 
262
  # Get all packages in a directory
 
263
  # ---------------------------------------------------------------------------
 
264
 
 
265
  def __get_packages (self, directory, package):
 
266
    content = os.listdir (directory)
 
267
    result = []
 
268
    if "__init__.py" in content:
 
269
      result = [package]
 
270
      for name in content:
 
271
        fullname = os.path.join (directory, name)
 
272
        if os.path.isdir (fullname):
 
273
          result = result + self.__get_packages (fullname, package + "." + name)
 
274
    return result
 
275
 
 
276
  # ---------------------------------------------------------------------------
 
277
  # Call the actual setup routine
 
278
  # ---------------------------------------------------------------------------
 
279
 
 
280
  def run (self):
 
281
    global _setup
 
282
 
 
283
    # set global variable
 
284
    _setup = self
 
285
 
 
286
    setup_params = {"cmdclass_sdist": sdist,
 
287
                    "cmdclass_build": build,
 
288
                    "cmdclass_install": install,
 
289
                    "suffix_var" : 'GNUE_VERSION_SUFFIX'
 
290
                   }
 
291
 
 
292
    _setup.set_params (setup_params)
 
293
    
 
294
    # handle version suffix
 
295
    try:
 
296
      suffix = os.environ[setup_params['suffix_var']]
 
297
    except KeyError:
 
298
      suffix = ""
 
299
 
 
300
 
 
301
    # find out all packages
 
302
    if not setup_params.has_key ("packages"):
 
303
      packages = []
 
304
      for module, directory in setup_params["package_dir"].items ():
 
305
        packages = packages + self.__get_packages (directory, module)
 
306
      setup_params ["packages"] = packages
 
307
 
 
308
    # remove data files that are not available
 
309
    for target, files in setup_params ["data_files"]:
 
310
      i = 0
 
311
      while i < len (files):
 
312
        file = files [i]
 
313
        if os.path.isfile (file):
 
314
          i += 1
 
315
        else:
 
316
          print "warning: did not find file %s" % file
 
317
          files.remove (file)
 
318
 
 
319
    # now call setup
 
320
    setup (name             = setup_params ["name"],
 
321
           version          = setup_params ["version"] + suffix,
 
322
           description      = setup_params ["description"],
 
323
           long_description = setup_params ["long_description"],
 
324
           author           = setup_params ["author"],
 
325
           author_email     = setup_params ["author_email"],
 
326
           url              = setup_params ["url"],
 
327
           license          = setup_params ["license"],
 
328
           packages         = setup_params ["packages"],
 
329
           package_dir      = setup_params ["package_dir"],
 
330
           scripts          = setup_params ["scripts"],
 
331
           data_files       = setup_params ["data_files"],
 
332
 
 
333
           # Override certain command classes with our own ones
 
334
           cmdclass = {"sdist":   setup_params["cmdclass_sdist"],
 
335
                       "build":   setup_params["cmdclass_build"],
 
336
                       "install": setup_params["cmdclass_install"]})