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

Contents of /trunk/scons/accumulate.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1622 - (show annotations) (download) (as text)
Mon Sep 10 13:19:45 2007 UTC (15 years, 2 months ago) by jpye
File MIME type: text/x-python
File size: 4199 byte(s)
Added initial code for 'accumulate builder' based on SCons Wiki code.
1 # AccumulateBuilder
2 #
3 # Based on code from http://www.scons.org/wiki/AccumulateBuilder
4 #
5 # Converted to SCons 'tool' format by John Pye, 10 Sept 2007.
6
7 import os, os.path, shutil
8
9 def packagetree(src,dest, symlinks=False):
10 """Recursive copy of files listed in a local file named PACKAGE
11
12 Look in the current directory for a file named PACKAGE. If found,
13 copy the files listed there.
14
15 Then recurse into subdirectories.
16
17 Behaviour is intended to facilitate packaging of a user-contributed
18 code library, where many files are 'experimental' but some files are
19 tested and intended to distribution. Suitable for software plugins,
20 code examples, documentation fragments, etc.
21 """
22
23 files = os.listdir(src)
24 if 'PACKAGE' in files:
25 plistfile = os.path.join(src,'PACKAGE')
26 plist = file(plistfile)
27 pfiles = []
28 for line in plist:
29 l = line.strip()
30 if l[0]=="#":
31 continue
32 if l in files:
33 print "Package '%s'" % l
34 pfiles.append(l)
35
36
37 def copytree(src, dest, symlinks=False):
38 """My own copyTree which does not fail if the directory exists.
39
40 Recursively copy a directory tree using copy2().
41
42 If the optional symlinks flag is true, symbolic links in the
43 source tree result in symbolic links in the destination tree; if
44 it is false, the contents of the files pointed to by symbolic
45 links are copied.
46
47 Behavior is meant to be identical to GNU 'cp -R'.
48 """
49 def copyItems(src, dest, symlinks=False):
50 """Function that does all the work.
51
52 It is necessary to handle the two 'cp' cases:
53 - destination does exist
54 - destination does not exist
55
56 See 'cp -R' documentation for more details
57 """
58 files = os.listdir(src)
59 if 'PACKAGE' in files:
60 use_package_list = True
61
62 if use_package_list:
63 plistfile = os.path.join(src,'PACKAGE')
64 plist = file(plistfile)
65 for line in plist:
66 if line[0]=="#":
67 continue
68 pfiles
69 ...
70
71 for item in files:
72 srcPath = os.path.join(src, item)
73 if os.path.isdir(srcPath):
74 srcBasename = os.path.basename(srcPath)
75 destDirPath = os.path.join(dest, srcBasename)
76 if not os.path.exists(destDirPath):
77 os.makedirs(destDirPath)
78 copyItems(srcPath, destDirPath
79 , use_package_list=use_package_list
80 )
81 elif os.path.islink(item) and symlinks:
82 linkto = os.readlink(item)
83 os.symlink(linkto, dest)
84 else:
85 shutil.copy2(srcPath, dest)
86
87 # case 'cp -R src/ dest/' where dest/ already exists
88 if os.path.exists(dest):
89 destPath = os.path.join(dest, os.path.basename(src))
90 if not os.path.exists(destPath):
91 os.makedirs(destPath)
92 # case 'cp -R src/ dest/' where dest/ does not exist
93 else:
94 os.makedirs(dest)
95 destPath = dest
96 # actually copy the files
97 copyItems(src, destPath)
98
99
100 ##
101 ## AccumulatorAction.py
102 ##
103
104 def accumulatorFunction(target, source, env):
105 """Function called when builder is called"""
106 destDir = str(target[0])
107 if not os.path.exists(destDir):
108 os.makedirs(destDir)
109 for s in source:
110 s = str(s)
111 if os.path.isdir(s):
112 myShutil.copytree(s, destDir, symlinks = False)
113 else:
114 shutil.copy2(s, destDir)
115
116
117 ##
118 ## Zipper.py
119 ##
120 import distutils.archive_util
121
122 def zipperFunction(target, source, env):
123 """Function to use as an action which creates a ZIP file from the arguments"""
124 targetName = str(target[0])
125 sourceDir = str(source[0])
126 distutils.archive_util.make_archive(targetName, 'zip', sourceDir)
127
128
129 ##
130 ## register the above builders...
131 ##
132
133 def generate(env):
134
135 # add builder to accumulate files
136 accuBuilder = env.Builder(action=AccumulatorAction.accumulatorFunction,
137 source_factory=SCons.Node.FS.default_fs.Entry,
138 target_factory=SCons.Node.FS.default_fs.Entry,
139 multi=1)
140 env['BUILDERS']['Accumulate'] = accuBuilder
141
142 # add builder to zip files
143 zipBuilder = env.Builder(action=Zipper.zipperFunction,
144 source_factory=SCons.Node.FS.default_fs.Entry,
145 target_factory=SCons.Node.FS.default_fs.Entry,
146 multi=0)
147 env['BUILDERS']['Zipper'] = zipBuilder
148
149 def exists(env):
150 """
151 Make sure this tool exists.
152 """
153 try:
154 import distutils.archive_util
155 import shutil
156 import os, os.path
157 except ImportError:
158 return False
159 else:
160 return True
161

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