File: bundle_tool.py

package info (click to toggle)
java-imaging-utilities 0.14.3-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,556 kB
  • sloc: java: 31,233; python: 71; xml: 31; makefile: 26; sh: 5
file content (56 lines) | stat: -rw-r--r-- 1,747 bytes parent folder | download | duplicates (5)
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
'''
SCons tool for bundling files into the formats zip, tar.gz and tar.bz2.
'''

import os, tarfile, zipfile

def build_tar(target_fname, sources, prefix, mode):
    print 'creating tar file:', target_fname
    tar = tarfile.open(target_fname, mode=mode)
    for src in sources:
        path = str(src)
        tar.add(path, prefix + path)
    tar.close()

def build_zip(target_fname, sources, prefix):
    print 'creating zip file:', target_fname
    zip = zipfile.ZipFile(target_fname, 'w', zipfile.ZIP_DEFLATED)
    for src in sources:
        path = str(src)
        if os.path.isdir(path):
            # Add all files below dir
            for base, dirnames, filenames in os.walk(path):
                for fname in filenames:
                    fpath = os.path.join(base, fname)
                    zip.write(fpath, prefix + fpath)
        else:
            # Normal file
            zip.write(path, prefix + path)
    zip.close()

def build_bundle(target, source, env):
    if env.has_key('BUNDLE_PREFIX'):
        prefix = env['BUNDLE_PREFIX']
    else:
        prefix = ''
    for target_file in target:
        fname = str(target_file)
        if fname.endswith('.zip'):
            build_zip(fname, source, prefix)
        elif fname.endswith('.tar.gz'):
            build_tar(fname, source, prefix, 'w:gz')
        elif fname.endswith('.tar.bz2'):
            build_tar(fname, source, prefix, 'w:bz2')
        else:
            raise Exception('Unknown target file type: %s' % (fname,))
    return None

def print_null(*args):
    pass

def generate(env):
    env['PRINT_CMD_LINE_FUNC'] = print_null # silence printing of all sources
    env['BUILDERS']['Bundle'] = env.Builder(action=build_bundle)

def exists(env):
    return True