aboutsummaryrefslogtreecommitdiff
path: root/bin/WIP/taler-local
blob: 82107d45faae47dcb6626c5dd689863a4b8cea90 (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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env python3

# This file is part of GNU Taler.
#
# GNU Taler 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 3 of the License, or
# (at your option) any later version.
#
# GNU Taler 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 GNU Taler.  If not, see <https://www.gnu.org/licenses/>.

import click
import types
import os
import sys
import os.path
import subprocess
import time
import random
from os import listdir
from os.path import isdir, join
from pathlib import Path
from dataclasses import dataclass
from typing import List, Callable
from shutil import copy
from taler_urls import get_urls
from string import ascii_letters, ascii_uppercase

@dataclass
class Repo:
    name: str
    url: str
    deps: List[str]
    builder: Callable[["Repo", Path], None]

@click.group()
def cli():
    pass

def split_repos_list(repos):
    return [repo for repo in repos.split(",") if repo != ""]

def update_checkout(r: Repo, p: Path):
    """Clean the repository's working directory and
    update it to the match the latest version of the upstream branch
    that we are tracking."""
    subprocess.run(["git", "-C", str(p), "clean", "-fdx"], check=True)
    subprocess.run(["git", "-C", str(p), "fetch"], check=True)
    subprocess.run(["git", "-C", str(p), "reset"], check=True)
    res = subprocess.run(
        [
            "git",
            "-C",
            str(p),
            "rev-parse",
            "--abbrev-ref",
            "--symbolic-full-name",
            "@{u}",
        ],
        stderr=subprocess.DEVNULL,
        stdout=subprocess.PIPE,
        encoding="utf-8",
    )
    if res.returncode != 0:
        ref = "HEAD"
    else:
        ref = res.stdout.strip("\n ")
    print(f"resetting {r.name} to ref {ref}")
    subprocess.run(["git", "-C", str(p), "reset", "--hard", ref], check=True)


def default_configure(*extra):
    pfx = Path.home() / ".local"
    extra_list = list(extra)
    subprocess.run(["./configure", f"--prefix={pfx}"] + extra_list, check=True)

def pyconfigure(*extra):
    """For python programs, --prefix doesn't work."""
    subprocess.run(["./configure"] + list(extra), check=True)

def build_libeufin(r: Repo, p: Path):
    update_checkout(r, p)
    subprocess.run(["./bootstrap"], check=True)
    default_configure()
    subprocess.run(["make", "install"], check=True)
    (p / "taler-buildstamp").touch()

def build_libmicrohttpd(r: Repo, p: Path):
    update_checkout(r, p)
    subprocess.run(["./bootstrap"], check=True)
    # Debian gnutls packages are too old ...
    default_configure("--with-gnutls=/usr/local")
    subprocess.run(["make"], check=True)
    subprocess.run(["make", "install"], check=True)
    (p / "taler-buildstamp").touch()

def build_gnunet(r: Repo, p: Path):
    update_checkout(r, p)
    subprocess.run(["./bootstrap"], check=True)
    pfx = Path.home() / ".local"
    default_configure(
        "--enable-logging=verbose",
        f"--with-microhttpd={pfx}",
        "--disable-documentation",
    )
    subprocess.run(["make", "install"], check=True)
    (p / "taler-buildstamp").touch()

def build_exchange(r: Repo, p: Path):
    update_checkout(r, p)
    subprocess.run(["./bootstrap"], check=True)
    pfx = Path.home() / ".local"
    default_configure(
        "CFLAGS=-ggdb -O0",
        "--enable-logging=verbose",
        f"--with-microhttpd={pfx}",
        f"--with-gnunet={pfx}",
    )
    subprocess.run(["make", "install"], check=True)
    (p / "taler-buildstamp").touch()

def build_wallet(r, p):
    update_checkout(r, p)
    subprocess.run(["./bootstrap"], check=True)
    default_configure()
    subprocess.run(["make", "install"], check=True)
    (p / "taler-buildstamp").touch()

def build_twister(r, p):
    update_checkout(r, p)
    subprocess.run(["./bootstrap"], check=True)
    pfx = Path.home() / ".local"
    default_configure(
        "CFLAGS=-ggdb -O0",
        "--enable-logging=verbose",
        f"--with-exchange={pfx}",
        f"--with-gnunet={pfx}",
    )
    subprocess.run(["make", "install"], check=True)
    (p / "taler-buildstamp").touch()


def build_merchant(r, p):
    update_checkout(r, p)
    subprocess.run(["./bootstrap"], check=True)
    pfx = Path.home() / ".local"
    default_configure(
        "CFLAGS=-ggdb -O0",
        "--enable-logging=verbose",
        f"--with-microhttpd={pfx}",
        f"--with-exchange={pfx}",
        f"--with-gnunet={pfx}",
        "--disable-doc",
    )
    subprocess.run(["make", "install"], check=True)
    (p / "taler-buildstamp").touch()

def build_sync(r, p):
    update_checkout(r, p)
    subprocess.run(["./bootstrap"], check=True)
    pfx = Path.home() / ".local"
    default_configure(
        "CFLAGS=-ggdb -O0",
        "--enable-logging=verbose",
        f"--with-microhttpd={pfx}",
        f"--with-exchange={pfx}",
        f"--with-merchant={pfx}",
        f"--with-gnunet={pfx}",
        "--disable-doc",
    )
    subprocess.run(["make", "install"], check=True)
    (p / "taler-buildstamp").touch()


def build_anastasis(r, p):
    update_checkout(r, p)
    subprocess.run(["./bootstrap"], check=True)
    pfx = Path.home() / ".local"
    default_configure(
        "CFLAGS=-ggdb -O0",
        "--enable-logging=verbose",
        f"--with-microhttpd={pfx}",
        f"--with-exchange={pfx}",
        f"--with-merchant={pfx}",
        f"--with-gnunet={pfx}",
        "--disable-doc",
    )
    subprocess.run(["make", "install"], check=True)
    (p / "taler-buildstamp").touch()


def build_demos(r, p):
    update_checkout(r, p)
    pfx = Path.home() / ".local"
    pyconfigure()
    subprocess.run(["make", "install"], check=True)
    (p / "taler-buildstamp").touch()

def build_backoffice(r, p):
    update_checkout(r, p)
    subprocess.run(["./bootstrap"])
    subprocess.run(["./configure"])
    subprocess.run(["make", "build-single"])
    (p / "taler-buildstamp").touch()

repos = {
    "libmicrohttpd": Repo(
        "libmicrohttpd",
        "git://git.gnunet.org/libmicrohttpd.git",
        [],
        build_libmicrohttpd,
    ),
    "gnunet": Repo(
        "gnunet",
        "git://git.gnunet.org/gnunet.git",
        ["libmicrohttpd"],
        build_gnunet
    ),
    "exchange": Repo(
        "exchange",
        "git://git.taler.net/exchange",
        ["gnunet", "libmicrohttpd"],
        build_exchange,
    ),
    "merchant": Repo(
        "merchant",
        "git://git.taler.net/merchant",
        ["exchange","libmicrohttpd","gnunet"],
        build_merchant,
    ),
    "sync": Repo(
       "sync",
       "git://git.taler.net/sync",
       ["exchange",
        "merchant",
        "gnunet",
        "libmicrohttpd"],
       build_sync,
   ),
    "anastasis": Repo(
       "anastasis",
       "git://git.taler.net/anastasis",
       ["exchange",
        "merchant",
        "libmicrohttpd",
        "gnunet"],
       build_anastasis,
    ),
    "wallet-core": Repo(
        "wallet-core",
        "git://git.taler.net/wallet-core",
        [],
        build_wallet,
    ),
    "libeufin": Repo(
        "libeufin",
        "git://git.taler.net/libeufin.git",
        [],
        build_libeufin,
    ),
    "taler-merchant-demos": Repo(
        "taler-merchant-demos",
        "git://git.taler.net/taler-merchant-demos",
        [],
        build_demos,
    ),
    "twister": Repo(
        "twister",
        "git://git.taler.net/twister",
        ["gnunet", "libmicrohttpd"],
        build_twister,
    ),
}

def get_repos_names() -> List[str]:
    r_dir = Path.home() / ".taler-sources"
    return [el for el in listdir(r_dir) if isdir(join(r_dir, el)) and repos.get(el)]

# Get the installed repositories from the sources directory.
def load_repos(reposNames) -> List[Repo]:
    return [repos.get(r) for r in reposNames if repos.get(r)]

def update_repos(repos: List[Repo]) -> None:
    for r in repos:
        r_dir = Path.home() / ".taler-sources" / r.name
        subprocess.run(["git", "-C", str(r_dir), "fetch"], check=True)
        res = subprocess.run(
            ["git", "-C", str(r_dir), "status", "-sb"],
            check=True,
            stdout=subprocess.PIPE,
            encoding="utf-8",
        )
        if "behind" in res.stdout:
            print(f"new commits in {r}")
            s = r_dir / "taler-buildstamp"
            if s.exists():
                s.unlink()

def get_stale_repos(repos: List[Repo]) -> List[Repo]:
    timestamps = {}
    stale = []
    for r in repos:
        r_dir = Path.home() / ".taler-sources" / r.name
        s = r_dir / "taler-buildstamp"
        if not s.exists():
            timestamps[r.name] = time.time()
            stale.append(r)
        ts = timestamps[r.name] = s.stat().st_mtime
        for dep in r.deps:
            if timestamps[dep] > ts:
                stale.append(r)
                break
    return stale

@cli.command()
@click.option(
    "--without-repos", metavar="WITHOUT REPOS",
    help="WITHOUT REPOS is a unspaced and comma-separated list \
of the repositories to _exclude_ from compilation",
    default="")
def build(without_repos) -> None:
    """Build the deployment from source."""
    exclude = split_repos_list(without_repos)
    # Get the repositories names from the source directory
    repos_names = get_repos_names()
    # Reorder the list of repositories so that the
    # most fundamental dependecies appear left-most.
    repos_keys = repos.keys() # Has the precedence rules.
    sorted_repos = sorted(
        set(repos_keys).intersection(repos_names),
        key=lambda x: list(repos_keys).index(x)
    )
    target_repos = load_repos(sorted_repos) # Get Repo objects
    update_repos(target_repos)
    stale = get_stale_repos(target_repos)
    print(f"found stale repos: {[r.name for r in stale]}")
    for r in stale:
        if r.name in exclude:
            print(f"not building: {r.name}")
            continue
        # Warn, if a dependency is not being built:
        diff = set(r.deps) - set(repos_names)
        if len(diff) > 0:
            print(f"WARNING: those dependencies are not being built: {diff}")
        p = Path.home() / ".taler-sources" / r.name
        os.chdir(str(p))
        r.builder(r, p)

# Download the repository.
def checkout_repos(repos: List[Repo]):
    if len(repos) == 0:
        print("No repositories can be checked out.  Spelled correctly?")
        return
    home = Path.home()
    sources = home / ".taler-sources"
    for r in repos:
        r_dir = home / ".taler-sources" / r.name
        if not r_dir.exists():
            r_dir.mkdir(parents=True, exist_ok=True)
            subprocess.run(["git", "-C", str(sources), "clone", r.url], check=True)

@cli.command()
@click.option(
    "--repos", "-r",
    metavar="REPOS",
    help="REPOS is a unspaced and comma-separated list of the repositories to clone.",
    default="libmicrohttpd,gnunet,exchange,merchant,wallet-core,taler-merchant-demos,sync,anastasis,libeufin",
    show_default=True,
)
def bootstrap(repos) -> None:

    """Clone all the specified repositories."""

    home = Path.home()
    reposList = split_repos_list(repos)    
    checkout_repos(load_repos(reposList))

if __name__ == "__main__":
    cli()