summaryrefslogtreecommitdiff
path: root/git-clone-all
blob: c9eede9654ecfadf46a4c024ef9b829c20ee7e4e (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
#!/usr/bin/python3

############################################################################
#   Copyright © 2015-2016 José Manuel Santamaría Lema <panfaust@gmail.com> #
#   Copyright © 2015 Philip Muskovac <yofel@gmx.net>                       #
#                                                                          #
#   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; either version 2 of the License, or      #
#   (at your option) any later version.                                    #
############################################################################

import os
import sys
import argparse
import subprocess
import time
import datetime

from libka.ka_configuration import *
from libka.kde_ftp import *
from libka.ka_data_utils import *
from libka.ka_print import *

#Take note of the start time to find out the time elapsed when the program ends
time_start = time.time()

ka_config = getConfigParser()

default_branch = ka_config['git-clone-all']['default-branch']
git_subdirectory = ka_config['areas']['git-subdirectory']
skip_list = ka_config['skip']['git-clone-all']

#Argument parser
parser = argparse.ArgumentParser(
    description="Clones all the qt, frameworks, plasma or apps git repositories"
    " from Launchpad to the current directory")
parser.add_argument("-r", "--releasetype",
    help="KDE Release Type [frameworks,plasma,applications] "
    "if not specified downloads all the git repositories",
    required=False)
parser.add_argument("-b", "--branch",
    help="Checkout the given branch for the newly created clones, "
    "if not specified defaults to %s" % ka_config['git-clone-all']['default-branch'],
    default=default_branch,
    required=False)
parser.add_argument("-s", "--single-repo",
    help="Clone only the repository given.",
    required=False)
parser.add_argument("-t", "--tmpdir",
    help="Temporary dir where the packages are prepared",
    required=False)

#Check arguments
args = parser.parse_args()

releaseTypes = []

if args.releasetype != None:
    if args.releasetype not in ["qt", "frameworks", "plasma", "applications"]:
        print("Invalid releasetype %s" % args.releasetype)
        print("Accepted release types are qt, frameworks, plasma, applications")
        sys.exit(1)
    else:
        releaseTypes.append(args.releasetype)
else:
    if args.single_repo:
        releaseTypes = ["other"]
    else:
        releaseTypes = ["frameworks", "plasma", "applications"]

workdir = args.tmpdir if args.tmpdir else os.getcwd()

#Find out the base url to clone and the extra remotes
clone_this_one = ka_config['git-clone-all']['clone-this-one']
template_url_clone = ka_config['git-clone-all'][clone_this_one]
clone_this_one_qt = ka_config['git-clone-all']['clone-this-one-qt']
template_url_clone_qt = ka_config['git-clone-all'][clone_this_one_qt]
print("Template url to clone: %s" % template_url_clone)
print("Template url for Qt: %s" % template_url_clone_qt)
remotes_list = ka_config['git-clone-all']['extra-remotes'].split(',')

#Load the repository map list
git_repo_map = {}
dist_name = clone_this_one.split('-')[2]
git_repo_map[dist_name] = readJsonDataFile(
    os.path.join("name-conversions","kde-tarball-to-distro-git",dist_name+".json") )
for remote in remotes_list:
    remote_name = remote.split('-')[2]
    git_repo_map[remote_name] = readJsonDataFile(
        os.path.join("name-conversions","kde-tarball-to-distro-git",remote_name+".json") )

#Initialize list
packages_not_cloned = []

#Change to temporary work directory
if args.tmpdir:
    os.chdir(args.tmpdir)

#Find out the repositories to clone.
repositories_to_clone = {}
n = 0
if args.single_repo:
    repositories_to_clone["other"] = [args.single_repo]
    n = 1
else:
    for releaseType in releaseTypes:
        ftp_list = getFtpVersionMap(releaseType).keys()
        repositories_to_clone[releaseType] = ftp_list
        n += len(ftp_list)

#Clone the repositories and add the extra remotes
i = 0
for releaseType in releaseTypes:
    #Get package list
    packages = sorted(repositories_to_clone[releaseType])
    for package in packages:
        i += 1
        ka_print_good_stuff("\n ==== Kloning package %s (%s of %s) ===" % (package,i,n))
        #Skip this package if it's in the configuration skip list
        if package in skip_list:
            ka_print_info("This clone was skipped as requested by the configuration files.")
            continue
        #Find out the git repository name
        if package in git_repo_map[dist_name]:
            repo_name = git_repo_map[dist_name][package]
        else:
            repo_name = package
        #Handle the name if it's a qt package
        if releaseType == "qt":
            repo_name = repo_name.split("-opensource-src")[0]
            final_url = template_url_clone_qt % {'component': releaseType, 'repo': repo_name}
        else:
            final_url = template_url_clone % {'component': releaseType, 'repo': repo_name}
        if git_subdirectory == "":
            clone_dir = "."
        else:
            clone_dir = git_subdirectory
        command = "git clone --branch %s %s %s" % (args.branch,final_url,clone_dir)
        try:
            old_cwd = os.getcwd()
            if not os.path.isdir(package):
                os.mkdir(package)
            os.chdir(package)
            print("CWD="+os.getcwd())
            print("Executing: " + command)
            subprocess.check_output(command.split())
            os.chdir(old_cwd)
        except KeyboardInterrupt:
            print("abort by user request")
            sys.exit(130)
        except:
            #In any other exception we continue, this way we can use
            #git-clone-all against a directory with some repositories already cloned.
            os.chdir(old_cwd)
            if not os.path.exists(workdir + '/' + package + '/' + git_subdirectory + '/.git'):
                packages_not_cloned.append(package)
                continue
            else:
                print("Skipping clone, apparently it was already cloned, trying to update remotes...")
        for remote in remotes_list:
            template_url_remote = ka_config['git-clone-all'][remote]
            remote_name = remote.split('-')[-1]
            #Remove previous remotes if they are already there
            try:
                command = "git remote rm " + remote_name
                old_cwd = os.getcwd()
                os.chdir(package + '/' + git_subdirectory)
                print("CWD="+os.getcwd())
                print("Executing: " + command)
                subprocess.check_call(command.split())
                subprocess.check_call(command.split())
                os.chdir(old_cwd)
            except:
                os.chdir(old_cwd)
                pass
            #Add remotes
            try:
                old_cwd = os.getcwd()
                os.chdir(package + '/' + git_subdirectory)
                remote_repo_name = package
                if package in git_repo_map[remote_name]:
                    remote_repo_name = git_repo_map[remote_name][package]
                command = "git remote add " + remote_name + " " + (template_url_remote % {'component': releaseType, 'repo': remote_repo_name})
                print("CWD="+os.getcwd())
                print("Executing: " + command)
                subprocess.check_call(command.split())
                os.chdir(old_cwd)
            except TypeError:
                print("The remote %s couldn't be added using the template url:\n%s" % (remote_name,template_url_remote))

print("\n === Summary ===")
if not packages_not_cloned:
    ka_print_good_stuff("All packages were cloned successfully")
else:
    ka_print_error("The following packages couldn't be cloned:")
    for p in packages_not_cloned:
       ka_print_error(p)

time_elapsed = time.time() - time_start
time_elapsed_string = str(datetime.timedelta(seconds=time_elapsed)).split('.')[0]
ka_print_good_stuff("Time elapsed = %s" % time_elapsed_string)

if packages_not_cloned:
    sys.exit(1)

# vim: expandtab ts=4