This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
__init__.py
executable file
·123 lines (103 loc) · 3.95 KB
/
__init__.py
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
# --------------------------------------------------------------------------
# Copyright (c) 2015-2017 Digital Sapphire
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the
# following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
# ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
# TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
# --------------------------------------------------------------------------
from __future__ import unicode_literals
import logging
from logging.handlers import RotatingFileHandler
import os
import sys
from appdirs import user_log_dir
from dsdev_utils.logger import logging_formatter
from pyupdater import __version__, settings
from pyupdater.cli import commands
from pyupdater.cli.options import get_parser
log = logging.getLogger()
log.setLevel(logging.DEBUG)
# Log to pyu.log if available
local_debug_file_path = os.path.join(os.getcwd(), 'pyu.log')
if os.path.exists(local_debug_file_path): # pragma: no cover
fh = logging.FileHandler(local_debug_file_path)
fh.setLevel(logging.DEBUG)
fh.setFormatter(logging_formatter)
log.addHandler(fh)
# Console logger
fmt = logging.Formatter('[%(levelname)s] %(message)s')
sh = logging.StreamHandler()
sh.setFormatter(fmt)
sh.setLevel(logging.INFO)
log.addHandler(sh)
# Default log directory
LOG_DIR = user_log_dir(settings.APP_NAME, settings.APP_AUTHOR)
if not os.path.exists(LOG_DIR): # pragma: no cover
os.makedirs(LOG_DIR)
log_file = os.path.join(LOG_DIR, settings.LOG_FILENAME_DEBUG)
rfh = RotatingFileHandler(log_file, maxBytes=1048576, backupCount=2)
rfh.setFormatter(logging_formatter)
rfh.setLevel(logging.DEBUG)
log.addHandler(rfh)
# The collect_debug_info command will use this
commands.LOG_DIR = LOG_DIR
def _real_main(args, namespace_test_helper=None): # pragma: no cover
pyi_args = None
if args is None:
args = sys.argv[1:]
if namespace_test_helper is None:
parser = get_parser()
args, pyi_args = parser.parse_known_args(args)
else:
# Used for tests
args = namespace_test_helper
dispatch_command(args, pyi_args)
# Dispatch the passed in command to its respective function in
# pyupdater.cli.commands
def dispatch_command(args, pyi_args=None, test=False):
# Turns collect-debug-info into collect_debug_info
cmd_str = "_cmd_" + args.command.replace('-', '_')
try:
cmd = getattr(commands, cmd_str)
# We are just making sure we can load the function
if test:
return True
cmd(args, pyi_args)
except AttributeError:
# This should only get hit by misconfigured tests.
# "Should" being the key word here :)
log.error('Unknown Command: %s', cmd_str)
return False
def main(args=None): # pragma: no cover
log.info('PyUpdater %s', __version__)
try:
_real_main(args)
except KeyboardInterrupt:
# Someones quick on the draw
print('\n')
msg = 'Exited by user'
log.warning(msg)
except Exception as err:
print(err)
log.error(err)
log.debug(err, exc_info=True)
if __name__ == '__main__': # pragma: no cover
args = sys.argv[1:]
main(args)