# Part of the A-A-P recipe executive: printing messages
# Copyright (C) 2002 Stichting NLnet Labs
# Permission to copy and use this file is specified in the file COPYING.
# If this file is missing you can find it here:
http://www.a-a-p.org/COPYING
# All messages are written into a log file.
# Only selected messages are printed.
# It's OK to import * from this file.
import string
import os
import os.path
import Global
from Error import *
msgt_warning = "warning" # warning message
msgt_error = "error" # error message
msgt_system = "system" # executed system command
msgt_result = "result" # result of :sys command
msgt_info = "info" # informational message
msgt_extra = "extra" # extra informational message
msgt_changedir = "changedir" # changing directories
msgt_depend = "depend" # message about dependencies
msgt_all = "all" # all messages
msgt_print = "print" # :print message
# File for logging message. Is opened only after the main recipe is located.
logfile = None
# Full path of the file used for logging.
logname = None
def msg_startlog():
"""Open the logfile. Can only be done after locating the main recipe file
and changing to it's directory."""
# First need the "aap" directory.
from Util import assert_aap_dir
if not assert_aap_dir():
return
# Rename the old files if aap/log already exists. log8 -> log9, log7 ->
# log8, ..., log1 -> log2, log -> log1
if os.path.exists("aap/log"):
i = 8
while i >= 0:
if i == 0:
src = "aap/log"
else:
src = "aap/log%d" % i
if os.path.exists(src):
dst = "aap/log%d" % (i + 1)
# May have to delete the destination file log9.
if i == 8:
try:
os.remove(dst)
except:
pass
try:
os.rename(src, dst)
except StandardError, e:
print _('Warning: Could not rename "%s" to "%s"') % (src, dst)
break
i = i - 1
# Open aap/log
global logfile
try:
logfile = open("aap/log", "w")
except StandardError, e:
print _("Warning: Cannot open log file for writing: "), str(e)
else:
# Turn into an absolute name, we may use chdir.
global logname
logname = os.path.abspath("aap/log")
def msg_logname():
"""Return the name of the log file."""
return logname
def msg_msg(msg, type):
"""Generic function to print a message "msg" of type "type".
All messages are written in the log file (if it exists).
A message is displayed if its type is in the MESSAGE variable."""
if (not Global.work
or string.find(Global.work.globals["MESSAGE"], type) >= 0
or string.find(Global.work.globals["MESSAGE"], msgt_all) >= 0):
print "Aap: " + msg
msg_log(msg, type)
def msg_log(msg, type):
if logfile:
try:
logfile.write("%s:\t%s\n" % (type, msg))
except IOError, e:
raise UserError, _('Error while writing to log file: ') + str(e)
def msg_warning(msg):
"""Print a warning message."""
msg_msg(msg, msgt_warning)
def msg_error(msg):
"""Print an error message."""
msg_msg(msg, msgt_error)
def msg_system(msg):
"""Print an executed system command."""
if msg[-1] == '\n':
msg = msg[:-1] # remove trailing newline
msg_msg(msg, msgt_system)
def msg_result(msg):
"""Print an executed system command."""
if msg[-1] == '\n':
msg = msg[:-1] # remove trailing newline
msg_msg(msg, msgt_result)
def msg_info(msg):
"""Print an informational message."""
msg_msg(msg, msgt_info)
def msg_extra(msg):
"""Print an extra informational message."""
msg_msg(msg, msgt_extra)
def msg_changedir(msg):
"""Print a message about changing directories."""
msg_msg(msg, msgt_changedir)
def msg_depend(msg):
"""Print a message about dependencies."""
msg_msg(msg, msgt_depend)
def msg_print(msg):
"""Print a message always, and log it."""
print msg
msg_log(msg, msgt_print)
def msg_init(globals):
"""Set the MESSAGE variable according to the command line arguments.
Skip this when it's already set from the command line."""
if not globals.has_key("MESSAGE"):
if Global.cmd_args.has_option("silent"):
globals["MESSAGE"] = msgt_error
elif Global.cmd_args.has_option("verbose"):
globals["MESSAGE"] = msgt_all
else:
globals["MESSAGE"] = (msgt_changedir + ","
+ msgt_error + "," + msgt_system + "," + msgt_info)
# vim: set sw=4 sts=4 tw=79 fo+=l: