dev.py 5.92 KB
Newer Older
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
# Copyright 2018 Markus Scheidgen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an"AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import os
import click

from nomad.cli.cli import cli


@cli.group(help='Commands related to the nomad source code.')
def dev():
    pass


@dev.command(help='Runs tests and linting of the nomad source code. Useful before committing code.')
@click.option('--skip-tests', help='Do no tests, just do code checks.', is_flag=True)
29
30
@click.option('-x', '--exitfirst', help='Stop testing after first failed test case.', is_flag=True)
def qa(skip_tests: bool, exitfirst: bool):
31
32
33
34
    os.chdir(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
    ret_code = 0
    if not skip_tests:
        click.echo('Run tests ...')
35
        ret_code += os.system('python -m pytest -sv%s tests' % ('x' if exitfirst else ''))
36
    click.echo('Run code style checks ...')
37
    ret_code += os.system('python -m pycodestyle --ignore=E501,E701,E731 nomad tests')
38
    click.echo('Run linter ...')
39
    ret_code += os.system('python -m pylint --load-plugins=pylint_mongoengine,nomad/metainfo/pylint_plugin nomad tests')
40
41
42
43
    click.echo('Run static type checks ...')
    ret_code += os.system('python -m mypy --ignore-missing-imports --follow-imports=silent --no-strict-optional nomad tests')

    sys.exit(ret_code)
44
45


46
47
48
@dev.command(help='Generates a JSON with all metainfo.')
def metainfo():
    import json
49
    from nomad.metainfo import Package, Environment
50

51
    # TODO the __init_metainfo__() should not be necessary and automatically performed
52
    # Also load and initialize the datamodel definitions
53
54
55
56
57
58
59
60
61
62
63
64
    import nomad.metainfo.metainfo
    import nomad.datamodel.datamodel
    import nomad.datamodel.dft
    import nomad.datamodel.ems
    import nomad.datamodel.optimade
    import nomad.datamodel.encyclopedia
    nomad.metainfo.metainfo.m_package.__init_metainfo__()
    nomad.datamodel.datamodel.m_package.__init_metainfo__()
    nomad.datamodel.dft.m_package.__init_metainfo__()  # pylint: disable=no-member
    nomad.datamodel.ems.m_package.__init_metainfo__()  # pylint: disable=no-member
    nomad.datamodel.optimade.m_package.__init_metainfo__()  # pylint: disable=no-member
    nomad.datamodel.encyclopedia.m_package.__init_metainfo__()
65

66
67
    # Ensure all parser metainfo is loaded
    from nomad.parsing.parsers import parsers
68
69
70
    for parser in parsers:
        _ = parser.metainfo_env

71
72
73
    export = Environment()
    for package in Package.registry.values():
        export.m_add_sub_section(Environment.packages, package)
74

75
    print(json.dumps(export.m_to_dict(with_meta=True), indent=2))
76
77


Markus Scheidgen's avatar
Setup    
Markus Scheidgen committed
78
79
80
81
82
@dev.command(help='Generates a JSON with all search quantities.')
def search_quantities():
    from nomad import search
    # Due to this import, the parsing module will register all code_names based on parser
    # implementations.
83
    from nomad.parsing.parsers import parser_dict  # pylint: disable=unused-import
Markus Scheidgen's avatar
Setup    
Markus Scheidgen committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    import json

    def to_dict(search_quantity):
        result = {
            'name': search_quantity.qualified_name,
            'description': search_quantity.description,
            'many': search_quantity.many,
        }

        if search_quantity.statistic_fixed_size is not None:
            result['statistic_size'] = search_quantity.statistic_fixed_size
        if search_quantity.statistic_values is not None:
            result['statistic_values'] = search_quantity.statistic_values

        return result

    export = {
        search_quantity.qualified_name: to_dict(search_quantity)
        for search_quantity in search.search_quantities.values()
    }
    print(json.dumps(export, indent=2))


107
@dev.command(help='Generates source-code for the new metainfo from .json files of the old.')
108
109
@click.argument('path', nargs=-1)
def legacy_metainfo(path):
110
111
    from nomad.metainfo.legacy import convert, generate_metainfo_code

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
    if len(path) == 0:
        path = [
            'abinit.nomadmetainfo.json',
            'aptfim.nomadmetainfo.json',
            'atk.nomadmetainfo.json',
            'band.nomadmetainfo.json',
            'bigdft.nomadmetainfo.json',
            'castep.nomadmetainfo.json',
            'cp2k.nomadmetainfo.json',
            'cpmd.nomadmetainfo.json',
            'crystal.nomadmetainfo.json',
            'dl_poly.nomadmetainfo.json',
            'dmol3.nomadmetainfo.json',
            'eels.nomadmetainfo.json',
            'elastic.nomadmetainfo.json',
            'elk.nomadmetainfo.json',
            'exciting.nomadmetainfo.json',
            'fhi_aims.nomadmetainfo.json',
            'fleur.nomadmetainfo.json',
            'gamess.nomadmetainfo.json',
            'gaussian.nomadmetainfo.json',
            'gpaw.nomadmetainfo.json',
            'gulp.nomadmetainfo.json',
            'lib_atoms.nomadmetainfo.json',
            'molcas.nomadmetainfo.json',
            'mpes.nomadmetainfo.json',
            'nwchem.nomadmetainfo.json',
            'octopus.nomadmetainfo.json',
            'onetep.nomadmetainfo.json',
            'orca.nomadmetainfo.json',
            'phonopy.nomadmetainfo.json',
            'photoemission.nomadmetainfo.json',
            'qbox.nomadmetainfo.json',
            'quantum_espresso.nomadmetainfo.json',
            'siesta.nomadmetainfo.json',
            'skeleton.nomadmetainfo.json',
            'turbomole.nomadmetainfo.json',
            'vasp.nomadmetainfo.json',
            'wien2k.nomadmetainfo.json',
            'dft.nomadmetainfo.json',
            'ems.nomadmetainfo.json']

    for element in path:
        env = convert(element)
        generate_metainfo_code(env)