-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththesis-o-meter.py
113 lines (95 loc) · 3.27 KB
/
thesis-o-meter.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
#!/usr/bin/env python3
import os
import argparse
import subprocess
import git
from datetime import datetime
import json
import collections
from file_utils import data_has_changed, generate_pdf, get_page_count, proccess_data
from tex_file_processor import process_project
from detex_proc import get_word_count, get_chapter_word_count
def git_pull(git_dir: os.PathLike):
g = git.cmd.Git(git_dir)
g.pull()
def main(args):
if not os.path.isdir(args.git_dir):
raise ValueError(f"Could not find project git dir `{args.git_dir}`")
main_tex_file = os.path.join(args.git_dir, args.main_tex)
if not os.path.isfile(main_tex_file):
raise ValueError(f"Could not find main TeX file `{main_tex_file}`")
# Update to latest version
git_pull(args.git_dir)
# generate the PDF that we will parse some of our data from
generate_pdf(main_tex_file)
# Collect and store data from PDF
data = collections.defaultdict(int)
data["time"] = datetime.today().strftime("%Y-%m-%d %H:%M:%S")
data["word_count"] = get_word_count(main_tex_file, os.path.dirname(main_tex_file))
main_pdf_file = main_tex_file.replace(".tex", ".pdf")
data["page_count"] = get_page_count(main_pdf_file)
# get info from our tex files
unqiue_queries = ["\\cite{"]
commands = [
"\\begin{figure*}",
"\\begin{figure}",
"\\includegraphics[",
"\\begin{table}",
"\\begin{table*}",
"\\begin{algorithm*}",
"\\begin{algorithm}",
"\\newglossaryentry{",
"\\newacronym{",
"\\begin{listing}",
]
counts = process_project(main_tex_file, unqiue_queries, commands)
# combine values with meaningful names
for (x, y) in [
("references", "\\cite{"),
("figures", "\\begin{figure*}"),
("figures", "\\begin{figure}"),
("tables", "\\begin{table*}"),
("tables", "\\begin{table}"),
("algorithm", "\\begin{algorithm*}"),
("algorithm", "\\begin{algorithm}"),
("glossary_entries", "\\newglossaryentry{"),
("glossary_acronym", "\\newacronym{"),
("code_listings", "\\begin{listing}"),
]:
data[x] += counts[y]
del counts[y]
for k, v in counts.items():
data[k] = v
# Check if we need to save the data to file
os.makedirs(args.log_dir, exist_ok=True)
if not data_has_changed(data, args.log_dir):
return
# Create logfile, if data has changed
with open(
os.path.join(args.log_dir, data["time"].replace(" ", "-") + ".json"), "w"
) as f:
json.dump(data, f, indent=2)
# Update CSV that can be used for plotting
proccess_data(args.log_dir, os.path.join(args.log_dir, "thesis_data.csv"))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Get the word count for a TeX project")
parser.add_argument(
"--git_dir",
required=True,
type=str,
help="Path to main TeX file of project",
)
parser.add_argument(
"--main_tex",
required=True,
type=str,
help="Path to main TeX file of project",
)
parser.add_argument(
"--log_dir",
type=str,
required=True,
help="Location to store logged data",
)
args = parser.parse_args()
main(args)