1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
# -*- coding: utf-8 -*-
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
# Copyright (c) 2012 dput authors
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
"""
Stuff that everything uses and shouldn't keep pulling on their own.
"""
import sys
import os.path
import logging
import traceback
import getpass
import xdg
import dput.logger
from logging.handlers import RotatingFileHandler
# used for searching for config files. place in order of precedence
CONFIG_LOCATIONS = {
"/usr/share/dput-ng/": 30,
"/etc/dput.d/": 20,
os.path.expanduser("~/.dput.d"): 10,
"skel/": 100
}
"""
Locations to look for JSON-ey config files. Under each directory may exist
a ``class``, which is a folder full of json files, which may be loaded.
The order dicates which has the most precedence.
"""
# dput reads from either of these locations but not both; do the same here
def _find_user_dput_cf():
xdg_name = os.path.join(xdg.xdg_config_home(), "dput", "dput.cf")
try:
open(xdg_name).close()
except EnvironmentError:
return os.path.expanduser("~/.dput.cf")
else:
return xdg_name
DPUT_CONFIG_LOCATIONS = {
"/etc/dput.cf": 15,
_find_user_dput_cf(): 5,
}
"""
Locations to look for old-style dput.cf configuration files.
"""
SCHEMA_DIRS = [
"/usr/share/dput-ng/schemas",
"skel/schemas"
]
"""
json schemas
"""
# logging routines
logging.setLoggerClass(dput.logger.DputLogger)
logger = logging.getLogger("dput")
"""
Logger, for general output and stuff.
"""
logger.setLevel(dput.logger.TRACE)
# basic config
_ch = logging.StreamHandler()
_ch.setLevel(logging.INFO)
_formatter = logging.Formatter(
'%(message)s')
_ch.setFormatter(_formatter)
def _write_upload_log(logfile, full_log):
upload_log_formatter = logging.Formatter(
"%(asctime)s - dput[%(process)d]: "
"%(module)s.%(funcName)s - %(message)s"
)
upload_log_handler = RotatingFileHandler(logfile)
upload_log_handler.setFormatter(upload_log_formatter)
if full_log:
upload_log_handler.setLevel(logging.DEBUG)
else:
upload_log_handler.setLevel(logging.INFO)
logger.addHandler(upload_log_handler)
def _enable_debugging(level):
_ch = logging.StreamHandler()
if level == 1:
_ch.setLevel(logging.DEBUG)
if level >= 2:
_ch.setLevel(dput.logger.TRACE)
_formatter = logging.Formatter(
'[%(levelname)s] %(created)f: (%(funcName)s) %(message)s')
_ch.setFormatter(_formatter)
logger.addHandler(_ch)
logger.addHandler(_ch)
def mangle_sys():
for root in CONFIG_LOCATIONS:
pth = "%s/scripts" % (root)
pth = os.path.abspath(pth)
if pth not in sys.path:
logger.debug("Loading external script location %s" % (pth))
sys.path.insert(0, pth)
def maybe_print_traceback(debug_level, stack):
if debug_level > 1:
tb = traceback.format_tb(stack[2])
logger.trace("Traceback:")
for level in tb:
for tier in level.split("\n"):
logger.trace(tier)
def get_local_username():
try:
local_user = getpass.getuser()
except Exception as e:
logger.warn("Could not determine local username: %s" % e)
local_user = None
return local_user
|