From 7e79bcd4ab0030db5c5a7998c81f63c48656be80 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Guido=20G=C3=BCnther?= Date: Fri, 3 Sep 2010 13:07:10 +0200 Subject: [PATCH] Add logging functions This allows us to color and prefix the output. Closes: #544332 --- gbp-clone | 14 +++--- gbp-pull | 25 +++++------ gbp/command_wrappers.py | 6 +-- gbp/config.py | 3 ++ gbp/git.py | 5 ++- gbp/log.py | 110 ++++++++++++++++++++++++++++++++++++++++++++++++ git-buildpackage | 59 +++++++++++++------------- git-dch | 30 ++++++------- git-import-dsc | 53 ++++++++++++----------- git-import-dscs | 16 ++++--- git-import-orig | 67 ++++++++++++++--------------- 11 files changed, 248 insertions(+), 140 deletions(-) create mode 100644 gbp/log.py diff --git a/gbp-clone b/gbp-clone index ed835f9..1a291f3 100755 --- a/gbp-clone +++ b/gbp-clone @@ -27,7 +27,7 @@ from gbp.git import (GitRepositoryError, GitRepository) from gbp.command_wrappers import (GitClone, Command, CommandExecFailed, GitBranch, PristineTar) from gbp.errors import GbpError - +import gbp.log def main(argv): retval = 0 @@ -44,22 +44,20 @@ def main(argv): help="Track all branches, not only debian and upstream") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="verbose command execution") + parser.add_config_file_option(option_name="color", dest="color") (options, args) = parser.parse_args(argv) + gbp.log.setup(options.color, options.verbose) if len(args) != 2: - print >>sys.stderr, "Need a repository to clone.\n" - parser.print_help() + gbp.log.err("Need a repository to clone.") return 1 else: source = args[1] - if options.verbose: - Command.verbose = True - try: GitRepository(os.path.curdir) - print >>sys.stderr, "Can't run inside a git repository." + gbp.log.err("Can't run inside a git repository.") return 1 except GitRepositoryError: pass @@ -94,7 +92,7 @@ def main(argv): retval = 1 except GbpError, err: if len(err.__str__()): - print >>sys.stderr, err + gbp.log.err(err) retval = 1 return retval diff --git a/gbp-pull b/gbp-pull index 79c12a4..4f814b2 100755 --- a/gbp-pull +++ b/gbp-pull @@ -27,6 +27,7 @@ from gbp.command_wrappers import (GitFetch, GitMerge, Command, from gbp.config import (GbpOptionParser, GbpOptionGroup) from gbp.errors import GbpError from gbp.git import (GitRepositoryError, GitRepository) +import gbp.log def fast_forward_branch(branch, repo, options): """ @@ -39,26 +40,26 @@ def fast_forward_branch(branch, repo, options): remote = repo.get_merge_branch(branch) if not remote: - print >>sys.stderr, "Warning: no branch tracking '%s' found - skipping." % branch + gbp.log.err("Warning: no branch tracking '%s' found - skipping." % branch) return False can_fast_forward, up_to_date = repo.is_fast_forward(branch, remote) if up_to_date: # Great, we're done - print "Branch '%s' is already up to date." % branch + gbp.log.info("Branch '%s' is already up to date." % branch) return True if can_fast_forward: update = True else: if options.force: - print "Non-fast forwarding '%s' due to --force" % branch + gbp.log.info("Non-fast forwarding '%s' due to --force" % branch) update = True else: - print >>sys.stderr, "Warning: Skipping non-fast forward of '%s' - use --force" % branch + gbp.log.warn("Skipping non-fast forward of '%s' - use --force" % branch) if update: - print "Updating '%s'" % branch + gbp.log.info("Updating '%s'" % branch) repo.set_branch(branch) GitMerge(remote)() return update @@ -80,16 +81,16 @@ def main(argv): help="Redo the patch queue branch after a pull. Warning: this drops the old patch-queue branch") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="verbose command execution") + parser.add_config_file_option(option_name="color", dest="color") - (options, args) = parser.parse_args(argv) - if options.verbose: - Command.verbose = True + (options, args) = parser.parse_args(argv) + gbp.log.setup(options.color, options.verbose) try: repo = GitRepository(os.path.curdir) except GitRepositoryError: - print >>sys.stderr, "%s is not a git repository" % (os.path.abspath('.')) + gbp.log.err("%s is not a git repository" % (os.path.abspath('.'))) return 1 try: @@ -105,8 +106,8 @@ def main(argv): (ret, out) = repo.is_clean() if not ret: - print >>sys.stderr, "You have uncommitted changes in your source tree:" - print >>sys.stderr, out + gbp.log.err("You have uncommitted changes in your source tree:") + gbp.log.err(out) raise GbpError GitFetch()() @@ -124,7 +125,7 @@ def main(argv): retval = 1 except GbpError, err: if len(err.__str__()): - print >>sys.stderr, err + gbp.log.err(err) retval = 1 return retval diff --git a/gbp/command_wrappers.py b/gbp/command_wrappers.py index f500eee..60a317f 100644 --- a/gbp/command_wrappers.py +++ b/gbp/command_wrappers.py @@ -11,6 +11,7 @@ import sys import os import os.path import signal +import log from errors import GbpError class CommandExecFailed(Exception): @@ -23,8 +24,6 @@ class Command(object): Wraps a shell command, so we don't have to store any kind of command line options in one of the git-buildpackage commands """ - verbose = False - def __init__(self, cmd, args=[], shell=False, extra_env=None): self.cmd = cmd self.args = args @@ -42,8 +41,7 @@ class Command(object): "restore default signal handler (http://bugs.python.org/issue1652)" signal.signal(signal.SIGPIPE, signal.SIG_DFL) - if self.verbose: - print self.cmd, self.args, args + log.debug("%s %s %s" % (self.cmd, self.args, args)) cmd = [ self.cmd ] + self.args + args if self.shell: # subprocess.call only cares about the first argument if shell=True cmd = " ".join(cmd) diff --git a/gbp/config.py b/gbp/config.py index e9c108c..aced5b1 100644 --- a/gbp/config.py +++ b/gbp/config.py @@ -78,6 +78,7 @@ class GbpOptionParser(OptionParser): 'dist' : 'sid', 'arch' : '', 'interactive' : 'True', + 'color' : 'auto', } help = { 'debian-branch': @@ -124,6 +125,8 @@ class GbpOptionParser(OptionParser): "Build for this architecture when using git-pbuilder, default is '%(arch)s'", 'interactive': "Run command interactive, default is '%(interactive)s'", + 'color': + "color output, default is '%(color)s'", } config_files = [ '/etc/git-buildpackage/gbp.conf', os.path.expanduser('~/.gbp.conf'), diff --git a/gbp/git.py b/gbp/git.py index 917333a..fa29ea0 100644 --- a/gbp/git.py +++ b/gbp/git.py @@ -8,6 +8,7 @@ import subprocess import os.path from command_wrappers import (GitAdd, GitRm, GitCheckoutBranch, GitInit, GitCommand, copy_from) from errors import GbpError +import log import dateutil.parser import calendar @@ -43,7 +44,9 @@ class GitRepository(object): output = [] env = self.__build_env(extra_env) - popen = subprocess.Popen(['git', command] + args, stdout=subprocess.PIPE, env=env) + cmd = ['git', command] + args + log.debug(cmd) + popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=env) while popen.poll() == None: output += popen.stdout.readlines() ret = popen.poll() diff --git a/gbp/log.py b/gbp/log.py new file mode 100644 index 0000000..bf8a3e3 --- /dev/null +++ b/gbp/log.py @@ -0,0 +1,110 @@ +# vim: set fileencoding=utf-8 : +# +# (C) 2010 Guido Guenther +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +"""Simple colored logging classes""" + +import sys + +logger = None + +class Logger(object): + + DEBUG, INFO, WARNING, ERROR = range(4) + + COLOR_NONE = 0 + COLOR_BLACK, COLOR_RED, COLOR_GREEN = range(30,33) + + COLOR_SEQ = "\033[%dm" + BOLD_SEQ = "\033[1m" + + + format = ("%(color)s" + "gbp:%(levelname)s: " + "%(message)s" + "%(coloroff)s") + + def __init__(self): + self.levels = { self.DEBUG: [ 'debug', self.COLOR_GREEN ], + self.INFO: [ 'info', self.COLOR_GREEN ], + self.WARNING: [ 'warn', self.COLOR_RED ], + self.ERROR: [ 'error', self.COLOR_RED ], } + self.color = False + self.level = self.INFO + + def set_level(self, level): + self.level = level + + def set_color(self, color): + if type(color) == type(True): + self.color = color + else: + if color.lower() == "on": + self.color = True + elif color.lower() == "auto": + if (sys.stderr.isatty() and + sys.stdout.isatty()): + self.color = True + else: + self.color = False + + if self.color: + self.get_color = self._color + self.get_coloroff = self._color_off + else: + self.get_color = self.get_coloroff = self._color_dummy + + def _color_dummy(self, level=None): + return "" + + def _color(self, level): + return self.COLOR_SEQ % (self.levels[level][1]) + + def _color_off(self): + return self.COLOR_SEQ % self.COLOR_NONE + + + def log(self, level, message): + if level < self.level: + return + + out = [sys.stdout, sys.stderr][level >= self.WARNING] + print >>out, self.format % { 'levelname': self.levels[level][0], + 'color': self.get_color(level), + 'message': message, + 'coloroff': self.get_coloroff()} + + +def err(msg): + logger.log(Logger.ERROR, msg) + +def warn(msg): + logger.log(Logger.WARNING, msg) + +def info(msg): + logger.log(Logger.INFO, msg) + +def debug(msg): + logger.log(Logger.DEBUG, msg) + +def setup(color, verbose): + logger.set_color(color) + if verbose: + logger.set_level(Logger.DEBUG) + +if not logger: + logger = Logger() + diff --git a/git-buildpackage b/git-buildpackage index f6fb4c1..e37a1ff 100755 --- a/git-buildpackage +++ b/git-buildpackage @@ -31,6 +31,7 @@ from gbp.command_wrappers import (GitTag, Command, RunAtCommand, CommandExecFail from gbp.config import (GbpOptionParser, GbpOptionGroup) from gbp.errors import GbpError from glob import glob +import gbp.log # when we want to reference the index in a treeish context we call it: index_name = "INDEX" @@ -45,13 +46,13 @@ def git_archive_pipe(prefix, pipe, output, treeish): try: ret = pipe.copy('', output) if ret: - print >>sys.stderr, "Error creating %s: %d" % (output, ret) + gbp.log.err("Error creating %s: %d" % (output, ret)) return False except OSError, err: - print >>sys.stderr, "Error creating %s: %s" % (output, err[0]) + gbp.log.err("Error creating %s: %s" % (output, err[0])) return False except: - print >>sys.stderr, "Error creating %s" % (output,) + gbp.log.err("Error creating %s" % (output,)) return False return True @@ -111,7 +112,7 @@ def pristine_tar_build_orig(repo, cp, output_dir, options): if options.pristine_tar: pt = PristineTar() if not repo.has_branch(pt.branch): - print >>sys.stderr, 'Pristine-tar branch "%s" not found' % pt.branch + gbp.log.warn('Pristine-tar branch "%s" not found' % pt.branch) pt.checkout(os.path.join(output_dir, du.orig_file(cp, options.comp_type))) return True else: @@ -128,14 +129,13 @@ def git_archive_build_orig(repo, cp, output_dir, options): # fall back to the upstream-branch tip if the tag doesn't exist if not repo.has_treeish(upstream_tree): upstream_tree = GbpOptionParser.defaults['upstream-branch'] - print "%s does not exist, creating from '%s'" % (du.orig_file(cp, - options.comp_type), - upstream_tree) + gbp.log.info("%s does not exist, creating from '%s'" % (du.orig_file(cp, + options.comp_type), + upstream_tree)) if not repo.has_treeish(upstream_tree): raise GbpError # git-ls-tree printed an error message already - if options.verbose: - print "Building upstream tarball with compression '%s -%s'" % (options.comp_type, - options.comp_level) + gbp.log.debug("Building upstream tarball with compression '%s -%s'" % (options.comp_type, + options.comp_level)) if not git_archive(cp, output_dir, upstream_tree, options.comp_type, options.comp_level): raise GbpError, "Cannot create upstream tarball at '%s'" % output_dir @@ -157,7 +157,7 @@ def drop_index(repo): def extract_orig(orig_tarball, dest_dir): """extract orig tarball to export dir before exporting from git""" - print "Extracting %s to '%s'" % (os.path.basename(orig_tarball), dest_dir) + gbp.log.info("Extracting %s to '%s'" % (os.path.basename(orig_tarball), dest_dir)) move_old_export(dest_dir) du.unpack_orig(orig_tarball, dest_dir, '') @@ -182,7 +182,7 @@ def guess_comp_type(repo, comp_type, srcpkg, upstream_version): try: dummy = du.compressor_opts[comp_type] except KeyError: - print >>sys.stderr, "Unknown compression type - guessing." + gbp.log.warn("Unknown compression type - guessing.") comp_type = 'auto' else: if not repo.has_branch(PristineTar.branch): @@ -192,13 +192,15 @@ def guess_comp_type(repo, comp_type, srcpkg, upstream_version): commits = repo.grep_log(regex, PristineTar.branch) if commits: commit = commits[0] + gbp.log.debug("Found pristine-tar commit at '%s'" % commit) else: commit = PristineTar.branch tarball = repo.get_subject(commit) comp_type = du.get_compression(tarball) + gbp.log.debug("Determined compression type '%s'" % comp_type) if not comp_type: comp_type = 'gzip' - print >>sys.stderr, "Unknown compression type of %s, assuming %s" % (tarball, comp_type) + gbp.log.warn("Unknown compression type of %s, assuming %s" % (tarball, comp_type)) return comp_type @@ -228,7 +230,7 @@ def main(argv): try: parser = GbpOptionParser(command=os.path.basename(argv[0]), prefix=prefix) except ConfigParser.ParsingError, err: - print >>sys.stderr, err + gbp.log.err(err) return 1 tag_group = GbpOptionGroup(parser, "tag options", "options related to git tag creation") @@ -245,6 +247,7 @@ def main(argv): parser.add_boolean_config_file_option(option_name = "ignore-new", dest="ignore_new") parser.add_option("--git-verbose", action="store_true", dest="verbose", default=False, help="verbose command execution") + parser.add_config_file_option(option_name="color", dest="color") tag_group.add_option("--git-tag", action="store_true", dest="tag", default=False, help="create a tag after a successful build") tag_group.add_option("--git-tag-only", action="store_true", dest="tag_only", default=False, @@ -290,13 +293,11 @@ def main(argv): help="retain exported package build directory") export_group.add_boolean_config_file_option(option_name="overlay", dest="overlay") (options, args) = parser.parse_args(args) - - if options.verbose: - Command.verbose = True + gbp.log.setup(options.color, options.verbose) if options.retag: if not options.tag and not options.tag_only: - print >>sys.stderr, "'--%sretag' needs either '--%stag' or '--%stag-only'" % (prefix, prefix, prefix) + gbp.log.err("'--%sretag' needs either '--%stag' or '--%stag-only'" % (prefix, prefix, prefix)) return 1 if options.overlay and not options.export_dir: @@ -305,7 +306,7 @@ def main(argv): try: repo = GitRepository(os.path.curdir) except GitRepositoryError: - print >>sys.stderr, "%s is not a git repository" % (os.path.abspath('.')) + gbp.log.err("%s is not a git repository" % (os.path.abspath('.'))) return 1 else: repo_dir = os.path.abspath(os.path.curdir) @@ -316,13 +317,13 @@ def main(argv): if not options.ignore_new: (ret, out) = repo.is_clean() if not ret: - print >>sys.stderr, "You have uncommitted changes in your source tree:" - print >>sys.stderr, out + gbp.log.err("You have uncommitted changes in your source tree:") + gbp.log.err(out) raise GbpError, "Use --git-ignore-new to ignore." if not options.ignore_new and not options.ignore_branch: if branch != options.debian_branch: - print >>sys.stderr, "You are not on branch '%s' but on '%s'" % (options.debian_branch, branch) + gbp.log.err("You are not on branch '%s' but on '%s'" % (options.debian_branch, branch)) raise GbpError, "Use --git-ignore-branch to ignore or --git-debian-branch to set the branch name." try: @@ -354,11 +355,11 @@ def main(argv): # look in tarball_dir first, if found force a symlink to it if options.tarball_dir: - print "Looking for orig tarball '%s' at '%s'" % (orig_file, tarball_dir) + gbp.log.info("Looking for orig tarball '%s' at '%s'" % (orig_file, tarball_dir)) if not du.symlink_orig(cp, options.comp_type, tarball_dir, output_dir, force=True): - print "Orig tarball '%s' not found at '%s'" % (orig_file, tarball_dir) + gbp.log.info("Orig tarball '%s' not found at '%s'" % (orig_file, tarball_dir)) else: - print "Orig tarball '%s' found at '%s'" % (orig_file, tarball_dir) + gbp.log.info("Orig tarball '%s' found at '%s'" % (orig_file, tarball_dir)) # build an orig unless the user forbids it, always build (and overwrite pre-existing) if user forces it if options.force_create or (not options.no_create_orig and not du.has_orig(cp, options.comp_type, output_dir)): if not pristine_tar_build_orig(repo, cp, output_dir, options): @@ -383,11 +384,11 @@ def main(argv): raise GbpError, "Cannot overlay Debian native package" extract_orig(os.path.join(output_dir, du.orig_file(cp, options.comp_type)), tmp_dir) - print "Exporting '%s' to '%s'" % (options.export, tmp_dir) + gbp.log.info("Exporting '%s' to '%s'" % (options.export, tmp_dir)) dump_tree(tmp_dir, tree) cp = du.parse_changelog(filename=os.path.join(tmp_dir, 'debian', 'changelog')) export_dir = os.path.join(output_dir, "%s-%s" % (cp['Source'], major)) - print "Moving '%s' to '%s'" % (tmp_dir, export_dir) + gbp.log.info("Moving '%s' to '%s'" % (tmp_dir, export_dir)) move_old_export(export_dir) os.rename(tmp_dir, export_dir) @@ -416,7 +417,7 @@ def main(argv): extra_env={'GBP_CHANGES_FILE': changes, 'GBP_BUILD_DIR': build_dir})() if options.tag or options.tag_only: - print "Tagging %s" % version + gbp.log.info("Tagging %s" % version) tag = build_tag(options.debian_tag, version) if options.retag and repo.has_tag(tag): repo.remove_tag(tag) @@ -432,7 +433,7 @@ def main(argv): retval = 1 except GbpError, err: if len(err.__str__()): - print >>sys.stderr, err + gbp.log.err(err) retval = 1 finally: drop_index(repo) diff --git a/git-dch b/git-dch index 84e195b..ceed19f 100755 --- a/git-dch +++ b/git-dch @@ -25,6 +25,7 @@ import sys import shutil import subprocess import gbp.command_wrappers as gbpc +import gbp.log from gbp.git import (GitRepositoryError, GitRepository, build_tag, tag_to_version) from gbp.config import GbpOptionParser, GbpOptionGroup from gbp.errors import GbpError @@ -124,14 +125,12 @@ def add_changelog_section(msg, distribution, repo, options, cp, tag = repo.find_tag('HEAD', pattern=pattern) upstream = tag_to_version(tag, options.upstream_tag) if upstream: - if options.verbose: - print "Found %s." % upstream + gbp.log.debug("Found %s." % upstream) new_version = "%s-1" % upstream if compare_versions(upstream, cp['Version']) > 0: version['version'] = new_version except GitRepositoryError: - if options.verbose: - print "No tag found matching pattern %s." % pattern + gbp.log.debug("No tag found matching pattern %s." % pattern) spawn_dch(msg=msg, newversion=True, version=version, author=author, email=email, distribution=distribution, dch_options=dch_options) @@ -325,7 +324,7 @@ def guess_snapshot_commit(cp, repo, options): # was last touched. last = repo.commits(paths="debian/changelog", options=["-1"]) if last: - print "Changelog last touched at '%s'" % last[0] + gbp.log.info("Changelog last touched at '%s'" % last[0]) return last[0] return None @@ -342,7 +341,7 @@ def main(argv): parser = GbpOptionParser(command=os.path.basename(argv[0]), prefix='', usage='%prog [options] paths') except ConfigParser.ParsingError, err: - print >>sys.stderr, err + gbp.log.errror(err) return 1 range_group = GbpOptionGroup(parser, "commit range options", "which commits to add to the changelog") version_group = GbpOptionGroup(parser, "release & version number options", "what version number and release to use") @@ -363,6 +362,7 @@ def main(argv): help="options to pass to git-log, default is '%(git-log)s'") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="verbose command execution") + parser.add_config_file_option(option_name="color", dest="color") range_group.add_option("-s", "--since", dest="since", help="commit to start from (e.g. HEAD^^^, debian/0.4.3)") range_group.add_option("-a", "--auto", action="store_true", dest="auto", default=False, help="autocomplete changelog from last snapshot or tag") @@ -391,6 +391,7 @@ def main(argv): commit_group.add_boolean_config_file_option(option_name="multimaint-merge", dest="multimaint_merge") (options, args) = parser.parse_args(argv[1:]) + gbp.log.setup(options.color, options.verbose) if options.snapshot and options.release: parser.error("'--snapshot' and '--release' are incompatible options") @@ -404,9 +405,6 @@ def main(argv): dch_options = "--nomultimaint-merge" try: - if options.verbose: - gbpc.Command.verbose = True - try: repo = GitRepository('.') except GitRepositoryError: @@ -414,7 +412,7 @@ def main(argv): branch = repo.get_branch() if options.debian_branch != branch and not options.ignore_branch: - print >>sys.stderr, "You are not on branch '%s' but on '%s'" % (options.debian_branch, branch) + gbp.log.err("You are not on branch '%s' but on '%s'" % (options.debian_branch, branch)) raise GbpError, "Use --ignore-branch to ignore or --debian-branch to set the branch name." cp = parse_changelog(filename=changelog) @@ -426,17 +424,17 @@ def main(argv): if options.auto: since = guess_snapshot_commit(cp, repo, options) if since: - print "Continuing from commit '%s'" % since + gbp.log.info("Continuing from commit '%s'" % since) found_snapshot_header = True else: - print "Couldn't find snapshot header, using version info" + gbp.log.info("Couldn't find snapshot header, using version info") if not since: since = repo.find_version(options.debian_tag, cp['Version']) if not since: raise GbpError, "Version %s not found" % cp['Version'] if args: - print "Only looking for changes on '%s'" % " ".join(args) + gbp.log.info("Only looking for changes on '%s'" % " ".join(args)) commits = repo.commits(since=since, until=until, paths=" ".join(args), options=options.git_log.split(" ")) # add a new changelog section if: @@ -487,7 +485,7 @@ def main(argv): # Show a message if there were no commits (not even ignored # commits). if not commits: - print "No changes detected from %s to %s." % (since, until) + gbp.log.info("No changes detected from %s to %s." % (since, until)) if add_section: # If we end up here, then there were no commits to include, @@ -508,11 +506,11 @@ def main(argv): dch_options=dch_options) elif options.snapshot: (snap, version) = do_snapshot(changelog, repo, options.snapshot_number) - print "Changelog has been prepared for snapshot #%d at %s" % (snap, version) + gbp.log.info("Changelog has been prepared for snapshot #%d at %s" % (snap, version)) except (GbpError, GitRepositoryError, NoChangelogError), err: if len(err.__str__()): - print >>sys.stderr, err + gbp.log.err(err) ret = 1 return ret diff --git a/git-import-dsc b/git-import-dsc index d9de908..f6f8db0 100755 --- a/git-import-dsc +++ b/git-import-dsc @@ -32,7 +32,7 @@ from gbp.git import (build_tag, create_repo, GitRepository, GitRepositoryError, rfc822_date_to_git) from gbp.config import GbpOptionParser, GbpOptionGroup from gbp.errors import GbpError - +import gbp.log class SkipImport(Exception): pass @@ -45,7 +45,7 @@ def download_source(pkg, dirs): mode='apt-get' dirs['download'] = os.path.abspath(tempfile.mkdtemp()) - print "Downloading '%s' using '%s'..." % (pkg, mode) + gbp.log.info("Downloading '%s' using '%s'..." % (pkg, mode)) if mode == 'apt-get': gbpc.RunAtCommand('apt-get', ['-qq', '--download-only', 'source', pkg], @@ -67,10 +67,10 @@ def apply_patch(diff, unpack_dir): try: ret = pipe.copy('', '') if ret: - print >>sys.stderr, "Error import %s: %d" % (diff, ret) + gbp.log.err("Error import %s: %d" % (diff, ret)) return False except OSError, err: - print >>sys.stderr, "Error importing %s: %s" % (diff, err[0]) + gbp.log.err("Error importing %s: %s" % (diff, err[0])) return False return True @@ -102,7 +102,7 @@ def apply_debian_patch(repo, unpack_dir, src, options, parents): date= rfc822_date_to_git(dch['Date']) author, email = parseaddr(dch['Maintainer']) if not (author and email): - print >>sys.stderr, "Warning: failed to parse maintainer" + gbp.log.warn("Failed to parse maintainer") commit = repo.commit_dir(unpack_dir, "Imported Debian patch %s" % version, @@ -114,7 +114,7 @@ def apply_debian_patch(repo, unpack_dir, src, options, parents): gitTag(build_tag(options.debian_tag, version), msg="Debian release %s" % version, commit=commit) except gbpc.CommandExecFailed: - print >>sys.stderr, "Failed to import Debian package" + gbp.log.err("Failed to import Debian package") raise GbpError finally: os.chdir(repo.path) @@ -122,16 +122,16 @@ def apply_debian_patch(repo, unpack_dir, src, options, parents): def print_dsc(dsc): if dsc.native: - print "Debian Native Package" - print "Version:", dsc.upstream_version - print "Debian tarball:", dsc.tgz + gbp.log.debug("Debian Native Package") + gbp.log.debug("Version:", dsc.upstream_version) + gbp.log.debug("Debian tarball:", dsc.tgz) else: - print "Upstream version:", dsc.upstream_version - print "Debian version:", dsc.debian_version - print "Upstream tarball:", dsc.tgz - print "Debian diff:", dsc.diff + gbp.log.debug("Upstream version:", dsc.upstream_version) + gbp.log.debug("Debian version:", dsc.debian_version) + gbp.log.debug("Upstream tarball:", dsc.tgz) + gbp.log.debug("Debian diff:", dsc.diff) if dsc.epoch: - print "Epoch: %s" % dsc.epoch + gbp.log.debug("Epoch: %s" % dsc.epoch) def main(argv): dirs = {'top': os.path.abspath(os.curdir)} @@ -143,7 +143,7 @@ def main(argv): parser = GbpOptionParser(command=os.path.basename(argv[0]), prefix='', usage='%prog [options] /path/to/package.dsc') except ConfigParser.ParsingError, err: - print >>sys.stderr, err + gbp.log.err(err) return 1 import_group = GbpOptionGroup(parser, "import options", @@ -158,6 +158,7 @@ def main(argv): parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="verbose command execution") + parser.add_config_file_option(option_name="color", dest="color") parser.add_option("--download", action="store_true", dest="download", default=False, help="download source package") branch_group.add_config_file_option(option_name="debian-branch", @@ -182,9 +183,7 @@ def main(argv): dest="ignore_same_version", default=False, help="don't import already imported version") (options, args) = parser.parse_args(argv[1:]) - - if options.verbose: - gbpc.Command.verbose = True + gbp.log.setup(options.color, options.verbose) gitTag = gbpc.GitTag(options.sign_tags, options.keyid) @@ -211,7 +210,7 @@ def main(argv): (clean, out) = repo.is_clean() if not clean and not is_empty: - print >>sys.stderr, "Repository has uncommitted changes, commit these first: " + gbp.log.err("Repository has uncommitted changes, commit these first: ") raise GbpError, out except GitRepositoryError: @@ -220,7 +219,7 @@ def main(argv): is_empty = True if needs_repo: - print "No git repository found, creating one." + gbp.log.info("No git repository found, creating one.") repo = create_repo(src.pkg) os.chdir(repo.path) @@ -234,11 +233,11 @@ def main(argv): if options.ignore_same_version: if repo.find_version(options.debian_tag, src.version): - print "Version %s already imported." % src.version + gbp.log.info("Version %s already imported." % src.version) raise SkipImport if not repo.find_version(format[0], src.upstream_version): - print "tag %s not found, importing %s tarball" % (tag, format[1]) + gbp.log.info("Tag %s not found, importing %s tarball" % (tag, format[1])) if is_empty: branch = None else: @@ -261,21 +260,21 @@ def main(argv): if src.diff or src.deb_tgz: apply_debian_patch(repo, unpack_dir, src, options, parents) else: - print >>sys.stderr, "Warning: Didn't find a diff to apply." + gbp.log.warn("Didn't find a diff to apply.") if repo.get_branch() == options.debian_branch: # Update HEAD if we modified the checkout out branch repo.force_head(options.debian_branch, hard=True) except KeyboardInterrupt: ret = 1 - print >>sys.stderr, "Interrupted. Aborting." + gbp.log.err("Interrupted. Aborting.") except gbpc.CommandExecFailed: ret = 1 except GitRepositoryError, msg: - print >>sys.stderr, "Git command failed: %s" % msg + gbp.log.err("Git command failed: %s" % msg) ret = 1 except GbpError, err: if len(err.__str__()): - print >>sys.stderr, err + gbp.log.err(err) ret = 1 except SkipImport: pass @@ -287,7 +286,7 @@ def main(argv): gbpc.RemoveTree(dirs[d])() if not ret: - print "Everything imported under '%s'" % src.pkg + gbp.log.info("Everything imported under '%s'" % src.pkg) return ret if __name__ == '__main__': diff --git a/git-import-dscs b/git-import-dscs index bfece56..d934760 100755 --- a/git-import-dscs +++ b/git-import-dscs @@ -27,7 +27,7 @@ import gbp.command_wrappers as gbpc from gbp.deb import parse_dsc, DscFile, DpkgCompareVersions from gbp.errors import GbpError from gbp.git import GitRepository, GitRepositoryError - +import gbp.log class DscCompareVersions(DpkgCompareVersions): def __init__(self): @@ -52,7 +52,7 @@ def fetch_snapshots(pkg, downloaddir): "Fetch snapshots using debsnap von snapshots.debian.org" dscs = None - print "Downloading snapshots of '%s' to '%s'..." % (pkg, downloaddir) + gbp.log.info("Downloading snapshots of '%s' to '%s'..." % (pkg, downloaddir)) gbpc.Command("debsnap", [ '--force', '--destdir=%s' % (downloaddir), pkg])() dscs = glob.glob(os.path.join(downloaddir, '*.dsc')) @@ -74,6 +74,7 @@ def main(argv): dirs = {'top': os.path.abspath(os.curdir)} dscs = [] ret = 0 + verbose = False dsc_cmp = DscCompareVersions() use_debsnap = False @@ -81,7 +82,8 @@ def main(argv): import_args = argv[1:] if '--verbose' in import_args: - gbpc.Command.verbose = True + verbose = True + gbp.log.setup(False, verbose) # Not using Configparser since we want to pass all unknown options # unaltered to git-import-dsc @@ -102,7 +104,7 @@ def main(argv): if use_debsnap: dirs['tmp'] = os.path.abspath(tempfile.mkdtemp()) - print "Downloading snapshots of '%s' to '%s'..." % (pkg, dirs['tmp']) + gbp.log.info("Downloading snapshots of '%s' to '%s'..." % (pkg, dirs['tmp'])) dscs = [ parse_dsc(f) for f in fetch_snapshots(pkg, dirs['tmp']) ] dscs.sort(cmp=dsc_cmp) @@ -112,7 +114,7 @@ def main(argv): repo = GitRepository('.') (clean, out) = repo.is_clean() if not clean: - print >>sys.stderr, "Repository has uncommitted changes, commit these first: " + gbp.log.err("Repository has uncommitted changes, commit these first: ") raise GbpError, out else: dirs['pkg'] = dirs['top'] @@ -128,7 +130,7 @@ def main(argv): except (GbpError, gbpc.CommandExecFailed), err: if len(err.__str__()): - print >>sys.stderr, err + gbp.log.err(err) ret = 1 finally: if dirs.has_key('tmp'): @@ -136,7 +138,7 @@ def main(argv): os.chdir(dirs['top']) if not ret: - print 'Everything imported under %s' % dirs['pkg'] + gbp.log.info('Everything imported under %s' % dirs['pkg']) return ret if __name__ == '__main__': diff --git a/git-import-orig b/git-import-orig index b407370..f7ce667 100755 --- a/git-import-orig +++ b/git-import-orig @@ -37,6 +37,7 @@ from gbp.deb import (parse_changelog, unpack_orig, repack_orig, from gbp.git import (FastImport, GitRepositoryError, GitRepository, build_tag) from gbp.config import GbpOptionParser, GbpOptionGroup from gbp.errors import (GbpError, GbpNothingImported) +import gbp.log # Try to import readline, since that will cause raw_input to get fancy # line editing and history capabilities. However, if readline is not @@ -51,7 +52,7 @@ def cleanup_tmp_tree(tree): try: gbpc.RemoveTree(tree)() except gbpc.CommandExecFailed: - print >>sys.stderr, "Removal of tmptree %s failed." % tree + gbp.log.err("Removal of tmptree %s failed." % tree) def is_link_target(target, link): @@ -101,8 +102,7 @@ def fast_import_upstream_tree(repo, tarball, version, options): now = "%d %s" % (time.time(), time.strftime("%z")) fastimport = FastImport() name, email = repo.get_author_info() - if options.verbose: - print "Starting fastimport of %s" % tarball + gbp.log.debug("Starting fastimport of %s" % tarball) fastimport.start_commit(options.upstream_branch, name, email, now, upstream_import_commit_msg(options, version)) fastimport.do_deleteall() @@ -119,8 +119,7 @@ def fast_import_upstream_tree(repo, tarball, version, options): fastimport.add_symlink(item.name, item.linkname) # if tarinfo.isextended() not implemented: elif item.type in ( "x", "g", "X" ): - if options.verbose: - print "Skipping %s of type '%s'" % (item.name, item.type) + gbp.log.debug("Skipping %s of type '%s'" % (item.name, item.type)) continue else: raise GbpError, "'%s' is not a regular file (%s) - don't use fastimport." % (item.name, item.type) @@ -130,14 +129,13 @@ def fast_import_upstream_tree(repo, tarball, version, options): tar.close() fastimport.close() - if options.verbose: - print "FastImport done." + gbp.log.debug("FastImport done.") def turn_off_fastimport(options, msg): if options.fast_import: - print >>sys.stderr, msg - print >>sys.stderr, "Turning off fastimport." + gbp.log.warn(msg) + gbp.log.warn("Turning off fastimport.") options.fast_import = False @@ -157,7 +155,7 @@ def ask_package_name(default): # Not a valid package name. Print an extra # newline before the error to make the output a # bit clearer. - print "\nNot a valid package name: '%s'.\n%s" % (sourcepackage, packagename_msg) + gbp.log.warn("\nNot a valid package name: '%s'.\n%s" % (sourcepackage, packagename_msg)) def ask_package_version(default): """ @@ -175,7 +173,7 @@ def ask_package_version(default): # Not a valid upstream version. Print an extra # newline before the error to make the output a # bit clearer. - print "\nNot a valid upstream version: '%s'.\n%s" % (version, upstreamversion_msg) + gbp.log.warn("\nNot a valid upstream version: '%s'.\n%s" % (version, upstreamversion_msg)) def main(argv): ret = 0 @@ -186,7 +184,7 @@ def main(argv): parser = GbpOptionParser(command=os.path.basename(argv[0]), prefix='', usage='%prog [options] /path/to/upstream-version.tar.gz | --uscan') except ConfigParser.ParsingError, err: - print >>sys.stderr, err + gbp.log.err(err) return 1 cl_group = GbpOptionGroup(parser, "changelog mangling", @@ -237,6 +235,7 @@ def main(argv): dest='interactive') parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="verbose command execution") + parser.add_config_file_option(option_name="color", dest="color") # Accepted for compatibility parser.add_option("--no-dch", dest='no_dch', action="store_true", @@ -245,12 +244,10 @@ def main(argv): default=False, help="use uscan(1) to download the new tarball.") (options, args) = parser.parse_args(argv[1:]) - - if options.verbose: - gbpc.Command.verbose = True + gbp.log.setup(options.color, options.verbose) if options.no_dch: - print >>sys.stderr, "'--no-dch' passed. This is now the default, please remove this option." + gbp.log.warn("'--no-dch' passed. This is now the default, please remove this option.") if options.filters: turn_off_fastimport(options, "Import filters currently not supported with fastimport.") @@ -262,19 +259,19 @@ def main(argv): if args: raise GbpError, "you can't pass both --uscan and a filename." - print "Launching uscan...", + gbp.log.info("Launching uscan...") try: status, tarball = do_uscan() except KeyError: raise GbpError, "error running uscan - debug by running uscan --verbose" if not status and not tarball: - print "package is up to date, nothing to do." + gbp.log.info("package is up to date, nothing to do.") return 0 elif status and not tarball: raise GbpError, "uscan didn't download anything, and no tarball was found in ../" elif status and tarball: - print "using %s" % tarball + gbp.log.info("using %s" % tarball) args.append(tarball) # tarball specified @@ -298,11 +295,11 @@ def main(argv): turn_off_fastimport(options, "Fast importing into empty archives not yet supported.") if not repo.has_branch(options.upstream_branch) and not is_empty: - print >>sys.stderr, """ + gbp.log.err(""" Repository does not have branch '%s' for upstream sources. If there is none see file:///usr/share/doc/git-buildpackage/manual-html/gbp.import.html#GBP.IMPORT.CONVERT on howto create it otherwise use --upstream-branch to specify it. -""" % options.upstream_branch +""" % options.upstream_branch) raise GbpError # Guess defaults for the package name and version from the @@ -337,7 +334,7 @@ on howto create it otherwise use --upstream-branch to specify it. (clean, out) = repo.is_clean() if not clean and not is_empty: - print >>sys.stderr, "Repository has uncommitted changes, commit these first: " + gbp.log.err("Repository has uncommitted changes, commit these first: ") raise GbpError, out if os.path.isdir(archive): @@ -347,8 +344,7 @@ on howto create it otherwise use --upstream-branch to specify it. if not options.fast_import: tmpdir = tempfile.mkdtemp(dir='../') unpack_orig(archive, tmpdir, options.filters) - if options.verbose: - print "Unpacked %s to '%s'" % (archive , tmpdir) + gbp.log.debug("Unpacked %s to '%s'" % (archive , tmpdir)) orig_dir = tar_toplevel(tmpdir) # Don't mess up or repo with git metadata from an upstream tarball @@ -359,8 +355,7 @@ on howto create it otherwise use --upstream-branch to specify it. pass if options.pristine_tar and options.filter_pristine_tar and len(options.filters) > 0: - if options.verbose: - print "Filter pristine-tar: repacking %s from '%s'" % (archive, tmpdir) + gbp.log.debug("Filter pristine-tar: repacking %s from '%s'" % (archive, tmpdir)) archive = os.path.join( os.path.dirname(archive), os.path.basename(archive).replace(".tar", ".gbp.tar") @@ -372,11 +367,11 @@ on howto create it otherwise use --upstream-branch to specify it. upstream_branch = [ options.upstream_branch, 'master' ][is_empty] filter_msg = ["", " (filtering out %s)" % options.filters][len(options.filters) > 0] - print "Importing '%s' to branch '%s'%s..." % (archive, - upstream_branch, - filter_msg) - print "Source package is %s" % sourcepackage - print "Upstream version is %s" % version + gbp.log.info("Importing '%s' to branch '%s'%s..." % (archive, + upstream_branch, + filter_msg)) + gbp.log.info("Source package is %s" % sourcepackage) + gbp.log.info("Upstream version is %s" % version) if options.fast_import: fast_import_upstream_tree(repo, pristine_orig, version, options) @@ -392,7 +387,7 @@ on howto create it otherwise use --upstream-branch to specify it. if pristine_orig: gbpc.PristineTar().commit(pristine_orig, 'refs/heads/%s' % upstream_branch) else: - print >>sys.stderr, "Warning: '%s' not an archive, skipping pristine-tar" % archive + gbp.log.warn("'%s' not an archive, skipping pristine-tar" % archive) tag = build_tag(options.upstream_tag, version) gbpc.GitTag(options.sign_tags, options.keyid)(tag, @@ -402,7 +397,7 @@ on howto create it otherwise use --upstream-branch to specify it. gbpc.GitBranch()(options.upstream_branch, remote=commit) repo.force_head(options.upstream_branch, hard=True) elif options.merge: - print "Merging to '%s'" % options.debian_branch + gbp.log.info("Merging to '%s'" % options.debian_branch) repo.set_branch(options.debian_branch) try: gbpc.GitMerge(tag)() @@ -423,19 +418,19 @@ on howto create it otherwise use --upstream-branch to specify it. except gbpc.CommandExecFailed: raise GbpError, "Import of %s failed" % archive except GbpNothingImported, err: - print >>sys.stderr, err + gbp.log.err(err) repo.set_branch(initial_branch) ret = 1 except GbpError, err: if len(err.__str__()): - print >>sys.stderr, err + gbp.log.err(err) ret = 1 if tmpdir: cleanup_tmp_tree(tmpdir) if not ret: - print "Succesfully imported version %s of %s" % (version, archive) + gbp.log.info("Succesfully imported version %s of %s" % (version, archive)) return ret if __name__ == "__main__": -- 2.7.4