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
, Options
5 from samba_utils
import *
11 def add_symlink(tar
, fname
, abspath
, basedir
):
12 '''handle symlinks to directories that may move during packaging'''
13 if not os
.path
.islink(abspath
):
15 tinfo
= tar
.gettarinfo(name
=abspath
, arcname
=fname
)
16 tgt
= os
.readlink(abspath
)
19 # we need to find the target relative to the main directory
20 # this is here to cope with symlinks into the buildtools
21 # directory from within the standalone libraries in Samba. For example,
22 # a symlink to ../../builtools/scripts/autogen-waf.sh needs
23 # to be rewritten as a symlink to buildtools/scripts/autogen-waf.sh
24 # when the tarball for talloc is built
26 # the filename without the appname-version
27 rel_fname
= '/'.join(fname
.split('/')[1:])
29 # join this with the symlink target
30 tgt_full
= os
.path
.join(os
.path
.dirname(rel_fname
), tgt
)
32 # join with the base directory
33 tgt_base
= os
.path
.normpath(os
.path
.join(basedir
, tgt_full
))
35 # see if this is inside one of our dist_dirs
36 for dir in dist_dirs
.split():
37 if dir.find(':') != -1:
38 destdir
=dir.split(':')[1]
43 # internal links don't get rewritten
45 if dir == tgt_base
[0:len(dir)] and tgt_base
[len(dir)] == '/':
46 new_tgt
= destdir
+ tgt_base
[len(dir):]
47 tinfo
.linkname
= new_tgt
57 def add_tarfile(tar
, fname
, abspath
, basedir
):
58 '''add a file to the tarball'''
59 if add_symlink(tar
, fname
, abspath
, basedir
):
62 tinfo
= tar
.gettarinfo(name
=abspath
, arcname
=fname
)
64 Logs
.error('Unable to find file %s - missing from git checkout?' % abspath
)
71 tar
.addfile(tinfo
, fileobj
=fh
)
75 def vcs_dir_contents(path
):
76 """Return the versioned files under a path.
78 :return: List of paths relative to path
82 if os
.path
.isdir(os
.path
.join(repo
, ".git")):
83 ls_files_cmd
= [ 'git', 'ls-files', '--full-name',
84 os_path_relpath(path
, repo
) ]
86 env
= dict(os
.environ
)
87 env
["GIT_DIR"] = os
.path
.join(repo
, ".git")
89 repo
= os
.path
.dirname(repo
)
91 raise Exception("unsupported or no vcs for %s" % path
)
92 return Utils
.cmd_output(ls_files_cmd
, cwd
=cwd
, env
=env
).split()
95 def dist(appname
='', version
=''):
97 def add_files_to_tarball(tar
, srcdir
, srcsubdir
, dstdir
, dstsubdir
, blacklist
, files
):
101 abspath
= os
.path
.join(srcdir
, f
)
104 f
= f
[len(srcsubdir
)+1:]
106 # Remove files in the blacklist
110 # Remove directories in the blacklist
116 if os
.path
.isdir(abspath
):
119 f
= dstsubdir
+ '/' + f
120 fname
= dstdir
+ '/' + f
121 add_tarfile(tar
, fname
, abspath
, srcsubdir
)
124 def list_directory_files(path
):
128 for root
, dirs
, files
in os
.walk(path
):
130 out_files
.append(os
.path
.join(root
, f
))
135 if not isinstance(appname
, str) or not appname
:
136 # this copes with a mismatch in the calling arguments for dist()
137 appname
= Utils
.g_module
.APPNAME
138 version
= Utils
.g_module
.VERSION
140 version
= Utils
.g_module
.VERSION
142 srcdir
= os
.path
.normpath(os
.path
.join(os
.path
.dirname(Utils
.g_module
.root_path
), Utils
.g_module
.srcdir
))
145 Logs
.error('You must use samba_dist.DIST_DIRS() to set which directories to package')
148 dist_base
= '%s-%s' % (appname
, version
)
150 if Options
.options
.SIGN_RELEASE
:
151 dist_name
= '%s.tar' % (dist_base
)
152 tar
= tarfile
.open(dist_name
, 'w')
154 dist_name
= '%s.tar.gz' % (dist_base
)
155 tar
= tarfile
.open(dist_name
, 'w:gz')
157 blacklist
= dist_blacklist
.split()
159 for dir in dist_dirs
.split():
160 if dir.find(':') != -1:
161 destdir
=dir.split(':')[1]
162 dir=dir.split(':')[0]
165 absdir
= os
.path
.join(srcdir
, dir)
167 files
= vcs_dir_contents(absdir
)
169 Logs
.error('unable to get contents of %s: %s' % (absdir
, e
))
171 add_files_to_tarball(tar
, srcdir
, dir, dist_base
, destdir
, blacklist
, files
)
174 for file in dist_files
.split():
175 if file.find(':') != -1:
176 destfile
= file.split(':')[1]
177 file = file.split(':')[0]
181 absfile
= os
.path
.join(srcdir
, file)
183 if os
.path
.isdir(absfile
):
186 files
= list_directory_files(dir)
187 add_files_to_tarball(tar
, srcdir
, dir, dist_base
, destdir
, blacklist
, files
)
189 fname
= dist_base
+ '/' + destfile
190 add_tarfile(tar
, fname
, absfile
, destfile
)
194 if Options
.options
.SIGN_RELEASE
:
197 os
.unlink(dist_name
+ '.asc')
201 cmd
= "gpg --detach-sign --armor " + dist_name
203 uncompressed_tar
= open(dist_name
, 'rb')
204 compressed_tar
= gzip
.open(dist_name
+ '.gz', 'wb')
206 buffer = uncompressed_tar
.read(1048576)
208 compressed_tar
.write(buffer)
211 uncompressed_tar
.close()
212 compressed_tar
.close()
214 Logs
.info('Created %s.gz %s.asc' % (dist_name
, dist_name
))
215 dist_name
= dist_name
+ '.gz'
217 Logs
.info('Created %s' % dist_name
)
224 '''set the directories to package, relative to top srcdir'''
230 def DIST_FILES(files
, extend
=False):
231 '''set additional files for packaging, relative to top srcdir'''
236 dist_files
= dist_files
+ " " + files
239 def DIST_BLACKLIST(blacklist
):
240 '''set the files to exclude from packaging, relative to top srcdir'''
241 global dist_blacklist
242 if not dist_blacklist
:
243 dist_blacklist
= blacklist
245 Scripting
.dist
= dist