aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/pull.py
blob: f797ffd77ca21e5b39fe0dd1e08c110650d6234a (plain) (blame)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python3

import contextlib
from enum import Enum
import logging
import os
import os.path
import shutil
import socket
import sys
import subprocess

env = os.environ.copy()
env['GIT_SSH_COMMAND'] = 'ssh -oStrictHostKeyChecking=no -oBatchMode=yes'

CGIT_CLONE_USER = 'egor'
CGIT_CLONE_HOST = 'tensin-ext1.home'
CGIT_CLONE_IP = '127.0.0.1'

REPOS_DIR = 'repos'

DEFAULT_OWNER = 'Egor Tensin'
DEFAULT_GITHUB_USER = 'egor-tensin'
DEFAULT_BITBUCKET_USER = 'egor-tensin'


def set_up_logging():
    logging.basicConfig(
        level=logging.DEBUG,
        datefmt='%Y-%m-%d %H:%M:%S',
        format='%(asctime)s | %(levelname)s | %(message)s')


def make_dir(rel_path):
    script_path = os.path.abspath(__file__)
    script_dir = os.path.dirname(script_path)
    abs_path = os.path.join(script_dir, rel_path)
    os.makedirs(abs_path, exist_ok=True)
    return abs_path


@contextlib.contextmanager
def chdir(new_cwd):
    old_cwd = os.getcwd()
    os.chdir(new_cwd)
    try:
        yield
    finally:
        os.chdir(old_cwd)


def extract_repo_name(repo_id):
    return os.path.basename(repo_id)


def check_output(*args, stdout=subprocess.PIPE):
    result = subprocess.run(args, stdout=stdout, stderr=subprocess.STDOUT,
                            env=env, encoding='utf-8')
    try:
        result.check_returncode()
        if result.stdout is None:
            logging.debug('%s', args)
        else:
            logging.debug('%s\n%s', args, result.stdout)
        return result.returncode == 0, result.stdout
    except subprocess.CalledProcessError as e:
        logging.error('%s\n%s', e, e.output)
        return e.returncode == 0, e.output


def run(*args, discard_output=False):
    if discard_output:
        success, _ = check_output(*args, stdout=subprocess.DEVNULL)
    else:
        success, _ = check_output(*args)
    return success


class RepoVerdict(Enum):
    SHOULD_MIRROR = 1
    SHOULD_UPDATE = 2
    CANT_DECIDE = 3


class Repo:
    def __init__(self, repo_id, clone_url, owner=None, desc=None,
                 homepage=None):
        self.repo_id = repo_id
        self.repo_name = extract_repo_name(repo_id)
        self.repo_dir = os.path.join(REPOS_DIR, self.repo_id)
        self.clone_url = clone_url
        if owner is None:
            owner = DEFAULT_OWNER
        self.owner = owner
        if desc is None:
            if homepage is not None:
                desc = homepage
            elif clone_url is not None:
                desc = clone_url
            else:
                desc = self.repo_name
        self.desc = desc
        self.homepage = homepage

    def write_cgitrc(self):
        with open(self.get_cgitrc_path(), 'w') as fd:
            self.write_cgitrc_field(fd, 'clone-url', self.build_cgitrc_clone_url())
            self.write_cgitrc_field(fd, 'owner', self.owner)
            self.write_cgitrc_field(fd, 'desc', self.desc)
            self.write_cgitrc_field(fd, 'homepage', self.homepage)

    def build_cgitrc_clone_url(self):
        clone_urls = []
        if self.clone_url is not None:
            clone_urls.append(self.clone_url)
        clone_urls.append(self.build_cgit_clone_url())
        clone_urls = ' '.join(clone_urls)
        return clone_urls

    def write_cgitrc_field(self, fd, field, value):
        if value is None:
            return
        fd.write(f'{field}={value}\n')

    def build_cgit_clone_url(self):
        return f'http://{CGIT_CLONE_USER}@{CGIT_CLONE_IP}:8080/git/{self.repo_id}'

    def get_cgitrc_path(self):
        return os.path.join(self.repo_dir, 'cgitrc')

    def pull(self):
        success = False
        verdict = self.judge()
        if verdict is RepoVerdict.SHOULD_MIRROR:
            success = self.mirror()
        elif verdict is RepoVerdict.SHOULD_UPDATE:
            success = self.update()
        elif verdict is RepoVerdict.CANT_DECIDE:
            success = False
        else:
            raise NotImplementedError(f'Unknown repository verdict: {verdict}')
        if success:
            self.write_cgitrc()
        return success

    def judge(self):
        if not os.path.isdir(self.repo_dir):
            return RepoVerdict.SHOULD_MIRROR
        with chdir(self.repo_dir):
            if not run('git', 'rev-parse', '--is-inside-work-tree', discard_output=True):
                # What is this directory?
                return RepoVerdict.SHOULD_MIRROR
            success, output = check_output('git', 'config', '--get', 'remote.origin.url')
            if not success:
                # Every repository managed by this script should have the
                # 'origin' remote. If it doesn't, it's trash.
                return RepoVerdict.SHOULD_MIRROR
            if f'{self.clone_url}\n' != output:
                logging.warning("Existing repository '%s' URL doesn't match" \
                                " the specified clone URL: %s", self.repo_id,
                                self.clone_url)
                return RepoVerdict.CANT_DECIDE
            # Looks like a legit clone of the specified remote.
            return RepoVerdict.SHOULD_UPDATE

    def mirror(self):
        logging.info("Mirroring repository '%s' from: %s", self.repo_id,
                     self.clone_url)
        if os.path.isdir(self.repo_dir):
            try:
                shutil.rmtree(self.repo_dir)
            except Exception as e:
                logging.exception(e)
                return False
        return run('git', 'clone', '--mirror', self.clone_url, self.repo_dir)

    def update(self):
        logging.info("Updating repository '%s'", self.repo_id)
        with chdir(self.repo_dir):
            return run('git', 'remote', 'update', '--prune')


class GithubRepo(Repo):
    def __init__(self, repo_id, clone_url=None, owner=None, desc=None,
                 homepage=None, github_user=DEFAULT_GITHUB_USER):
        if clone_url is None:
            clone_url = self.build_clone_url(github_user, repo_id)
        if homepage is None:
            homepage = self.build_homepage_url(github_user, repo_id)
        super().__init__(repo_id, clone_url, owner=owner, desc=desc,
                         homepage=homepage)

    @staticmethod
    def build_clone_url(user, repo_id):
        name = extract_repo_name(repo_id)
        return f'ssh://git@github.com/{user}/{name}.git'

    @staticmethod
    def build_homepage_url(user, repo_id):
        name = extract_repo_name(repo_id)
        return f'https://github.com/egor-tensin/{name}'


class BitbucketRepo(Repo):
    def __init__(self, repo_id, clone_url=None, owner=None, desc=None,
                 homepage=None, bitbucket_user=DEFAULT_BITBUCKET_USER):
        if clone_url is None:
            clone_url = self.build_clone_url(bitbucket_user, repo_id)
        if homepage is None:
            homepage = self.build_homepage_url(bitbucket_user, repo_id)
        super().__init__(repo_id, clone_url, owner=owner, desc=desc,
                         homepage=homepage)

    @staticmethod
    def build_clone_url(user, repo_id):
        name = extract_repo_name(repo_id)
        return f'ssh://git@bitbucket.org/{user}/{name}.git'

    @staticmethod
    def build_homepage_url(user, repo_id):
        name = extract_repo_name(repo_id)
        return f'https://bitbucket.org/egor-tensin/{name.lower()}'


repos = (
    GithubRepo('personal/aes-tools'),
    GithubRepo('personal/blog'),
    GithubRepo('personal/chess-games'),
    GithubRepo('personal/cmake-common'),
    GithubRepo('personal/config-links'),
    GithubRepo('personal/cv'),
    GithubRepo('personal/egor-tensin.github.io'),
    GithubRepo('personal/filters'),
    GithubRepo('personal/linux-home'),
    GithubRepo('personal/linux-status'),
    GithubRepo('personal/notes'),
    GithubRepo('personal/pdb-repo'),
    GithubRepo('personal/privilege-check'),
    GithubRepo('personal/simple-interpreter'),
    GithubRepo('personal/sorting-algorithms'),
    GithubRepo('personal/vk-scripts'),
    GithubRepo('personal/windows-env'),
    GithubRepo('personal/windows-home'),
    GithubRepo('personal/windows-tmp'),
    GithubRepo('personal/windows7-drivers'),
    GithubRepo('personal/writable-dirs'),

    BitbucketRepo('etc/etc-tensin-laptop1'),
    BitbucketRepo('etc/etc-tensin-laptop2'),
    BitbucketRepo('etc/etc-tensin-pc1'),
    BitbucketRepo('etc/etc-tensin-raspi1'),
    BitbucketRepo('etc/etc-tensin-raspi2'),
    BitbucketRepo('fr24/fr24-cover-letter'),
    BitbucketRepo('fr24/fr24-home'),
    BitbucketRepo('fr24/fr24-tmp'),
    BitbucketRepo('netwrix/etc-wiki'),
    BitbucketRepo('netwrix/netwrix-copyright'),
    BitbucketRepo('netwrix/netwrix-lab'),
    BitbucketRepo('netwrix/netwrix-logs'),
    #BitbucketRepo('netwrix/netwrix-webapi'),
    BitbucketRepo('netwrix/netwrix-xml'),
    BitbucketRepo('netwrix/netwrix.sh'),
    BitbucketRepo('netwrix/wiki-backup'),
    BitbucketRepo('shadow'),
    BitbucketRepo('staging/361_Tensin_E_D_report'),
    BitbucketRepo('staging/361_Tensin_E_D_slides'),
    BitbucketRepo('staging/461_Tensin_E_D_report'),
    BitbucketRepo('staging/461_Tensin_E_D_slides'),
    BitbucketRepo('staging/cgit-repos'),
    BitbucketRepo('staging/deposit-calculator'),
    BitbucketRepo('staging/raspi-temp-client'),
    BitbucketRepo('staging/raspi-temp-server'),
    BitbucketRepo('staging/x64-decoder'),

    Repo('fr24/key_mgmt', 'ssh://egor@tensin-raspi2/~/tmp/key_mgmt.git'),
    Repo('fr24/openfortivpn', 'ssh://egor@tensin-raspi2/~/tmp/openfortivpn.git'),
)


def main():
    set_up_logging()
    try:
        global REPOS_DIR
        REPOS_DIR = make_dir(REPOS_DIR)
        global CGIT_CLONE_IP
        CGIT_CLONE_IP = socket.gethostbyname(CGIT_CLONE_HOST)
        success = True
        for repo in repos:
            if not repo.pull():
                success = False
        if success:
            logging.info('All repositories were updated successfully')
            return 0
        else:
            logging.warning("Some repositories couldn't be updated!")
            return 1
    except Exception as e:
        logging.exception(e)
        raise


if __name__ == '__main__':
    sys.exit(main())