File: functionDifferences.py

package info (click to toggle)
dynare 4.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 49,408 kB
  • sloc: cpp: 84,998; ansic: 29,058; pascal: 13,843; sh: 4,833; objc: 4,236; yacc: 3,622; makefile: 2,278; lex: 1,541; python: 236; lisp: 69; xml: 8
file content (204 lines) | stat: -rw-r--r-- 6,270 bytes parent folder | download | duplicates (4)
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
##
## This script recursively goes through the matlab directory and its
## subdirectories in the unstable branch, comparing the calling structures of the
## functions to those in another branch/commit, in this case the 4.2.5 branch
## The output is compatible with a Dynare Wiki page
##
## NB: To err on the side of safety, this script won't clean your workspace so a
## git command to checkout a different branch will fail if your workspace is
## dirty.
##

##
## Copyright (C) 2012 Dynare Team
##
## This file is part of Dynare.
##
## Dynare is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Dynare is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
##

import os
import string
import subprocess
import sys

def runCompare():
    unstable = getFuncDict()
    subprocess.call(['git','checkout','4.2'])

    stable = getFuncDict()
    subprocess.call(['git','checkout','master'])

    us = set(unstable.keys())
    ss = set(stable.keys())

    deletedFunctions = sorted(ss - us)
    makeOldFunctionsWikiTable(deletedFunctions, stable)

    newFunctions = sorted(us - ss)
    makeNewFunctionsWikiTable(newFunctions, unstable)

    outputChanged = set([])
    inputChanged = set([])
    commonFunctions = sorted(ss & us)
    for le in commonFunctions:
        sOut = set(stable[le]['output'])
        uOut = set(unstable[le]['output'])
        sIn = set(stable[le]['input'])
        uIn = set(unstable[le]['input'])

        if len(uOut - sOut):
            outputChanged.add(le)

        if len(uIn - sIn):
            inputChanged.add(le)

    unionChanged = sorted(outputChanged | inputChanged)
    makeUnionChangedWikiTable(unionChanged, stable, unstable)


def makeUnionChangedWikiTable(keys, stable, unstable):
    write = sys.stdout.write
    print "= Functions Whose Arguments Have Changed between Dynare 4.2.5 and " \
        "Dynare 4.3 ="
    print "|| '''Location''' || '''Old Output''' || " \
        "'''New Output''' || '''Old Input''' || '''New Input''' ||"
    for k in keys:
        write('|| {{{' + unstable[k]['filename'][10:]  + '}}} || ')
        write(str(stable[k]['output']) + ' || ')
        write(str(unstable[k]['output']) + ' || ')
        write(str(stable[k]['input']) + ' || ')
        write(str(unstable[k]['input']) + ' ||')
        print


def makeNewFunctionsWikiTable(keys, dictionary):
    print '= New Functions in Dynare 4.3 ='
    makeWikiTable(keys, dictionary)


def makeOldFunctionsWikiTable(keys, dictionary):
    print '= Functions Removed in Dynare 4.3 ='
    makeWikiTable(keys, dictionary)


def makeWikiTable(keys, dictionary):
    write = sys.stdout.write
    print "|| '''Location''' || '''Output''' || '''Input''' ||"
    for k in keys:
        write('|| {{{' + dictionary[k]['filename'][10:]  + '}}} || ')
        write(str(dictionary[k]['output']) + ' || ')
        write(str(dictionary[k]['input']) + ' ||')
        print


def printDict(dictionary, title):
    print
    print '***********************************'
    print '** ' + title
    print '***********************************'
    for key in sorted(dictionary.iterkeys()):
        b = dictionary[key]
        print key + ':' + b['filename']
    print '***********************************'
    print '***********************************'
    print


def getFuncDict():
    functions = {}

    for dirname, dirnames, filenames in os.walk('../matlab'):
        for filename in filenames:
            filename = string.strip(filename)

            if filename[-2:] != '.m' or filename == 'msstart2.m' or filename == 'msstart_setup.m' or filename == 'qmc_sequence.m':
                continue

            fullfilename = os.path.join(dirname, filename)
            f = open(fullfilename, 'r')
            funcDef = ''
            inComment = False
            while True:
                funcDef += f.read(1)
                if inComment:
                    if funcDef[-1:] == '\n':
                        inComment = False
                else:
                    if funcDef[-1:] == '%':
                        inComment = True
                    elif funcDef[-1:] == ')':
                        break
            funcDef = funcDef.strip()
            f.close()

            funcDict = {}
            funcDict['filename'] = fullfilename

            # OUTPUT
            spliteq = string.rsplit(funcDef, '=')
            if len(spliteq) == 1:
                # no output
                outputs = ['']
                funcDict['output'] = outputs
                rhs = string.split(funcDef, ' ', 1).pop(1).strip()
            else:
                outputs = string.split(spliteq.pop(0), ' ', 1).pop(1)
                outputs = [string.strip(outputs, '[] ')]
                outputs = getputs(outputs)
                funcDict['output'] = outputs
                rhs = string.strip(spliteq.pop(0), ' .\n')

            # FUNCTION NAME
            splitfn = string.split(rhs, '(')
            fn = splitfn.pop(0)

            # INPUT
            inputs = [string.strip(le,')') for le in splitfn]
            inputs = getputs(inputs)
            funcDict['input'] = inputs

            functions[fn] = funcDict

    return functions


def getputs(puts):
    ''' Can be of the forms
    a
    a
    a, b
    a, b   % aoeu
    a, ...
       b
    a, ... % aoeu
       b

    Return a list of (in/out puts)
    '''
    splitout = string.split(string.join(puts), '\n')

    for i,le in enumerate(splitout[:]):
        indx = string.find(le, '%')
        if indx != -1:
            splitout[i] = string.strip(le[:indx], '\t .')
        else:
            splitout[i] = string.strip(le, '\t .')

    splitcommas = string.split(string.join(splitout), ',')
    return [string.strip(le) for le in splitcommas]


if __name__ == "__main__":
    runCompare()