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 | from SCons.Builder import Builder |
---|
22 | |
---|
23 | def disttar_emitter(target,source,env): |
---|
24 | |
---|
25 | source,origsource = [], source |
---|
26 | |
---|
27 | excludeexts = env.Dictionary().get('DISTTAR_EXCLUDEEXTS',[]) |
---|
28 | excludedirs = env.Dictionary().get('DISTTAR_EXCLUDEDIRS',['.sconsign']) |
---|
29 | |
---|
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 | #print "Adding source",relpath |
---|
46 | source.append(relpath) |
---|
47 | for d in excludedirs: |
---|
48 | if d in dirs: |
---|
49 | dirs.remove(d) # don't visit CVS directories etc |
---|
50 | |
---|
51 | return target, source |
---|
52 | |
---|
53 | def disttar_string(target, source, env): |
---|
54 | """This is what gets printed on the console. We'll strip out the list |
---|
55 | or source files, since it tends to get very long. If you want to see the |
---|
56 | contents, the easiest way is to uncomment the line 'Adding to TAR file' |
---|
57 | below. """ |
---|
58 | return 'DistTar(%s,...)' % str(target[0]) |
---|
59 | |
---|
60 | def disttar(target, source, env): |
---|
61 | """tar archive builder""" |
---|
62 | |
---|
63 | import tarfile |
---|
64 | |
---|
65 | env_dict = env.Dictionary() |
---|
66 | |
---|
67 | if env_dict.get("DISTTAR_FORMAT") in ["gz", "bz2"]: |
---|
68 | tar_format = env_dict["DISTTAR_FORMAT"] |
---|
69 | else: |
---|
70 | tar_format = "" |
---|
71 | |
---|
72 | # split the target directory, filename, and stuffix |
---|
73 | base_name = str(target[0]).split('.tar')[0] |
---|
74 | (target_dir, dir_name) = os.path.split(base_name) |
---|
75 | |
---|
76 | # create the target directory if it does not exist |
---|
77 | if target_dir and not os.path.exists(target_dir): |
---|
78 | os.makedirs(target_dir) |
---|
79 | |
---|
80 | # open our tar file for writing |
---|
81 | sys.stderr.write("DistTar: Writing "+str(target[0])) |
---|
82 | tar = tarfile.open(str(target[0]), "w:%s" % (tar_format,)) |
---|
83 | |
---|
84 | # write sources to our tar file |
---|
85 | for item in source: |
---|
86 | item = str(item) |
---|
87 | sys.stderr.write(".") |
---|
88 | #print "Adding to TAR file: %s/%s" % (dir_name,item) |
---|
89 | tar.add(item,'%s/%s' % (dir_name,item)) |
---|
90 | |
---|
91 | # all done |
---|
92 | sys.stderr.write("\n") #print "Closing TAR file" |
---|
93 | tar.close() |
---|
94 | |
---|
95 | def disttar_suffix(env, sources): |
---|
96 | """tar archive suffix generator""" |
---|
97 | |
---|
98 | env_dict = env.Dictionary() |
---|
99 | if env_dict.has_key("DISTTAR_FORMAT") \ |
---|
100 | and env_dict["DISTTAR_FORMAT"] in ["gz", "bz2"]: |
---|
101 | return ".tar." + env_dict["DISTTAR_FORMAT"] |
---|
102 | else: |
---|
103 | return ".tar" |
---|
104 | |
---|
105 | def generate(env): |
---|
106 | """ |
---|
107 | Add builders and construction variables for the DistTar builder. |
---|
108 | """ |
---|
109 | |
---|
110 | disttar_action=SCons.Action.Action(disttar, disttar_string) |
---|
111 | env['BUILDERS']['DistTar'] = Builder( |
---|
112 | action=disttar_action, |
---|
113 | emitter=disttar_emitter, |
---|
114 | suffix = disttar_suffix, |
---|
115 | target_factory = env.fs.Entry) |
---|
116 | |
---|
117 | env.AppendUnique( DISTTAR_FORMAT = 'gz') |
---|
118 | |
---|
119 | def exists(env): |
---|
120 | """ |
---|
121 | Make sure this tool exists. |
---|
122 | """ |
---|
123 | try: |
---|
124 | import os |
---|
125 | import tarfile |
---|
126 | except ImportError: |
---|
127 | return False |
---|
128 | else: |
---|
129 | return True |
---|