10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-11-14 01:53:55 +01:00
quantum_package/scripts/module/update_README.py

161 lines
3.9 KiB
Python
Raw Normal View History

2014-04-04 00:41:43 +02:00
#!/usr/bin/env python
"""Updates the README.rst file as the include directive is disabled on GitHub."""
2015-04-13 10:44:49 +02:00
__date__ = "Thu Apr 3 23:06:18 CEST 2014"
__author__ = "Anthony Scemama<scemama@irsamc.ups-tlse.fr> & TApplencourt "
2014-04-04 00:41:43 +02:00
2015-04-13 10:44:49 +02:00
README = "README.rst"
Assum_key = "Assumptions\n===========\n"
Needed_key = "Needed Modules\n==============\n"
Doc_key = "Documentation\n=============\n"
Sentinel = "@@$%&@@"
URL = "http://github.com/LCPQ/quantum_package/tree/master/src/"
2014-04-04 00:41:43 +02:00
2014-05-13 13:57:58 +02:00
import os
import subprocess
2015-06-04 12:09:28 +02:00
from collections import namedtuple
2014-05-13 13:57:58 +02:00
header = """
.. Do not edit this section. It was auto-generated from the
2015-06-04 12:09:28 +02:00
.. by the `update_README.py` script.
2014-05-13 13:57:58 +02:00
"""
2014-04-04 00:41:43 +02:00
def fetch_splitted_data():
2014-05-13 13:57:58 +02:00
"""Read the README.rst file and split it in strings:
2015-04-13 10:44:49 +02:00
* The documentation
2014-04-04 00:41:43 +02:00
* The needed modules
The result is given as a list of strings
"""
2015-06-04 16:40:00 +02:00
with open(README, 'r') as f:
data = f.read()
2014-04-04 00:41:43 +02:00
# Place sentinels
2015-04-13 10:44:49 +02:00
data = data.replace(Doc_key, Sentinel + Doc_key)
data = data.replace(Needed_key, Sentinel + Needed_key)
2014-04-04 00:41:43 +02:00
# Now Split data using the sentinels
result = data.split(Sentinel)
return result
2015-06-04 12:09:28 +02:00
2014-04-04 00:41:43 +02:00
def update_needed(data):
2015-05-15 16:30:43 +02:00
"""Read the NEEDED_CHILDREN_MODULES file, and replace the data with it.
Create the links to the GitHub pages."""
2014-04-04 00:41:43 +02:00
2015-06-04 12:09:28 +02:00
with open('NEEDED_CHILDREN_MODULES', 'r') as f:
modules = f.read()
2014-04-04 00:41:43 +02:00
2015-06-02 18:17:59 +02:00
header_image = ".. image:: tree_dependency.png\n\n"
2015-06-04 12:09:28 +02:00
if modules.strip():
modules = ['* `{0} <{1}>`_'.format(name, os.path.join(URL, name))
for name in modules.split()]
modules = "\n".join(modules)
modules = Needed_key + header + header_image + modules + '\n\n'
2014-04-04 00:41:43 +02:00
has_modules = False
for i in range(len(data)):
if data[i].startswith(Needed_key):
has_modules = True
data[i] = modules
if not has_modules:
data.append(modules)
return data
2014-05-13 13:57:58 +02:00
2015-06-04 12:09:28 +02:00
def extract_doc(item):
"""Extracts the documentation contained in IRPF90_man file"""
with open("IRPF90_man/%s.l" % (item), 'r') as f:
l_line = f.readlines()
result = []
inside = False
for line in l_line:
if not inside:
inside = line.startswith(".SH Description")
else:
if line.startswith(".SH"):
break
result.append(" {0}".format(line.strip()))
if not result:
result = [" Undocumented"]
return "\n".join(result) + '\n'
2014-05-13 13:57:58 +02:00
def update_documentation(data):
2015-04-13 10:44:49 +02:00
"""Reads the BEGIN_DOC ... END_DOC blocks and builds the documentation"""
2015-06-04 12:09:28 +02:00
IRP_info = namedtuple('IRP_info', ["name", "file", "line"])
2015-04-13 10:44:49 +02:00
# If the file does not exist, don't do anything
2015-06-04 12:09:28 +02:00
with open('tags', 'r') as f:
l_info = [IRP_info(*i.split()) for i in f.readlines()
if "/" not in i.split()[1]]
l_line = []
module_name = os.path.basename(os.getcwd())
for irp in l_info:
url = os.path.join(URL, module_name, irp.file)
doc = extract_doc(irp.name)
l_line += ["`{0} <{1}#L{2}>`_".format(irp.name, url, irp.line), doc,
""]
documentation = Doc_key + header + "\n".join(l_line)
2015-04-13 10:44:49 +02:00
has_doc = False
for i in range(len(data)):
if data[i].startswith(Doc_key):
has_doc = True
data[i] = documentation
if not has_doc:
data.append(documentation)
return data
2014-05-13 13:57:58 +02:00
2014-04-04 00:41:43 +02:00
def git_add():
"""Executes:
git add README.rst
2015-04-13 10:44:49 +02:00
throw an error if git is not precent"""
try:
# pipe output to /dev/null for silence
null = open("/dev/null", "w")
subprocess.Popen("git add README.rst", stdout=null, stderr=null)
null.close()
except OSError:
raise
2014-04-04 00:41:43 +02:00
def main():
data = fetch_splitted_data()
2015-06-04 12:09:28 +02:00
2014-05-13 13:57:58 +02:00
data = update_documentation(data)
2014-04-04 00:41:43 +02:00
data = update_needed(data)
output = ''.join(data)
2015-04-13 10:44:49 +02:00
with open(README, 'w') as f:
f.write(output)
2014-04-04 00:41:43 +02:00
2014-06-07 22:07:50 +02:00
try:
2015-04-13 10:44:49 +02:00
git_add()
except OSError:
pass
2014-04-04 00:41:43 +02:00
2015-06-02 18:40:24 +02:00
2015-04-13 10:44:49 +02:00
if __name__ == '__main__':
main()