10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-11-08 07:03:57 +01:00
quantum_package/scripts/update_README.py

219 lines
5.6 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"
2014-04-04 00:41:43 +02:00
__author__ = "Anthony Scemama <scemama@irsamc.ups-tlse.fr>"
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
header = """
.. Do not edit this section. It was auto-generated from the
.. NEEDED_MODULES file.
"""
try:
2015-04-13 10:44:49 +02:00
# subprocess.check_output("git status".split())
has_git = True
2014-05-13 13:57:58 +02:00
except OSError:
2015-04-13 10:44:49 +02:00
has_git = False
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:
2014-04-04 00:41:43 +02:00
* The description
* The assumptions
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-04-13 10:44:49 +02:00
file = open(README, 'r')
2014-04-04 00:41:43 +02:00
data = file.read()
file.close()
# Place sentinels
2015-04-13 10:44:49 +02:00
data = data.replace(Assum_key, Sentinel + Assum_key)
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
def update_assumptions(data):
"""Read the ASSUMPTIONS.rst file, and replace the data with it."""
try:
2015-04-13 10:44:49 +02:00
file = open('ASSUMPTIONS.rst', 'r')
2014-04-04 00:41:43 +02:00
except IOError:
2015-04-13 10:44:49 +02:00
file = open('ASSUMPTIONS.rst', 'w')
2014-04-04 00:41:43 +02:00
assumptions = ""
else:
assumptions = file.read()
file.close()
if assumptions.strip() != "":
assumptions = Assum_key + header + assumptions + '\n\n'
has_assumptions = False
for i in range(len(data)):
if data[i].startswith(Assum_key):
has_assumptions = True
data[i] = assumptions
if not has_assumptions:
2015-04-13 10:44:49 +02:00
data.insert(1, assumptions)
2014-04-04 00:41:43 +02:00
return data
def update_needed(data):
"""Read the NEEDED_MODULES file, and replace the data with it.
Create the links to the GitHub pages."""
2014-04-04 00:41:43 +02:00
file = open('NEEDED_CHILDREN_MODULES', 'r')
2014-04-04 00:41:43 +02:00
modules = file.read()
file.close()
if modules.strip() != "":
2015-04-13 10:44:49 +02:00
modules = ['* `%s <%s%s>`_' % (x, URL, x) for x in modules.split()]
modules = "\n".join(modules)
2014-04-04 00:41:43 +02:00
modules = Needed_key + header + modules + '\n\n'
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
def update_documentation(data):
2015-04-13 10:44:49 +02:00
"""Reads the BEGIN_DOC ... END_DOC blocks and builds the documentation"""
# If the file does not exist, don't do anything
try:
file = open('tags', 'r')
except:
return
tags = file.readlines()
2014-05-13 13:57:58 +02:00
file.close()
2015-04-13 10:44:49 +02:00
def extract_doc(item):
"""Extracts the documentation contained in IRPF90_man file"""
file = open("IRPF90_man/%s.l" % (item), 'r')
lines = file.readlines()
file.close()
result = []
inside = False
for line in lines:
if not inside:
inside = line.startswith(".SH Description")
else:
if line.startswith(".SH"):
break
result.append(" " + line.strip())
if result == []:
result = [" Undocumented"]
return "\n".join(result) + '\n'
items = []
dirname = os.path.basename(os.getcwd())
command = "git ls-tree --full-tree --name-only HEAD:src/%s"
command = command % (dirname)
try:
if dirname != 'src':
p = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
tracked_files = p.stdout.read()
else:
tracked_files = ""
tracked_files = tracked_files.splitlines()
except:
tracked_files = []
for filename in tracked_files:
if filename.endswith('.irp.f'):
# Search for providers, subroutines and functions in each file using
# the tags file
search = "\t" + filename + "\t"
tmp = filter(lambda line: search in line, tags)
# Search for the documentation in the IRPF90_man directory
for item in tmp:
item, _, line = item.strip().split('\t')
doc = extract_doc(item)
items.append((item, filename, doc, line))
dirname = os.path.basename(os.getcwd())
# Write the documentation in the README
template = "`%(item)s <%(url)s%(dirname)s/%(filename)s#L%(line)s>`_\n%(doc)s\n"
documentation = Doc_key + header
url = URL
for item, filename, doc, line in items:
documentation += template % locals()
documentation += '\n\n'
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"""
import subprocess
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()
data = update_assumptions(data)
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-04-13 10:44:49 +02:00
if __name__ == '__main__':
main()