summaryrefslogtreecommitdiff
path: root/python/which/build.py
blob: 3c8f09d39e1afe2c6fec9558ad8e96024686461c (plain)
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/bin/env python
# Copyright (c) 2002-2005 ActiveState
# See LICENSE.txt for license details.

"""
    which.py dev build script

    Usage:
        python build.py [<options>...] [<targets>...]

    Options:
        --help, -h      Print this help and exit.
        --targets, -t   List all available targets.

    This is the primary build script for the which.py project. It exists
    to assist in building, maintaining, and distributing this project.
    
    It is intended to have Makefile semantics. I.e. 'python build.py'
    will build execute the default target, 'python build.py foo' will
    build target foo, etc. However, there is no intelligent target
    interdependency tracking (I suppose I could do that with function
    attributes).
"""

import os
from os.path import basename, dirname, splitext, isfile, isdir, exists, \
                    join, abspath, normpath
import sys
import getopt
import types
import getpass
import shutil
import glob
import logging
import re



#---- exceptions

class Error(Exception):
    pass



#---- globals

log = logging.getLogger("build")




#---- globals

_project_name_ = "which"



#---- internal support routines

def _get_trentm_com_dir():
    """Return the path to the local trentm.com source tree."""
    d = normpath(join(dirname(__file__), os.pardir, "trentm.com"))
    if not isdir(d):
        raise Error("could not find 'trentm.com' src dir at '%s'" % d)
    return d

def _get_local_bits_dir():
    import imp
    info = imp.find_module("tmconfig", [_get_trentm_com_dir()])
    tmconfig = imp.load_module("tmconfig", *info)
    return tmconfig.bitsDir

def _get_project_bits_dir():
    d = normpath(join(dirname(__file__), "bits"))
    return d

def _get_project_version():
    import imp, os
    data = imp.find_module(_project_name_, [os.path.dirname(__file__)])
    mod = imp.load_module(_project_name_, *data)
    return mod.__version__


# Recipe: run (0.5.1) in /Users/trentm/tm/recipes/cookbook
_RUN_DEFAULT_LOGSTREAM = ("RUN", "DEFAULT", "LOGSTREAM")
def __run_log(logstream, msg, *args, **kwargs):
    if not logstream:
        pass
    elif logstream is _RUN_DEFAULT_LOGSTREAM:
        try:
            log.debug(msg, *args, **kwargs)
        except NameError:
            pass
    else:
        logstream(msg, *args, **kwargs)

def _run(cmd, logstream=_RUN_DEFAULT_LOGSTREAM):
    """Run the given command.

        "cmd" is the command to run
        "logstream" is an optional logging stream on which to log the command.
            If None, no logging is done. If unspecifed, this looks for a Logger
            instance named 'log' and logs the command on log.debug().

    Raises OSError is the command returns a non-zero exit status.
    """
    __run_log(logstream, "running '%s'", cmd)
    retval = os.system(cmd)
    if hasattr(os, "WEXITSTATUS"):
        status = os.WEXITSTATUS(retval)
    else:
        status = retval
    if status:
        #TODO: add std OSError attributes or pick more approp. exception
        raise OSError("error running '%s': %r" % (cmd, status))

def _run_in_dir(cmd, cwd, logstream=_RUN_DEFAULT_LOGSTREAM):
    old_dir = os.getcwd()
    try:
        os.chdir(cwd)
        __run_log(logstream, "running '%s' in '%s'", cmd, cwd)
        _run(cmd, logstream=None)
    finally:
        os.chdir(old_dir)


# Recipe: rmtree (0.5) in /Users/trentm/tm/recipes/cookbook
def _rmtree_OnError(rmFunction, filePath, excInfo):
    if excInfo[0] == OSError:
        # presuming because file is read-only
        os.chmod(filePath, 0777)
        rmFunction(filePath)
def _rmtree(dirname):
    import shutil
    shutil.rmtree(dirname, 0, _rmtree_OnError)


# Recipe: pretty_logging (0.1) in /Users/trentm/tm/recipes/cookbook
class _PerLevelFormatter(logging.Formatter):
    """Allow multiple format string -- depending on the log level.
    
    A "fmtFromLevel" optional arg is added to the constructor. It can be
    a dictionary mapping a log record level to a format string. The
    usual "fmt" argument acts as the default.
    """
    def __init__(self, fmt=None, datefmt=None, fmtFromLevel=None):
        logging.Formatter.__init__(self, fmt, datefmt)
        if fmtFromLevel is None:
            self.fmtFromLevel = {}
        else:
            self.fmtFromLevel = fmtFromLevel
    def format(self, record):
        record.levelname = record.levelname.lower()
        if record.levelno in self.fmtFromLevel:
            #XXX This is a non-threadsafe HACK. Really the base Formatter
            #    class should provide a hook accessor for the _fmt
            #    attribute. *Could* add a lock guard here (overkill?).
            _saved_fmt = self._fmt
            self._fmt = self.fmtFromLevel[record.levelno]
            try:
                return logging.Formatter.format(self, record)
            finally:
                self._fmt = _saved_fmt
        else:
            return logging.Formatter.format(self, record)

def _setup_logging():
    hdlr = logging.StreamHandler()
    defaultFmt = "%(name)s: %(levelname)s: %(message)s"
    infoFmt = "%(name)s: %(message)s"
    fmtr = _PerLevelFormatter(fmt=defaultFmt,
                              fmtFromLevel={logging.INFO: infoFmt})
    hdlr.setFormatter(fmtr)
    logging.root.addHandler(hdlr)
    log.setLevel(logging.INFO)


def _getTargets():
    """Find all targets and return a dict of targetName:targetFunc items."""
    targets = {}
    for name, attr in sys.modules[__name__].__dict__.items():
        if name.startswith('target_'):
            targets[ name[len('target_'):] ] = attr
    return targets

def _listTargets(targets):
    """Pretty print a list of targets."""
    width = 77
    nameWidth = 15 # min width
    for name in targets.keys():
        nameWidth = max(nameWidth, len(name))
    nameWidth += 2  # space btwn name and doc
    format = "%%-%ds%%s" % nameWidth
    print format % ("TARGET", "DESCRIPTION")
    for name, func in sorted(targets.items()):
        doc = _first_paragraph(func.__doc__ or "", True)
        if len(doc) > (width - nameWidth):
            doc = doc[:(width-nameWidth-3)] + "..."
        print format % (name, doc)


# Recipe: first_paragraph (1.0.1) in /Users/trentm/tm/recipes/cookbook
def _first_paragraph(text, join_lines=False):
    """Return the first paragraph of the given text."""
    para = text.lstrip().split('\n\n', 1)[0]
    if join_lines:
        lines = [line.strip() for line in  para.splitlines(0)]
        para = ' '.join(lines)
    return para



#---- build targets

def target_default():
    target_all()

def target_all():
    """Build all release packages."""
    log.info("target: default")
    if sys.platform == "win32":
        target_launcher()
    target_sdist()
    target_webdist()


def target_clean():
    """remove all build/generated bits"""
    log.info("target: clean")
    if sys.platform == "win32":
        _run("nmake -f Makefile.win clean")

    ver = _get_project_version()
    dirs = ["dist", "build", "%s-%s" % (_project_name_, ver)]
    for d in dirs:
        print "removing '%s'" % d
        if os.path.isdir(d): _rmtree(d)

    patterns = ["*.pyc", "*~", "MANIFEST",
                os.path.join("test", "*~"),
                os.path.join("test", "*.pyc"),
               ]
    for pattern in patterns:
        for file in glob.glob(pattern):
            print "removing '%s'" % file
            os.unlink(file)


def target_launcher():
    """Build the Windows launcher executable."""
    log.info("target: launcher")
    assert sys.platform == "win32", "'launcher' target only supported on Windows"
    _run("nmake -f Makefile.win")


def target_docs():
    """Regenerate some doc bits from project-info.xml."""
    log.info("target: docs")
    _run("projinfo -f project-info.xml -R -o README.txt --force")
    _run("projinfo -f project-info.xml --index-markdown -o index.markdown --force")


def target_sdist():
    """Build a source distribution."""
    log.info("target: sdist")
    target_docs()
    bitsDir = _get_project_bits_dir()
    _run("python setup.py sdist -f --formats zip -d %s" % bitsDir,
         log.info)


def target_webdist():
    """Build a web dist package.
    
    "Web dist" packages are zip files with '.web' package. All files in
    the zip must be under a dir named after the project. There must be a
    webinfo.xml file at <projname>/webinfo.xml. This file is "defined"
    by the parsing in trentm.com/build.py.
    """ 
    assert sys.platform != "win32", "'webdist' not implemented for win32"
    log.info("target: webdist")
    bitsDir = _get_project_bits_dir()
    buildDir = join("build", "webdist")
    distDir = join(buildDir, _project_name_)
    if exists(buildDir):
        _rmtree(buildDir)
    os.makedirs(distDir)

    target_docs()
    
    # Copy the webdist bits to the build tree.
    manifest = [
        "project-info.xml",
        "index.markdown",
        "LICENSE.txt",
        "which.py",
        "logo.jpg",
    ]
    for src in manifest:
        if dirname(src):
            dst = join(distDir, dirname(src))
            os.makedirs(dst)
        else:
            dst = distDir
        _run("cp %s %s" % (src, dst))

    # Zip up the webdist contents.
    ver = _get_project_version()
    bit = abspath(join(bitsDir, "%s-%s.web" % (_project_name_, ver)))
    if exists(bit):
        os.remove(bit)
    _run_in_dir("zip -r %s %s" % (bit, _project_name_), buildDir, log.info)


def target_install():
    """Use the setup.py script to install."""
    log.info("target: install")
    _run("python setup.py install")


def target_upload_local():
    """Update release bits to *local* trentm.com bits-dir location.
    
    This is different from the "upload" target, which uploads release
    bits remotely to trentm.com.
    """
    log.info("target: upload_local")
    assert sys.platform != "win32", "'upload_local' not implemented for win32"

    ver = _get_project_version()
    localBitsDir = _get_local_bits_dir()
    uploadDir = join(localBitsDir, _project_name_, ver)

    bitsPattern = join(_get_project_bits_dir(),
                       "%s-*%s*" % (_project_name_, ver))
    bits = glob.glob(bitsPattern)
    if not bits:
        log.info("no bits matching '%s' to upload", bitsPattern)
    else:
        if not exists(uploadDir):
            os.makedirs(uploadDir)
        for bit in bits:
            _run("cp %s %s" % (bit, uploadDir), log.info)


def target_upload():
    """Upload binary and source distribution to trentm.com bits
    directory.
    """
    log.info("target: upload")

    ver = _get_project_version()
    bitsDir = _get_project_bits_dir()
    bitsPattern = join(bitsDir, "%s-*%s*" % (_project_name_, ver))
    bits = glob.glob(bitsPattern)
    if not bits:
        log.info("no bits matching '%s' to upload", bitsPattern)
        return

    # Ensure have all the expected bits.
    expectedBits = [
        re.compile("%s-.*\.zip$" % _project_name_),
        re.compile("%s-.*\.web$" % _project_name_)
    ]
    for expectedBit in expectedBits:
        for bit in bits:
            if expectedBit.search(bit):
                break
        else:
            raise Error("can't find expected bit matching '%s' in '%s' dir"
                        % (expectedBit.pattern, bitsDir))

    # Upload the bits.
    user = "trentm"
    host = "trentm.com"
    remoteBitsBaseDir = "~/data/bits"
    remoteBitsDir = join(remoteBitsBaseDir, _project_name_, ver)
    if sys.platform == "win32":
        ssh = "plink"
        scp = "pscp -unsafe"
    else:
        ssh = "ssh"
        scp = "scp"
    _run("%s %s@%s 'mkdir -p %s'" % (ssh, user, host, remoteBitsDir), log.info)
    for bit in bits:
        _run("%s %s %s@%s:%s" % (scp, bit, user, host, remoteBitsDir),
             log.info)


def target_check_version():
    """grep for version strings in source code
    
    List all things that look like version strings in the source code.
    Used for checking that versioning is updated across the board.  
    """
    sources = [
        "which.py",
        "project-info.xml",
    ]
    pattern = r'[0-9]\+\(\.\|, \)[0-9]\+\(\.\|, \)[0-9]\+'
    _run('grep -n "%s" %s' % (pattern, ' '.join(sources)), None)



#---- mainline

def build(targets=[]):
    log.debug("build(targets=%r)" % targets)
    available = _getTargets()
    if not targets:
        if available.has_key('default'):
            return available['default']()
        else:   
            log.warn("No default target available. Doing nothing.")
    else:
        for target in targets:
            if available.has_key(target):
                retval = available[target]()
                if retval:
                    raise Error("Error running '%s' target: retval=%s"\
                                % (target, retval))
            else:
                raise Error("Unknown target: '%s'" % target)

def main(argv):
    _setup_logging()

    # Process options.
    optlist, targets = getopt.getopt(argv[1:], 'ht', ['help', 'targets'])
    for opt, optarg in optlist:
        if opt in ('-h', '--help'):
            sys.stdout.write(__doc__ + '\n')
            return 0
        elif opt in ('-t', '--targets'):
            return _listTargets(_getTargets())

    return build(targets)

if __name__ == "__main__":
    sys.exit( main(sys.argv) )