/[ascend]/trunk/scons/disttar.py
ViewVC logotype

Contents of /trunk/scons/disttar.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2651 - (show annotations) (download) (as text)
Thu Dec 13 07:29:48 2012 UTC (6 years ago) by jpye
File MIME type: text/x-python
File size: 4033 byte(s)
Fixing GPL header, removing postal address (rpmlint incorrect-fsf-address)
1 # DistTarBuilder: tool to generate tar files using SCons
2 # Copyright (C) 2005, 2006 Matthew A. Nicholson
3 # Copyright (C) 2006-2010 John Pye
4 #
5 # This file is free software; you can redistribute it and/or
6 # 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 # This file is distributed in the hope that it will be useful,
10 # 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 # 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
17 import os,sys
18 from SCons.Script import *
19 import re
20
21 def disttar_emitter(target,source,env):
22
23 source,origsource = [], source
24
25 excludeexts = env.Dictionary().get('DISTTAR_EXCLUDEEXTS',[])
26 excludedirs = env.Dictionary().get('DISTTAR_EXCLUDEDIRS',[])
27 re1 = env.Dictionary().get('DISTTAR_EXCLUDERES',[])
28 excluderes = [re.compile(r) for r in re1]
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 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 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 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 """tar archive builder"""
70
71 import tarfile
72
73 env_dict = env.Dictionary()
74
75 if env_dict.get("DISTTAR_FORMAT") in ["gz", "bz2"]:
76 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 sys.stderr.write("DistTar: Writing "+str(target[0]))
90 tar = tarfile.open(str(target[0]), "w:%s" % (tar_format,))
91
92 # write sources to our tar file
93 for item in source:
94 item = str(item)
95 sys.stderr.write(".")
96 #print "Adding to TAR file: %s/%s" % (dir_name,item)
97 tar.add(item,'%s/%s' % (dir_name,item))
98
99 # all done
100 sys.stderr.write("\n") #print "Closing TAR file"
101 tar.close()
102
103 def disttar_suffix(env, sources):
104 """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 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 env.AppendUnique(
126 DISTTAR_FORMAT = 'gz'
127 )
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
141 # vim:set ts=4 sw=4 noexpandtab:
142

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22