~ubuntu-branches/ubuntu/lucid/pytagsfs/lucid

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
#!/usr/bin/env python

# Author: Forest Bond <forest@alittletooquiet.net>
# This file is in the public domain.

import os, sys, commands, glob, inspect
from distutils.command.build import build as _build
from distutils.command.clean import clean as _clean
from distutils.command.build_py import build_py
from distutils.core import setup, Command
from distutils.spawn import spawn
from distutils import log
from distutils.dir_util import remove_tree
from distutils.dist import Distribution

project_dir = os.path.dirname(__file__)
modules_dir = os.path.join(project_dir, 'modules')

sys.path.insert(0, modules_dir)
sys.path.insert(0, project_dir)

from pytagsfs.fs import UMOUNT_COMMAND
from tests.common import TEST_DATA_DIR


################################################################################


def find_modules(package):
    modules = [package]
    for name in dir(package):
        value = getattr(package, name)
        if (inspect.ismodule(value)) and (
          value.__name__.rpartition('.')[0] == package.__name__):
            modules.extend(find_modules(value))
    return modules


class test(Command):
    description = 'run tests'
    user_options = [
      ('tests=', None, 'names of tests to run'),
      ('print-only', None, "don't run tests, just print their names"),
      ('coverage', None, "print coverage analysis (requires coverage.py)"),
    ]

    def initialize_options(self):
        self.tests = None
        self.print_only = False
        self.coverage = False

    def finalize_options(self):
        if self.tests is not None:
            self.tests = self.tests.split(',')

    def run(self):
        if self.coverage:
            import coverage
            coverage.use_cache(0)
            coverage.start()

        from tests import load, main, print_names
        load()

        try:
            if self.print_only:
                print_names(self.tests)
            else:
                main(test_names = self.tests)
        finally:
            if self.coverage:
                import pytagsfs
                coverage.report(find_modules(pytagsfs))

################################################################################

class clean(_clean):
    temporary_files = []
    nontemporary_files = []

    temporary_dirs = []
    nontemporary_dirs = []

    def clean_file(self, filename):
        if not os.path.exists(filename):
            log.info("'%s' does not exist -- can't clean it", filename)
            return

        log.info("removing '%s'" % filename)
        if not self.dry_run:
            try:
                os.unlink(filename)
            except (IOError, OSError):
                log.warn("failed to remove '%s'" % filename)

    def clean_dir(self, dirname):
        if not os.path.exists(dirname):
            log.info("'%s' does not exist -- can't clean it", dirname)
            return

        log.info("removing '%s' (and everything under it)" % dirname)
        if not self.dry_run:
            try:
                remove_tree(dirname)
            except (IOError, OSError):
                log.warn("failed to remove '%s'" % dirname)

    def clean_test_data(self):
        try:
            dirs = os.listdir(TEST_DATA_DIR)
        except (IOError, OSError):
            log.warn(
              "not cleaning '%s': failed to read directory" % TEST_DATA_DIR)
        else:
            for dir in dirs:
                full_dir = os.path.join(TEST_DATA_DIR, dir)
                mnt_dir = os.path.join(full_dir, 'mnt')

                log.info("unmounting '%s'" % mnt_dir)
                status, output = commands.getstatusoutput(
                  UMOUNT_COMMAND % mnt_dir)
                if status != 0:
                    print >>sys.stderr, output

                self.clean_dir(full_dir)

        self.clean_dir(TEST_DATA_DIR)

    def run(self):
        self.clean_test_data()

        remove_files = list(self.temporary_files)
        if self.all:
            remove_files = remove_files + self.nontemporary_files

        for filename in remove_files:
            if callable(filename):
                filename = filename(self.distribution)
            self.clean_file(filename)

        remove_dirs = list(self.temporary_dirs)
        if self.all:
            remove_dirs = remove_dirs + self.nontemporary_dirs

        for dirname in remove_dirs:
            if callable(dirname):
                dirname = dirname(self.distribution)
            self.clean_dir(dirname)

        _clean.run(self)

################################################################################

def find_docbook_manpage_stylesheet():
    from libxml2 import catalogResolveURI
    return catalogResolveURI(
      'http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl'
    )

class build_manpages(Command):
    xsltproc = ['xsltproc', '--nonet', '--novalid', '--xinclude']
    description = 'Build manual pages from docbook XML.'
    user_options = []
    man_build_dir = 'build/man'
    stylesheet = find_docbook_manpage_stylesheet()

    def initialize_options(self):
        pass

    def finalize_options(self):
        if self.distribution.manpage_sources is not None:
            self.docbook_files = [
              os.path.abspath(p) for p in self.distribution.manpage_sources
              if p.endswith('.xml')
            ]

    def build_manpage_from_docbook(self, docbook_file):
        assert self.stylesheet is not None, 'failed to find stylesheet'

        command = self.xsltproc + [self.stylesheet, docbook_file]
        orig_wd = os.getcwd()
        os.chdir(self.man_build_dir)
        try:
            spawn(command, dry_run = self.dry_run)
        finally:
            os.chdir(orig_wd)

    def run(self):
        if self.stylesheet is None:
            log.warn(
              'Warning: missing docbook XSL stylesheets; '
              'manpages will not be built.\n'
              'Please install the docbook XSL stylesheets from '
              'http://docbook.org/.'
            )

        manpage_sources = self.docbook_files
        if manpage_sources:
            if not os.path.exists(self.man_build_dir):
                os.mkdir(self.man_build_dir)
            for docbook_file in self.docbook_files:
                self.build_manpage_from_docbook(docbook_file)

clean.nontemporary_dirs.append('build/man')
Distribution.manpage_sources = None

################################################################################

class build_version_file(build_py):
    def initialize_options(self):
        build_py.initialize_options(self)

        self.version = None
        self.version_file = None

    def finalize_options(self):
        build_py.finalize_options(self)

        self.packages = self.distribution.packages
        self.py_modules = [self.distribution.version_module]

        self.version = self.distribution.get_version()
        self.version_file = self.distribution.version_file

    def check_module(self, *args, **kwargs):
        pass

    def build_modules(self, *args, **kwargs):
        log.info("creating version file '%s'" % self.version_file)
        if not self.dry_run:
            f = open(self.version_file, 'w')
            f.write('version = %s' % repr(self.version))
            f.close()
        build_py.build_modules(self, *args, **kwargs)

clean.temporary_files.append(lambda distribution: distribution.version_file)
Distribution.version_module = None
Distribution.release_file = None

def get_bzr_version():
    status, output = commands.getstatusoutput('bzr revno')
    return 'bzr%s' % output.strip()

def get_version(release_file):
    try:
        f = open(release_file, 'r')
        try:
            version = f.read().strip()
        finally:
            f.close()
    except (IOError, OSError):
        version = get_bzr_version()
    return version

def get_version_file(version_module):
    return '%s.py' % os.path.join(
      *(['modules'] + version_module.split('.'))
    )

def wrap_init(fn):
    def __init__(self, *args, **kwargs):
        fn(self, *args, **kwargs)
        self.version_file = get_version_file(self.version_module)
        self.metadata.version = get_version(self.release_file)
    return __init__

Distribution.__init__ = wrap_init(Distribution.__init__)

################################################################################

class build(_build):
    sub_commands = _build.sub_commands + [
      ('build_version_file', (lambda self: True)),
      ('build_manpages', (lambda self: True)),
    ]

################################################################################

data_files = []
manpage_sources = []

if build_manpages.stylesheet is not None:
    manpage_sources = ['pytagsfs.xml', 'pytags.xml']
    manpages = [
      os.path.join(
        'build',
        'man',
        s.replace('xml', '1'),
      )
      for s in manpage_sources
    ]
    data_files.append(('share/man/man1', manpages))

setup(
  cmdclass = {
    'test': test,
    'build': build,
    'build_version_file': build_version_file,
    'build_manpages': build_manpages,
    'clean': clean,
  },
  name = 'pytagsfs',
  version_module = 'pytagsfs.version',
  package_dir = {
    'pytagsfs': os.path.join('modules', 'pytagsfs')
  },
  packages = [
    'pytagsfs',
    'pytagsfs.fs',
    'pytagsfs.metastore',
    'pytagsfs.pathstore',
    'pytagsfs.sourcetreemon',
    'pytagsfs.sourcetreerep',
    'pytagsfs.specialfile',
  ],
  scripts = [
    'pytagsfs',
    'pytags',
  ],
  manpage_sources = manpage_sources,
  release_file = 'release',
  data_files = data_files,
)