OpenCores
URL https://opencores.org/ocsvn/a-z80/a-z80/trunk

Subversion Repositories a-z80

[/] [a-z80/] [trunk/] [modelsim_pre_commit.py] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 8 gdevic
#!/usr/bin/env python3
2 3 gdevic
#
3
# This script prepares ModelSim *.mpf files for committing to git
4
#
5
# Why is this necessary?
6
#
7
# ModelSim notoriously shuffles the list of project files that are stored in its
8
# configuration file (*.mpf) even if no files were added or removed. That results
9
# in a version system (git, in this case) to always report mpf files as changed
10
# and needed to be committed even if there has been no _effective_ change to it.
11
#
12
# This script sorts the list of project files in a consistent way so no change
13
# will result in files looking the same way. In addition, the same is done with
14
# (key value) pairs within each file's line containing properties.
15
#
16
# Run this script before committing changes to git and bogus modifications will
17
# magically dissapear!
18
#
19
#-------------------------------------------------------------------------------
20
#  Copyright (C) 2014  Goran Devic
21
#
22
#  This program is free software; you can redistribute it and/or modify it
23
#  under the terms of the GNU General Public License as published by the Free
24
#  Software Foundation; either version 2 of the License, or (at your option)
25
#  any later version.
26
#
27
#  This program is distributed in the hope that it will be useful, but WITHOUT
28
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
29
#  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
30
#  more details.
31
#-------------------------------------------------------------------------------
32
import os
33
import glob
34
 
35
def fixup():
36
    # Open and read any ModelSim project file (we normally have one per project)
37
    for file in glob.glob("*.mpf"):
38
        in_file_section = 0
39
        # We use the fact that a file property line immediately follows the file name
40
        current_name = ""
41
        pf = {}
42
        with open(file, "r") as f, open(file+".new", "w") as g:
43
            for line in f:
44
                if "Project_File_P_" in line:
45
                    # In addition, sort the "key value" pairs in the property line
46
                    # since ModelSim randomly shuffles them as well
47
                    pp = {}
48
                    prop = line.partition(" = ")[2].split(" ")
49
                    i = 0
50
                    while(i<len(prop)):
51
                        key = prop[i]
52
                        value = prop[i+1]
53
                        # A property value that has a space is enclosed in { .. }
54
                        if "{}" not in value and "{" in value:
55
                            i = i + 1
56
                            value = value + " " + prop[i+1]
57
                        # Another hack: ignore property "last_compile" since it always changes
58
                        if "last_compile" in key:
59
                            value = "1"
60
                        # Another hack: ignore property "ood"
61
                        if "ood" in key:
62
                            value = "0"
63
                        pp[key] = value.strip()
64
                        i = i + 2
65
                    sorted_prop = ""
66
                    for k,v in sorted(pp.items()):
67
                        sorted_prop = sorted_prop + " {0} {1}".format(k,v)
68
                    pf[current_name] = sorted_prop.lstrip() + "\n"
69
                    in_file_section = 1
70
                    continue
71
                if "Project_File_" in line:
72
                    current_name = line.partition(" = ")[2]
73
                    in_file_section = 1
74
                    # When we are already at it, make sure project files are relative to the $ROOT
75
                    if "$ROOT" not in line:
76
                        g.write("; Warning: Path {0} is not relative to the $ROOT!\n".format(current_name.strip()))
77
                    continue
78
                # We are not in the file section any more since we are here
79
                if in_file_section:
80
                    # Flush out our file list in a predictable order
81
                    i = 0
82
                    for k,v in sorted(pf.items()):
83
                        g.write("Project_File_{0} = {1}".format(i, k))
84
                        g.write("Project_File_P_{0} = {1}".format(i, v))
85
                        i = i + 1
86
                    in_file_section = 0
87 8 gdevic
                g.write(line.strip() + "\n") # Trim whitespaces that ModelSim sometimes adds randomly
88 3 gdevic
        # Lastly, replace old mpf file with the new one
89
        os.remove(file)
90
        os.rename(file+".new", file)
91
 
92
# Return to our current directory after each module has been visited
93
abspath = os.path.abspath(__file__)
94
dname = os.path.dirname(abspath)
95
 
96
# Visit each ModelSim project directory...
97
os.chdir("cpu/alu/simulation/modelsim")
98
fixup()
99
os.chdir(dname)
100
 
101
os.chdir("cpu/bus/simulation/modelsim")
102
fixup()
103
os.chdir(dname)
104
 
105
os.chdir("cpu/control/simulation/modelsim")
106
fixup()
107
os.chdir(dname)
108
 
109
os.chdir("cpu/registers/simulation/modelsim")
110
fixup()
111
os.chdir(dname)
112
 
113
os.chdir("cpu/toplevel/simulation/modelsim")
114
fixup()
115
os.chdir(dname)
116
 
117 8 gdevic
os.chdir("host/basic_de1/simulation/modelsim")
118 3 gdevic
fixup()
119
os.chdir(dname)

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.