summaryrefslogtreecommitdiff
path: root/buildtools/wafsamba/samba_dist.py
blob: fae2759dac0d6d6646c5c81117b591d943f3f8ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# customised version of 'waf dist' for Samba tools
# uses git ls-files to get file lists

import Utils, os, sys, tarfile, stat, Scripting, Logs, Options
from samba_utils import *

dist_dirs = None
dist_files = None
dist_blacklist = ""

def add_symlink(tar, fname, abspath, basedir):
    '''handle symlinks to directories that may move during packaging'''
    if not os.path.islink(abspath):
        return False
    tinfo = tar.gettarinfo(name=abspath, arcname=fname)
    tgt = os.readlink(abspath)

    if dist_dirs:
        # we need to find the target relative to the main directory
        # this is here to cope with symlinks into the buildtools
        # directory from within the standalone libraries in Samba. For example,
        # a symlink to ../../builtools/scripts/autogen-waf.sh needs
        # to be rewritten as a symlink to buildtools/scripts/autogen-waf.sh
        # when the tarball for talloc is built

        # the filename without the appname-version
        rel_fname = '/'.join(fname.split('/')[1:])

        # join this with the symlink target
        tgt_full = os.path.join(os.path.dirname(rel_fname), tgt)

        # join with the base directory
        tgt_base = os.path.normpath(os.path.join(basedir, tgt_full))

        # see if this is inside one of our dist_dirs
        for dir in dist_dirs.split():
            if dir.find(':') != -1:
                destdir=dir.split(':')[1]
                dir=dir.split(':')[0]
            else:
                destdir = '.'
            if dir == basedir:
                # internal links don't get rewritten
                continue
            if dir == tgt_base[0:len(dir)] and tgt_base[len(dir)] == '/':
                new_tgt = destdir + tgt_base[len(dir):]
                tinfo.linkname = new_tgt
                break

    tinfo.uid   = 0
    tinfo.gid   = 0
    tinfo.uname = 'root'
    tinfo.gname = 'root'
    tar.addfile(tinfo)
    return True

def add_tarfile(tar, fname, abspath, basedir):
    '''add a file to the tarball'''
    if add_symlink(tar, fname, abspath, basedir):
        return
    try:
        tinfo = tar.gettarinfo(name=abspath, arcname=fname)
    except OSError:
        Logs.error('Unable to find file %s - missing from git checkout?' % abspath)
        sys.exit(1)
    tinfo.uid   = 0
    tinfo.gid   = 0
    tinfo.uname = 'root'
    tinfo.gname = 'root'
    fh = open(abspath)
    tar.addfile(tinfo, fileobj=fh)
    fh.close()


def vcs_dir_contents(path):
    """Return the versioned files under a path.

    :return: List of paths relative to path
    """
    repo = path
    while repo != "/":
        if os.path.isdir(os.path.join(repo, ".git")):
            ls_files_cmd = [ 'git', 'ls-files', '--full-name',
                             os_path_relpath(path, repo) ]
            cwd = None
            env = dict(os.environ)
            env["GIT_DIR"] = os.path.join(repo, ".git")
            break
        elif os.path.isdir(os.path.join(repo, ".bzr")):
            ls_files_cmd = [ 'bzr', 'ls', '--recursive', '--versioned',
                             os_path_relpath(path, repo)]
            cwd = repo
            env = None
            break
        repo = os.path.dirname(repo)
    if repo == "/":
        raise Exception("unsupported or no vcs for %s" % path)
    return Utils.cmd_output(ls_files_cmd, cwd=cwd, env=env).split()


def dist(appname='', version=''):

    def add_files_to_tarball(tar, srcdir, srcsubdir, dstdir, dstsubdir, blacklist, files):
        if blacklist is None:
            blacklist = []
        for f in files:
            abspath = os.path.join(srcdir, f)

            if srcsubdir != '.':
                f = f[len(srcsubdir)+1:]

            # Remove files in the blacklist
            if f in blacklist:
                continue
            blacklisted = False
            # Remove directories in the blacklist
            for d in blacklist:
                if f.startswith(d):
                    blacklisted = True
            if blacklisted:
                continue
            if os.path.isdir(abspath):
                continue
            if dstsubdir != '.':
                f = dstsubdir + '/' + f
            fname = dstdir + '/' + f
            add_tarfile(tar, fname, abspath, srcsubdir)


    def list_directory_files(abspath):
        out_files = []
        for root, dirs, files in os.walk(abspath):
            for f in files:
                out_files.append(os.path.join(root, f))
        return out_files


    if not isinstance(appname, str) or not appname:
        # this copes with a mismatch in the calling arguments for dist()
        appname = Utils.g_module.APPNAME
        version = Utils.g_module.VERSION
    if not version:
        version = Utils.g_module.VERSION

    srcdir = os.path.normpath(os.path.join(os.path.dirname(Utils.g_module.root_path), Utils.g_module.srcdir))

    if not dist_dirs:
        Logs.error('You must use samba_dist.DIST_DIRS() to set which directories to package')
        sys.exit(1)

    dist_base = '%s-%s' % (appname, version)

    if Options.options.SIGN_RELEASE:
        dist_name = '%s.tar' % (dist_base)
        tar = tarfile.open(dist_name, 'w')
    else:
        dist_name = '%s.tar.gz' % (dist_base)
        tar = tarfile.open(dist_name, 'w:gz')

    blacklist = dist_blacklist.split()

    for dir in dist_dirs.split():
        if dir.find(':') != -1:
            destdir=dir.split(':')[1]
            dir=dir.split(':')[0]
        else:
            destdir = '.'
        absdir = os.path.join(srcdir, dir)
        try:
            files = vcs_dir_contents(absdir)
        except Exception, e:
            Logs.error('unable to get contents of %s: %s' % (absdir, e))
            sys.exit(1)
        add_files_to_tarball(tar, srcdir, dir, dist_base, destdir, blacklist, files)

    if dist_files:
        for file in dist_files.split():
            if file.find(':') != -1:
                destfile = file.split(':')[1]
                file = file.split(':')[0]
            else:
                destfile = file

            absfile = os.path.join(srcdir, file)

            if os.path.isdir(absfile):
                destdir = destfile
                dir = file
                files = list_directory_files(dir)
                add_files_to_tarball(tar, srcdir, dir, dist_base, destdir, blacklist, files)
            else:
                fname = dist_base + '/' + destfile
                add_tarfile(tar, fname, absfile, destfile)

    tar.close()

    if Options.options.SIGN_RELEASE:
        import gzip
        try:
            os.unlink(dist_name + '.asc')
        except OSError:
            pass

        cmd = "gpg --detach-sign --armor " + dist_name
        os.system(cmd)
        uncompressed_tar = open(dist_name, 'rb')
        compressed_tar = gzip.open(dist_name + '.gz', 'wb')
        while 1:
            buffer = uncompressed_tar.read(1048576)
            if buffer:
                compressed_tar.write(buffer)
            else:
                break
        uncompressed_tar.close()
        compressed_tar.close()
        os.unlink(dist_name)
        Logs.info('Created %s.gz %s.asc' % (dist_name, dist_name))
        dist_name = dist_name + '.gz'
    else:
        Logs.info('Created %s' % dist_name)

    return dist_name


@conf
def DIST_DIRS(dirs):
    '''set the directories to package, relative to top srcdir'''
    global dist_dirs
    if not dist_dirs:
        dist_dirs = dirs

@conf
def DIST_FILES(files, extend=False):
    '''set additional files for packaging, relative to top srcdir'''
    global dist_files
    if not dist_files:
        dist_files = files
    elif extend:
        dist_files = dist_files + " " + files

@conf
def DIST_BLACKLIST(blacklist):
    '''set the files to exclude from packaging, relative to top srcdir'''
    global dist_blacklist
    if not dist_blacklist:
        dist_blacklist = blacklist

Scripting.dist = dist