xref: /netbsd-src/external/bsd/nsd/dist/contrib/bind2nsd/bind2nsd/Utils.py (revision d83a80ee7fb31190352cf1f781441e06ca6a86db)
1#!/usr/bin/env python
2# Copyright (c) 2007, Secure64 Software Corporation
3#
4# Permission is hereby granted, free of charge, to any person obtaining a copy
5# of this software and associated documentation files (the "Software"), to deal
6# in the Software without restriction, including without limitation the rights
7# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8# copies of the Software, and to permit persons to whom the Software is
9# furnished to do so, subject to the following conditions:
10#
11# The above copyright notice and this permission notice shall be included in
12# all copies or substantial portions of the Software.
13#
14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20# THE SOFTWARE.
21#
22#
23#
24#	utility functions used throughout the package
25#
26#
27
28import popen2
29import sys
30
31VERBOSE = False
32
33def set_verbosity(flag):
34   global VERBOSE
35   VERBOSE = flag
36   return
37
38def isVerbose():
39   global VERBOSE
40   return VERBOSE
41
42def report_error(somestuff):
43   if len(somestuff) > 0:
44      print >> sys.stderr, somestuff
45      sys.stderr.flush()
46   return
47
48def report_info(somestuff):
49   global VERBOSE
50   if VERBOSE and len(somestuff) > 0:
51      print >> sys.stdout, somestuff
52      sys.stdout.flush()
53   return
54
55def bail(somestuff):
56   report_error(somestuff)
57   sys.exit(1)
58   return
59
60def run_cmd_capture(cmd):
61   (cout, cin, cerr) = popen2.popen3(cmd)
62
63   cin.close()
64
65   output = cout.readlines()
66   cout.close()
67
68   errors = cerr.readlines()
69   cerr.close()
70
71   return (''.join(output), ''.join(errors))
72
73def run_cmd(cmd, desc):
74   #-- run a command using our own particular idiom
75   global VERBOSE
76
77   result = True		# T => ran without error reports, F otherwise
78
79   if VERBOSE and len(desc) > 0:
80      report_info('=> ' + desc)
81
82   (cout, cin, cerr) = popen2.popen3(cmd)
83
84   cin.close()
85
86   output = cout.readlines()
87   cout.close()
88   if VERBOSE:
89      report_info(''.join(output))
90
91   errors = cerr.readlines()
92   cerr.close()
93   if len(errors) > 0:
94      result = False
95      report_error(''.join(errors))
96
97   return result
98
99