I use an out-of-tree builds with CMake.
I have a CMake custom command that generates *_pb2.py files from proto-files.
Since proto-files may reside in an unknown number of subdirectories (package namespace), like $SRC/package1/package2/file.proto
, then the build directory will contain something like $BLD/package1/package2/file_pb2.py
.
I want to implicitly make packages from auto-generated *_pb2.py files and, thus, I want to automagically generate __init__.py files in all subfolders ($BLD/package1
, $BLD/package1/package2
, etc.) and then install them.
How can I do that?
P.S. I've tried macro from CMake : How to get the name of all subdirectories of a directory? (changed GLOB to GLOB_RECURSE) but it returns only subdirs that contain files. I can't get package1
subdir from example above.
If you are working under a *NIX os (including mac) you could use the shell find command like:
ROOT="./"
for DIR in $(find $ROOT -type d); do
touch $DIR/__init__.py
done
or with a python script:
from os.path import isdir, walk, join
root = "/path/to/project"
finit = '__init__.py'
def visitor(arg, dirname, fnames):
fnames = [fname for fname in fnames if isdir(fname)]
# here you could do some additional checks ...
print "adding %s to : %s" %(finit, dirname)
with open(join(dirname, finit), 'w') as file_: file_.write('')
walk(root, visitor, None)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With