diff options
author | Rob Austein <sra@hactrn.net> | 2013-02-03 22:15:47 +0000 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2013-02-03 22:15:47 +0000 |
commit | 97aedc2884a48313712e9eabef5fb4c8f3cc7109 (patch) | |
tree | 35a8f7b3ed629655310e78ede37d64a37b40dc4e /buildtools | |
parent | e3917b724aeecbbf0f6c4d46cbe2ff2e7e12007d (diff) |
Helper for generating package skeletons.
svn path=/branches/tk377/; revision=5022
Diffstat (limited to 'buildtools')
-rw-r--r-- | buildtools/wrap-tree.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/buildtools/wrap-tree.py b/buildtools/wrap-tree.py new file mode 100644 index 00000000..5aa1c8bc --- /dev/null +++ b/buildtools/wrap-tree.py @@ -0,0 +1,58 @@ +""" +Package a directory tree inside a Python script. This is mostly +useful when generating templates for small trees of files one wants to +generate automatically with some customizations (eg, the skeleton for +some the packaging files needed by some platform or another). + +$Id$ + +Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC") + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. +""" + +import os +import sys + +result = {} + +for root, dirs, files in os.walk("."): + for dn in dirs: + dn = os.path.relpath(os.path.join(root, dn)) + result[dn] = None + for fn in files: + fn = os.path.relpath(os.path.join(root, fn)) + with open(fn, "r") as f: + result[fn] = f.read() + +sys.stdout.write("# Automatically generated. Hack if you like, but beware of overwriting.\n\nimport os\n") + +for k in sorted(result): + v = result[k] + if v is None: + sys.stdout.write("\nos.makedirs(%r)\n" % k) + else: + sys.stdout.write("\nwith open(%r, \"wb\") as f:\n" % k) + lines = v.splitlines() + if v.endswith("\n"): + lines.append("") + sys.stdout.write(" f.write('''\\\n") + while lines: + words = lines.pop(0).replace("\\", "\\\\").split("'''") + sys.stdout.write(words[0]) + for word in words[1:]: + sys.stdout.write("''' + \"'''\" + '''") + sys.stdout.write(word) + if not lines: + sys.stdout.write("''')") + sys.stdout.write("\n") |