summaryrefslogtreecommitdiff
path: root/baserockimport/exts/utils.py
blob: aadefdf255afb065966a95768e15cf2f8006c4c2 (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
# -*- coding: utf-8 -*-
#
# Copyright © 2014, 2015  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from __future__ import print_function

import json
import sys

def warn(*args, **kwargs):
    print('%s:' % sys.argv[0], *args, file=sys.stderr, **kwargs)

def error(*args, **kwargs):
    warn(*args, **kwargs)
    sys.exit(1)

def get_compression(url):
    ''' Given a tarball url, return its compression type '''

    bzip = 'bzip2'
    gzip = 'gzip'
    lzma = 'lzma'

    m = {'tar.gz': gzip, 'tgz': gzip, 'tar.Z': gzip,
           'tar.bz2': bzip, 'tbz2': bzip,
           'tar.lzma': lzma, 'tar.xz': lzma, 'tlz': lzma, 'txz': lzma}

    for x in [1, 2]:
        ext = '.'.join(url.split('.')[-x:])
        if ext in m: return m[ext]

    return None

# Assumption: url passed to this function must have a 'standard' tar extension
def str_tarball_lorry(extname, lorry_prefix, package_name, url):
    ''' Create a json lorry file '''

    name = '%s/%s' % (lorry_prefix, package_name)

    lorry = {'type': 'tarball',
             'url': url,
             'x-products-%s' % extname: [package_name]}

    compression = get_compression(url)
    if compression:
        lorry['compression'] = compression

    return json.dumps({name + "-tarball": lorry}, indent=4, sort_keys=True)

def str_repo_lorry(extname, lorry_prefix, package_name, repo_type, url):
    name = '%s/%s' % (lorry_prefix, package_name)

    lorry = {'type': repo_type,
             'url': url,
             'x-products-%s' % extname: [package_name]}

    if repo_type == 'svn':
        lorry['layout'] = 'standard'

    return json.dumps({name: lorry}, indent=4, sort_keys=True)