def ispythonsource(path): if path is None: return False assert_exists(path) if splitext(path)[1] == ".py": return True binary = isbinaryfile(path) if not binary: _shebang = shebang(path) if not _shebang: return False return "python" in _shebang return False
def shebang(path): """return file/string shebang""" if not path: return path = str(path) if not os.path.exists(path): return if isbinaryfile(path): return content = read(path) lines = content.splitlines() if lines: l = lines[0] # first line if isshebang(l): l = l.replace("#!", "", 1) return l.lstrip().rstrip()
def getdecoratorslines(obj): module = inspect.getmodule(obj) if not module: return [] if not hasattr(module, "__file__"): return [] filename = module.__file__.replace(".pyc", ".py") if not os.path.exists(filename) or isbinaryfile(filename): return [] lines, lineno = inspect.getsourcelines(obj) for l in lines: l = l.lstrip().rstrip() if l.lstrip().rstrip().find("#") == 0: continue # skip comment if l.lstrip().rstrip().find("@") == 0: lines.append(l.lstrip().rstrip()[1:]) else: if lineno > 0: # object if inspect.isclass(obj): continue break return lines