s4-waf: added 'waf dist' to build the tarball
[samba.git] / buildtools / wafsamba / samba_dist.py
1 # customised version of 'waf dist' for Samba tools
2 # uses git ls-files to get file lists
3
4 import Utils, os, sys, tarfile, stat
5 from samba_utils import *
6
7 def add_tarfile(tar, fname, abspath):
8     '''add a file to the tarball'''
9     tinfo = tar.gettarinfo(name=abspath, arcname=fname)
10     tinfo.uid   = 0
11     tinfo.gid   = 0
12     tinfo.uname = 'root'
13     tinfo.gname = 'root'
14     fh = open(abspath)
15     tar.addfile(tinfo, fileobj=fh)
16     fh.close()
17
18
19 def dist(appname='', version=''):
20
21     if not appname: appname = Utils.g_module.APPNAME
22     if not version: version = Utils.g_module.VERSION
23
24     env = LOAD_ENVIRONMENT()
25     srcdir = os.path.normpath(os.path.join(os.path.dirname(Utils.g_module.root_path), Utils.g_module.srcdir))
26
27     if not env.DIST_DIRS:
28         print('You must use conf.DIST_DIRS() to set which directories to package')
29         sys.exit(1)
30
31     if not env.GIT:
32         print('You need git installed to run waf dist')
33         sys.exit(1)
34
35     dist_base = '%s-%s' % (appname, version)
36     dist_name = '%s.tar.gz' % (dist_base)
37
38     tar = tarfile.open(dist_name, 'w:gz')
39
40     for dir in env.DIST_DIRS.split():
41         if dir.find(':') != -1:
42             destdir=dir.split(':')[1]
43             dir=dir.split(':')[0]
44         else:
45             destdir = '.'
46         absdir = os.path.join(srcdir, dir)
47         git_cmd = [ env.GIT, 'ls-files', '--full-name', absdir ]
48         files = Utils.cmd_output(git_cmd).split()
49         for f in files:
50             abspath = os.path.join(srcdir, f)
51             if dir != '.':
52                 f = f[len(dir)+1:]
53             if destdir != '.':
54                 f = destdir + '/' + f
55             fname = dist_base + '/' + f
56             add_tarfile(tar, fname, abspath)
57
58     tar.close()
59
60     print('Created %s' % dist_name)
61
62
63 @conf
64 def DIST_DIRS(conf, dirs):
65     '''set the directories to package, relative to top srcdir'''
66     if not conf.env.DIST_DIRS:
67         conf.env.DIST_DIRS = dirs