summaryrefslogtreecommitdiff
path: root/xnt/tasks.py
blob: a647b0b09829169637d07d792ed46a0265879b74 (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
#!/usr/bin/env python
"""Common Tasks Module

Defines a set of operations that are common enough but also are tedious to
define
"""

#   Xnt -- A Wrapper Build Tool
#   Copyright (C) 2012  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 sys
import subprocess
import shutil
import zipfile
import contextlib
import glob
import logging

LOGGER = logging.getLogger(__name__)

#File associated tasks
def expandpath(path):
    """
    Expand path using globs to a possibly empty list of directories
    """
    return glob.iglob(path)

def cp(src="", dst="", files=None): #pylint: disable-msg=C0103
    """Copy `src` to `dst` or copy `files` to `dst`

    Copy a file or folder to a different file/folder
    If no `src` file is specified, will attempt to copy `files` to `dst`
    """
    assert dst and src or len(files) > 0
    LOGGER.info("Copying %s to %s", src, dst)
    def copy(source, destination):
        """Copy file or folder to destination.

        Depending on the type of source, call the appropriate method
        """
        if os.path.isdir(source):
            shutil.copytree(source, destination)
        else:
            shutil.copy2(source, destination)
    if src:
        copy(src, dst)
    else:
        for file_to_copy in files:
            copy(file_to_copy, dst)

def mv(src, dst): #pylint: disable-msg=C0103
    """Move file or folder to destination"""
    LOGGER.info("Moving %s to %s", src, dst)
    shutil.move(src, dst)

def mkdir(directory, mode=0o777):
    """Make a directory with mode"""
    if os.path.exists(directory):
        return
    LOGGER.info("Making directory %s with mode %o", directory, mode)
    try:
        os.mkdir(directory, mode)
    except IOError as io_error:
        log(io_error, logging.WARNING)
    except:
        raise

def rm(*fileset): #pylint: disable-msg=C0103
    """Remove a set of files"""
    try:
        for glob_set in fileset:
            for file_to_delete in expandpath(glob_set):
                if not os.path.exists(file_to_delete):
                    continue
                LOGGER.info("Removing %s", file_to_delete)
                if os.path.isdir(file_to_delete):
                    shutil.rmtree(file_to_delete)
                else:
                    os.remove(file_to_delete)
    except OSError as os_error:
        log(os_error, logging.WARNING)
    except:
        raise

def create_zip(directory, zipfilename):
    """Compress (Zip) folder"""
    LOGGER.info("Zipping %s as %s", directory, zipfilename)
    assert os.path.isdir(directory) and zipfilename
    with contextlib.closing(zipfile.ZipFile(
        zipfilename,
        "w",
        zipfile.ZIP_DEFLATED)) as zip_file:
        for paths in os.walk(directory):
            for file_name in paths[2]:
                absfn = os.path.join(paths[0], file_name)
                zip_file_name = absfn[len(directory) + len(os.sep):]
                zip_file.write(absfn, zip_file_name)

#Misc Tasks
def echo(msg, tofile):
    """Write a string to file"""
    with open(tofile, "w") as file_to_write:
        file_to_write.write(msg)

def log(msg="", lvl=logging.INFO):
    """Log message using tasks global logger"""
    LOGGER.log(lvl, msg)

def xntcall(path, targets=None, props=None):
    """Invoke xnt on another build file in a different directory

    param: path - to the build file (including build file)
    param: targets - list of targets to execute
    param: props - dictionary of properties to pass to the build module
    """
    import xnt.xenant
    from xnt.commands.target import TargetCommand
    command = TargetCommand(xnt.xenant.load_build(path))
    return command.run(targets=targets, props=props)

def call(command, stdout=None, stderr=None):
    """ Execute the given command, redirecting stdout and stderr
    to optionally given files

    param: command - list of command and arguments
    param: stdout - file to redirect standard output to, if given
    param: stderr - file to redirect standard error to, if given
    """
    return subprocess.call(args=command, stdout=stdout, stderr=stderr)

def setup(commands, directory=""):
    """Invoke the ``setup.py`` file in the current or specified directory

    param: commands - list of commands and options to run/ append
    param: dir - (optional) directory to run from
    """
    cmd = [sys.executable, "setup.py",]
    for command in commands:
        cmd.append(command)
    cwd = os.getcwd()
    if directory:
        os.chdir(directory)
    error_code = call(cmd)
    os.chdir(cwd)
    return error_code