1#! /usr/bin/env python3
2
3# template.py (and other filenames)
4# By Max Eliaser (max.eliaser@intel.com)
5
6# Copyright (c) 2014 Intel Corp.
7
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24# THE SOFTWARE.
25
26# This program acts like a dummy version of the texinfo utilities, creating
27# the right output files but leaving them blank. It will parse out the name
28# of the executable from argv[0] and emulate the corresponding program, so
29# multiple copies of this script will exist under different names.
30
31import sys, os, argparse
32
33
34this_binary = sys.argv[0].split("/")[-1]
35
36# To be outputted if functionality that hasn't been stubbed yet is invoked.
37stub_msg = """
38This stand-in version of %s is not yet fully capable of emulating
39the real version from the GNU texinfo suite. If you see this message, file a
40bug report with details on the recipe that failed.
41""" % this_binary
42
43# Autotools setups query the version, so this is actually necessary. Some of
44# them (lookin' at you, glibc) actually look for the substring "GNU texinfo,"
45# so we put that substring in there without actually telling a lie.
46version_str = """%s (fake texinfo, emulating GNU texinfo) 5.2
47
48Super amazing version which is totally not fake in any way whatsoever.
49Copyright (C) 2014 Intel Corp. Distributed under the terms of the MIT
50license.
51""" % this_binary
52
53simple_binaries = "pod2texi texi2dvi pdftexi2dvi texindex texi2pdf \
54                   txixml2texi install-info ginstall-info \
55                   update-info-dir".split()
56
57# These utilities use a slightly different set of options and flags.
58complex_binaries = "makeinfo texi2any".split()
59
60valid_binaries = simple_binaries + complex_binaries
61
62assert this_binary in valid_binaries, \
63       this_binary + " is not one of " + ', '.join(valid_binaries)
64
65# For debugging
66log_interceptions = False
67if log_interceptions:
68    with open("/tmp/intercepted_" + this_binary, "a") as f:
69        f.write(' '.join([this_binary] + sys.argv[1:]) + '\n')
70
71# Look through the options and flags, and if necessary, touch any output
72# files.
73p = argparse.ArgumentParser()
74if this_binary in complex_binaries:
75    p.add_argument('-E', '--macro-expand', metavar='FILE')
76p.add_argument('-o', '--output', metavar='DEST')
77p.add_argument('--version', action='store_true')
78
79args, unknown = p.parse_known_args()
80
81if args.version:
82    print(version_str)
83    sys.exit(0)
84
85# Check for functionality that isn't implemented yet.
86assert not getattr(args, 'macro_expand', None), \
87    "-E/--macro-expand option not yet supported" + stub_msg
88
89# Check if -o or --output is specified.
90if args.output:
91    with open(args.output, 'w'):
92        pass
93    sys.exit(0)
94
95# The -o/--output option overrides the default. For makeinfo and texi2any,
96# that default is to look for a @setfilename command in the input file.
97# Otherwise, printing nothing to stdout and then exiting should suffice.
98assert this_binary in simple_binaries, \
99       "Don't know how to get default output file name from input file!" + \
100       stub_msg
101