def _iter_log_names(self): dups = set() for well_known in ('Application', 'Security', 'System'): dups.add(well_known) yield well_known key = OpenKeyEx(HKEY_LOCAL_MACHINE, r'SYSTEM\CurrentControlSet\Services\EventLog', 0, KEY_READ) try: idx = 0 while True: try: source = EnumKey(key, idx) if source in dups: continue dups.add(source) yield source except WindowsError: break finally: idx += 1 finally: CloseKey(key)
def getEditorCommand(self): """Return the editor command""" editor = self.options.get('editor') if win32 and editor is None: from _winreg import HKEY_CLASSES_ROOT, OpenKeyEx, \ QueryValueEx, EnumKey from win32api import FindExecutable import pywintypes # Find editor application based on mime type and extension content_type = self.metadata.get('content_type') extension = self.options.get('extension') if content_type: # Search registry for the extension by MIME type try: key = 'MIME\\Database\\Content Type\\%s' % content_type key = OpenKeyEx(HKEY_CLASSES_ROOT, key) extension, nil = QueryValueEx(key, 'Extension') except EnvironmentError: pass if extension is None: url = self.metadata['url'] dot = url.rfind('.') if dot != -1 and dot > url.rfind('/'): extension = url[dot:] if extension is not None: try: key = OpenKeyEx(HKEY_CLASSES_ROOT, extension) classname, nil = QueryValueEx(key, None) except EnvironmentError: classname = None if classname is not None: try: # Look for Edit action in registry key = OpenKeyEx(HKEY_CLASSES_ROOT, classname+'\\Shell\\Edit\\Command') editor, nil = QueryValueEx(key, None) except EnvironmentError: pass if editor is None: # Enumerate the actions looking for one # starting with 'Edit' try: key = OpenKeyEx(HKEY_CLASSES_ROOT, classname+'\\Shell') index = 0 while 1: try: subkey = EnumKey(key, index) index += 1 if str(subkey).lower().startswith('edit'): subkey = OpenKeyEx(key, subkey + '\\Command') editor, nil = QueryValueEx(subkey, None) else: continue except EnvironmentError: break except EnvironmentError: pass if editor is None: try: # Look for Open action in registry key = OpenKeyEx(HKEY_CLASSES_ROOT, classname+'\\Shell\\Open\\Command') editor, nil = QueryValueEx(key, None) except EnvironmentError: pass if editor is None: try: nil, editor = FindExecutable(self.content_file, '') except pywintypes.error: pass # Don't use IE as an "editor" if editor is not None and editor.find('\\iexplore.exe') != -1: editor = None if not editor and not win32 and has_tk(): from tkSimpleDialog import askstring editor = askstring('Zope External Editor', 'Enter the command to launch the default editor') if not editor: sys.exit(0) self.config.set('general', 'editor', editor) self.config.save() if editor is not None: return editor else: fatalError('No editor was found for that object.\n' 'Specify an editor in the configuration file:\n' '(%s)' % self.config.path)
if ev_obj.SourceName not in self._formatters_cache and ev_obj.SourceName not in BLACKLIST: source_name = ev_obj.SourceName if type(source_name) == str: source_name = source_name.decode(getdefaultencoding()) subkey = ur'SYSTEM\CurrentControlSet\Services\EventLog\{}\{}'.format( logtype, source_name ) try: subkey = subkey.encode(getdefaultencoding()) except UnicodeEncodeError: subkey = subkey.encode('utf-8') try: key = OpenKeyEx(HKEY_LOCAL_MACHINE, subkey, 0, KEY_READ) except WindowsError: continue try: dllNames, _ = QueryValueEx(key, 'EventMessageFile') for dllName in dllNames.split(';'): dllName = expandvars(dllName.strip()) if not isfile(dllName): continue dllHandle = LoadLibraryEx( dllName, 0, LOAD_LIBRARY_AS_DATAFILE) if not dllHandle:
def getEditorCommand(self): """Return the editor command""" editor = self.options.get('editor') if win32 and editor is None: from _winreg import HKEY_CLASSES_ROOT, OpenKeyEx, \ QueryValueEx, EnumKey from win32api import FindExecutable, ExpandEnvironmentStrings # Find editor application based on mime type and extension content_type = self.metadata.get('content_type') extension = self.options.get('extension') logger.debug('Have content type: %r, extension: %r', content_type, extension) if content_type: # Search registry for the extension by MIME type try: key = 'MIME\\Database\\Content Type\\%s' % content_type key = OpenKeyEx(HKEY_CLASSES_ROOT, key) extension, nil = QueryValueEx(key, 'Extension') logger.debug( 'Registry has extension %r for ' 'content type %r', extension, content_type) except EnvironmentError: pass if extension is None: url = self.metadata['url'] dot = url.rfind('.') if dot != -1 and dot > url.rfind('/'): extension = url[dot:] logger.debug('Extracted extension from url: %r', extension) classname = editor = None if extension is not None: try: key = OpenKeyEx(HKEY_CLASSES_ROOT, extension) classname, nil = QueryValueEx(key, None) logger.debug('ClassName for extension %r is: %r', extension, classname) except EnvironmentError: classname = None if classname is not None: try: # Look for Edit action in registry key = OpenKeyEx(HKEY_CLASSES_ROOT, classname + '\\Shell\\Edit\\Command') editor, nil = QueryValueEx(key, None) logger.debug('Edit action for %r is: %r', classname, editor) except EnvironmentError: pass if classname is not None and editor is None: logger.debug( 'Could not find Edit action for %r. ' 'Brute-force enumeration.', classname) # Enumerate the actions looking for one # starting with 'Edit' try: key = OpenKeyEx(HKEY_CLASSES_ROOT, classname + '\\Shell') index = 0 while 1: try: subkey = EnumKey(key, index) index += 1 if str(subkey).lower().startswith('edit'): subkey = OpenKeyEx(key, subkey + '\\Command') editor, nil = QueryValueEx(subkey, None) if editor is None: continue logger.debug( 'Found action %r for %r. ' 'Command will be: %r', subkey, classname, editor) except EnvironmentError: break except EnvironmentError: pass if classname is not None and editor is None: try: # Look for Open action in registry key = OpenKeyEx(HKEY_CLASSES_ROOT, classname + '\\Shell\\Open\\Command') editor, nil = QueryValueEx(key, None) logger.debug('Open action for %r has command: %r. ', classname, editor) except EnvironmentError: pass if editor is None: try: nil, editor = FindExecutable(self.content_file, '') logger.debug('Executable for %r is: %r. ', self.content_file, editor) except pywintypes.error: pass # Don't use IE as an "editor" if editor is not None and editor.find('\\iexplore.exe') != -1: logger.debug('Found iexplore.exe. Skipping.') editor = None if editor is not None: return ExpandEnvironmentStrings(editor) if editor is None: fatalError('No editor was found for that object.\n' 'Specify an editor in the configuration file:\n' '(%s)' % self.config.path) return editor
def run(self): if sys.platform != 'win32': raise DistutilsPlatformError("InnoSetup distributions must be" " created on a Windows platform") # Locate information about Inno Setup from the registry from _winreg import OpenKeyEx, QueryValueEx, HKEY_LOCAL_MACHINE try: key = OpenKeyEx( HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\Windows' r'\CurrentVersion\Uninstall' r'\Inno Setup 5_is1') inno_version = QueryValueEx(key, 'DisplayVersion')[0] inno_path = QueryValueEx(key, 'InstallLocation')[0] except WindowsError: raise DistutilsPlatformError( 'Inno Setup version %s to %s is required to build the' ' installer, but was not found on this system.' % (INNO_MIN_VERSION, INNO_MAX_VERSION)) if inno_version < INNO_MIN_VERSION or inno_version > INNO_MAX_VERSION: raise DistutilsPlatformError( 'Inno Setup version %s to %s is required to build the' ' installer, but version %s was found on this system.' % (INNO_MIN_VERSION, INNO_MAX_VERSION, inno_version)) iss_compiler = os.path.join(inno_path, 'iscc.exe') if not self.skip_build: self.run_command('build') self.mkpath(self.bdist_dir) config = self.reinitialize_command('config') config.cache_filename = None config.prefix = 'PREFIX' config.ensure_finalized() install = self.reinitialize_command('install') install.root = self.bdist_dir install.skip_build = self.skip_build # Always include "compiled" py-files install.compile = install.optimize = self.byte_compile # don't warn about installing into a directory not in sys.path install.warn_dir = False # always include documentation install.with_docs = True self.announce("installing to %s" % self.bdist_dir, 2) install.ensure_finalized() install.run() if self.license_file: self.copy_file(self.license_file, self.bdist_dir) # create the InnoSetup script iss_file = self.build_iss_file() iss_path = os.path.join(self.bdist_dir, '%s.iss' % self.distribution.get_name()) self.announce("writing %r" % iss_path) if not self.dry_run: f = open(iss_path, "w") f.write(iss_file) f.close() # build distribution using the Inno Setup 5 Command-Line Compiler self.announce("creating Inno Setup installer", 2) # log.info self.spawn([iss_compiler, iss_path]) # Add an empty ZIP file to the installer executable to allow for # uploading to PyPI (it checks for the bdist_wininst format). dist_filename = self.get_installer_filename() if os.path.exists(dist_filename) and not self.dry_run: install_egg_info = self.get_finalized_command('install_egg_info') install_dir = install_egg_info.install_dir zip_dir = 'PLATLIB' if install_dir.endswith(os.sep): zip_dir += os.sep zip_file = zipfile.ZipFile(dist_filename, 'a') for filename in install_egg_info.get_outputs(): arcname = filename.replace(install_dir, zip_dir, 1) zip_file.write(filename, arcname) zip_file.close() # Add to 'Distribution.dist_files' so that the "upload" command works if hasattr(self.distribution, 'dist_files'): target_version = self.target_version or 'any' spec = ('bdist_wininst', target_version, dist_filename) self.distribution.dist_files.append(spec) if not self.keep_temp: remove_tree(self.bdist_dir, self.verbose, self.dry_run) return
def get_events(self, logtype, server=''): log = OpenEventLog(server, logtype) if not log: return flags = EVENTLOG_BACKWARDS_READ | EVENTLOG_SEQUENTIAL_READ events = ReadEventLog(log, flags, 0) try: events = True while events: events = ReadEventLog(log, flags, 0) if not events: break for ev_obj in events: if not ev_obj.StringInserts: continue message = None if ev_obj.SourceName not in self._formatters_cache and ev_obj.SourceName not in BLACKLIST: try: key = OpenKeyEx( HKEY_LOCAL_MACHINE, r'SYSTEM\CurrentControlSet\Services\EventLog\{}\{}' .format(logtype, ev_obj.SourceName), 0, KEY_READ) except WindowsError: continue try: dllNames, _ = QueryValueEx(key, 'EventMessageFile') for dllName in dllNames.split(';'): dllName = expandvars(dllName.strip()) if not isfile(dllName): continue dllHandle = LoadLibraryEx( dllName, 0, LOAD_LIBRARY_AS_DATAFILE) if not dllHandle: continue try: message = FormatMessageW( FORMAT_MESSAGE_FROM_HMODULE, dllHandle, ev_obj.EventID, LANGID, ev_obj.StringInserts) except error: FreeLibrary(dllHandle) continue if message: self._formatters_cache[ ev_obj.SourceName] = dllHandle break if not message: self._formatters_cache[ ev_obj.SourceName] = None message = '\n'.join(ev_obj.StringInserts) except WindowsError: self._formatters_cache[ev_obj.SourceName] = None elif ev_obj.SourceName in BLACKLIST or not self._formatters_cache[ ev_obj.SourceName]: message = '\n'.join(ev_obj.StringInserts) else: try: message = FormatMessageW( FORMAT_MESSAGE_FROM_HMODULE, self._formatters_cache[ev_obj.SourceName], ev_obj.EventID, LANGID, ev_obj.StringInserts) except error: message = '\n'.join(ev_obj.StringInserts) user = '' if ev_obj.Sid is not None: try: domain, domain_user, _ = LookupAccountSid( server, ev_obj.Sid) user = u'{}\\{}'.format(domain, domain_user) except error: user = str(ev_obj.Sid) if not message: continue yield { 'id': int(winerror.HRESULT_CODE(ev_obj.EventID)), 'record': ev_obj.RecordNumber, 'date': int(ev_obj.TimeGenerated), 'computer': ev_obj.ComputerName, 'category': ev_obj.EventCategory, 'msg': message, 'source': ev_obj.SourceName, 'type': EventLog.event_types.get(ev_obj.EventType, 'UNKNOWN'), 'user': user } except GeneratorExit: pass finally: for source in self._formatters_cache.keys(): if self._formatters_cache[source]: FreeLibrary(self._formatters_cache[source]) del self._formatters_cache[source] CloseEventLog(log)
'''Find where InnoSetup exeutable is''' __author__ = "Miki Tebeka <*****@*****.**>" from _winreg import OpenKeyEx, EnumKey, QueryValueEx, CloseKey, \ HKEY_LOCAL_MACHINE from os.path import join from itertools import count uninst = r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" key = OpenKeyEx(HKEY_LOCAL_MACHINE, uninst) i = 0 for i in count(): # When i will becode too large we'll get WindowsError innokey = EnumKey(key, i) if "Inno Setup" in innokey: break ikey = OpenKeyEx(key, innokey) instdir, type = QueryValueEx(ikey, "InstallLocation") CloseKey(ikey) CloseKey(key) print join(instdir, "ISCC.exe")