s3:winbind: Check return code of set_blocking()
[Samba.git] / bootstrap / template.py
blob0acfaa21e08d7ba724fa1f582bd7c61b65ea0941
1 #!/usr/bin/env python3
3 # Copyright (C) Catalyst.Net Ltd 2019
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 """
19 Manage dependencies and bootstrap environments for Samba.
21 CLI script to render bootstrap.sh/Dockerfile/Vagrantfile.
23 Author: Joe Guo <joeg@catalyst.net.nz>
24 """
26 import io
27 import os
28 import hashlib
29 import logging
30 import argparse
31 from config import DISTS, VAGRANTFILE, OUT
33 HERE = os.path.abspath(os.path.dirname(__file__))
34 SHA1SUM_FILE_PATH = os.path.join(HERE, 'sha1sum.txt')
35 README_FILE_PATH = os.path.join(HERE, 'README.md')
37 logging.basicConfig(level='INFO')
38 log = logging.getLogger(__file__)
41 def get_files(path):
42 """Get all files recursively in path as a list"""
43 filepaths = []
44 for root, dirnames, filenames in os.walk(path):
45 for filename in filenames:
46 filepath = os.path.join(root, filename)
47 filepaths.append(filepath)
48 return filepaths
51 def get_sha1sum(debug=False):
52 """Get sha1sum for dists + .gitlab-ci.yml"""
53 filepaths = get_files(HERE)
54 m = hashlib.sha1()
55 i = 0
56 for filepath in sorted(list(filepaths)):
57 _filepath = os.path.relpath(filepath)
58 i += 1
59 if filepath == SHA1SUM_FILE_PATH:
60 d = "skip "
61 if debug:
62 print("%s: %s: %s" % (i, d, _filepath))
63 continue
64 if filepath == README_FILE_PATH:
65 d = "skip "
66 if debug:
67 print("%s: %s: %s" % (i, d, _filepath))
68 continue
69 if filepath.endswith('.pyc'):
70 d = "skip "
71 if debug:
72 print("%s: %s: %s" % (i, d, _filepath))
73 continue
74 with io.open(filepath, mode='rb') as _file:
75 _bytes = _file.read()
77 m1 = hashlib.sha1()
78 m1.update(_bytes)
79 d = m1.hexdigest()
80 if debug:
81 print("%s: %s: %s" % (i, d, _filepath))
83 m.update(_bytes)
84 return m.hexdigest()
87 def render(dists):
88 """Render files for all dists"""
89 for dist, config in dists.items():
90 home = config['home']
91 os.makedirs(home, exist_ok=True)
92 for key in ['bootstrap.sh', 'locale.sh', 'packages.yml', 'Dockerfile']:
93 path = os.path.join(home, key)
94 log.info('%s: render "%s" to %s', dist, key, path)
95 with io.open(path, mode='wt', encoding='utf8') as fp:
96 fp.write(config[key])
97 if path.endswith('.sh'):
98 os.chmod(path, 0o755)
100 key = 'Vagrantfile'
101 path = os.path.join(OUT, key)
102 log.info('%s: render "%s" to %s', dist, key, path)
103 with io.open(path, mode='wt', encoding='utf8') as fp:
104 fp.write(VAGRANTFILE)
106 # always calc sha1sum after render
107 sha1sum = get_sha1sum()
108 log.info('write sha1sum to %s: %s', SHA1SUM_FILE_PATH, sha1sum)
109 with io.open(SHA1SUM_FILE_PATH, mode='wt', encoding='utf8') as fp:
110 fp.write(sha1sum + "\n")
113 def main():
114 parser = argparse.ArgumentParser(
115 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
116 description=('Render templates with samba dependencies '
117 'to bootstrap multiple distributions.'))
119 parser.add_argument(
120 '-r', '--render', action='store_true', help='Render templates')
122 parser.add_argument(
123 '-s', '--sha1sum', action='store_true', help='Print sha1sum')
124 parser.add_argument(
125 '-d', '--debug', action='store_true', help='Debug sha1sum')
127 args = parser.parse_args()
128 need_help = True
130 if args.render:
131 render(DISTS)
132 need_help = False
133 if args.sha1sum:
134 # we will use the output to check sha1sum in ci
135 print(get_sha1sum(args.debug))
136 need_help = False
137 if need_help:
138 parser.print_help()
141 if __name__ == '__main__':
142 main()