Package gmisclib :: Module makemake
[frames] | no frames]

Source Code for Module gmisclib.makemake

  1   
  2  """This module is designed to build makefiles.""" 
  3   
  4  import os 
  5  import re 
  6  import sys 
  7  import tempfile 
  8  import datetime 
  9  import subprocess 
 10   
 11  from gmisclib import die 
 12  from gmisclib import avio 
 13  from gmisclib import fiatio 
 14  from gmisclib import gpkmisc 
 15   
 16  _debug = False 
 17  _log = None 
 18  makeflags = [] 
 19  makefile = None 
 20  _makefd = None 
 21  _makeprog = ['make'] 
 22  _did_header = False 
 23  DB_format = 'fiatio' 
 24   
 25   
26 -def maketemp(dir='/tmp', prefix='maketemp', suffix=''):
27 global makefile, _makefd 28 gpkmisc.makedirs(dir) 29 handle, makefile = tempfile.mkstemp(dir=dir, prefix=prefix, suffix=suffix) 30 _makefd = os.fdopen(handle, 'w')
31 32
33 -def _write(*s):
34 global _makefd 35 for x in s: 36 _makefd.writelines([x, '\n'])
37 38
39 -def _header():
40 global _did_header 41 if _did_header: 42 return 43 _write("# Makefile produced by gmisclib/makemake.py") 44 _write(".SUFFIXES:", "") 45 _write(".PHONY: all", "") 46 _did_header = True
47 48
49 -def setlog(f):
50 global _log 51 _log = open(f, 'a')
52 53
54 -def log(*s):
55 """Log some strings, one per line.""" 56 for x in s: 57 _log.writelines([x, '\n']) 58 _log.flush()
59 60 61 62 _qp = re.compile('[^a-zA-Z0-9_.+,/:-]')
63 -def quote(s):
64 def replf(x): 65 return '\\%s' % x
66 return re.sub(_qp, replf, s) 67 68
69 -def var(k, v):
70 """Pass a variable to make.""" 71 _header() 72 _write('%s = %s' % (k, v))
73 74
75 -def rule(a, *b):
76 """Writes a rule into a makefile. All lines except the 77 first are indented. 78 """ 79 _write('', a) 80 _write( *['\t%s' % t for t in b] ) 81 _write('')
82 83
84 -def blank():
85 _header() 86 _write("")
87 88
89 -def set_debug():
90 global _debug 91 _debug = True
92 93
94 -def finish():
95 global _makefd, makefile, makeflags 96 # _makefd.flush() 97 # os.fsync(_makefd.fileno()) 98 _makefd.close() 99 if _debug: 100 tmp = open(makefile, 'r') 101 sys.stdout.writelines(tmp.readlines()) 102 else: 103 args = _makeprog + ['-f', makefile] + makeflags 104 print '# calling', args 105 rv = subprocess.call(args) 106 os.remove(makefile) 107 if rv != 0: 108 die.info("CALL: %s" % (' '.join(args))) 109 die.die("Make fails with %d" % rv)
110
111 -def date():
112 return datetime.datetime.now().ctime()
113 114
115 -class FileNotFound(Exception):
116 - def __init__(self, *s):
117 Exception.__init__(self, *s)
118 119 120
121 -def path_to(s):
122 for d in os.environ['PATH'].split(':'): 123 tmp = os.path.join(d, s) 124 if os.access(tmp, os.X_OK): 125 return tmp 126 raise FileNotFound, s
127 128
129 -def set_make_prog(*s):
130 global _makeprog 131 try: 132 path_to(s[0]) 133 except FileNotFound: 134 die.die("Specified make program '%s' not on PATH" % s[0]) 135 _makeprog = s
136 137
138 -def ncpu():
139 """ 140 @rtype: str !Not an integer! 141 @return: a string representation of the integer number of cores that 142 the computer has. 143 """ 144 n = 0 145 for x in open('/proc/cpuinfo', 'r'): 146 if x.split(':')[0].strip() == 'processor': 147 n += 1 148 assert n > 0, "Silly!" 149 return str(n)
150 151
152 -def read(fn):
153 if DB_format == 'avio': 154 h, d, c = avio.read_hdc(open(fn, 'r')) 155 elif DB_format == 'fiatio': 156 h, d, c = fiatio.read(open(fn, 'r')) 157 else: 158 die.die("Unknown metadata format: %s" % DB_format) 159 return h, d
160