1# -*- coding: utf-8 -*-
2
3"""
4***************************************************************************
5    versioncheck.py
6    ---------------------
7    Date                 : December 2014
8    Copyright            : (C) 2014 by Victor Olaya
9    Email                : volayaf at gmail dot com
10***************************************************************************
11*                                                                         *
12*   This program is free software; you can redistribute it and/or modify  *
13*   it under the terms of the GNU General Public License as published by  *
14*   the Free Software Foundation; either version 2 of the License, or     *
15*   (at your option) any later version.                                   *
16*                                                                         *
17***************************************************************************
18"""
19
20__author__ = 'Victor Olaya'
21__date__ = 'December 2014'
22__copyright__ = '(C) 2014, Victor Olaya'
23
24import os
25import subprocess
26
27
28def getAlgParams(f):
29    params = []
30    booleanparams = []
31    numparams = []
32    with open(f) as lines:
33        line = lines.readline().strip('\n').strip()
34        name = line
35        if '|' in name:
36            tokens = name.split('|')
37            cmdname = tokens[1]
38        else:
39            cmdname = name
40        line = lines.readline().strip('\n').strip()
41        group = line
42        line = lines.readline().strip('\n').strip()
43        while line != '':
44            if line.startswith('Hardcoded'):
45                pass
46            elif line.startswith('AllowUnmatching'):
47                pass
48            elif line.startswith('Extent'):
49                extentParamNames = line[6:].strip().split(' ')
50                params.extend(["-" + p for p in extentParamNames])
51            else:
52                tokens = line.split("|")
53                if tokens[0] == "ParameterBoolean":
54                    booleanparams.append("-" + tokens[1].strip())
55                elif tokens[0] == "ParameterNumber":
56                    numparams.append("-" + tokens[1].strip())
57                else:
58                    params.append("-" + tokens[1])
59            line = lines.readline().strip('\n').strip()
60    return cmdname, group, params, booleanparams, numparams
61
62
63def testDescriptionFile(f):
64    usage = ""
65    cmdname, group, params, booleanparams, numparams = getAlgParams(f)
66    command = [r'd:\saga2.1.2\saga_cmd.exe', group, cmdname]
67    for p in params:
68        command.append(p)
69        command.append("dummy")
70    for p in numparams:
71        command.append(p)
72        command.append("0")
73    command.extend(booleanparams)
74    proc = subprocess.Popen(
75        command,
76        shell=True,
77        stdout=subprocess.PIPE,
78        stdin=subprocess.DEVNULL,
79        stderr=subprocess.STDOUT,
80        universal_newlines=True,
81    ).stdout
82    lines = []
83    for line in iter(proc.readline, ''):
84        lines.append(line)
85        if "Usage" in line:
86            usage = line
87
88    if usage and not lines[0].startswith("_"):
89        # fix_print_with_import
90        print("-" * 50)
91        # fix_print_with_import
92        print(f + " [ERROR]")
93        # fix_print_with_import
94        print(lines)
95        # fix_print_with_import
96        print(usage)
97        # fix_print_with_import
98        print("Name in description:" + cmdname)
99        # fix_print_with_import
100        print("Parameters in description:" + str(params))
101        # fix_print_with_import
102        print("-" * 50)
103        print()
104
105
106if __name__ == '__main__':
107    folder = os.path.join(os.path.dirname(__file__), "description")
108    for descriptionFile in os.listdir(folder):
109        if descriptionFile.endswith('txt'):
110            testDescriptionFile(os.path.join(folder, descriptionFile))
111