# Copyright 2018 Red Hat, Inc # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import cgi html_escape = getattr(cgi, 'escape', None) if not html_escape: import html html_escape = html.escape import argparse import gzip import sys from ansible.module_utils.basic import AnsibleModule HEADER = '''
''' FOOTER = '''''' def run(inpath, outpath): if inpath.endswith('.gz'): infile = gzip.open(inpath, 'rt') outfile = gzip.open(outpath, 'wt') else: infile = open(inpath, 'r') outfile = open(outpath, 'w') outfile.write(HEADER) for i, line in enumerate(infile): i = i + 1 line = html_escape(line.rstrip('\n')) outfile.write('' % (i, i, i)) outfile.write(line.rstrip('\n')) outfile.write("\n") outfile.write(FOOTER) def ansible_main(): module = AnsibleModule( argument_spec=dict( input=dict(required=True, type='path'), output=dict(required=True, type='path'), ) ) p = module.params run(p.get('input'), p.get('output')) module.exit_json(changed=True) def cli_main(): parser = argparse.ArgumentParser( description="HTMLify text files" ) parser.add_argument('input', help='Input path') parser.add_argument('output', help='Output path') args = parser.parse_args() run(args.input, args.output) if __name__ == '__main__': # The zip/ansible/modules check is required for Ansible 5 because # stdin may be a tty, but does not work in ansible 2.8. The tty # check works on versions 2.8, 2.9, and 6. if ('.zip/ansible/modules' in sys.argv[0] or not sys.stdin.isatty()): ansible_main() else: cli_main()