#!/usr/bin/env python2 # -*- coding: UTF-8 -*- # Copyright 2015 Red Hat, Inc. # Part of clufter project # Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt) """Machinery entry point -- to be run from in-repo location""" __author__ = "Jan Pokorný " # influenced with env. variables: # - LOGLEVEL: redefine logging level (default: debug) if LOGSKIP not present # - PREFERSYS: whether to prefer site-package upon clash (default: false) import sys from inspect import getabsfile from os.path import abspath, basename, dirname, join, realpath, split from os import environ, listdir # inspired by http://stackoverflow.com/a/22881871 def get_script_path(follow_symlinks=True): if getattr(sys, 'frozen', False): # PyInstaller, cx_Freeze path = abspath(sys.executable) else: path = getabsfile(get_script_path) return path script_path = get_script_path() real_script_path = realpath(script_path) if (basename(real_script_path) != basename(sys.argv[0]) and 'LOGSKIP' not in globals()): # when execfile'd from tests or so, verbose logging desired import logging logging.basicConfig() logging.getLogger().setLevel(environ.get('LOGLEVEL') or logging.DEBUG) root = dirname(real_script_path) # XXX very similar to setup.py, but better self_discovery_plan = [join(root, '__project__')] while True: pkg, backup_mod, maybe_root = {}, None, self_discovery_plan.pop() maybe_root_real_head, maybe_root_real = split(realpath(maybe_root)) maybe_root = basename(maybe_root) try: sys.path.insert(0, maybe_root_real_head) backup_mod = sys.modules.get(maybe_root_real) if backup_mod: if not hasattr(backup_mod, '__path__'): # not the case for builtins continue backup_mod = None if not environ.get('PREFERSYS', 0): backup_mod = sys.modules.pop(maybe_root_real) pkg = __import__(maybe_root_real, globals=pkg) break except ImportError: sys.path.pop(0) if backup_mod: sys.modules[maybe_root_real] = backup_mod if maybe_root == '__project__': from fnmatch import filter as fnfilter self_discovery_plan.extend(p[:-len('.egg-info')].split('-', 1)[0] for p in fnfilter(listdir(root), '*.egg-info')) if not self_discovery_plan: print "Cannot find myself, please help me with __project__ symlink" raise sys.path.pop(0) # set the correct __package__ for relative imports __package__ = maybe_root_real # XXX optionally strip whole '-..' part assert __package__ in sys.modules if __name__ == '__main__': if basename(real_script_path) == basename(sys.argv[0]): # __file__ undef? from . import __main__ # XXX previous attempt to get unittest.main executed automagically at the end, # which was failing likely because unittest uses Threading that register # another atexit handler somehow interfering w/ its main started from here # elif ('tests' == basename(dirname(script_path)) # == basename(dirname(abspath(__file__))): # from atexit import register # from unittest import main # register(main) # # hmm, see https://code.google.com/p/modwsgi/issues/detail?id=197 # # https://github.com/GrahamDumpleton/mod_wsgi/commit/fdef274 # register(lambda: __import__('dummy_threading'))