torture4: Replace direct netlogon ping calls with netlogon_pings()
[samba4-gss.git] / buildtools / wafsamba / samba_dist.py
blob0218cad62715f5b65641eddd5047553e6f3ab72d
1 # customised version of 'waf dist' for Samba tools
2 # uses git ls-files to get file lists
4 import os, sys, tarfile
5 from waflib import Utils, Scripting, Logs, Options
6 from waflib.Configure import conf
7 from samba_utils import get_string
8 from waflib import Context
10 dist_dirs = None
11 dist_files = None
12 dist_blacklist = ""
13 dist_archive = None
15 class Dist(Context.Context):
16 # TODO remove
17 cmd = 'dist'
18 fun = 'dist'
19 def execute(self):
20 Context.g_module.dist()
22 class DistCheck(Scripting.DistCheck):
23 fun = 'distcheck'
24 cmd = 'distcheck'
25 def execute(self):
26 Options.options.distcheck_args = ''
27 if Context.g_module.distcheck is Scripting.distcheck:
28 # default
29 Context.g_module.distcheck(self)
30 else:
31 Context.g_module.distcheck()
32 Context.g_module.dist()
33 self.check()
34 def get_arch_name(self):
35 global dist_archive
36 return dist_archive
37 def make_distcheck_cmd(self, tmpdir):
38 waf = os.path.abspath(sys.argv[0])
39 return [sys.executable, waf, 'configure', 'build', 'install', 'uninstall', '--destdir=' + tmpdir]
41 def add_symlink(tar, fname, abspath, basedir):
42 '''handle symlinks to directories that may move during packaging'''
43 if not os.path.islink(abspath):
44 return False
45 tinfo = tar.gettarinfo(name=abspath, arcname=fname)
46 tgt = os.readlink(abspath)
48 if dist_dirs:
49 # we need to find the target relative to the main directory
50 # this is here to cope with symlinks into the buildtools
51 # directory from within the standalone libraries in Samba. For example,
52 # a symlink to ../../builtools/scripts/autogen-waf.sh needs
53 # to be rewritten as a symlink to buildtools/scripts/autogen-waf.sh
54 # when the tarball for talloc is built
56 # the filename without the appname-version
57 rel_fname = '/'.join(fname.split('/')[1:])
59 # join this with the symlink target
60 tgt_full = os.path.join(os.path.dirname(rel_fname), tgt)
62 # join with the base directory
63 tgt_base = os.path.normpath(os.path.join(basedir, tgt_full))
65 # see if this is inside one of our dist_dirs
66 for dir in dist_dirs.split():
67 if dir.find(':') != -1:
68 destdir=dir.split(':')[1]
69 dir=dir.split(':')[0]
70 else:
71 destdir = '.'
72 if dir == basedir:
73 # internal links don't get rewritten
74 continue
75 if dir == tgt_base[0:len(dir)] and tgt_base[len(dir)] == '/':
76 new_tgt = destdir + tgt_base[len(dir):]
77 tinfo.linkname = new_tgt
78 break
80 tinfo.uid = 0
81 tinfo.gid = 0
82 tinfo.uname = 'root'
83 tinfo.gname = 'root'
84 tar.addfile(tinfo)
85 return True
87 def add_tarfile(tar, fname, abspath, basedir):
88 '''add a file to the tarball'''
89 if add_symlink(tar, fname, abspath, basedir):
90 return
91 try:
92 tinfo = tar.gettarinfo(name=abspath, arcname=fname)
93 except OSError:
94 Logs.error('Unable to find file %s - missing from git checkout?' % abspath)
95 sys.exit(1)
96 tinfo.uid = 0
97 tinfo.gid = 0
98 tinfo.uname = 'root'
99 tinfo.gname = 'root'
100 fh = open(abspath, "rb")
101 tar.addfile(tinfo, fileobj=fh)
102 fh.close()
105 def vcs_dir_contents(path):
106 """Return the versioned files under a path.
108 :return: List of paths relative to path
110 repo = path
111 while repo != "/":
112 if os.path.exists(os.path.join(repo, ".git")):
113 ls_files_cmd = [ 'git', 'ls-files', '--full-name',
114 os.path.relpath(path, repo) ]
115 cwd = None
116 env = dict(os.environ)
117 env["GIT_DIR"] = os.path.join(repo, ".git")
118 break
119 repo = os.path.dirname(repo)
120 if repo == "/":
121 raise Exception("unsupported or no vcs for %s" % path)
122 return get_string(Utils.cmd_output(ls_files_cmd, cwd=cwd, env=env)).split('\n')
125 def dist(appname='', version=''):
127 def add_files_to_tarball(tar, srcdir, srcsubdir, dstdir, dstsubdir, blacklist, files):
128 if blacklist is None:
129 blacklist = []
130 for f in files:
131 abspath = os.path.join(srcdir, f)
133 if srcsubdir != '.':
134 f = f[len(srcsubdir)+1:]
136 # Remove files in the blacklist
137 if f in blacklist:
138 continue
139 blacklisted = False
140 # Remove directories in the blacklist
141 for d in blacklist:
142 if f.startswith(d):
143 blacklisted = True
144 if blacklisted:
145 continue
146 if os.path.isdir(abspath) and not os.path.islink(abspath):
147 continue
148 if dstsubdir != '.':
149 f = dstsubdir + '/' + f
150 fname = dstdir + '/' + f
151 add_tarfile(tar, fname, abspath, srcsubdir)
154 def list_directory_files(path):
155 curdir = os.getcwd()
156 os.chdir(srcdir)
157 out_files = []
158 for root, dirs, files in os.walk(path):
159 for f in files:
160 out_files.append(os.path.join(root, f))
161 os.chdir(curdir)
162 return out_files
165 if not isinstance(appname, str) or not appname:
166 # this copes with a mismatch in the calling arguments for dist()
167 appname = Context.g_module.APPNAME
168 version = Context.g_module.VERSION
169 if not version:
170 version = Context.g_module.VERSION
172 srcdir = os.path.normpath(
173 os.path.join(os.path.dirname(Context.g_module.root_path),
174 Context.g_module.top))
176 if not dist_dirs:
177 Logs.error('You must use samba_dist.DIST_DIRS() to set which directories to package')
178 sys.exit(1)
180 dist_base = '%s-%s' % (appname, version)
182 if Options.options.SIGN_RELEASE:
183 dist_name = '%s.tar' % (dist_base)
184 tar = tarfile.open(dist_name, 'w')
185 else:
186 dist_name = '%s.tar.gz' % (dist_base)
187 tar = tarfile.open(dist_name, 'w:gz')
189 blacklist = dist_blacklist.split()
191 for dir in dist_dirs.split():
192 if dir.find(':') != -1:
193 destdir=dir.split(':')[1]
194 dir=dir.split(':')[0]
195 else:
196 destdir = '.'
197 absdir = os.path.join(srcdir, dir)
198 try:
199 files = vcs_dir_contents(absdir)
200 except Exception as e:
201 Logs.error('unable to get contents of %s: %s' % (absdir, e))
202 sys.exit(1)
203 add_files_to_tarball(tar, srcdir, dir, dist_base, destdir, blacklist, files)
205 if dist_files:
206 for file in dist_files.split():
207 if file.find(':') != -1:
208 destfile = file.split(':')[1]
209 file = file.split(':')[0]
210 else:
211 destfile = file
213 absfile = os.path.join(srcdir, file)
215 if os.path.isdir(absfile) and not os.path.islink(absfile):
216 destdir = destfile
217 dir = file
218 files = list_directory_files(dir)
219 add_files_to_tarball(tar, srcdir, dir, dist_base, destdir, blacklist, files)
220 else:
221 fname = dist_base + '/' + destfile
222 add_tarfile(tar, fname, absfile, destfile)
224 tar.close()
226 if Options.options.SIGN_RELEASE:
227 import gzip
228 try:
229 os.unlink(dist_name + '.asc')
230 except OSError:
231 pass
233 cmd = "gpg --detach-sign --armor " + dist_name
234 os.system(cmd)
235 uncompressed_tar = open(dist_name, 'rb')
236 compressed_tar = gzip.open(dist_name + '.gz', 'wb')
237 while 1:
238 buffer = uncompressed_tar.read(1048576)
239 if buffer:
240 compressed_tar.write(buffer)
241 else:
242 break
243 uncompressed_tar.close()
244 compressed_tar.close()
245 os.unlink(dist_name)
246 Logs.info('Created %s.gz %s.asc' % (dist_name, dist_name))
247 dist_name = dist_name + '.gz'
248 else:
249 Logs.info('Created %s' % dist_name)
251 # TODO use the ctx object instead
252 global dist_archive
253 dist_archive = dist_name
254 return dist_name
257 @conf
258 def DIST_DIRS(dirs):
259 '''set the directories to package, relative to top srcdir'''
260 global dist_dirs
261 if not dist_dirs:
262 dist_dirs = dirs
264 @conf
265 def DIST_FILES(files, extend=False):
266 '''set additional files for packaging, relative to top srcdir'''
267 global dist_files
268 if not dist_files:
269 dist_files = files
270 elif extend:
271 dist_files = dist_files + " " + files
273 @conf
274 def DIST_BLACKLIST(blacklist):
275 '''set the files to exclude from packaging, relative to top srcdir'''
276 global dist_blacklist
277 if not dist_blacklist:
278 dist_blacklist = blacklist
280 Scripting.dist = dist