#!/usr/bin/python import struct import os import sys import string import datetime import re #### util def oldness(f): if not os.path.exists(f): return 'file doesn\'t exist' d = datetime.datetime.now() - datetime.datetime.fromtimestamp(os.stat(f).st_mtime) if d.days > 0: s = '%d days' % d.days elif d.seconds > 3600: s = '%d hours' % int(d.seconds / 3600) elif d.seconds > 60: s = '%d minutes' % int(d.seconds / 60) s = '%d seconds' % d.seconds return s, d.seconds def get_lib(fname, rexp): l = os.popen('ldd "%s"' % fname).readlines() refs_raw=[x.split('=>') for x in l if '=>' in x] refs=[(r[0].strip().split()[0], r[1].strip().split()[0]) for r in refs_raw] ref = [r[1] for r in refs if re.match(rexp, r[0])] if len(ref) == 0: return 'library not found' return ref[0] #### small resource parsing library """ typedef struct { DWORD DataSize; DWORD HeaderSize; DWORD TYPE; DWORD NAME; DWORD DataVersion; WORD MemoryFlags; WORD LanguageId; DWORD Version; DWORD Characteristics; } RESOURCEHEADER; """ def parse_resource_proper(rsrc): # http://www.fine-view.com/jp/labs/doc/res32fmt.htm # TODO off = 0 def get(d): l = struct.calcsize(d) return off + l, struct.unpack(d, rsrc[off:off+l]) # data_size - extra space following header, not including padding # header_size - size of header structure # res_type - resource type id (standard or custom) off, (data_size, header_size) = get(' 1] out = {} ind = 0 #print '\n'.join('%2d: %r' % (i, s) for i, s in enumerate(strings)) while ind < len(strings) - 1: key = strings[ind] #print ind, repr(key) if key == 'VS_VERSION_INFO': out['VS_VERSION_INFO'] = strings[ind+1] ind += 2 continue if set(key) & printable == set(): ind += 1 continue key_i = looks_like_a_string_key(key) if key_i: out[key[key_i:]] = strings[ind + 1] ind += 2 continue ind += 1 return out parse_resource = resource_to_strings #### try: import pefile except: if os.path.exists('/usr/bin/yum'): print "please do 'yum install python-pefile'" else: print "missing python pefile module" raise SystemExit if __name__ == '__main__': f=sys.argv[-1] if not os.path.exists(f): print "%s doesn't exist" % f raise SystemExit try: pe=pefile.PE(f) except: print "can't parse pe header - is this a PE file?" raise SystemExit rsrc = [s for s in pe.sections if s.Name.startswith('.rsrc\x00')] if len(rsrc) == 0: print "missing .rsrc section, no version information in this file" raise SystemExit d = parse_resource(rsrc[0].data) n = max(map(len, d.keys())) #print '\n'.join('%s: %r' % (k.ljust(n), v) for k,v in d.items()) age_string, age_seconds = oldness(f) print '%s: FV: %r, PV: %r\nage: %s' % (f, d['FileVersion'], d['ProductVersion'], age_string) if age_seconds < 60: sys.exit(0) sys.exit(1)