summaryrefslogtreecommitdiff
path: root/xnt/tasks.py
blob: 41577b475c12fe2fcb95e4e6ab1d8e01be4032c5 (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
#!/usr/bin/env python

import os
import sys
import subprocess
import time
import shutil
import zipfile
import contextlib

#File associated tasks
def cp(src,dst):
    if os.path.isdir(src):
        shutil.copytree(src,dst)
    else:
        shutil.copy2(src,dst)

def mv(src,dst):
    shutil.move(src,dst)

def mkdir(dir,mode=0777):
    try:
        os.mkdir(dir,mode)
    except IOError:
        pass

def rm(path):
    if os.path.isdir(path):
        shutil.rmtree(path)
    else:
        os.remove(path)

def zip(dir,zipfilename):
    assert os.path.isdir(dir) and zipfilename
    with contextlib.closing(zipfile.ZipFile(
        zipfilename,
        "w",
        zipfile.ZIP_DEFLATED)) as z:
        for root, dirs, files in os.walk(dir):
            for fn in files:
                absfn = os.path.join(root, fn)
                zfn = absfn[len(dir)+len(os.sep):]
                z.write(absfn, zfn)

def make(path="",target=""):
    makeCmd = ["ant", target]
    if path and os.path.exists(path):
        oldPath = os.getcwd()
        os.chdir(os.path.abspath(path))
    result = subprocess.call(makeCmd)
    os.chdir(os.path.abspath(oldPath))
    return result

#Misc Tasks
def echo(message="",tofile=""):
    if tofile:
        with open(tofile, "w") as f:
            subprocess.call(["echo", message], stdout=f)
    else:
        print(message)

#HG Tasks
def hgclone(url):
    subprocess.call(["hg", "clone", url])

def hgfetch(path):
    oldPath = os.getcwd()
    os.chdir(path)
    subprocess.call(["hg", "pull", "-u"])
    os.chdir(oldPath)