2020-09-26 01:33:37 +02:00
|
|
|
#! /usr/bin/python3 -B
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2020-10-09 21:09:26 +02:00
|
|
|
MAIN = 'main'
|
|
|
|
TMP = 'tmp'
|
|
|
|
|
2020-10-18 00:24:08 +02:00
|
|
|
ENGLISH = 'english'
|
|
|
|
FRENCH = 'french'
|
2020-10-07 13:50:11 +02:00
|
|
|
LANGUAGES = [ENGLISH, FRENCH]
|
2020-10-09 21:09:26 +02:00
|
|
|
|
2020-09-26 01:33:37 +02:00
|
|
|
DOCUMENTS = [
|
2020-10-07 13:50:11 +02:00
|
|
|
{ENGLISH: 'thesis', FRENCH: 'mémoire'},
|
2021-04-14 20:53:04 +02:00
|
|
|
# {ENGLISH: 'presentation', FRENCH: 'présentation'},
|
2020-09-26 01:33:37 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def run(command):
|
|
|
|
subprocess.call(command)
|
|
|
|
|
|
|
|
|
|
|
|
def errun(command):
|
|
|
|
return subprocess.check_output(
|
|
|
|
command, stderr=subprocess.STDOUT)
|
|
|
|
|
|
|
|
|
2020-10-07 14:41:54 +02:00
|
|
|
def build(directory, sign):
|
|
|
|
# temporary directory
|
|
|
|
tmp = os.path.join(directory, TMP)
|
|
|
|
# for each language
|
2020-10-07 13:50:11 +02:00
|
|
|
for language in LANGUAGES:
|
2020-10-09 08:10:17 +02:00
|
|
|
# languages
|
|
|
|
other_languages = [lang for lang in LANGUAGES if lang is not language]
|
|
|
|
languages = [language, *other_languages]
|
|
|
|
other_languages = ','.join(other_languages)
|
|
|
|
summaries = ''.join(
|
2020-10-19 16:11:13 +02:00
|
|
|
[f'\\summary{{{lang}}}' for lang in languages])
|
2020-10-08 21:07:48 +02:00
|
|
|
# display languages
|
2020-10-09 08:10:17 +02:00
|
|
|
for item in ['', language, other_languages, languages, summaries]:
|
|
|
|
print(item)
|
2020-10-07 14:41:54 +02:00
|
|
|
# for each document
|
2020-10-07 13:50:11 +02:00
|
|
|
for document in DOCUMENTS:
|
2020-10-08 21:07:48 +02:00
|
|
|
# display language
|
|
|
|
print()
|
|
|
|
print(document[language])
|
2020-10-07 14:41:54 +02:00
|
|
|
# clean
|
|
|
|
os.chdir(directory)
|
|
|
|
wipe(tmp)
|
|
|
|
os.makedirs(tmp)
|
|
|
|
# move into document directory
|
|
|
|
os.chdir(document[ENGLISH])
|
2020-10-08 08:42:59 +02:00
|
|
|
# prepare variables
|
2020-10-08 21:07:48 +02:00
|
|
|
variables = {
|
2020-10-12 10:34:03 +02:00
|
|
|
'name': MAIN,
|
2020-10-29 08:14:00 +01:00
|
|
|
'ENGLISH': ENGLISH,
|
|
|
|
'FRENCH': FRENCH,
|
2020-10-08 21:07:48 +02:00
|
|
|
'mainlanguage': language,
|
|
|
|
'otherlanguages': other_languages,
|
2020-10-09 08:10:17 +02:00
|
|
|
'summaries': summaries,
|
2020-10-08 21:07:48 +02:00
|
|
|
}
|
2020-10-08 08:42:59 +02:00
|
|
|
# transform variables
|
|
|
|
variables = ''.join([f'\\def\\{k}{{{v}}}'
|
|
|
|
for k, v in variables.items()])
|
2020-10-07 14:41:54 +02:00
|
|
|
# prepare build command
|
|
|
|
command = ['xelatex',
|
|
|
|
'-output-directory', tmp,
|
2020-10-08 08:42:59 +02:00
|
|
|
f'{variables}\\input{{{MAIN}}}',
|
2020-10-07 14:41:54 +02:00
|
|
|
]
|
2020-10-20 23:07:53 +02:00
|
|
|
# pre build
|
|
|
|
run(command)
|
2020-10-07 14:41:54 +02:00
|
|
|
# if it's the main document
|
|
|
|
if document[ENGLISH] == 'thesis':
|
|
|
|
# build glossaries
|
2020-10-12 10:32:43 +02:00
|
|
|
run(['makeglossaries',
|
|
|
|
'-d', tmp,
|
|
|
|
MAIN,
|
|
|
|
])
|
2020-10-07 14:41:54 +02:00
|
|
|
# build references
|
2020-10-07 13:50:11 +02:00
|
|
|
run(['biber',
|
2020-10-07 14:41:54 +02:00
|
|
|
'--input-directory', tmp,
|
|
|
|
'--output-directory', tmp,
|
|
|
|
MAIN,
|
|
|
|
])
|
|
|
|
# re build
|
2020-10-07 13:50:11 +02:00
|
|
|
run(command)
|
2020-10-07 14:41:54 +02:00
|
|
|
# final build
|
2020-10-07 12:47:33 +02:00
|
|
|
run(command)
|
2020-10-07 14:41:54 +02:00
|
|
|
# rename the document
|
|
|
|
pdf = f'{document[language]}.pdf'
|
|
|
|
os.rename(os.path.join(tmp, f'{MAIN}.pdf'),
|
|
|
|
os.path.join(tmp, pdf),
|
|
|
|
)
|
|
|
|
# if signature is disabled
|
2020-10-07 13:50:11 +02:00
|
|
|
if not sign:
|
2020-10-07 14:41:54 +02:00
|
|
|
# fetch the document from temporary directory
|
|
|
|
os.rename(os.path.join(tmp, pdf),
|
|
|
|
os.path.join(directory, pdf),
|
|
|
|
)
|
|
|
|
# if signature is enabled
|
2020-10-07 13:50:11 +02:00
|
|
|
else:
|
2020-10-07 14:41:54 +02:00
|
|
|
# sign the document
|
2020-10-07 13:50:11 +02:00
|
|
|
run(['gpg',
|
2020-10-07 14:41:54 +02:00
|
|
|
'--armor',
|
|
|
|
'--detach-sign',
|
|
|
|
os.path.join(tmp, pdf),
|
|
|
|
])
|
2020-10-07 13:50:11 +02:00
|
|
|
signature = f'{pdf}.asc'
|
2020-10-07 14:41:54 +02:00
|
|
|
# fetch the document and signature from temporary directory
|
2020-10-07 13:50:11 +02:00
|
|
|
for f in [pdf, signature]:
|
2020-10-07 14:41:54 +02:00
|
|
|
os.rename(os.path.join(tmp, f),
|
|
|
|
os.path.join(directory, f),
|
|
|
|
)
|
|
|
|
# verify the document signature
|
2020-10-07 14:45:36 +02:00
|
|
|
lines = errun(['gpg', '--verify',
|
|
|
|
os.path.join(directory, signature),
|
|
|
|
os.path.join(directory, pdf),
|
2020-10-07 14:41:54 +02:00
|
|
|
]).decode('u8').splitlines()
|
2020-10-07 13:50:11 +02:00
|
|
|
id = lines[2].index('"')
|
|
|
|
lines = [
|
2023-07-29 15:58:25 +02:00
|
|
|
*lines[:2],
|
|
|
|
*[line.replace('@', ' @ ').replace('.', ' ⋅ ')
|
|
|
|
for line in lines[2:4]],
|
|
|
|
*lines[-2:]
|
|
|
|
]
|
2020-10-07 14:41:54 +02:00
|
|
|
# write verification file
|
2020-10-07 13:50:11 +02:00
|
|
|
buffer = os.linesep.join(lines).encode('u8')
|
2020-10-07 14:41:54 +02:00
|
|
|
with open(os.path.join(directory, f'{pdf}.vrf'), 'bw') as f:
|
2020-10-07 13:50:11 +02:00
|
|
|
f.write(buffer)
|
2020-10-07 14:41:54 +02:00
|
|
|
# clean
|
|
|
|
wipe(tmp)
|
2020-09-26 01:33:37 +02:00
|
|
|
|
|
|
|
|
2020-10-07 14:41:54 +02:00
|
|
|
def wipe(directory):
|
|
|
|
shutil.rmtree(directory, ignore_errors=True)
|
2020-09-26 01:33:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
file = os.path.realpath(__file__)
|
|
|
|
directory = os.path.dirname(file)
|
2020-10-07 14:41:54 +02:00
|
|
|
build(directory, len(sys.argv) == 1)
|
2020-09-26 01:33:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|