1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
import os
import re
import runpy
import traceback
import warnings
from os.path import join
from pathlib import Path
from stat import ST_MTIME
from subprocess import DEVNULL, CalledProcessError, check_call
from docutils import nodes
from docutils.parsers.rst.roles import set_classes
def mol_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
n = []
t = ''
while text:
if text[0] == '_':
n.append(nodes.inline(text=t))
t = ''
m = re.match(r'\d+', text[1:])
if m is None:
raise RuntimeError('Expected one or more digits after "_"')
digits = m.group()
n.append(nodes.subscript(text=digits))
text = text[1 + len(digits) :]
else:
t += text[0]
text = text[1:]
n.append(nodes.inline(text=t))
return n, []
def git_role_tmpl(
urlroot, role, rawtext, text, lineno, inliner, options={}, content=[]
):
env = inliner.document.settings.env
srcdir = Path(env.srcdir)
project_root = srcdir.parent
# The asserts below are commented out because this role template
# is also used by other projects, such as GPAW and Asap.
# assert srcdir.name == 'doc'
# assert project_root.name == 'ase'
if text[-1] == '>':
i = text.index('<')
name = text[: i - 1]
text = text[i + 1 : -1]
else:
name = text
if name[0] == '~':
name = name.split('/')[-1]
text = text[1:]
if '?' in name:
name = name[: name.index('?')]
# Check if the link is broken
is_tag = text.startswith('..') # Tags are like :git:`3.19.1 <../3.19.1>`
path = project_root / text
if not (is_tag or path.exists()):
msg = f'Broken link: {rawtext}: Non-existing path: {path}'
msg = inliner.reporter.error(msg, line=lineno)
prb = inliner.problematic(rawtext, rawtext, msg)
return [prb], [msg]
ref = urlroot + text
set_classes(options)
node = nodes.reference(rawtext, name, refuri=ref, **options)
return [node], []
def git_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
return git_role_tmpl(
'https://gitlab.com/ase/ase/blob/master/',
role,
rawtext,
text,
lineno,
inliner,
options,
content,
)
def setup(app):
app.add_role('mol', mol_role)
app.add_role('git', git_role)
create_png_files()
def creates():
"""Generator for Python scripts and their output filenames."""
for dirpath, dirnames, filenames in sorted(os.walk('.')):
if dirpath.startswith('./build'):
# Skip files in the build/ folder
continue
for filename in sorted(filenames):
if filename.endswith('.py'):
path = join(dirpath, filename)
with open(path) as fd:
lines = fd.readlines()
if len(lines) == 0:
continue
if 'coding: utf-8' in lines[0]:
lines.pop(0)
outnames = []
for line in lines:
if line.startswith('# creates:'):
outnames.extend(
[file.rstrip(',') for file in line.split()[2:]]
)
else:
break
if outnames:
yield dirpath, filename, outnames
def create_png_files(raise_exceptions=False):
from ase.utils import workdir
try:
check_call(['povray', '-h'], stderr=DEVNULL)
except (FileNotFoundError, CalledProcessError):
warnings.warn('No POVRAY!')
# Replace write_pov with write_png:
from ase.io import pov
from ase.io.png import write_png
def write_pov(
filename,
atoms,
povray_settings={},
isosurface_data=None,
**generic_projection_settings,
):
write_png(
Path(filename).with_suffix('.png'),
atoms,
**generic_projection_settings,
)
class DummyRenderer:
def render(self):
pass
return DummyRenderer()
pov.write_pov = write_pov
for dir, pyname, outnames in creates():
path = join(dir, pyname)
t0 = os.stat(path)[ST_MTIME]
run = False
for outname in outnames:
try:
t = os.stat(join(dir, outname))[ST_MTIME]
except OSError:
run = True
break
else:
if t < t0:
run = True
break
if run:
print('running:', path)
with workdir(dir):
import matplotlib.pyplot as plt
plt.figure()
try:
runpy.run_path(pyname)
except KeyboardInterrupt:
return
except Exception:
if raise_exceptions:
raise
else:
traceback.print_exc()
for n in plt.get_fignums():
plt.close(n)
for outname in outnames:
print(dir, outname)
def clean():
"""Remove all generated files."""
for dir, pyname, outnames in creates():
for outname in outnames:
if os.path.isfile(os.path.join(dir, outname)):
os.remove(os.path.join(dir, outname))
def visual_inspection():
"""Manually inspect generated files."""
import subprocess
images = []
text = []
pdf = []
for dir, pyname, outnames in creates():
for outname in outnames:
path = os.path.join(dir, outname)
ext = path.rsplit('.', 1)[1]
if ext == 'pdf':
pdf.append(path)
elif ext in ['csv', 'txt', 'out', 'css', 'LDA', 'rst']:
text.append(path)
else:
images.append(path)
subprocess.call(['eog'] + images)
subprocess.call(['evince'] + pdf)
subprocess.call(['more'] + text)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Process generated files.')
parser.add_argument(
'command',
nargs='?',
default='list',
choices=['list', 'inspect', 'clean', 'run'],
)
args = parser.parse_args()
if args.command == 'clean':
clean()
elif args.command == 'list':
for dir, pyname, outnames in creates():
for outname in outnames:
print(os.path.join(dir, outname))
elif args.command == 'run':
create_png_files(raise_exceptions=True)
else:
visual_inspection()
|