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