File: setup.py

package info (click to toggle)
lottanzb 0.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 6,132 kB
  • ctags: 1,207
  • sloc: xml: 5,872; python: 5,749; makefile: 9; sh: 2
file content (403 lines) | stat: -rwxr-xr-x 15,274 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/env python

# Copyright (C) 2008-2010 LottaNZB Development Team
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

import os
import sys

from glob import glob
from stat import ST_MODE
from imp import find_module, load_module
from subprocess import call, Popen, PIPE
from os.path import (
    join, isfile, exists, isdir, dirname, basename, abspath, splitext)

from distutils import log
from distutils.dir_util import remove_tree
from distutils.errors import DistutilsFileError, DistutilsArgError
from distutils.command.install_lib import install_lib
from distutilsextra.auto import (
    setup, clean_build_tree, install_auto, sdist_auto, build_help_auto)

from lottanzb import __version__ as version

def remove(path, dry_run=False):
    """
    Remove a certain file or directory.
    
    Similar to the remove_tree function provided by the distutils module,
    it has an optional dry_run parameter. If set to true,
    no files are removed at all.
    """
    
    if not exists(path):
        log.debug("%s does not exist.", path)
        return
    
    if isfile(path):
        try:
            log.info("removing %s", path)
            
            if not dry_run:
                os.remove(path)
        except OSError:
            raise DistutilsFileError("Unable to remove %s.", path)
    elif isdir(path):
        remove_tree(path, dry_run=dry_run)

class CleanCommand(clean_build_tree):
    def run(self):
        clean_build_tree.run(self)
        
        # Remove pyc files.
        for root, dirs, files in os.walk("."):
            for a_file in files:
                if a_file.endswith(".pyc"):
                    self.remove(join(root, a_file))
        
        # The 'dist' directory contains a clean LottaNZB branch after running
        # 'util/release'. Running 'python setup.py install' afterwards will
        # fail because of that.
        self.remove("dist")
        self.remove("MANIFEST")
        self.remove(".xml2po.mo")
        
        # If the installation is interrupted in the middle (e. g. because of an
        # error) the following two temporary files created by distutilsextra
        # will still exist and cause an error in future installation attempts.
        self.remove("bin/lottanzb.py")
        self.remove("po/POTFILES.in")
    
    def remove(self, path):
        """
        Wraps the remove function, so that the dry_run parameter doesn't need
        to be set whenever it's called.
        """
        
        remove(path, self.dry_run)

class InstallWrapper(install_auto):
    """
    Doesn't contain much more than the complete_installation method, used to
    complete the GNOME integration or to reverse it, respectively.
    """
    
    DEFAULT_RECORD_FILE = "INSTALLED_FILES"
    
    def complete_installation(self):
        """
        Try to register LottaNZB as an application used to handle NZB files,
        make sure NZB files and the LottaNZB menu entry get a shiny icon
        and register the help content.
        """
        
        omf_dir = join(self.install_data, "share", "omf")
        mime_dir = join(self.install_data, "share", "mime")
        icon_dir = join(self.install_data, "share", "icons", "hicolor")
        
        def try_to_call(cmd):
            try:
                if not self.dry_run:
                    call(cmd)
            except OSError:
                log.warn("Could not call %s.", cmd)
        
        log.info("updating MIME types")
        try_to_call(["update-mime-database", mime_dir])
        
        log.info("updating desktop database")
        try_to_call(["update-desktop-database"])
        
        log.info("updating icon cache %s", icon_dir)
        try_to_call(["gtk-update-icon-cache", icon_dir])
        
        log.info("updating scrollkeeper database")
        try_to_call(["scrollkeeper-update", "-q", "-o", omf_dir])
     
    def remove(self, path):
        """
        Wraps the remove function, so that the dry_run parameter doesn't need
        to be set whenever it's called.
        """
        
        remove(path, self.dry_run)

class InstallCommand(InstallWrapper):
    user_options = InstallWrapper.user_options + [
        ("packaging-mode", "p", "don't perform post-installation operations"),
        ("upgrade", "u", "upgrade existing installations without confirmation")
    ]
    
    boolean_options = InstallWrapper.boolean_options + [
        "packaging-mode", "upgrade"
    ]
    
    def initialize_options(self):
        self.packaging_mode = False
        self.upgrade = False
        self.record = None
        
        InstallWrapper.initialize_options(self)
    
    def finalize_options(self):
        if self.upgrade:
            self.force = True
        
        if not self.record and not self.packaging_mode:
            self.record = self.DEFAULT_RECORD_FILE
        
        # Don't install the application to /usr/local on Debian/Ubuntu.
        # The problem is that numerous things related to the GNOME desktop
        # integration will cease to work otherwise.
        if not getattr(self, "install_layout", None):
            self.install_layout = "deb"
        
        InstallWrapper.finalize_options(self)
    
    def run(self):
        if not self.packaging_mode:
            self.upgrade_existing_installation()
        
        InstallWrapper.run(self)
        
        if os.name == "posix":
            # Make postprocessor.py executable.
            name = self.distribution.get_name()
            script = join(self.install_purelib, name, "plugins", "categories",
                "postprocessor.py")
            
            self.make_executable(script)
        
        if not self.packaging_mode:
            self.complete_installation()
    
    def upgrade_existing_installation(self):
        """
        Looks for exiting installations of LottaNZB in the target installation
        directory, which is usually /usr/lib/python2.x/site-packages.
        
        If the user didn't specify the force or upgrade flag explicitly,
        request a confirmation for the removal of all found installation dirs.
        
        Data files aren't removed, but overwritten by setting the force flag.
        """
        
        lotta_dirs = glob(join(self.install_purelib, "lottanzb*"))
        name = self.distribution.get_name()
        installed_version = ""
        
        try:
            module_info = find_module(name, [self.install_purelib])
            module = load_module(name, *module_info)
        except ImportError:
            pass
        else:
            if hasattr(module, "__version__"):
                installed_version = module.__version__
            else:
                installed_version = "<= 0.3"
        
        if installed_version:
            log.info("An existing installation of LottaNZB %s has been "
                "detected on your system.", installed_version)
            log.info("To avoid conflicts with the new version, the following "
                "folders will be removed:")
            
            for lotta_dir in lotta_dirs:
                log.info(" * %s", lotta_dir)
            
            if not self.upgrade and not self.force:
                selection = raw_input("\nWould you like to upgrade to LottaNZB "
                    "%s? [Y/n]: " % version)
                
                if selection and selection.lower() != "y":
                    raise DistutilsArgError("Aborting installation...")
            
            log.info("Upgrading to LottaNZB %s...", version)
            
            for lotta_dir in lotta_dirs:
                self.remove(lotta_dir)
            
            self.force = True
    
    def make_executable(self, a_file):
        if not self.dry_run:
            os.chmod(a_file, ((os.stat(a_file)[ST_MODE]) | 0555) & 07777)
        
        log.info("making %s executable", a_file)

class InstallLibCommand(install_lib):
    def get_outputs(self):
        files = install_lib.get_outputs(self)
        files.extend([self.platform_file_name])
        files.extend(self._bytecode_filenames([self.platform_file_name]))
        
        return files
    
    def install(self):
        files = install_lib.install(self)
        
        log.info("creating platform file %s", self.platform_file_name)
        
        if not self._dry_run:
            self.mkpath(dirname(self.platform_file_name))
            
            name = self.distribution.get_name()
            
            platform_file = open(self.platform_file_name, "w")
            platform_in = open(self.platform_file_name_in, "r").read()
            
            install_command = self.distribution.get_command_obj("install")
            data_dir = join(install_command.install_data, "share")
            
            # Make sure that the platform file contains the paths where the
            # built package will be installed to and not the ones of the
            # build environment.
            usr_index = data_dir.find("/usr")
            
            if usr_index != -1:
                data_dir = data_dir[usr_index:]
            
            replacement_map = {
                "{DATA_DIR}": join(data_dir, name),
                "{HELP_DIR}": join(data_dir, "gnome", "help", name),
                "{LOCALE_DIR}": join(data_dir, "locale")
            }
            
            for key, value in replacement_map.items():
                platform_in = platform_in.replace(key, value)
            
            platform_file.write(platform_in)
            platform_file.close()
        
        return files
    
    @property
    def platform_file_name(self):
        name = self.distribution.get_name()
        
        return join(self.install_dir, name, "resources/platform.py")
    
    @property
    def platform_file_name_in(self):
        name = self.distribution.get_name()
        
        return join(name, "resources/platform.py.in")

class UpgradeCommand(InstallCommand):
    description = "upgrade existing installation"
    
    def finalize_options(self):
        InstallCommand.finalize_options(self)
        
        self.upgrade = True

class UninstallCommand(InstallWrapper):
    description = "will uninstall the installed package"
    
    def finalize_options(self):
        InstallWrapper.finalize_options(self)
        
        if not self.record:
            self.record = self.DEFAULT_RECORD_FILE
    
    def run(self):
        try:
            files = open(self.record, "r").readlines()
        except:
            raise DistutilsFileError("Could not find list of installed files: "
                "%s" % self.record)
        
        for a_file in [a_file.strip() for a_file in files]:
            self.remove(a_file)
        
        self.remove(self.record)
        self.complete_installation()
        
        log.info("\nLottaNZB has been uninstalled successfully.")

class SourceDistributionCommand(sdist_auto):
    filter_prefix = sdist_auto.filter_prefix + [
        "help/po", "util", "debian", "rpm", ".pydevproject", ".project"]
    
    def add_defaults(self):
        sdist_auto.add_defaults(self)
        
        self.filelist.append("lottanzb/resources/platform.py.in")

options = {
    "name"             : "lottanzb",
    "version"          : version,
    "description"      : "LottaNZB - Automated Usenet Client",
    "long_description" : ("LottaNZB aims to simplify and automate the download "
                          "of binary news from the Usenet. You can tell "
                          "LottaNZB what to download using NZB files, which "
                          "are created by many Usenet search engines. LottaNZB "
                          "integrates nicely with GNOME desktops, but is not "
                          "limited to them and uses the mature HellaNZB "
                          "software as its foundation."),
    "author"           : "LottaNZB Development Team",
    "author_email"     : "avirulence@lottanzb.org",
    "url"              : "http://www.lottanzb.org/",
    "license"          : "GPL",
    "keywords"         : ["usenet", "nzb", "download", "hellanzb", "frontend",
                          "gtk"],
    "requires"         : ["gtk (>= 2.16)", "pygtk (>= 2.14)",
                          "kiwi (>= 1.9.9)"],
    "provides"         : ["lottanzb"],
    "data_files"       : [("/etc/apport/crashdb.conf.d",
                          ["apport/lottanzb-crashdb.conf"])],
    "cmdclass"         : {
                            "clean": CleanCommand,
                            "install": InstallCommand,
                            "install_lib": InstallLibCommand,
                            "uninstall": UninstallCommand,
                            "upgrade": UpgradeCommand,
                            "sdist": SourceDistributionCommand
                         },
    "classifiers"      : [
                            "Development Status :: 5 - Production/Stable",
                            "Environment :: X11 Applications :: Gnome",
                            "Environment :: X11 Applications :: GTK",
                            "Intended Audience :: End Users/Desktop",
                            "License :: OSI Approved :: "
                            "GNU General Public License (GPL)",
                            "Operating System :: POSIX :: Linux",
                            "Natural Language :: Bulgarian",
                            "Natural Language :: Chinese (Simplified)",
                            "Natural Language :: Danish",
                            "Natural Language :: Dutch",
                            "Natural Language :: English",
                            "Natural Language :: English (Australia)",
                            "Natural Language :: English (United Kingdom)",
                            "Natural Language :: French",
                            "Natural Language :: German",
                            "Natural Language :: Hebrew",
                            "Natural Language :: Indonesian",
                            "Natural Language :: Italian",
                            "Natural Language :: Latvian",
                            "Natural Language :: Norwegian Bokmal",
                            "Natural Language :: Polish",
                            "Natural Language :: Portuguese",
                            "Natural Language :: Russian",
                            "Natural Language :: Spanish",
                            "Natural Language :: Turkish",
                            "Programming Language :: Python",
                            "Topic :: Communications :: Usenet News"
                         ]
}

setup(**options)