execute.py 1.52 KB
Newer Older
1
import os, subprocess, logging
2

3
from i18n.config import BASE_DIR
4

5
LOG = logging.getLogger(__name__)
6

7
def execute(command, working_directory=BASE_DIR):
8 9 10
    """
    Executes shell command in a given working_directory.
    Command is a string to pass to the shell.
Steve Strassmann committed
11
    Output is ignored.
12
    """
13
    LOG.info("Executing in %s ...", working_directory)
14
    LOG.info(command)
15
    subprocess.check_call(command, cwd=working_directory, stderr=subprocess.STDOUT, shell=True)
16 17


18
def call(command, working_directory=BASE_DIR):
19 20
    """
    Executes shell command in a given working_directory.
21
    Command is a list of strings to execute as a command line.
22
    Returns a tuple of two strings: (stdout, stderr)
Steve Strassmann committed
23

24
    """
25
    LOG.info(command)
26
    p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=working_directory, shell=True)
27 28
    out, err = p.communicate()
    return (out, err)
29 30


31 32 33 34 35 36
def create_dir_if_necessary(pathname):
    dirname = os.path.dirname(pathname)
    if not os.path.exists(dirname):
        os.makedirs(dirname)


37
def remove_file(filename, verbose=True):
38 39
    """
    Attempt to delete filename.
Steve Strassmann committed
40
    log is boolean. If true, removal is logged.
41 42 43 44
    Log a warning if file does not exist.
    Logging filenames are releative to BASE_DIR to cut down on noise in output.
    """
    if verbose:
45
        LOG.info('Deleting file %s' % os.path.relpath(filename, BASE_DIR))
46
    if not os.path.exists(filename):
47
        LOG.warn("File does not exist: %s" % os.path.relpath(filename, BASE_DIR))
48 49
    else:
        os.remove(filename)