1 |
johnpye |
554 |
# DistTarBuilder: tool to generate tar files using SCons |
2 |
johnpye |
553 |
# Copyright (C) 2005, 2006 Matthew A. Nicholson |
3 |
mahesh |
2150 |
# Copyright (C) 2006-2010 John Pye |
4 |
johnpye |
553 |
# |
5 |
johnpye |
554 |
# This file is free software; you can redistribute it and/or |
6 |
johnpye |
553 |
# modify it under the terms of the GNU Lesser General Public |
7 |
|
|
# License version 2.1 as published by the Free Software Foundation. |
8 |
|
|
# |
9 |
johnpye |
554 |
# This file is distributed in the hope that it will be useful, |
10 |
johnpye |
553 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
|
|
# Lesser General Public License for more details. |
13 |
|
|
# |
14 |
jpye |
2651 |
# You should have received a copy of the GNU General Public License |
15 |
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 |
johnpye |
553 |
|
17 |
johnpye |
561 |
import os,sys |
18 |
|
|
from SCons.Script import * |
19 |
mahesh |
2150 |
import re |
20 |
johnpye |
556 |
|
21 |
johnpye |
561 |
def disttar_emitter(target,source,env): |
22 |
johnpye |
556 |
|
23 |
|
|
source,origsource = [], source |
24 |
|
|
|
25 |
|
|
excludeexts = env.Dictionary().get('DISTTAR_EXCLUDEEXTS',[]) |
26 |
|
|
excludedirs = env.Dictionary().get('DISTTAR_EXCLUDEDIRS',[]) |
27 |
mahesh |
2150 |
re1 = env.Dictionary().get('DISTTAR_EXCLUDERES',[]) |
28 |
|
|
excluderes = [re.compile(r) for r in re1] |
29 |
johnpye |
556 |
|
30 |
|
|
# assume the sources are directories... need to check that |
31 |
|
|
for item in origsource: |
32 |
|
|
for root, dirs, files in os.walk(str(item)): |
33 |
|
|
|
34 |
|
|
# don't make directory dependences as that triggers full build |
35 |
|
|
# of that directory |
36 |
|
|
if root in source: |
37 |
|
|
#print "Removing directory %s" % root |
38 |
|
|
source.remove(root) |
39 |
|
|
|
40 |
|
|
# loop through files in a directory |
41 |
|
|
for name in files: |
42 |
|
|
ext = os.path.splitext(name) |
43 |
|
|
if not ext[1] in excludeexts: |
44 |
|
|
relpath = os.path.join(root,name) |
45 |
mahesh |
2150 |
failre = False |
46 |
|
|
for r in excluderes: |
47 |
|
|
#print "Match( %s against %s)" % (r,relpath) |
48 |
|
|
if r.search(relpath): |
49 |
|
|
failre = True |
50 |
|
|
#print "Excluding '%s' from tarball" % relpath |
51 |
|
|
break |
52 |
|
|
if not failre: |
53 |
|
|
#print "Adding source",relpath |
54 |
|
|
source.append(relpath) |
55 |
johnpye |
556 |
for d in excludedirs: |
56 |
|
|
if d in dirs: |
57 |
|
|
dirs.remove(d) # don't visit CVS directories etc |
58 |
|
|
|
59 |
|
|
return target, source |
60 |
|
|
|
61 |
johnpye |
561 |
def disttar_string(target, source, env): |
62 |
|
|
"""This is what gets printed on the console. We'll strip out the list |
63 |
|
|
or source files, since it tends to get very long. If you want to see the |
64 |
|
|
contents, the easiest way is to uncomment the line 'Adding to TAR file' |
65 |
|
|
below. """ |
66 |
|
|
return 'DistTar(%s,...)' % str(target[0]) |
67 |
|
|
|
68 |
|
|
def disttar(target, source, env): |
69 |
johnpye |
553 |
"""tar archive builder""" |
70 |
|
|
|
71 |
|
|
import tarfile |
72 |
|
|
|
73 |
|
|
env_dict = env.Dictionary() |
74 |
|
|
|
75 |
johnpye |
554 |
if env_dict.get("DISTTAR_FORMAT") in ["gz", "bz2"]: |
76 |
johnpye |
553 |
tar_format = env_dict["DISTTAR_FORMAT"] |
77 |
|
|
else: |
78 |
|
|
tar_format = "" |
79 |
|
|
|
80 |
|
|
# split the target directory, filename, and stuffix |
81 |
|
|
base_name = str(target[0]).split('.tar')[0] |
82 |
|
|
(target_dir, dir_name) = os.path.split(base_name) |
83 |
|
|
|
84 |
|
|
# create the target directory if it does not exist |
85 |
|
|
if target_dir and not os.path.exists(target_dir): |
86 |
|
|
os.makedirs(target_dir) |
87 |
|
|
|
88 |
|
|
# open our tar file for writing |
89 |
johnpye |
561 |
sys.stderr.write("DistTar: Writing "+str(target[0])) |
90 |
johnpye |
553 |
tar = tarfile.open(str(target[0]), "w:%s" % (tar_format,)) |
91 |
|
|
|
92 |
|
|
# write sources to our tar file |
93 |
|
|
for item in source: |
94 |
johnpye |
556 |
item = str(item) |
95 |
johnpye |
561 |
sys.stderr.write(".") |
96 |
|
|
#print "Adding to TAR file: %s/%s" % (dir_name,item) |
97 |
johnpye |
556 |
tar.add(item,'%s/%s' % (dir_name,item)) |
98 |
johnpye |
553 |
|
99 |
|
|
# all done |
100 |
johnpye |
561 |
sys.stderr.write("\n") #print "Closing TAR file" |
101 |
johnpye |
553 |
tar.close() |
102 |
|
|
|
103 |
johnpye |
561 |
def disttar_suffix(env, sources): |
104 |
johnpye |
553 |
"""tar archive suffix generator""" |
105 |
|
|
|
106 |
|
|
env_dict = env.Dictionary() |
107 |
|
|
if env_dict.has_key("DISTTAR_FORMAT") and env_dict["DISTTAR_FORMAT"] in ["gz", "bz2"]: |
108 |
|
|
return ".tar." + env_dict["DISTTAR_FORMAT"] |
109 |
|
|
else: |
110 |
|
|
return ".tar" |
111 |
|
|
|
112 |
|
|
def generate(env): |
113 |
|
|
""" |
114 |
|
|
Add builders and construction variables for the DistTar builder. |
115 |
|
|
""" |
116 |
|
|
|
117 |
johnpye |
561 |
disttar_action=SCons.Action.Action(disttar, disttar_string) |
118 |
|
|
env['BUILDERS']['DistTar'] = Builder( |
119 |
|
|
action=disttar_action |
120 |
|
|
, emitter=disttar_emitter |
121 |
|
|
, suffix = disttar_suffix |
122 |
|
|
, target_factory = env.fs.Entry |
123 |
|
|
) |
124 |
|
|
|
125 |
johnpye |
553 |
env.AppendUnique( |
126 |
johnpye |
554 |
DISTTAR_FORMAT = 'gz' |
127 |
johnpye |
553 |
) |
128 |
|
|
|
129 |
|
|
def exists(env): |
130 |
|
|
""" |
131 |
|
|
Make sure this tool exists. |
132 |
|
|
""" |
133 |
|
|
try: |
134 |
|
|
import os |
135 |
|
|
import tarfile |
136 |
|
|
except ImportError: |
137 |
|
|
return False |
138 |
|
|
else: |
139 |
|
|
return True |
140 |
mahesh |
2150 |
|
141 |
|
|
# vim:set ts=4 sw=4 noexpandtab: |
142 |
|
|
|