cnam.marc/cnam/travaux/blanc/build.py

82 lines
1.9 KiB
Python
Raw Normal View History

2020-04-22 18:56:05 +02:00
#! /usr/bin/python3 -B
import os
2020-04-27 04:25:41 +02:00
import shutil
2020-04-22 18:56:05 +02:00
import subprocess
2020-05-02 03:25:03 +02:00
import sys
2020-04-22 18:56:05 +02:00
DOCUMENTS = [
2020-09-26 00:52:12 +02:00
# ('document', 'mémoire'),
# ('présentation', 'présentation'),
2020-04-22 18:56:05 +02:00
]
2020-04-27 04:25:41 +02:00
TMP = 'tmp'
2020-04-22 18:56:05 +02:00
2020-05-01 19:24:37 +02:00
def run(command):
subprocess.call(command)
def errun(command):
return subprocess.check_output(
command, stderr=subprocess.STDOUT)
2020-04-27 00:39:49 +02:00
2020-05-02 03:25:03 +02:00
def build(sign):
2020-05-01 22:01:50 +02:00
for en, fr in DOCUMENTS:
2020-05-02 19:24:52 +02:00
command = ['xelatex', '-output-directory', TMP, en]
2020-04-27 04:25:41 +02:00
run(command)
2020-05-02 19:24:52 +02:00
run(['makeglossaries', '-d', TMP, en])
2020-04-27 22:52:32 +02:00
run(['biber',
'--input-directory', TMP,
'--output-directory', TMP,
2020-05-01 22:01:50 +02:00
en,
2020-04-27 22:52:32 +02:00
])
2020-04-27 04:25:41 +02:00
run(command)
2020-04-28 01:39:51 +02:00
run(command)
2020-05-02 03:42:08 +02:00
pdf = f'{fr}.pdf'
2020-05-01 22:01:50 +02:00
os.rename(os.path.join(TMP, f'{en}.pdf'),
2020-05-02 03:42:08 +02:00
os.path.join(TMP, pdf))
2020-05-02 05:41:30 +02:00
if not sign:
os.rename(os.path.join(TMP, pdf), pdf)
else:
2020-05-02 03:25:03 +02:00
run(['gpg',
'--armor',
'--detach-sign',
os.path.join(TMP, pdf),
])
signature = f'{pdf}.asc'
2020-05-02 05:41:30 +02:00
for f in [pdf, signature]:
os.rename(os.path.join(TMP, f), f)
2020-05-02 03:25:03 +02:00
lines = errun(['gpg',
'--verify', signature, pdf,
]).decode('u8').splitlines()
id = lines[2].index('"')
lines = [
2020-05-22 18:11:16 +02:00
lines[0],
lines[1],
2020-05-02 03:25:03 +02:00
lines[2][:id] + lines[4][id:]
.replace('@', ' @ ')
.replace('.', ' ⋅ ')
] + lines[5:]
buffer = os.linesep.join(lines).encode('u8')
with open(f'{pdf}.vrf', 'bw') as f:
f.write(buffer)
2020-04-22 18:56:05 +02:00
2020-04-27 04:25:41 +02:00
def clean():
shutil.rmtree(TMP, ignore_errors=True)
2020-04-22 18:56:05 +02:00
def main():
file = os.path.realpath(__file__)
directory = os.path.dirname(file)
2020-04-27 04:25:41 +02:00
os.chdir(directory)
clean()
os.makedirs(TMP)
2020-05-02 03:25:03 +02:00
build(len(sys.argv) == 1)
2020-04-27 04:25:41 +02:00
clean()
2020-04-22 18:56:05 +02:00
if __name__ == '__main__':
main()