summaryrefslogtreecommitdiff
path: root/xnt/build/make.py
blob: 74dcf49f5f075d906db867125ba62b0d5422be09 (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
#!/usr/bin/env python
"""Wrapping methods around build tools"""

#   Xnt -- A Wrapper Build Tool
#   Copyright (C) 2013  Kenny Ballou

#   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, either version 3 of the License, or
#   (at your option) any later version.

#   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, see <http://www.gnu.org/licenses/>.

import os
import subprocess

def ant(target, path="", flags=None, pkeys=None, pvalues=None):
    """Wrapper around Apache Ant"""
    cmd = __add_params(["ant"],
                       __build_param_list(pkeys, pvalues),
                       lambda x: "-D%s" % x)
    cmd = __add_flags(cmd, flags)
    cmd.append(target)
    return __run_in(path, lambda: subprocess.call(cmd))

def make(target, path="", flags=None, pkeys=None, pvalues=None):
    """Wrapper around GNU Make"""
    cmd = __add_params(["make"], __build_param_list(pkeys, pvalues))
    cmd = __add_flags(cmd, flags)
    cmd.append(target)
    return __run_in(path, lambda: subprocess.call(cmd))

def nant(target, path="", flags=None, pkeys=None, pvalues=None):
    """Wrapper around .NET Ant"""
    cmd = __add_params(["nant"],
                        __build_param_list(pkeys, pvalues),
                        lambda x: "-D:%s" % x)
    cmd = __add_flags(cmd, flags)
    cmd.append(target)
    return __run_in(path, lambda: subprocess.call(cmd))

def __add_flags(cmd, flags):
    """Add flags to command and return new list"""
    if not flags:
        return cmd
    command = list(cmd)
    for flag in flags:
        command.append(flag)
    return command

def __build_param_list(keys, values):
    """Build a list of key-value for appending to the command list"""
    parameters = []
    if not keys or not values:
        return parameters
    params = zip(keys, values)
    for param in params:
        parameters.append("%s=%s" % param)
    return parameters

def __add_params(cmd, params, param_map=lambda x: x):
    """Append parameters to cmd list using fn"""
    if not params:
        return cmd
    command = list(cmd)
    for param in params:
        command.append(param_map(param))
    return command

def __run_in(path, function):
    """Execute function while in a different running directory"""
    cwd = os.path.abspath(os.getcwd())
    if path and os.path.exists(path):
        os.chdir(os.path.abspath(path))
    result = function()
    if cwd:
        os.chdir(cwd)
    return result