1 # customised version of 'waf dist' for Samba tools
2 # uses git ls-files to get file lists
4 import Utils, os, sys, tarfile, gzip, stat, Scripting, Logs, Options
5 from samba_utils import *
10 def add_symlink(tar, fname, abspath, basedir):
11 '''handle symlinks to directories that may move during packaging'''
12 if not os.path.islink(abspath):
14 tinfo = tar.gettarinfo(name=abspath, arcname=fname)
15 tgt = os.readlink(abspath)
18 # we need to find the target relative to the main directory
19 # this is here to cope with symlinks into the buildtools
20 # directory from within the standalone libraries in Samba. For example,
21 # a symlink to ../../builtools/scripts/autogen-waf.sh needs
22 # to be rewritten as a symlink to buildtools/scripts/autogen-waf.sh
23 # when the tarball for talloc is built
25 # the filename without the appname-version
26 rel_fname = '/'.join(fname.split('/')[1:])
28 # join this with the symlink target
29 tgt_full = os.path.join(os.path.dirname(rel_fname), tgt)
31 # join with the base directory
32 tgt_base = os.path.normpath(os.path.join(basedir, tgt_full))
34 # see if this is inside one of our dist_dirs
35 for dir in dist_dirs.split():
36 if dir.find(':') != -1:
37 destdir=dir.split(':')[1]
42 # internal links don't get rewritten
44 if dir == tgt_base[0:len(dir)] and tgt_base[len(dir)] == '/':
45 new_tgt = destdir + tgt_base[len(dir):]
46 tinfo.linkname = new_tgt
56 def add_tarfile(tar, fname, abspath, basedir):
57 '''add a file to the tarball'''
58 if add_symlink(tar, fname, abspath, basedir):
61 tinfo = tar.gettarinfo(name=abspath, arcname=fname)
63 Logs.error('Unable to find file %s - missing from git checkout?' % abspath)
70 tar.addfile(tinfo, fileobj=fh)
74 def dist(appname='',version=''):
75 if not isinstance(appname, str) or not appname:
76 # this copes with a mismatch in the calling arguments for dist()
77 appname = Utils.g_module.APPNAME
78 version = Utils.g_module.VERSION
80 version = Utils.g_module.VERSION
82 srcdir = os.path.normpath(os.path.join(os.path.dirname(Utils.g_module.root_path), Utils.g_module.srcdir))
85 Logs.error('You must use samba_dist.DIST_DIRS() to set which directories to package')
88 dist_base = '%s-%s' % (appname, version)
90 if Options.options.SIGN_RELEASE:
91 dist_name = '%s.tar' % (dist_base)
92 tar = tarfile.open(dist_name, 'w')
94 dist_name = '%s.tar.gz' % (dist_base)
95 tar = tarfile.open(dist_name, 'w:gz')
97 blacklist = dist_blacklist.split()
99 for dir in dist_dirs.split():
100 if dir.find(':') != -1:
101 destdir=dir.split(':')[1]
102 dir=dir.split(':')[0]
105 absdir = os.path.join(srcdir, dir)
106 git_cmd = [ 'git', 'ls-files', '--full-name', absdir ]
108 files = Utils.cmd_output(git_cmd).split()
110 Logs.error('git command failed: %s' % ' '.join(git_cmd))
113 abspath = os.path.join(srcdir, f)
117 # Remove files in the blacklist
118 if f in dist_blacklist:
121 # Remove directories in the blacklist
128 f = destdir + '/' + f
129 fname = dist_base + '/' + f
130 add_tarfile(tar, fname, abspath, dir)
134 if Options.options.SIGN_RELEASE:
136 os.unlink(dist_name + '.asc')
140 cmd = "gpg --detach-sign --armor " + dist_name
142 uncompressed_tar = open(dist_name, 'rb')
143 compressed_tar = gzip.open(dist_name + '.gz', 'wb')
145 buffer = uncompressed_tar.read(1048576)
147 compressed_tar.write(buffer)
150 uncompressed_tar.close()
151 compressed_tar.close()
153 Logs.info('Created %s.gz %s.asc' % (dist_name, dist_name))
154 dist_name = dist_name + '.gz'
156 Logs.info('Created %s' % dist_name)
163 '''set the directories to package, relative to top srcdir'''
169 def DIST_BLACKLIST(blacklist):
170 '''set the directories to package, relative to top srcdir'''
171 global dist_blacklist
172 if not dist_blacklist:
173 dist_blacklist = blacklist
175 Scripting.dist = dist