/ pinxml.py
pinxml.py
 1  #!/usr/bin/python
 2  # coding=utf-8
 3  #    build hostmot2 firmwares
 4  #    Copyright © 2009 Jeff Epler <jepler@unpythonic.net>
 5  #
 6  #    This program is free software; you can redistribute it and/or modify
 7  #    it under the terms of the GNU General Public License as published by
 8  #    the Free Software Foundation; either version 2 of the License, or
 9  #    (at your option) any later version.
10  #
11  #    This program is distributed in the hope that it will be useful,
12  #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  #    GNU General Public License for more details.
15  #
16  #    You should have received a copy of the GNU General Public License
17  #    along with this program; if not, write to the Free Software
18  #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  import atexit
20  import os
21  import shutil
22  import string
23  import sys
24  import re
25  import tempfile
26  import cards
27  
28  card = sys.argv[1]
29  card = cards.get_card(card)
30  pinvhdl = sys.argv[2]
31  cardvhdl = card.name + "card"
32  
33  _sq_whitelist = string.lowercase + string.uppercase + string.digits + ".-_/"
34  def sq(a):
35      if not a.strip(_sq_whitelist):
36          return a
37      return "'" + a.replace("'", "'\\''") + "'"
38  
39  def run(*args):
40      print >>sys.stderr, "#", " ".join([sq(a) for a in args])
41      r = os.spawnvp(os.P_WAIT, args[0], args)
42      print >>sys.stderr, "# exited with", r
43      if r:
44          raise SystemExit, r
45  
46  def p(*x): return os.path.join(d, *x)
47  
48  def subst(in_, out, **kw):
49      def rfn(m):
50          g = m.group(1)
51          if g == '': return '@'
52          return kw[g.upper()]
53      r = re.compile("@([^@]*)@")
54      s = open(in_).read()
55      s = r.sub(rfn, s)
56      open(out, "w").write(s)
57  
58  d = tempfile.mkdtemp(prefix='hm2')
59  print >>sys.stderr, "# tempdir", sq(d)
60  atexit.register(shutil.rmtree, d)
61  
62  shutil.copy("IDROMConst.vhd", p("IDROMConst.vhd"))
63  shutil.copy("idrom_tools.vhd", p("idrom_tools.vhd"))
64  shutil.copy("PIN_%s.vhd" % pinvhdl, p("PIN_%s.vhd") % pinvhdl)
65  shutil.copy("%s.vhd" % cardvhdl, p("%s.vhd") % cardvhdl)
66  subst("xmlrom.vhd.in", p("xmlrom_%s.vhd") % pinvhdl,
67      PIN=pinvhdl, CARD=cardvhdl)
68  
69  orgdir = os.getcwd()
70  os.chdir(d)
71  run("ghdl", "-a", "-fexplicit", "--ieee=synopsys",
72      "IDROMConst.vhd",
73      "idrom_tools.vhd",
74      "PIN_%s.vhd" % pinvhdl,
75      "%s.vhd" % cardvhdl,
76      "xmlrom_%s.vhd" % pinvhdl)
77  run("ghdl", "-e", "-fexplicit", "--ieee=synopsys", "xmlrom_%s" % pinvhdl)
78  run("ghdl", "-r", "xmlrom_%s" % pinvhdl)