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