s4-waf: Fix 'waf dist' app name.
[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(appname='',version=''):
22     if not appname:
23         # this copes with a mismatch in the calling arguments for dist()
24         appname = Utils.g_module.APPNAME
25         version = Utils.g_module.VERSION
26     if not version:
27         version = Utils.g_module.VERSION
28
29     srcdir = os.path.normpath(os.path.join(os.path.dirname(Utils.g_module.root_path), Utils.g_module.srcdir))
30
31     if not dist_dirs:
32         print('You must use samba_dist.DIST_DIRS() to set which directories to package')
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 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 = [ 'git', 'ls-files', '--full-name', absdir ]
48         try:
49             files = Utils.cmd_output(git_cmd).split()
50         except:
51             print('git command failed: %s' % ' '.join(git_cmd))
52             sys.exit(1)
53         for f in files:
54             abspath = os.path.join(srcdir, f)
55             if dir != '.':
56                 f = f[len(dir)+1:]
57             if destdir != '.':
58                 f = destdir + '/' + f
59             fname = dist_base + '/' + f
60             add_tarfile(tar, fname, abspath)
61
62     tar.close()
63
64     print('Created %s' % dist_name)
65     return dist_name
66
67
68 @conf
69 def DIST_DIRS(dirs):
70     '''set the directories to package, relative to top srcdir'''
71     global dist_dirs
72     if not dist_dirs:
73         dist_dirs = dirs
74
75 Scripting.dist = dist