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 |
20 |
|
21 |
def disttar_modify_sources(target,source,env): |
22 |
|
23 |
source,origsource = [], source |
24 |
|
25 |
excludeexts = env.Dictionary().get('DISTTAR_EXCLUDEEXTS',[]) |
26 |
excludedirs = env.Dictionary().get('DISTTAR_EXCLUDEDIRS',[]) |
27 |
|
28 |
# assume the sources are directories... need to check that |
29 |
for item in origsource: |
30 |
for root, dirs, files in os.walk(str(item)): |
31 |
|
32 |
# don't make directory dependences as that triggers full build |
33 |
# of that directory |
34 |
if root in source: |
35 |
#print "Removing directory %s" % root |
36 |
source.remove(root) |
37 |
|
38 |
# loop through files in a directory |
39 |
for name in files: |
40 |
ext = os.path.splitext(name) |
41 |
if not ext[1] in excludeexts: |
42 |
relpath = os.path.join(root,name) |
43 |
#print "Adding source",relpath |
44 |
source.append(relpath) |
45 |
for d in excludedirs: |
46 |
if d in dirs: |
47 |
dirs.remove(d) # don't visit CVS directories etc |
48 |
|
49 |
return target, source |
50 |
|
51 |
def DistTar(target, source, env): |
52 |
"""tar archive builder""" |
53 |
|
54 |
import tarfile |
55 |
|
56 |
env_dict = env.Dictionary() |
57 |
|
58 |
if env_dict.get("DISTTAR_FORMAT") in ["gz", "bz2"]: |
59 |
tar_format = env_dict["DISTTAR_FORMAT"] |
60 |
else: |
61 |
tar_format = "" |
62 |
|
63 |
# split the target directory, filename, and stuffix |
64 |
base_name = str(target[0]).split('.tar')[0] |
65 |
(target_dir, dir_name) = os.path.split(base_name) |
66 |
|
67 |
# create the target directory if it does not exist |
68 |
if target_dir and not os.path.exists(target_dir): |
69 |
os.makedirs(target_dir) |
70 |
|
71 |
# open our tar file for writing |
72 |
tar = tarfile.open(str(target[0]), "w:%s" % (tar_format,)) |
73 |
|
74 |
# write sources to our tar file |
75 |
for item in source: |
76 |
item = str(item) |
77 |
print "Adding to TAR file: %s/%s" % (dir_name,item) |
78 |
tar.add(item,'%s/%s' % (dir_name,item)) |
79 |
|
80 |
# all done |
81 |
print "Closing TAR file" |
82 |
tar.close() |
83 |
|
84 |
def DistTarSuffix(env, sources): |
85 |
"""tar archive suffix generator""" |
86 |
|
87 |
env_dict = env.Dictionary() |
88 |
if env_dict.has_key("DISTTAR_FORMAT") and env_dict["DISTTAR_FORMAT"] in ["gz", "bz2"]: |
89 |
return ".tar." + env_dict["DISTTAR_FORMAT"] |
90 |
else: |
91 |
return ".tar" |
92 |
|
93 |
def generate(env): |
94 |
""" |
95 |
Add builders and construction variables for the DistTar builder. |
96 |
""" |
97 |
env.Append(BUILDERS = { |
98 |
'DistTar': env.Builder( |
99 |
action = DistTar, |
100 |
suffix = DistTarSuffix, |
101 |
target_factory = env.fs.Entry, |
102 |
emitter = disttar_modify_sources |
103 |
), |
104 |
}) |
105 |
|
106 |
env.AppendUnique( |
107 |
DISTTAR_FORMAT = 'gz' |
108 |
, DISTTAR_EXCLUDEEXTS = ['.o','.os','.so','.a','.dll','.lib'] |
109 |
, DISTTAR_EXCLUDEDIRS = ['CVS','.svn','.sconf_temp'] |
110 |
) |
111 |
|
112 |
def exists(env): |
113 |
""" |
114 |
Make sure this tool exists. |
115 |
""" |
116 |
try: |
117 |
import os |
118 |
import tarfile |
119 |
except ImportError: |
120 |
return False |
121 |
else: |
122 |
return True |