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
|
#!/usr/bin/env python
# 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 time
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=[]):
assert dst and src or len(files) > 0
logger.info("Copying %s to %s", src, dst)
def copy(s,d):
if os.path.isdir(s):
shutil.copytree(s,d)
else:
shutil.copy2(s,d)
if src:
copy(src, dst)
else:
for f in files:
copy(f, dst)
def mv(src,dst):
logger.info("Moving %s to %s", src, dst)
shutil.move(src,dst)
def mkdir(dir,mode=0o777):
if os.path.exists(dir):
return
logger.info("Making directory %s with mode %o", dir, mode)
try:
os.mkdir(dir,mode)
except IOError as e:
log(e, logging.WARNING)
except:
raise
def rm(*fileset):
try:
for g in fileset:
for f in expandpath(g):
if not os.path.exists(f):
continue
logger.info("Removing %s", f)
if os.path.isdir(f):
shutil.rmtree(f)
else:
os.remove(f)
except OSError as e:
log(e, logging.WARNING)
except:
raise
def zip(dir,zipfilename):
logger.info("Zipping %s as %s", 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)
#Misc Tasks
def echo(msg, tofile):
with open(tofile, "w") as f:
f.write(msg)
def log(msg="",lvl=logging.INFO):
logger.log(lvl, msg)
def xnt(target, path):
"""
Invoke xnt on another build file in a different directory
"""
import xnt.xenant
xnt.xenant.invokeBuild(
xnt.xenant.__loadBuild(path),
target)
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, dir=""):
"""
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 c in commands:
cmd.append(c)
cwd = os.getcwd()
if dir:
os.chdir(dir)
ec = call(cmd)
os.chdir(cwd)
return ec
|