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

189 lines
4.4 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
2015-06-24 11:33:27 +02:00
import sys
"""
NEED to call in a module
First arg can be the root parent
"""
try:
ROOT_module = os.path.realpath(sys.argv[1])
except:
ROOT_module = os.getcwd()
2015-06-24 12:25:19 +02:00
if ROOT_module != os.getcwd():
2015-06-24 11:33:27 +02:00
change = True
else:
change = False
MODULE_NAME = os.path.basename(os.getcwd())
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"""
2015-06-24 11:33:27 +02:00
path = os.path.join(ROOT_module, "IRPF90_man/%s.l" % (item))
with open(path, 'r') as f:
2015-06-04 12:09:28 +02:00
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-24 11:33:27 +02:00
path = os.path.join(ROOT_module, "tags")
with open(path, 'r') as f:
dump = f.readlines()
l_info = []
for i in dump:
name, f, ligne = i.split()
if not change and "/" not in i:
l_info.append(IRP_info(name, f, ligne))
elif change and MODULE_NAME in i:
l_info.append(IRP_info(name, f.split("/")[-1], ligne))
2015-06-04 12:09:28 +02:00
l_line = []
for irp in l_info:
2015-06-24 11:33:27 +02:00
url = os.path.join(URL, MODULE_NAME, irp.file)
2015-06-04 12:09:28 +02:00
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()