1 # customised version of 'waf dist' for Samba tools
2 # uses git ls-files to get file lists
4 import Utils, os, sys, tarfile, stat, Scripting, Logs
5 from samba_utils import *
9 def add_symlink(tar, fname, abspath, basedir):
10 '''handle symlinks to directories that may move during packaging'''
11 if not os.path.islink(abspath):
13 tinfo = tar.gettarinfo(name=abspath, arcname=fname)
14 tgt = os.readlink(abspath)
17 # we need to find the target relative to the main directory
18 # this is here to cope with symlinks into the buildtools
19 # directory from within the standalone libraries in Samba. For example,
20 # a symlink to ../../builtools/scripts/autogen-waf.sh needs
21 # to be rewritten as a symlink to buildtools/scripts/autogen-waf.sh
22 # when the tarball for talloc is built
24 # the filename without the appname-version
25 rel_fname = '/'.join(fname.split('/')[1:])
27 # join this with the symlink target
28 tgt_full = os.path.join(os.path.dirname(rel_fname), tgt)
30 # join with the base directory
31 tgt_base = os.path.normpath(os.path.join(basedir, tgt_full))
33 # see if this is inside one of our dist_dirs
34 for dir in dist_dirs.split():
35 if dir.find(':') != -1:
36 destdir=dir.split(':')[1]
41 # internal links don't get rewritten
43 if dir == tgt_base[0:len(dir)] and tgt_base[len(dir)] == '/':
44 new_tgt = destdir + tgt_base[len(dir):]
45 tinfo.linkname = new_tgt
55 def add_tarfile(tar, fname, abspath, basedir):
56 '''add a file to the tarball'''
57 if add_symlink(tar, fname, abspath, basedir):
60 tinfo = tar.gettarinfo(name=abspath, arcname=fname)
62 Logs.error('Unable to find file %s - missing from git checkout?' % abspath)
69 tar.addfile(tinfo, fileobj=fh)
73 def dist(appname='',version=''):
74 if not isinstance(appname, str) or not appname:
75 # this copes with a mismatch in the calling arguments for dist()
76 appname = Utils.g_module.APPNAME
77 version = Utils.g_module.VERSION
79 version = Utils.g_module.VERSION
81 srcdir = os.path.normpath(os.path.join(os.path.dirname(Utils.g_module.root_path), Utils.g_module.srcdir))
84 Logs.error('You must use samba_dist.DIST_DIRS() to set which directories to package')
87 dist_base = '%s-%s' % (appname, version)
88 dist_name = '%s.tar.gz' % (dist_base)
90 tar = tarfile.open(dist_name, 'w:gz')
92 for dir in dist_dirs.split():
93 if dir.find(':') != -1:
94 destdir=dir.split(':')[1]
98 absdir = os.path.join(srcdir, dir)
99 git_cmd = [ 'git', 'ls-files', '--full-name', absdir ]
101 files = Utils.cmd_output(git_cmd).split()
103 Logs.error('git command failed: %s' % ' '.join(git_cmd))
106 abspath = os.path.join(srcdir, f)
110 f = destdir + '/' + f
111 fname = dist_base + '/' + f
112 add_tarfile(tar, fname, abspath, dir)
116 Logs.info('Created %s' % dist_name)
122 '''set the directories to package, relative to top srcdir'''
127 Scripting.dist = dist