Created
February 1, 2017 21:25
-
-
Save naterh/c8520addd42283381a89878de56c801f to your computer and use it in GitHub Desktop.
auto update __init__.py files for namespace packages installer
This file contains 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
ignore_keywords = ['build', 'egg-info', 'dist', 'frontend', 'scratch', 'doc'] | |
def ignore_check(folder): | |
for i in ignore_keywords: | |
if folder.endswith(i) or i in folder.split('/'): | |
return True | |
return False | |
def check_init( | |
filename, txt="__import__('pkg_resources').declare_namespace(__name__)" | |
): | |
with open(filename, 'rb') as f: | |
return any(txt in line for line in f) | |
def update_init( | |
filename, txt="__import__('pkg_resources').declare_namespace(__name__)" | |
): | |
with open(filename, 'a+') as f: | |
f.write('\n{}\n'.format(txt)) | |
def namespace_init(folder='.'): | |
for foldername, subfolders, filenames in os.walk(folder): | |
if not ignore_check(foldername): | |
for subfolder in [x for x in subfolders]: | |
namespace_init(subfolder) | |
if '__init__.py' in filenames: | |
if not check_init(os.path.join(foldername, '__init__.py')): | |
update_init(os.path.join(foldername, '__init__.py')) | |
else: | |
update_init(os.path.join(foldername, '__init__.py')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment