Created
December 18, 2017 14:52
-
-
Save jakab922/9a57e5230087d1be88a78002a30ba747 to your computer and use it in GitHub Desktop.
Script for sanitizing python projects so that code intelligence works in them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os.path | |
import os | |
def touch(fname, times=None): | |
"""via: https://stackoverflow.com/a/1160227/8785384 """ | |
with open(fname, "a"): | |
os.utime(fname, times) | |
def get_parent(path): | |
return os.path.abspath(os.path.join(path, os.pardir)) | |
base = os.path.dirname(os.path.realpath(__file__)) | |
stack = [base] | |
need = set() | |
while stack: | |
curr = stack.pop() | |
subdirs, py_count, has_init = [], 0, False | |
for el in os.listdir(curr): | |
fpath = os.path.join(curr, el) | |
if os.path.isdir(fpath): | |
subdirs.append(fpath) | |
if el.endswith(".py"): | |
py_count += 1 | |
if el == "__init__.py": | |
has_init = True | |
stack.extend(subdirs) | |
if py_count > 0: | |
if not has_init: | |
touch(os.path.join(curr, "__init__.py")) | |
curr = get_parent(curr) | |
while curr in need: | |
touch(os.path.join(curr, "__init__.py")) | |
need.remove(curr) | |
curr = get_parent(curr) | |
elif not has_init: | |
need.add(curr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment