-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.py
More file actions
659 lines (600 loc) · 15.9 KB
/
functions.py
File metadata and controls
659 lines (600 loc) · 15.9 KB
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
list_of_standard_envs = ['abstract', 'verbatim', 'quote', 'itemize', 'list', 'center', 'eqnarray*', 'eqnarray', 'align', 'align*', 'document', 'enumerate', 'proof', 'matrix', 'lemma', 'proposition', 'theorem', 'remark', 'remarks', 'example', 'exercise', 'situation', 'equation', 'definition', 'item']
# We also have labels for
# 'section', 'subsection', 'subsubsection' (every one of these has a label)
# 'item' (typically an item does not have a label)
list_of_labeled_envs = ['lemma', 'proposition', 'theorem', 'remark', 'remarks', 'example', 'exercise', 'situation', 'equation', 'definition']
list_parts = ['section', 'subsection', 'subsubsection', 'phantomsection']
list_of_proof_envs = ['lemma', 'proposition', 'theorem']
list_of_standard_labels = ['definition', 'lemma', 'proposition', 'theorem', 'remark', 'remarks', 'example', 'exercise', 'situation', 'equation', 'section', 'subsection', 'subsubsection', 'item']
# Get file name
def get_name():
from sys import argv
if not len(argv) == 2:
print
print "This script needs exactly one argument"
print "namely the path to the file"
print
raise Exception('Wrong arguments')
name = argv[1]
return name
# Find location of repository
def get_path():
from sys import argv
if not len(argv) == 2:
print
print "This script needs exactly one argument"
print "namely the path to the stacks project directory"
print
raise Exception('Wrong arguments')
path = argv[1]
path.rstrip("/")
path = path + "/"
return path
# List the stems of the TeX files in the project
def list_text_files(path):
Makefile_file = open(path + "Makefile", 'r')
for line in Makefile_file:
n = line.find("LIJST = ")
if n == 0:
break
lijst = ""
while line.find("\\") >= 0:
line = line.rstrip()
line = line.rstrip("\\")
lijst = lijst + " " + line
line = Makefile_file.next()
Makefile_file.close()
lijst = lijst + " " + line
lijst = lijst.replace("LIJST = ", "")
lijst = lijst + " fdl"
return lijst.split()
# Print error
def print_error(error_text, line, name, line_nr):
print "In file " + name + ".tex"
print "On line", line_nr
print "Line: " + line.rstrip()
print "Error: " + error_text
print "gvim +{} {}.tex".format(line_nr, name)
print
# Check length line
def length_of_line(line):
n = len(line)
if n > 81:
return "More than 80 characters on a line."
return ""
# Check if line starts with given pattern
def beginning_of_line(pattern, line):
n = line.find(pattern)
if n > 0:
return "Pattern: " + pattern + " not at the start of the line."
return ""
# See if line starts an environment
def beginning_of_env(line):
n = line.find("\\begin{")
if n == 0:
return 1
return 0
# See if line starts a definition
def beginning_of_definition(line):
n = line.find("\\begin{definition}")
if n == 0:
return 1
return 0
# See if line ends a definition
def end_of_definition(line):
n = line.find("\\end{definition}")
if n == 0:
return 1
return 0
# See if line starts a proof
def beginning_of_proof(line):
n = line.find("\\begin{proof}")
if n == 0:
return 1
return 0
# See if line ends a proof
def end_of_proof(line):
n = line.find("\\end{proof}")
if n == 0:
return 1
return 0
# See if line starts verbatim environment,
# also check if the \begin{verbatim} starts the line
def beginning_of_verbatim(line):
n = line.find("\\begin{verbatim}")
if n > 0:
raise Exception('\\begin{verbatim} not at start of line.')
if n == 0:
return 1
else:
return 0
# See if line ends verbatim environment,
# also check if the \begin{verbatim} starts the line
def end_of_verbatim(line):
n = line.find("\\end{verbatim}")
if n > 0:
raise Exception('\\end{verbatim} not at start of line.')
if n == 0:
return 1
return 0
# Find clause starting in specific spot with specific open and close characters
def find_sub_clause(text, spot, open, close):
nr_braces = 0
while nr_braces >= 0:
spot = spot + 1
if text[spot] == open:
nr_braces = nr_braces + 1
if text[spot] == close:
nr_braces = nr_braces - 1
return spot
# See if there is a clause immediately following the current spot, and
# return spot of closing brace
def next_clause(text, spot):
open = ""
if spot + 1 == len(text):
return spot
if text[spot + 1] == "[":
open = "["
close = "]"
if text[spot + 1] == "{":
open = "{"
close = "}"
if open:
spot = find_sub_clause(text, spot + 1, open, close)
return spot
# Find spot of last brace of sequence of clauses starting at spot
def rest_clauses(text, spot):
n = next_clause(text, spot)
while n > spot:
spot = n
n = next_clause(text, spot)
return n
# Check if the pattern starts the line and has only clauses following...
def only_on_line(pattern, spot, line):
line = line.rstrip()
n = line.find(pattern)
if n > 0:
return "Pattern: " + pattern + "not at the start of the line."
if n == 0:
m = find_sub_clause(line, spot, "{", "}")
m = rest_clauses(line, m)
if not m + 1 == len(line):
return "Pattern: " + pattern + "*} not by itself."
return ""
# Check if $$ is by itself and at the start of the line
def check_double_dollar(line):
n = line.find("$$")
if n < 0:
return ""
if n > 0:
return "Double dollar not at start of line."
line = line.rstrip()
if not len(line) == 2:
return "Double dollar not by itself on line."
# Check if the line contains the title of the document
def is_title(line):
n = line.find("\\title{")
if n < 0:
return 0
return 1
# Assuming there is a title on the line, find it.
def find_title(line):
n = line.find("\\title{")
if n < 0:
return ""
n = n + 6
m = find_sub_clause(line, n, "{", "}")
title = line[n + 1 : m]
return title
# Check if the line contains a label
def is_label(env_text):
n = env_text.find("\\label{")
if n < 0:
return 0
return 1
# Returns short label. Does not assume there is a label on the line
def find_label(env_text):
n = env_text.find("\\label{")
if n < 0:
return ""
n = n + 6
m = find_sub_clause(env_text, n, "{", "}")
label = env_text[n + 1 : m]
return label
# Check if there are references on the line
def contains_ref(line):
n = line.find("\\ref{")
if n < 0:
return 0
return 1
# Returns list of full references on the line
def find_refs(line, name):
refs = []
n = line.find("\\ref{")
while n >= 0:
m = find_sub_clause(line, n + 4, "{", "}")
ref = line[n + 5: m]
if standard_label(ref):
ref = name + "-" + ref
refs.append(ref)
n = line.find("\\ref{", m)
return refs
# Check if short label is standard
def standard_label(label):
n = 0
while n < len(list_of_standard_labels):
if label.find(list_of_standard_labels[n] + '-') == 0:
return 1
n = n + 1
return 0
# Split label into components
def split_label(label):
pieces = label.split('-')
name = pieces[0]
type = pieces[1]
rest = ""
n = 2
# Exceptions...
# We should automate this...
if name == "more" and type == "algebra":
name = "more-algebra"
type = pieces[2]
n = 3
if name == "sites" and type == "modules":
name = "sites-modules"
type = pieces[2]
n = 3
if name == "sites" and type == "cohomology":
name = "sites-cohomology"
type = pieces[2]
n = 3
if name == "more" and type == "morphisms":
name = "more-morphisms"
type = pieces[2]
n = 3
if name == "more" and type == "groupoids":
name = "more-groupoids"
type = pieces[2]
n = 3
if name == "etale" and type == "cohomology":
name = "etale-cohomology"
type = pieces[2]
n = 3
if name == "spaces" and type == "properties":
name = "spaces-properties"
type = pieces[2]
n = 3
if name == "spaces" and type == "morphisms":
name = "spaces-morphisms"
type = pieces[2]
n = 3
if name == "decent" and type == "spaces":
name = "decent-spaces"
type = pieces[2]
n = 3
if name == "spaces" and type == "cohomology":
name = "spaces-cohomology"
type = pieces[2]
n = 3
if name == "spaces" and type == "divisors":
name = "spaces-divisors"
type = pieces[2]
n = 3
if name == "spaces" and type == "limits":
name = "spaces-limits"
type = pieces[2]
n = 3
if name == "spaces" and type == "topologies":
name = "spaces-topologies"
type = pieces[2]
n = 3
if name == "spaces" and type == "descent":
name = "spaces-descent"
type = pieces[2]
n = 3
if name == "spaces" and type == "perfect":
name = "spaces-perfect"
type = pieces[2]
n = 3
if name == "spaces" and type == "more" and pieces[2] == "morphisms":
name = "spaces-more-morphisms"
type = pieces[3]
n = 4
if name == "spaces" and type == "over" and pieces[2] == "fields":
name = "spaces-over-fields"
type = pieces[3]
n = 4
if name == "formal" and type == "defos":
name = "formal-defos"
type = pieces[2]
n = 3
if name == "spaces" and type == "groupoids":
name = "spaces-groupoids"
type = pieces[2]
n = 3
if name == "spaces" and type == "more" and pieces[2] == "groupoids":
name = "spaces-more-groupoids"
type = pieces[3]
n = 4
if name == "examples" and type == "stacks":
name = "examples-stacks"
type = pieces[2]
n = 3
if name == "groupoids" and type == "quotients":
name = "groupoids-quotients"
type = pieces[2]
n = 3
if name == "stacks" and type == "sheaves":
name = "stacks-sheaves"
type = pieces[2]
n = 3
if name == "stacks" and type == "properties":
name = "stacks-properties"
type = pieces[2]
n = 3
if name == "stacks" and type == "morphisms":
name = "stacks-morphisms"
type = pieces[2]
n = 3
if name == "stacks" and type == "cohomology":
name = "stacks-cohomology"
type = pieces[2]
n = 3
if name == "stacks" and type == "introduction":
name = "stacks-introduction"
type = pieces[2]
n = 3
while n < len(pieces):
rest = rest + "-" + pieces[n]
n = n + 1
return [name, type, rest]
# Check if environment is standard
# The input should be a line from latex file containing the
# \begin{environment} statement
def standard_env(env):
n = 0
while n < len(list_of_standard_envs):
if env.find('{' + list_of_standard_envs[n] + '}') >= 0:
return 1
n = n + 1
return 0
# Check if environment should have a label
# The input should be a line from latex file containing the
# \begin{environment} statement
def labeled_env(env):
n = 0
while n < len(list_of_labeled_envs):
if env.find('\\begin{' + list_of_labeled_envs[n] + '}') == 0:
return 1
n = n + 1
return 0
def end_labeled_env(env):
n = 0
while n < len(list_of_labeled_envs):
if env.find('\\end{' + list_of_labeled_envs[n] + '}') == 0:
return 1
n = n + 1
return 0
# Check for start of new part
def new_part(line):
n = 0
while n < len(list_parts):
if line.find('\\' + list_parts[n]) == 0:
return 1
n = n + 1
return 0
# Check for start of new item
def new_item(line):
if line.find('\\item') == 0:
return 1
return 0
# Check if environment should have a proof
# The input should be a line from latex file containing the
# \begin{environment} statement
def proof_env(env):
n = 0
while n < len(list_of_proof_envs):
if env.find('{' + list_of_proof_envs[n] + '}') >= 0:
return 1
n = n + 1
return 0
# Checks inner text of definition for existence of a label (this is now
# obsolete) and for the existence of at least one term which is being
# defined
def check_def_text(def_text):
n = def_text.find("\\label{")
if n < 0:
return "No label in definition."
n = def_text.find("{\\it ")
if n < 0:
return "Nothing defined in definition."
return ""
# Returns list of terms being defined, which are pieces of the form
# {\it definition-text}
def find_defined_terms(def_text):
def_terms = []
n = def_text.find("{\\it ")
while n >= 0:
m = find_sub_clause(def_text, n, "{", "}")
def_terms.append(def_text[n : m + 1])
n = def_text.find("{\\it ", m)
return def_terms
# See if ref occurs in list labels
def check_ref(ref, labels):
try:
labels.index(ref)
except:
return 0
return 1
# See if references on line are internal
# this uses that the references have the correct shape
def internal_refs(line, refs, name):
n = 0
while n < len(refs):
ref = refs[n]
split = split_label(ref)
if name == split[0] and line.find(ref) >= 0:
return ref
n = n + 1
return ""
# See if references already occur in list labels
def check_refs(refs, labels):
n = 0
while n < len(refs):
ref = refs[n]
if ref == "":
return "empty reference"
if not check_ref(ref, labels):
return ref
n = n + 1
return ""
# Silly function to detect LaTeX commands. Not perfect.
def find_commands(line):
S = "( \\{_\n$[,'}])^/=.|+-:&\"@;"
commands = []
n = line.find("\\")
while n >= 0:
m = n + 1
while m < len(line):
if S.find(line[m]) >= 0:
break
m = m + 1
if m == n + 1:
m = m + 1
commands.append(line[n : m])
n = line.find("\\", m)
return commands
# See if a command already occurs
def new_command(new, commands):
try:
commands.index(new)
except:
return 1
return 0
# Structure of tags:
# Already created tags are listed in the file tags/tags
# Each line of tags/tags is of the form
# tag,full_label
# with no spaces and where
# tag: the actual tag
# full_label: label with "name-" prepended if the label occurs
# in the file name.tex
# See also the file tags/tags for an example.
# We can also have lines starting with a hash # marking comments.
# We may want to change the name/label if a result moves from one file to
# another, or if we split a long file into two pieces. We may also
# sometimes change the label of a result (eg if there is a typo in the
# label itself). But the tags should never change.
# The first tag is 0000 and the last tag is ZZZZ
# Starting at tag 04E6 we no longer use O
def next_tag(tag):
next = list(tag)
S = "0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ"
i = 3
while i >= 0:
n = S.find(next[i])
if n == 34:
next[i] = '0'
else:
next[i] = S[n + 1]
break
i = i - 1
return next[0] + next[1] + next[2] + next[3]
def get_tag_line(line):
line = line.rstrip()
return line.split(",")
def get_tags(path):
tags = []
tag_file = open(path + "tags/tags", 'r')
for line in tag_file:
if not line.find("#") == 0:
tags.append(get_tag_line(line))
tag_file.close()
return tags
def new_label(tags, label):
n = 0
new = 1
while new and n < len(tags):
if tags[n][1] == label:
new = 0
n = n + 1
return new
def get_title(path, name):
labels = []
tex_file = open(path + name + ".tex", 'r')
verbatim = 0
for line in tex_file:
title = find_title(line)
if title:
break
tex_file.close()
return title
def all_titles(path):
titles = {}
lijstje = list_text_files(path)
for name in lijstje:
extra = get_title(path, name)
titles[name] = extra
return titles
def get_all_labels(path, name):
labels = []
tex_file = open(path + name + ".tex", 'r')
verbatim = 0
for line in tex_file:
# Check for verbatim, because we do not add labels from
# verbatim environments.
verbatim = verbatim + beginning_of_verbatim(line)
if verbatim:
if end_of_verbatim(line):
verbatim = 0
continue
label = find_label(line)
if label:
label = name + "-" + label
labels.append(label)
tex_file.close()
return labels
def all_labels(path):
lijstje = list_text_files(path)
labels = []
for name in lijstje:
extra = get_all_labels(path, name)
labels = labels + extra
return labels
def get_new_tags(path, tags):
last_tag = tags[-1][0]
label_tags = dict((tags[n][1], tags[n][0]) for n in range(0, len(tags)))
lijstje = list_text_files(path)
new_tags = []
for name in lijstje:
labels = get_all_labels(path, name)
n = 0
while n < len(labels):
if labels[n] not in label_tags:
last_tag = next_tag(last_tag)
new_tags.append([last_tag, labels[n]])
n = n + 1
return new_tags
def print_new_tags(new_tags):
n = 0
while n < len(new_tags):
print new_tags[n][0] + "," + new_tags[n][1]
n = n + 1
return
def write_new_tags(path, new_tags):
tag_file = open(path + "tags/tags", 'a')
n = 0
while n < len(new_tags):
tag_file.write(new_tags[n][0] + "," + new_tags[n][1] + "\n")
n = n + 1
tag_file.close()
return
def cap_type(type):
return type[0].capitalize() + type[1:]
def git_version(path):
from subprocess import Popen, PIPE, STDOUT
cmd = 'git --git-dir=' + path + '.git log --pretty=format:%h -n1'
p = Popen(cmd, shell=True, stdout=PIPE).stdout
version = p.read()
p.close()
return version