aboutsummaryrefslogtreecommitdiff
path: root/buildtools/wrap-tree.py
blob: 2123b79bc3022e2fa7c35ef6d4ae34066963f30d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
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 in sys.argv[1:] or ["."]:
  if root != ".":
    result[root] = None
  for dirpath, dirs, files in os.walk(root):
    for dn in dirs:
      dn = os.path.relpath(os.path.join(dirpath, dn))
      result[dn] = None
    for fn in files:
      fn = os.path.relpath(os.path.join(dirpath, 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")