import samba_utils
def generate_winexesvc_c_from_exe(t):
+ '''generate a C source file with the contents of the given binary'''
src = t.inputs[0].bldpath(t.env)
tgt = t.outputs[0].bldpath(t.env)
fn = t.env.SAMBA_GENERATOR_VARS['WINEXE_FN']
- src_blob = samba_utils.load_file(src)
+
+ try:
+ with open(src, 'rb') as f:
+ src_blob = f.read()
+ f.close()
+ except:
+ print('Failed to read %s to convert to C array' % (src))
+ return -1
def c_array(src):
N = 0
while src:
l = src[:8]
src = src[8:]
- h = ' '.join(["0x%02X," % ord(x) for x in l])
+ # Even files opened in binary mode are read as type "str" in
+ # Python 2, so we need to get the integer ordinal of each
+ # character in the string before we try to convert it to hex.
+ if isinstance(l, str):
+ h = ' '.join(["0x%02X," % ord(x) for x in l])
+ # Files opened in binary mode are read as type "bytes" in
+ # Python 3, so we can convert each individual integer in the
+ # array of bytes to hex directly.
+ else:
+ h = ' '.join(["0x%02X," % x for x in l])
result += "\t\t%s\n" % (h)
return result
src_array = c_array(src_blob)
+ if len(src_array) <= 0:
+ print('Failed to convert %s to C array' % (src))
+ return -1
contents = '''
#include "replace.h"
}
''' % (fn, fn, src_array)
- ret = samba_utils.save_file(tgt, contents)
- assert(ret == True)
+ if not samba_utils.save_file(tgt, contents):
+ print('Failed to write C source file %s' % (tgt))
+ return -1
+ return 0
winexesvc_binaries = ''