1#!/usr/local/bin/python3.8
2############################################################################
3#
4# Copyright (C) 2016 The Qt Company Ltd.
5# Contact: https://www.qt.io/licensing/
6#
7# This file is part of Qt Creator.
8#
9# Commercial License Usage
10# Licensees holding valid commercial Qt licenses may use this file in
11# accordance with the commercial license agreement provided with the
12# Software or, alternatively, in accordance with the terms contained in
13# a written agreement between you and The Qt Company. For licensing terms
14# and conditions see https://www.qt.io/terms-conditions. For further
15# information use the contact form at https://www.qt.io/contact-us.
16#
17# GNU General Public License Usage
18# Alternatively, this file may be used under the terms of the GNU
19# General Public License version 3 as published by the Free Software
20# Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21# included in the packaging of this file. Please review the following
22# information to ensure the GNU General Public License requirements will
23# be met: https://www.gnu.org/licenses/gpl-3.0.html.
24#
25############################################################################
26
27import argparse
28import os
29import shutil
30import subprocess
31import tempfile
32
33def archive(repository, target_file, prefix='', crlf=False):
34    crlf_args = (['-c', 'core.autocrlf=true', '-c', 'core.eol=crlf'] if crlf
35                 else [])
36    subprocess.check_call(['git'] + crlf_args +
37                          ['archive', '--format=tar', '--prefix=' + prefix,
38                           '-o', target_file, 'HEAD'],
39                          cwd=repository)
40
41def extract_combined(archive_list, target_dir):
42    if not os.path.exists(target_dir):
43        os.makedirs(target_dir)
44    for a in archive_list:
45        subprocess.check_call(['tar', 'xf', a], cwd=target_dir)
46
47def createTarGz(source_dir, target_file):
48    (cwd, path) = os.path.split(source_dir)
49    subprocess.check_call(['tar', 'czf', target_file, path], cwd=cwd)
50
51def createTarXz(source_dir, target_file):
52    (cwd, path) = os.path.split(source_dir)
53    subprocess.check_call(['tar', 'cJf', target_file, path], cwd=cwd)
54
55def createZip(source_dir, target_file):
56    (cwd, path) = os.path.split(source_dir)
57    subprocess.check_call(['zip', '-9qr', target_file, path], cwd=cwd)
58
59def package_repos(repos, combined_prefix, target_file_base):
60    workdir = tempfile.mkdtemp(suffix="-createQtcSource")
61    def crlf_postfix(crlf):
62        return '_win' if crlf else ''
63    def tar_name(name, crlf):
64        sanitized_name = name.replace('/', '_').replace('\\', '_')
65        return os.path.join(workdir, sanitized_name + crlf_postfix(crlf) + '.tar')
66    def archive_path(crlf=False):
67        return os.path.join(workdir, 'src' + crlf_postfix(crlf), combined_prefix)
68    print('Working in "' + workdir + '"')
69    print('Pre-packaging archives...')
70    for (name, repo, prefix) in repos:
71        print('  ' + name + '...')
72        for crlf in [False, True]:
73            archive(repo, tar_name(name, crlf), prefix, crlf=crlf)
74    print('Preparing for packaging...')
75    for crlf in [False, True]:
76        archive_list = [tar_name(name, crlf) for (name, _, _) in repos]
77        extract_combined(archive_list, archive_path(crlf))
78    print('Creating .tar.gz...')
79    createTarGz(archive_path(crlf=False), target_file_base + '.tar.gz')
80    print('Creating .tar.xz...')
81    createTarXz(archive_path(crlf=False), target_file_base + '.tar.xz')
82    print('Creating .zip with CRLF...')
83    createZip(archive_path(crlf=True), target_file_base + '.zip')
84    print('Removing temporary directory...')
85    shutil.rmtree(workdir)
86
87def parse_arguments():
88    script_path = os.path.dirname(os.path.realpath(__file__))
89    qtcreator_repo = os.path.join(script_path, '..')
90    parser = argparse.ArgumentParser(description="Create Qt Creator source packages")
91    parser.add_argument('-p', default=qtcreator_repo, dest='repo', help='path to repository')
92    parser.add_argument('-n', default='', dest='name', help='name of plugin')
93    parser.add_argument('-s', action='append', default=[], dest='modules', help='submodule to add')
94    parser.add_argument('version', help='full version including tag, e.g. 4.2.0-beta1')
95    parser.add_argument('edition', help='(opensource | enterprise)')
96    return parser.parse_args()
97
98def main():
99    args = parse_arguments()
100    base_repo_name = args.name if args.name else "qtcreator"
101    if not args.name and not args.modules: # default Qt Creator repository
102        submodules = [os.path.join('src', 'shared', 'qbs'),
103                      os.path.join('src', 'tools', 'perfparser')]
104        args.modules = [path for path in submodules if os.path.exists(os.path.join(args.repo, path, '.git'))]
105    repos = [(base_repo_name, args.repo, '')]
106    for module in args.modules:
107        repos += [(module, os.path.join(args.repo, module), module + os.sep)]
108    name_part = '-' + args.name if args.name else ''
109    prefix = 'qt-creator-' + args.edition + name_part + '-src-' + args.version
110    package_repos(repos, prefix, os.path.join(os.getcwd(), prefix))
111
112if __name__ == "__main__":
113    main()
114