This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
lib.py
751 lines (653 loc) · 28.3 KB
/
lib.py
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
from pathlib import Path
import logging
import tempfile
import shutil
import tarfile
import signal
import re
import os
import stat
import platform
import subprocess
import pickle
from datetime import datetime
from typing import Iterable, Union, List, Tuple, Optional, Dict
from tempfile import TemporaryDirectory
import networkx as nx # type: ignore
import requests
from tqdm import tqdm # type: ignore
import toml
import yaml
from git import Repo, InvalidGitRepositoryError, GitCommandError # type: ignore
from mathlibtools.delayed_interrupt import DelayedInterrupt
from mathlibtools.auth_github import auth_github, Github
log = logging.getLogger("Mathlib tools")
log.setLevel(logging.INFO)
if (log.hasHandlers()):
log.handlers.clear()
log.addHandler(logging.StreamHandler())
class InvalidLeanProject(Exception):
pass
class InvalidMathlibProject(Exception):
"""A mathlib project is a Lean project depending on mathlib"""
pass
class LeanDownloadError(Exception):
pass
class LeanDirtyRepo(Exception):
pass
class InvalidLeanVersion(Exception):
pass
def nightly_url(rev: str, proj_repo: Optional[Repo] = None) -> str:
"""From a git rev, try to find an asset name and url, using the Github
authentication provided in auth."""
auth = auth_github(proj_repo) if proj_repo else Github()
repo = auth.get_repo("leanprover-community/mathlib-nightly")
tags = {tag.name: tag.commit.sha for tag in repo.get_tags()}
try:
release = next(r for r in repo.get_releases()
if r.tag_name.startswith('nightly-') and
tags[r.tag_name] == rev)
except StopIteration:
raise LeanDownloadError('Error: no nightly archive found')
try:
asset = next(x for x in release.get_assets()
if x.name.startswith('mathlib-olean-nightly-'))
except StopIteration:
raise LeanDownloadError("Error: Release " + release.tag_name +
" does not contains a olean archive (this shouldn't happen...)")
return asset.browser_download_url
DOT_MATHLIB = Path.home()/'.mathlib'
AZURE_URL = 'https://oleanstorage.azureedge.net/mathlib/'
DOT_MATHLIB.mkdir(parents=True, exist_ok=True)
DOWNLOAD_URL_FILE = DOT_MATHLIB/'url'
MATHLIB_URL = 'https://github.com/leanprover-community/mathlib.git'
LEAN_VERSION_RE = re.compile(r'(.*)\t.*refs/heads/lean-(.*)')
VersionTuple = Tuple[int, int, int]
def mathlib_lean_version() -> VersionTuple:
"""Return the latest Lean release supported by mathlib"""
resp = requests.get("https://raw.githubusercontent.com/leanprover-community/mathlib/master/leanpkg.toml")
assert resp.status_code == 200
conf = toml.loads(resp.text)
return parse_version(conf['package']['lean_version'])
def set_download_url(url: str = AZURE_URL) -> None:
"""Store the download url in .mathlib."""
DOWNLOAD_URL_FILE.write_text(url)
def get_download_url() -> str:
"""Get the download url from .mathlib."""
return DOWNLOAD_URL_FILE.read_text().strip('/\n')+'/'
if not DOWNLOAD_URL_FILE.exists():
set_download_url()
def pack(root: Path, srcs: Iterable[Path], target: Path) -> None:
"""Creates, as target, a tar.bz2 archive containing all paths from src,
relative to the folder root"""
try:
target.unlink()
except FileNotFoundError:
pass
cur_dir = Path.cwd()
with DelayedInterrupt([signal.SIGTERM, signal.SIGINT]):
os.chdir(str(root))
ar = tarfile.open(str(target), 'w|bz2')
for src in srcs:
ar.add(str(src.relative_to(root)))
ar.close()
os.chdir(str(cur_dir))
def unpack_archive(fname: Union[str, Path], tgt_dir: Union[str, Path]) -> None:
"""Unpack archive. This is needed for python < 3.7."""
shutil.unpack_archive(str(fname), str(tgt_dir))
def download(url: str, target: Path) -> None:
"""Download from url into target"""
log.info('Trying to download {} to {}'.format(url, target))
try:
req = requests.get(url, stream=True)
req.raise_for_status()
except ConnectionError:
raise LeanDownloadError("Can't connect to " + url)
except requests.HTTPError:
raise LeanDownloadError('Failed to download ' + url)
total_size = int(req.headers.get('content-length', 0))
BLOCK_SIZE = 1024
progress = tqdm(total=total_size, unit='iB', unit_scale=True)
with target.open('wb') as tgt:
for data in req.iter_content(BLOCK_SIZE):
progress.update(len(data))
tgt.write(data)
progress.close()
if total_size != 0 and progress.n != total_size:
raise LeanDownloadError('Failed to download ' + url)
def get_mathlib_archive(rev: str, url:str = '', force: bool = False,
repo: Optional[Repo] = None) -> Path:
"""Download a mathlib archive for revision rev into .mathlib
Return the archive Path. Will raise LeanDownloadError if nothing works.
"""
# we check for xz archives first
fnames = [rev + '.tar.xz', rev + '.tar.gz', rev + '.tar.bz2']
paths = [DOT_MATHLIB/fname for fname in fnames]
if not force:
log.info('Looking for local mathlib oleans')
for path in paths:
if path.exists():
log.info('Found local mathlib oleans')
return path
log.info('Looking for remote mathlib oleans')
for fname, path in zip(fnames, paths):
try:
base_url = url or get_download_url()
download(base_url+fname, path)
log.info('Found mathlib oleans at '+base_url)
return path
except LeanDownloadError:
pass
log.info('Looking for GitHub mathlib oleans')
# nightlies will only store gz archives
path = DOT_MATHLIB / (rev + '.tar.gz')
download(nightly_url(rev, repo), path)
log.info('Found GitHub mathlib oleans')
return path
def parse_version(version: str) -> VersionTuple:
"""Turn a lean version string into a tuple of integers or raise
InvalidLeanVersion"""
#Something that could be in a branch name or modern leanpkg.toml
m = re.match(r'.*lean[-:](.*)\.(.*)\.(.*).*', version)
if m:
return (int(m.group(1)), int(m.group(2)), int(m.group(3)))
# The output of `lean -- version`
m = re.match(r'.*version (.*)\.(.*)\.(.*),.*', version)
if m:
return (int(m.group(1)), int(m.group(2)), int(m.group(3)))
# Only a version string
m = re.match(r'(.*)\.(.*)\.(.*)', version)
if m:
return (int(m.group(1)), int(m.group(2)), int(m.group(3)))
raise InvalidLeanVersion(version)
def lean_version_toml(version: VersionTuple) -> str:
"""Turn a Lean version tuple into the string expected in leanpkg.toml."""
ver_str = '{:d}.{:d}.{:d}'.format(*version)
if version < (3, 5, 0):
return ver_str
else:
return 'leanprover-community/lean:' + ver_str
def clean(dir: Path) -> None:
log.info('cleaning {} ...'.format(str(dir)))
for path in dir.glob('**/*.olean'):
path.unlink()
def delete_zombies(dir: Path) -> None:
for path in dir.glob('**/*.olean'):
if not path.with_suffix('.lean').exists():
log.info('deleting zombie {} ...'.format(str(path)))
path.unlink()
def check_core_timestamps(toolchain: str) -> bool:
"""Check that oleans are more recent than their source in core lib"""
toolchain_path = Path.home()/'.elan'/'toolchains'/toolchain
try:
return all(p.stat().st_mtime < p.with_suffix('.olean').stat().st_mtime
for p in toolchain_path.glob('**/*.lean'))
except FileNotFoundError:
return False
def touch_oleans(path: Path) -> None:
"""Set modification time for oleans in path and its subfolders to now"""
now = datetime.now().timestamp()
for p in path.glob('**/*.olean'):
os.utime(str(p), (now, now))
class ImportGraph(nx.DiGraph):
def __init__(self, base_path: Optional[Path] = None) -> None:
"""A Lean project import graph."""
super().__init__(self)
self.base_path = base_path or Path('.')
def to_dot(self, path: Optional[Path] = None) -> None:
"""Writes itself to a graphviz dot file."""
path = path or self.base_path/'import_graph.dot'
nx.drawing.nx_pydot.to_pydot(self).write_dot(str(path))
def to_gexf(self, path: Optional[Path] = None) -> None:
"""Writes itself to a gexf dot file, suitable for Gephi."""
path = path or self.base_path/'import_graph.gexf'
nx.write_gexf(self, str(path))
def to_graphml(self, path: Optional[Path] = None) -> None:
"""Writes itself to a gexf dot file, suitable for yEd."""
path = path or self.base_path/'import_graph.graphml'
nx.write_graphml(self, str(path))
def write(self, path: Path):
if path.suffix == '.dot':
self.to_dot(path)
elif path.suffix == '.gexf':
self.to_gexf(path)
elif path.suffix == '.graphml':
self.to_graphml(path)
elif path.suffix in ['.pdf', '.svg', '.png']:
dot_format = '-T' + path.suffix[1:]
with tempfile.TemporaryDirectory() as tmpdirname:
tmpf = Path(tmpdirname)/'tmp.dot'
self.to_dot(tmpf)
with path.open('w') as outf:
subprocess.run(['dot', dot_format, str(tmpf)],
stdout=outf)
else:
raise ValueError('Unsupported graph output format. '
'Use .dot, .gexf, .graphml or a valid '
'graphviz output format (eg. .pdf).')
def ancestors(self, node: str) -> 'ImportGraph':
"""Returns the subgraph leading to node."""
H = self.subgraph(nx.ancestors(self, node).union([node]))
H.base_path = self.base_path
return H
def descendants(self, node: str) -> 'ImportGraph':
"""Returns the subgraph descending from node."""
H = self.subgraph(nx.descendants(self, node).union([node]))
H.base_path = self.base_path
return H
def path(self, start: str, end: str) -> 'ImportGraph':
"""Returns the subgraph descending from the start node and used by the
end node."""
D = self.descendants(start)
A = self.ancestors(end)
H = self.subgraph(set(D.nodes).intersection(A.nodes))
H.base_path = self.base_path
return H
class DeclInfo:
def __init__(self, origin: str, filepath: Path, line: int):
"""Implementation information for a declaration.
The origin argument is meant to be either 'core' or a project name,
including mathlib."""
self.origin = origin
self.filepath = filepath
self.line = line
def __repr__(self) -> str:
return "DeclInfo('{}', '{}', {})".format(self.origin, self.filepath, self.line)
class LeanProject:
def __init__(self, repo: Repo, is_dirty: bool, rev: str, directory: Path,
pkg_config: dict, deps: dict,
cache_url: str = '', force_download: bool = False,
upgrade_lean: bool = True) -> None:
"""A Lean project."""
self.repo = repo
self.is_dirty = is_dirty
self.rev = rev
self.directory = directory
self.pkg_config = pkg_config
self.src_directory = self.directory/pkg_config.get('path', '')
self.deps = deps
self.cache_url = cache_url or get_download_url()
self.force_download = force_download
self.upgrade_lean = upgrade_lean
self._import_graph = None # type: Optional[ImportGraph]
@classmethod
def from_path(cls, path: Path, cache_url: str = '',
force_download: bool = False,
upgrade_lean: bool = True) -> 'LeanProject':
"""Builds a LeanProject from a Path object"""
try:
repo = Repo(path, search_parent_directories=True)
except InvalidGitRepositoryError:
raise InvalidLeanProject('Invalid git repository')
if repo.bare:
raise InvalidLeanProject('Git repository is not initialized')
is_dirty = repo.is_dirty()
try:
rev = repo.commit().hexsha
except ValueError:
rev = ''
directory = Path(repo.working_dir)
try:
config = toml.load(directory/'leanpkg.toml')
except FileNotFoundError:
raise InvalidLeanProject('Missing leanpkg.toml')
return cls(repo, is_dirty, rev, directory,
config['package'], config['dependencies'],
cache_url, force_download, upgrade_lean)
@classmethod
def user_wide(cls, cache_url: str = '',
force_download: bool = False) -> 'LeanProject':
"""Return the user-wide LeanProject (living in ~/.lean)
If the project does not exist, it will be created, using the latest
version of Lean supported by mathlib."""
directory = Path.home()/'.lean'
try:
config = toml.load(directory/'leanpkg.toml')
except FileNotFoundError:
directory.mkdir(exist_ok=True)
version = mathlib_lean_version()
if version <= (3, 4, 2):
version_str = '.'.join(map(str, version))
else:
version_str = 'leanprover-community/lean:' +\
'.'.join(map(str, version))
pkg = { 'name': '_user_local_packages',
'version': '1',
'lean_version': version_str }
with (directory/'leanpkg.toml').open('w') as pkgtoml:
toml.dump({'package': pkg}, pkgtoml)
config = { 'package': pkg, 'dependencies': dict() }
return cls(None, False, '', directory,
config['package'], config['dependencies'],
cache_url, force_download)
@property
def name(self) -> str:
return self.pkg_config['name']
@property
def lean_version(self) -> VersionTuple:
return parse_version(self.pkg_config['lean_version'])
@lean_version.setter
def lean_version(self, version: VersionTuple) -> None:
self.pkg_config['lean_version'] = lean_version_toml(version)
@property
def is_mathlib(self) -> bool:
return self.name == 'mathlib'
@property
def toolchain(self) -> str:
ver_str = '{:d}.{:d}.{:d}'.format(*self.lean_version)
return ver_str if self.lean_version < (3, 5, 0) \
else 'leanprover-community-lean-' + ver_str
@property
def mathlib_rev(self) -> str:
if self.is_mathlib:
return self.rev
if 'mathlib' not in self.deps:
raise InvalidMathlibProject('This project does not depend on mathlib')
try:
rev = self.deps['mathlib']['rev']
except KeyError:
raise InvalidMathlibProject(
'Project seems to refer to a local copy of mathlib '
'instead of a GitHub repository')
return rev
@property
def mathlib_folder(self) -> Path:
if self.is_mathlib:
return self.directory
else:
return self.directory/'_target'/'deps'/'mathlib'
def read_config(self) -> None:
try:
config = toml.load(self.directory/'leanpkg.toml')
except FileNotFoundError:
raise InvalidLeanProject('Missing leanpkg.toml')
self.deps = config['dependencies']
self.pkg_config = config['package']
def write_config(self) -> None:
"""Write leanpkg.toml for this project."""
# Fix leanpkg lean_version bug if needed (the lean_version property
# setter is working here, hence the weird line).
self.lean_version = self.lean_version
# Note we can't blindly use toml.dump because we need dict as values
# for dependencies.
with (self.directory/'leanpkg.toml').open('w') as cfg:
cfg.write('[package]\n')
cfg.write(toml.dumps(self.pkg_config))
cfg.write('\n[dependencies]\n')
for dep, val in self.deps.items():
nval = str(val).replace("'git':", 'git =').replace(
"'rev':", 'rev =').replace("'", '"')
cfg.write('{} = {}\n'.format(dep, nval))
def get_mathlib_olean(self) -> None:
"""Get precompiled mathlib oleans for this project."""
# Just in case the user broke the workflow (for instance git clone
# mathlib by hand and then run `leanproject get-cache`)
if not (self.directory/'leanpkg.path').exists():
self.run(['leanpkg', 'configure'])
try:
archive = get_mathlib_archive(self.mathlib_rev, self.cache_url,
self.force_download, self.repo)
except (EOFError, shutil.ReadError):
log.info('Something wrong happened with the olean archive. '
'I will now retry downloading.')
archive = get_mathlib_archive(self.mathlib_rev, self.cache_url,
True, self.repo)
self.clean_mathlib()
self.mathlib_folder.mkdir(parents=True, exist_ok=True)
unpack_archive(archive, self.mathlib_folder)
# Let's now touch oleans, just in case
touch_oleans(self.mathlib_folder)
def mk_cache(self, force: bool = False) -> None:
"""Cache oleans for this project."""
if self.is_dirty and not force:
raise LeanDirtyRepo
if not self.rev:
raise ValueError('This project has no git commit.')
tgt_folder = DOT_MATHLIB if self.is_mathlib else self.directory/'_cache'
tgt_folder.mkdir(exist_ok=True)
archive = tgt_folder/(str(self.rev) + '.tar.bz2')
if archive.exists() and not force:
log.info('Cache for revision {} already exists'.format(self.rev))
return
pack(self.directory, filter(Path.exists, [self.src_directory, self.directory/'test']),
archive)
def get_cache(self, force: bool = False, url:str = '') -> None:
"""Tries to get olean cache.
Will raise LeanDownloadError or FileNotFoundError if no archive exists.
"""
if self.is_dirty and not force:
raise LeanDirtyRepo
if self.is_mathlib:
self.get_mathlib_olean()
else:
unpack_archive(self.directory/'_cache'/(str(self.rev)+'.tar.bz2'),
self.directory)
@classmethod
def from_git_url(cls, url: str, target: str = '',
branch: str = '', create_branch: bool = False,
cache_url: str = '',
force_download: bool = False) -> 'LeanProject':
"""Download a Lean project using git and prepare mathlib if needed."""
log.info('Cloning from ' + url)
target = target or url.split('/')[-1].split('.')[0]
repo = Repo.clone_from(url, target)
if create_branch and branch:
try:
repo.git.checkout('HEAD', b=branch)
except (IndexError, GitCommandError):
log.error('Cannot create new git branch')
shutil.rmtree(target)
raise
elif branch:
try:
repo.remote('origin').fetch(branch)
repo.git.checkout(branch)
except (IndexError, GitCommandError) as err:
log.error('Invalid git branch')
shutil.rmtree(target)
raise err
proj = cls.from_path(Path(repo.working_dir), cache_url, force_download)
proj.run_echo(['leanpkg', 'configure'])
if 'mathlib' in proj.deps or proj.is_mathlib:
proj.get_mathlib_olean()
return proj
@classmethod
def new(cls, path: Path = Path('.'), cache_url: str = '',
force_download: bool = False) -> 'LeanProject':
"""Create a new Lean project and prepare mathlib."""
if path == Path('.'):
subprocess.run(['leanpkg', 'init', path.absolute().name], check=True)
else:
if path.exists():
raise FileExistsError('Directory ' + str(path) + ' already exists')
subprocess.run(['leanpkg', 'new', str(path)], check=True)
proj = cls.from_path(path, cache_url, force_download)
proj.lean_version = mathlib_lean_version()
proj.write_config()
proj.add_mathlib()
proj.repo.git.checkout('-b', 'master')
return proj
def run(self, args: List[str]) -> str:
"""Run a command in the project directory, and returns stdout + stderr.
args is a list as in subprocess.run"""
return subprocess.run(args, cwd=str(self.directory),
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
check=True).stdout.decode()
def run_echo(self, args: List[str]) -> None:
"""Run a command in the project directory, letting stdin and stdout
flow.
args is a list as in subprocess.run"""
subprocess.run(args, cwd=str(self.directory), check=True)
def clean(self) -> None:
src_dir = self.directory/self.pkg_config['path']
test_dir = self.directory/'test'
if src_dir.exists():
clean(src_dir)
else:
raise InvalidLeanProject(
"Directory {} specified by 'path' does not exist".format(src_dir))
if test_dir.exists():
clean(test_dir)
def delete_zombies(self) -> None:
src_dir = self.directory/self.pkg_config['path']
test_dir = self.directory/'test'
if src_dir.exists():
delete_zombies(src_dir)
else:
raise InvalidLeanProject(
"Directory {} specified by 'path' does not exist".format(src_dir))
if test_dir.exists():
delete_zombies(test_dir)
def build(self) -> None:
log.info('Building project ' + self.name)
self.clean_mathlib()
self.run_echo(['leanpkg', 'build'])
def clean_mathlib(self, force: bool = False) -> None:
"""Restore git sanity in mathlib"""
if self.is_mathlib and (not self.is_dirty or force):
self.repo.head.reset(working_tree=True)
return
if self.mathlib_folder.exists():
mathlib = Repo(self.mathlib_folder)
mathlib.head.reset(working_tree=True)
mathlib.git.clean('-fd')
else:
self.run_echo(['leanpkg', 'configure'])
def upgrade_mathlib(self) -> None:
"""Upgrade mathlib in the project.
In case this project is mathlib, we assume we are already on the branch
we want.
"""
if self.is_mathlib:
try:
rem = next(remote for remote in self.repo.remotes
if any('leanprover' in url
for url in remote.urls))
rem.pull(self.repo.active_branch)
except (StopIteration, GitCommandError):
log.info("Couldn't pull from a relevant git remote. "
"You may try to git pull manually and then "
"run `leanproject get-cache`")
return
self.rev = self.repo.commit().hexsha
else:
self.clean_mathlib()
if self.upgrade_lean:
mathlib_lean = mathlib_lean_version()
if mathlib_lean > self.lean_version:
self.lean_version = mathlib_lean
self.write_config()
self.run_echo(['leanpkg', 'upgrade'])
self.read_config()
self.get_mathlib_olean()
def add_mathlib(self) -> None:
"""Add mathlib to the project."""
if 'mathlib' in self.deps:
log.info('This project already depends on mathlib')
return
log.info('Adding mathlib')
if self.upgrade_lean:
self.lean_version = mathlib_lean_version()
self.write_config()
self.run_echo(['leanpkg', 'add', 'leanprover-community/mathlib'])
self.read_config()
self.get_mathlib_olean()
def setup_git_hooks(self) -> None:
hook_dir = Path(self.repo.git_dir)/'hooks'
src = Path(__file__).parent
print('This script will copy post-commit and post-checkout scripts to ', hook_dir)
rep = input("Do you want to proceed (y/n)? ")
if rep in ['y', 'Y']:
shutil.copy(str(src/'post-commit'), str(hook_dir))
mode = (hook_dir/'post-commit').stat().st_mode
(hook_dir/'post-commit').chmod(mode | stat.S_IXUSR)
shutil.copy(str(src/'post-checkout'), str(hook_dir))
mode = (hook_dir/'post-checkout').stat().st_mode
(hook_dir/'post-checkout').chmod(mode | stat.S_IXUSR)
print("Successfully copied scripts")
else:
print("Cancelled...")
def check_timestamps(self) -> Tuple[bool, bool]:
"""Check that core and mathlib oleans are more recent than their
sources. Return a tuple (core_ok, mathlib_ok)"""
try:
mathlib_ok = all(p.stat().st_mtime < p.with_suffix('.olean').stat().st_mtime
for p in (self.mathlib_folder/'src').glob('**/*.lean'))
except FileNotFoundError:
mathlib_ok = False
return (check_core_timestamps(self.toolchain), mathlib_ok)
@property
def import_graph(self) -> ImportGraph:
if self._import_graph:
return self._import_graph
G = ImportGraph(self.directory)
for path in self.src_directory.glob('**/*.lean'):
rel = path.relative_to(self.src_directory)
label = str(rel.with_suffix('')).replace(os.sep, '.')
G.add_node(label)
imports = self.run(['lean', '--deps', str(path)])
for imp in map(Path, imports.split()):
try:
imp_rel = imp.relative_to(self.src_directory)
except ValueError:
# This import is not from the project
continue
imp_label = str(imp_rel.with_suffix('')).replace(os.sep, '.')
G.add_edge(imp_label, label)
for node in G:
G.nodes[node]['label'] = node
self._import_graph = G
return G
def make_all(self) -> None:
"""Creates all.lean importing everything from the project"""
with (self.src_directory/'all.lean').open('w') as all_file:
for path in self.src_directory.glob('**/*.lean'):
rel = str(path.relative_to(self.src_directory).with_suffix(''))
if rel == 'all':
continue
all_file.write('import ' + rel.replace(os.path.sep, '.') + '\n')
def list_decls(self) -> Dict[str, DeclInfo]:
"""Collect declarations seen from this project, as a dictionary of
DeclInfo"""
all_exists = (self.src_directory/'all.lean').exists()
list_decls_lean = self.src_directory/'list_decls.lean'
try:
list_decls_lean.unlink()
except FileNotFoundError:
pass
print('Gathering imports')
self.make_all()
imports = (self.src_directory/'all.lean').read_text()
decls_lean = (Path(__file__).parent/'decls.lean').read_text()
list_decls_lean.write_text(imports+decls_lean)
print('Collecting declarations')
self.run(['lean', '--run', str(list_decls_lean)])
data = yaml.safe_load((self.directory/'decls.yaml').open())
list_decls_lean.unlink()
if not all_exists:
(self.src_directory/'all.lean').unlink()
decls = dict()
for name, val in data.items():
fname = val['File']
line = val['Line']
if fname is None or line is None:
continue
path = Path(fname)
if '_target' in fname:
path = path.relative_to(self.directory/'_target'/'deps')
origin = path.parts[0]
path = path.relative_to(Path(origin)/'src')
elif '.elan' in fname:
origin = 'core'
parts = path.parts
path = Path('/'.join(parts[parts.index('.elan')+7:]))
else:
origin = self.name
path = path.relative_to(self.src_directory)
decls[name] = DeclInfo(origin, path, int(val['Line']))
return decls
def pickle_decls(self, target: Path) -> None:
"""Safe declaration into a pickle file target"""
with target.open('wb') as f:
pickle.dump(self.list_decls(), f, pickle.HIGHEST_PROTOCOL)