|
|
85cce4 |
"""
|
|
|
85a850 |
The main behavior of centpkg
|
|
|
85cce4 |
"""
|
|
|
85cce4 |
|
|
|
85a850 |
#
|
|
|
85a850 |
# Author(s):
|
|
|
85a850 |
# Jesse Keating <jkeating@redhat.com>
|
|
|
85a850 |
# Pat Riehecky <riehecky@fnal.gov>
|
|
|
85a850 |
# Brian Stinson <bstinson@ksu.edu>
|
|
|
85a850 |
#
|
|
|
85a850 |
# This program is free software; you can redistribute it and/or modify it
|
|
|
85a850 |
# under the terms of the GNU General Public License as published by the
|
|
|
85a850 |
# Free Software Foundation; either version 2 of the License, or (at your
|
|
|
85a850 |
# option) any later version. See http://www.gnu.org/copyleft/gpl.html for
|
|
|
85a850 |
# the full text of the license.
|
|
|
85a850 |
|
|
|
85a850 |
import os
|
|
|
85a850 |
import sys
|
|
|
85a850 |
import logging
|
|
|
85a850 |
|
|
|
b66e8a |
from configparser import ConfigParser
|
|
Brian Stinson |
3edf28 |
|
|
|
85a850 |
import pyrpkg
|
|
Brian Stinson |
3edf28 |
import pyrpkg.utils
|
|
Brian Stinson |
3edf28 |
import centpkg.cli
|
|
Brian Stinson |
3edf28 |
|
|
|
85a850 |
|
|
|
794ab0 |
def main():
|
|
Michal Konečný |
0d832c |
"""
|
|
Michal Konečný |
0d832c |
Centpkg main.
|
|
Michal Konečný |
0d832c |
|
|
Michal Konečný |
0d832c |
"""
|
|
Brian Stinson |
3edf28 |
program_name = os.path.basename(sys.argv[0])
|
|
Brian Stinson |
3edf28 |
|
|
|
5b6d17 |
default_user_config_path = os.path.join(
|
|
|
85cce4 |
os.path.expanduser("~"), ".config", "rpkg", "%s.conf" % program_name
|
|
|
85cce4 |
)
|
|
|
5b6d17 |
|
|
|
5b6d17 |
# Setup an argparser and parse the known commands to get the config file
|
|
|
5b6d17 |
|
|
|
5b6d17 |
# - use the custom ArgumentParser class from pyrpkg.cli and disable
|
|
|
5b6d17 |
# argument abbreviation to ensure that --user will be not treated as
|
|
|
5b6d17 |
# --user-config
|
|
|
5b6d17 |
parser = pyrpkg.cli.ArgumentParser(add_help=False, allow_abbrev=False)
|
|
Michal Konečný |
370301 |
|
|
|
85cce4 |
parser.add_argument(
|
|
|
85cce4 |
"-C",
|
|
|
85cce4 |
"--config",
|
|
|
85cce4 |
help="Specify a config file to use",
|
|
|
85cce4 |
default="/etc/rpkg/%s.conf" % program_name,
|
|
|
85cce4 |
)
|
|
|
85cce4 |
parser.add_argument(
|
|
|
85cce4 |
"--user-config",
|
|
|
85cce4 |
help="Specify a user config file to use",
|
|
|
85cce4 |
default=default_user_config_path,
|
|
|
85cce4 |
)
|
|
|
85a850 |
|
|
|
85a850 |
(args, other) = parser.parse_known_args()
|
|
|
85a850 |
|
|
|
85a850 |
# Make sure we have a sane config file
|
|
|
85cce4 |
if not os.path.exists(args.config) and not other[-1] in ["--help", "-h", "help"]:
|
|
|
85cce4 |
sys.stderr.write("Invalid config file %s\n" % args.config)
|
|
|
85a850 |
sys.exit(1)
|
|
|
85a850 |
|
|
Brian Stinson |
3edf28 |
# Setup a configuration object and read config file data
|
|
|
b66e8a |
config = ConfigParser()
|
|
|
85a850 |
config.read(args.config)
|
|
|
5b6d17 |
config.read(args.user_config)
|
|
|
85a850 |
|
|
|
794ab0 |
client = centpkg.cli.centpkgClient(config, name=program_name)
|
|
|
85cce4 |
client.do_imports(site="centpkg")
|
|
|
85a850 |
client.parse_cmdline()
|
|
|
85a850 |
|
|
Brian Stinson |
3edf28 |
# This is due to a difference argparse behavior to Python 2 version.
|
|
Brian Stinson |
3edf28 |
# In Python 3, argparse will proceed to here without reporting
|
|
Brian Stinson |
3edf28 |
# "too few arguments". Instead, client.args does not have attribute
|
|
Brian Stinson |
3edf28 |
# command.
|
|
|
85cce4 |
if not hasattr(client.args, "command"):
|
|
Brian Stinson |
3edf28 |
client.parser.print_help()
|
|
Brian Stinson |
3edf28 |
sys.exit(1)
|
|
Brian Stinson |
3edf28 |
|
|
|
85a850 |
if not client.args.path:
|
|
|
85a850 |
try:
|
|
Brian Stinson |
3edf28 |
client.args.path = pyrpkg.utils.getcwd()
|
|
Brian Stinson |
3edf28 |
except Exception:
|
|
|
85cce4 |
print("Could not get current path, have you deleted it?")
|
|
|
85a850 |
sys.exit(1)
|
|
|
85a850 |
|
|
Brian Stinson |
3edf28 |
# setup the logger -- This logger will take things of INFO or DEBUG and
|
|
Brian Stinson |
3edf28 |
# log it to stdout. Anything above that (WARN, ERROR, CRITICAL) will go
|
|
Brian Stinson |
3edf28 |
# to stderr. Normal operation will show anything INFO and above.
|
|
Brian Stinson |
3edf28 |
# Quiet hides INFO, while Verbose exposes DEBUG. In all cases WARN or
|
|
Brian Stinson |
3edf28 |
# higher are exposed (via stderr).
|
|
|
85a850 |
log = pyrpkg.log
|
|
|
85a850 |
client.setupLogging(log)
|
|
|
85a850 |
|
|
|
85a850 |
if client.args.v:
|
|
|
85a850 |
log.setLevel(logging.DEBUG)
|
|
|
85a850 |
elif client.args.q:
|
|
|
85a850 |
log.setLevel(logging.WARNING)
|
|
|
85a850 |
else:
|
|
|
85a850 |
log.setLevel(logging.INFO)
|
|
|
85a850 |
|
|
|
85a850 |
# Run the necessary command
|
|
|
85a850 |
try:
|
|
|
85a850 |
sys.exit(client.args.command())
|
|
|
85a850 |
except KeyboardInterrupt:
|
|
|
85a850 |
pass
|
|
Brian Stinson |
3edf28 |
except Exception as e:
|
|
|
85cce4 |
if getattr(client.args, "debug", False):
|
|
Brian Stinson |
3edf28 |
raise
|
|
|
85cce4 |
log.error("Could not execute %s: %s" % (client.args.command.__name__, e))
|
|
Brian Stinson |
13c432 |
sys.exit(1)
|
|
|
85a850 |
|
|
Brian Stinson |
3edf28 |
|
|
Brian Stinson |
3edf28 |
if __name__ == "__main__":
|
|
|
85a850 |
main()
|