s4-waf: avoid having to run waf configure before waf dist
[metze/samba/wip.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, Scripting
5 from samba_utils import *
6
7 dist_dirs = None
8
9 def add_tarfile(tar, fname, abspath):
10     '''add a file to the tarball'''
11     tinfo = tar.gettarinfo(name=abspath, arcname=fname)
12     tinfo.uid   = 0
13     tinfo.gid   = 0
14     tinfo.uname = 'root'
15     tinfo.gname = 'root'
16     fh = open(abspath)
17     tar.addfile(tinfo, fileobj=fh)
18     fh.close()
19
20
21 def dist():
22     appname = Utils.g_module.APPNAME
23     version = Utils.g_module.VERSION
24
25     srcdir = os.path.normpath(os.path.join(os.path.dirname(Utils.g_module.root_path), Utils.g_module.srcdir))
26
27     if not dist_dirs:
28         print('You must use samba_dist.DIST_DIRS() to set which directories to package')
29         sys.exit(1)
30
31     dist_base = '%s-%s' % (appname, version)
32     dist_name = '%s.tar.gz' % (dist_base)
33
34     tar = tarfile.open(dist_name, 'w:gz')
35
36     for dir in dist_dirs.split():
37         if dir.find(':') != -1:
38             destdir=dir.split(':')[1]
39             dir=dir.split(':')[0]
40         else:
41             destdir = '.'
42         absdir = os.path.join(srcdir, dir)
43         git_cmd = [ 'git', 'ls-files', '--full-name', absdir ]
44         try:
45             files = Utils.cmd_output(git_cmd).split()
46         except:
47             print('git command failed: %s' % ' '.join(git_cmd))
48             sys.exit(1)
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(dirs):
65     '''set the directories to package, relative to top srcdir'''
66     global dist_dirs
67     if not dist_dirs:
68         dist_dirs = dirs
69
70 Scripting.dist = dist