2011-08-31 15:38:32 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
"""Copyright 2011 Gentoo Foundation
|
|
|
|
Distributed under the terms of the GNU General Public License v2
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
""" Meta """
|
|
|
|
__author__ = "Corentin Chary (iksaif)"
|
|
|
|
__email__ = "corentin.chary@gmail.com"
|
|
|
|
__version__ = "git"
|
|
|
|
__productname__ = "euscan"
|
|
|
|
__description__ = "A tool to detect new upstream releases."
|
2011-09-21 10:09:50 +02:00
|
|
|
__version__ = "git"
|
2011-08-31 15:38:32 +02:00
|
|
|
|
|
|
|
""" Imports """
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import getopt
|
|
|
|
import errno
|
2011-09-21 10:09:50 +02:00
|
|
|
import httplib
|
2011-08-31 15:38:32 +02:00
|
|
|
|
|
|
|
from portage.output import white, yellow, turquoise, green, EOutput
|
2011-09-21 10:58:41 +02:00
|
|
|
from portage.exception import AmbiguousPackageName
|
2011-08-31 15:38:32 +02:00
|
|
|
|
2011-09-21 10:58:41 +02:00
|
|
|
from gentoolkit import pprinter as pp
|
2011-08-31 15:38:32 +02:00
|
|
|
from gentoolkit.eclean.search import (port_settings)
|
2011-09-21 10:58:41 +02:00
|
|
|
from gentoolkit.errors import GentoolkitException
|
2011-08-31 15:38:32 +02:00
|
|
|
|
2011-12-11 14:32:38 +01:00
|
|
|
import euscan
|
|
|
|
from euscan import CONFIG
|
2011-08-31 15:38:32 +02:00
|
|
|
from euscan.scan import scan_upstream
|
|
|
|
|
|
|
|
""" Globals """
|
|
|
|
|
|
|
|
def setupSignals():
|
|
|
|
""" This block ensures that ^C interrupts are handled quietly. """
|
|
|
|
import signal
|
|
|
|
|
|
|
|
def exithandler(signum,frame):
|
|
|
|
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
|
|
|
signal.signal(signal.SIGTERM, signal.SIG_IGN)
|
2011-09-06 17:35:17 +02:00
|
|
|
print ()
|
|
|
|
sys.exit(errno.EINTR)
|
2011-08-31 15:38:32 +02:00
|
|
|
|
|
|
|
signal.signal(signal.SIGINT, exithandler)
|
|
|
|
signal.signal(signal.SIGTERM, exithandler)
|
|
|
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
|
|
|
|
|
|
|
|
|
|
|
def printVersion():
|
|
|
|
"""Output the version info."""
|
|
|
|
print( "%s (%s) - %s" \
|
|
|
|
% (__productname__, __version__, __description__))
|
|
|
|
print()
|
|
|
|
print("Author: %s <%s>" % (__author__,__email__))
|
|
|
|
print("Copyright 2011 Gentoo Foundation")
|
|
|
|
print("Distributed under the terms of the GNU General Public License v2")
|
|
|
|
|
|
|
|
|
|
|
|
def printUsage(_error=None, help=None):
|
|
|
|
"""Print help message. May also print partial help to stderr if an
|
|
|
|
error from {'options'} is specified."""
|
|
|
|
|
|
|
|
out = sys.stdout
|
|
|
|
if _error:
|
|
|
|
out = sys.stderr
|
|
|
|
if not _error in ('global-options', 'packages',):
|
|
|
|
_error = None
|
|
|
|
if not _error and not help: help = 'all'
|
|
|
|
if _error in ('global-options',):
|
|
|
|
print( pp.error("Wrong option on command line."), file=out)
|
|
|
|
print( file=out)
|
|
|
|
if _error in ('packages',):
|
|
|
|
print( pp.error("You need to specify exactly one package."), file=out)
|
|
|
|
print( file=out)
|
|
|
|
print( white("Usage:"), file=out)
|
|
|
|
if _error in ('global-options', 'packages',) or help == 'all':
|
|
|
|
print( " "+turquoise(__productname__),
|
2011-09-06 17:35:17 +02:00
|
|
|
yellow("[options]"),
|
2011-09-21 10:58:41 +02:00
|
|
|
green("<package> [<package> [...]]"), file=out)
|
2011-08-31 15:38:32 +02:00
|
|
|
if _error in ('global-options',) or help == 'all':
|
2011-09-06 17:35:17 +02:00
|
|
|
print( " "+turquoise(__productname__),
|
|
|
|
yellow("[--help, --version]"), file=out)
|
2011-08-31 15:38:32 +02:00
|
|
|
|
|
|
|
print(file=out)
|
|
|
|
if _error in ('global-options',) or help:
|
|
|
|
print( "Available ", yellow("options")+":", file=out)
|
2011-09-06 17:35:17 +02:00
|
|
|
print( yellow(" -C, --nocolor")+
|
|
|
|
" - turn off colors on output", file=out)
|
2011-08-31 15:38:32 +02:00
|
|
|
print( yellow(" -q, --quiet")+
|
2011-09-06 17:35:17 +02:00
|
|
|
" - be as quiet as possible", file=out)
|
|
|
|
print( yellow(" -h, --help")+ \
|
|
|
|
" - display the help screen", file=out)
|
2011-08-31 15:38:32 +02:00
|
|
|
print( yellow(" -V, --version")+
|
2011-09-06 17:35:17 +02:00
|
|
|
" - display version info", file=out)
|
2011-08-31 15:38:32 +02:00
|
|
|
print( file=out)
|
|
|
|
print( yellow(" -1, --oneshot")+
|
2011-09-06 17:35:17 +02:00
|
|
|
" - stop as soon as a new version is found", file=out)
|
2011-08-31 15:38:32 +02:00
|
|
|
print( yellow(" -b, --brute-force=<level>")+
|
2011-09-06 17:35:17 +02:00
|
|
|
" - define the brute force "+yellow("<level>")+" (default: 2)\n" +
|
|
|
|
" " * 29 + "bigger levels will generate more versions numbers\n" +
|
|
|
|
" " * 29 + "0 means disabled", file=out)
|
2011-08-31 15:38:32 +02:00
|
|
|
print( file=out)
|
|
|
|
if _error in ('packages',) or help:
|
2011-09-06 17:35:17 +02:00
|
|
|
print( green(" package")+
|
2011-09-21 10:58:41 +02:00
|
|
|
" - the packages (or ebuilds) you want to scan", file=out)
|
2011-08-31 15:38:32 +02:00
|
|
|
print( file=out)
|
2011-09-06 17:35:17 +02:00
|
|
|
'''print( "More detailed instruction can be found in",
|
|
|
|
turquoise("`man %s`" % __productname__), file=out)'''
|
2011-08-31 15:38:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ParseArgsException(Exception):
|
|
|
|
"""For parseArgs() -> main() communications."""
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
def __str__(self):
|
|
|
|
return repr(self.value)
|
|
|
|
|
|
|
|
|
|
|
|
def parseArgs():
|
|
|
|
"""Parse the command line arguments. Raise exceptions on
|
|
|
|
errors. Returns package and affect the CONFIG dict.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def optionSwitch(opts):
|
|
|
|
"""local function for interpreting command line options
|
|
|
|
and setting options accordingly"""
|
|
|
|
return_code = True
|
|
|
|
for o, a in opts:
|
|
|
|
if o in ("-h", "--help"):
|
|
|
|
raise ParseArgsException('help')
|
|
|
|
elif o in ("-V", "--version"):
|
|
|
|
raise ParseArgsException('version')
|
|
|
|
elif o in ("-C", "--nocolor"):
|
|
|
|
CONFIG['nocolor'] = True
|
|
|
|
pp.output.nocolor()
|
|
|
|
elif o in ("-q", "--quiet"):
|
|
|
|
CONFIG['quiet'] = True
|
2011-09-21 10:09:50 +02:00
|
|
|
CONFIG['verbose'] = 0
|
2011-08-31 15:38:32 +02:00
|
|
|
elif o in ("-1", "--oneshot"):
|
|
|
|
CONFIG['oneshot'] = True
|
|
|
|
elif o in ("-b", "--brute-force"):
|
|
|
|
CONFIG['brute-force'] = int(a)
|
|
|
|
elif o in ("-v", "--verbose") and not CONFIG['quiet']:
|
2011-09-21 10:09:50 +02:00
|
|
|
CONFIG['verbose'] += 1
|
2011-08-31 15:38:32 +02:00
|
|
|
else:
|
|
|
|
return_code = False
|
|
|
|
|
|
|
|
return return_code
|
|
|
|
|
|
|
|
' here are the different allowed command line options (getopt args) '
|
|
|
|
getopt_options = {'short':{}, 'long':{}}
|
|
|
|
getopt_options['short']['global'] = "hVCqv1b:"
|
|
|
|
getopt_options['long']['global'] = ["help", "version", "nocolor", "quiet",
|
|
|
|
"verbose", "oneshot", "brute-force="]
|
|
|
|
|
|
|
|
short_opts = getopt_options['short']['global']
|
|
|
|
long_opts = getopt_options['long']['global']
|
|
|
|
opts_mode = 'global'
|
|
|
|
|
|
|
|
' apply getopts to command line, show partial help on failure '
|
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], short_opts, long_opts)
|
|
|
|
except:
|
|
|
|
raise ParseArgsException(opts_mode+'-options')
|
|
|
|
|
|
|
|
' set options accordingly '
|
|
|
|
optionSwitch(opts)
|
|
|
|
|
2011-09-21 10:58:41 +02:00
|
|
|
if len(args) < 1:
|
2011-08-31 15:38:32 +02:00
|
|
|
raise ParseArgsException('packages')
|
|
|
|
|
2011-09-21 10:58:41 +02:00
|
|
|
return args
|
2011-08-31 15:38:32 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Parse command line and execute all actions."""
|
|
|
|
CONFIG['nocolor'] = (port_settings["NOCOLOR"] in ('yes','true')
|
|
|
|
or not sys.stdout.isatty())
|
|
|
|
if CONFIG['nocolor']:
|
|
|
|
pp.output.nocolor()
|
|
|
|
' parse command line options and actions '
|
|
|
|
try:
|
2011-09-21 10:58:41 +02:00
|
|
|
packages = parseArgs()
|
2011-08-31 15:38:32 +02:00
|
|
|
except ParseArgsException as e:
|
|
|
|
if e.value == 'help':
|
|
|
|
printUsage(help='all')
|
|
|
|
sys.exit(0)
|
|
|
|
elif e.value[:5] == 'help-':
|
|
|
|
printUsage(help=e.value[5:])
|
|
|
|
sys.exit(0)
|
|
|
|
elif e.value == 'version':
|
|
|
|
printVersion()
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
printUsage(e.value)
|
|
|
|
sys.exit(errno.EINVAL)
|
|
|
|
|
|
|
|
""" Change euscan's output """
|
2011-12-11 14:32:38 +01:00
|
|
|
euscan.output = EOutput(CONFIG['quiet'])
|
2011-09-21 10:09:50 +02:00
|
|
|
if CONFIG['verbose'] > 2:
|
|
|
|
httplib.HTTPConnection.debuglevel = 1
|
|
|
|
|
2011-09-21 10:58:41 +02:00
|
|
|
for package in packages:
|
2011-10-02 10:03:49 +02:00
|
|
|
ret = []
|
|
|
|
|
2011-09-21 10:58:41 +02:00
|
|
|
try:
|
|
|
|
ret = scan_upstream(package)
|
|
|
|
except AmbiguousPackageName as e:
|
2011-12-11 14:32:38 +01:00
|
|
|
pkgs = e.args[0]
|
|
|
|
for candidate in pkgs:
|
|
|
|
print(candidate)
|
2011-09-21 10:58:41 +02:00
|
|
|
|
2011-12-11 14:32:38 +01:00
|
|
|
from os.path import basename # To get the short name
|
2011-09-21 10:58:41 +02:00
|
|
|
|
2011-12-11 14:32:38 +01:00
|
|
|
print(file=sys.stderr)
|
|
|
|
print(pp.error("The short ebuild name '%s' is ambiguous. Please specify" % basename(pkgs[0])),
|
|
|
|
file=sys.stderr, end="")
|
|
|
|
pp.die(1, "one of the above fully-qualified ebuild names instead.")
|
2011-09-21 10:58:41 +02:00
|
|
|
except GentoolkitException as err:
|
2011-12-11 14:32:38 +01:00
|
|
|
pp.die(1, str(err))
|
2011-10-02 10:03:49 +02:00
|
|
|
except Exception as err:
|
2011-12-11 14:32:38 +01:00
|
|
|
pp.die(1, str(err))
|
2011-09-21 10:58:41 +02:00
|
|
|
|
2011-12-11 14:32:38 +01:00
|
|
|
if not CONFIG['quiet']:
|
|
|
|
print ()
|
2011-08-31 15:38:32 +02:00
|
|
|
|
2011-12-11 14:32:38 +01:00
|
|
|
for cp, url, version in ret:
|
|
|
|
if not CONFIG['quiet']:
|
|
|
|
print ("Upstream Version: "
|
|
|
|
+ pp.number("%s" % version)
|
|
|
|
+ pp.path(" %s" % url))
|
|
|
|
else:
|
|
|
|
print (pp.cpv("%s-%s" % (cp, version))
|
|
|
|
+ ": " + pp.path(url))
|
2011-08-31 15:38:32 +02:00
|
|
|
|
2011-12-11 14:32:38 +01:00
|
|
|
if not len(ret) and not CONFIG['quiet']:
|
2011-09-21 10:58:41 +02:00
|
|
|
print (pp.warn("Didn't find any new version, "
|
|
|
|
+ "check package's homepage for "
|
|
|
|
+ "more informations"));
|
2011-08-31 15:38:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
try:
|
|
|
|
setupSignals()
|
|
|
|
main()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print( "Aborted.")
|
|
|
|
sys.exit(errno.EINTR)
|
|
|
|
sys.exit(0)
|