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 os_path_relpath
8 from waflib import Context
15 class Dist(Context.Context):
20 Context.g_module.dist()
22 class DistCheck(Scripting.DistCheck):
26 Options.options.distcheck_args = ''
27 if Context.g_module.distcheck is Scripting.distcheck:
29 Context.g_module.distcheck(self)
31 Context.g_module.distcheck()
32 Context.g_module.dist()
34 def get_arch_name(self):
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):
45 tinfo = tar.gettarinfo(name=abspath, arcname=fname)
46 tgt = os.readlink(abspath)
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]
73 # internal links don't get rewritten
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
87 def add_tarfile(tar, fname, abspath, basedir):
88 '''add a file to the tarball'''
89 if add_symlink(tar, fname, abspath, basedir):
92 tinfo = tar.gettarinfo(name=abspath, arcname=fname)
94 Logs.error('Unable to find file %s - missing from git checkout?' % abspath)
101 tar.addfile(tinfo, fileobj=fh)
105 def vcs_dir_contents(path):
106 """Return the versioned files under a path.
108 :return: List of paths relative to path
112 if os.path.isdir(os.path.join(repo, ".git")):
113 ls_files_cmd = [ 'git', 'ls-files', '--full-name',
114 os_path_relpath(path, repo) ]
116 env = dict(os.environ)
117 env["GIT_DIR"] = os.path.join(repo, ".git")
119 repo = os.path.dirname(repo)
121 raise Exception("unsupported or no vcs for %s" % path)
122 return 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:
131 abspath = os.path.join(srcdir, f)
134 f = f[len(srcsubdir)+1:]
136 # Remove files in the blacklist
140 # Remove directories in the blacklist
146 if os.path.isdir(abspath) and not os.path.islink(abspath):
149 f = dstsubdir + '/' + f
150 fname = dstdir + '/' + f
151 add_tarfile(tar, fname, abspath, srcsubdir)
154 def list_directory_files(path):
158 for root, dirs, files in os.walk(path):
160 out_files.append(os.path.join(root, f))
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
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))
177 Logs.error('You must use samba_dist.DIST_DIRS() to set which directories to package')
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')
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]
197 absdir = os.path.join(srcdir, dir)
199 files = vcs_dir_contents(absdir)
200 except Exception as e:
201 Logs.error('unable to get contents of %s: %s' % (absdir, e))
203 add_files_to_tarball(tar, srcdir, dir, dist_base, destdir, blacklist, files)
206 for file in dist_files.split():
207 if file.find(':') != -1:
208 destfile = file.split(':')[1]
209 file = file.split(':')[0]
213 absfile = os.path.join(srcdir, file)
215 if os.path.isdir(absfile) and not os.path.islink(absfile):
218 files = list_directory_files(dir)
219 add_files_to_tarball(tar, srcdir, dir, dist_base, destdir, blacklist, files)
221 fname = dist_base + '/' + destfile
222 add_tarfile(tar, fname, absfile, destfile)
226 if Options.options.SIGN_RELEASE:
229 os.unlink(dist_name + '.asc')
233 cmd = "gpg --detach-sign --armor " + dist_name
235 uncompressed_tar = open(dist_name, 'rb')
236 compressed_tar = gzip.open(dist_name + '.gz', 'wb')
238 buffer = uncompressed_tar.read(1048576)
240 compressed_tar.write(buffer)
243 uncompressed_tar.close()
244 compressed_tar.close()
246 Logs.info('Created %s.gz %s.asc' % (dist_name, dist_name))
247 dist_name = dist_name + '.gz'
249 Logs.info('Created %s' % dist_name)
251 # TODO use the ctx object instead
253 dist_archive = dist_name
259 '''set the directories to package, relative to top srcdir'''
265 def DIST_FILES(files, extend=False):
266 '''set additional files for packaging, relative to top srcdir'''
271 dist_files = dist_files + " " + files
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