Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
L
libloc
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
libloc
Commits
5a9b4c77
Commit
5a9b4c77
authored
Nov 17, 2019
by
Michael Tremer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
location-downloader: Add proper logging infrastructure
Signed-off-by:
Michael Tremer
<
michael.tremer@ipfire.org
>
parent
244a3b61
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
19 deletions
+42
-19
src/python/location-downloader.in
src/python/location-downloader.in
+42
-19
No files found.
src/python/location-downloader.in
View file @
5a9b4c77
...
...
@@ -19,6 +19,8 @@
import
argparse
import
gettext
import
logging
import
logging.handlers
import
lzma
import
os
import
random
...
...
@@ -33,15 +35,36 @@ import urllib.request
# Load our location module
import
location
import
logging
logging
.
basicConfig
(
level
=
logging
.
INFO
)
DATABASE_FILENAME
=
"test.db.xz"
MIRRORS
=
(
"https://location.ipfire.org/databases/"
,
"https://people.ipfire.org/~ms/location/"
,
)
def
setup_logging
(
level
=
logging
.
INFO
):
l
=
logging
.
getLogger
(
"location-downloader"
)
l
.
setLevel
(
level
)
# Log to console
h
=
logging
.
StreamHandler
()
h
.
setLevel
(
logging
.
DEBUG
)
l
.
addHandler
(
h
)
# Log to syslog
h
=
logging
.
handlers
.
SysLogHandler
(
address
=
"/dev/log"
,
facility
=
logging
.
handlers
.
SysLogHandler
.
LOG_DAEMON
)
h
.
setLevel
(
logging
.
INFO
)
l
.
addHandler
(
h
)
# Format syslog messages
formatter
=
logging
.
Formatter
(
"location-downloader[%(process)d]: %(message)s"
)
h
.
setFormatter
(
formatter
)
return
l
# Initialise logging
log
=
setup_logging
()
# i18n
def
_
(
singular
,
plural
=
None
,
n
=
None
):
if
plural
:
...
...
@@ -100,21 +123,21 @@ class Downloader(object):
def
_send_request
(
self
,
req
,
**
kwargs
):
# Log request headers
log
ging
.
debug
(
"HTTP %s Request to %s"
%
(
req
.
method
,
req
.
host
))
log
ging
.
debug
(
" URL: %s"
%
req
.
full_url
)
log
ging
.
debug
(
" Headers:"
)
log
.
debug
(
"HTTP %s Request to %s"
%
(
req
.
method
,
req
.
host
))
log
.
debug
(
" URL: %s"
%
req
.
full_url
)
log
.
debug
(
" Headers:"
)
for
k
,
v
in
req
.
header_items
():
log
ging
.
debug
(
" %s: %s"
%
(
k
,
v
))
log
.
debug
(
" %s: %s"
%
(
k
,
v
))
try
:
res
=
urllib
.
request
.
urlopen
(
req
,
**
kwargs
)
except
urllib
.
error
.
HTTPError
as
e
:
# Log response headers
log
ging
.
debug
(
"HTTP Response: %s"
%
e
.
code
)
log
ging
.
debug
(
" Headers:"
)
log
.
debug
(
"HTTP Response: %s"
%
e
.
code
)
log
.
debug
(
" Headers:"
)
for
header
in
e
.
headers
:
log
ging
.
debug
(
" %s: %s"
%
(
header
,
e
.
headers
[
header
]))
log
.
debug
(
" %s: %s"
%
(
header
,
e
.
headers
[
header
]))
# Handle 304
if
e
.
code
==
304
:
...
...
@@ -124,10 +147,10 @@ class Downloader(object):
raise
e
# Log response headers
log
ging
.
debug
(
"HTTP Response: %s"
%
res
.
code
)
log
ging
.
debug
(
" Headers:"
)
log
.
debug
(
"HTTP Response: %s"
%
res
.
code
)
log
.
debug
(
" Headers:"
)
for
k
,
v
in
res
.
getheaders
():
log
ging
.
debug
(
" %s: %s"
%
(
k
,
v
))
log
.
debug
(
" %s: %s"
%
(
k
,
v
))
return
res
...
...
@@ -166,12 +189,12 @@ class Downloader(object):
# Nothing to do when the database on the server is up to date
except
NotModifiedError
:
log
ging
.
info
(
"Local database is up to date"
)
log
.
info
(
"Local database is up to date"
)
return
# Catch decompression errors
except
lzma
.
LZMAError
as
e
:
log
ging
.
warning
(
"Could not decompress downloaded file: %s"
%
e
)
log
.
warning
(
"Could not decompress downloaded file: %s"
%
e
)
continue
# XXX what do we catch here?
...
...
@@ -224,7 +247,7 @@ class CLI(object):
# Enable debug logging
if
args
.
debug
:
log
ging
.
basicConfig
(
level
=
logging
.
DEBUG
)
log
.
setLevel
(
logging
.
DEBUG
)
# Print usage if no action was given
if
not
"func"
in
args
:
...
...
@@ -265,7 +288,7 @@ class CLI(object):
# If no file could be downloaded, log a message
except
FileNotFoundError
as
e
:
log
ging
.
error
(
"Could not download a new database"
)
log
.
error
(
"Could not download a new database"
)
return
1
# If we have not received a new file, there is nothing to do
...
...
@@ -283,10 +306,10 @@ class CLI(object):
# Check if the downloaded file is newer
if
db
.
created_at
<=
created_at
:
log
ging
.
warning
(
"Downloaded database is older than the current version"
)
log
.
warning
(
"Downloaded database is older than the current version"
)
return
1
log
ging
.
info
(
"Downloaded new database from %s"
%
(
time
.
strftime
(
log
.
info
(
"Downloaded new database from %s"
%
(
time
.
strftime
(
"%a, %d %b %Y %H:%M:%S GMT"
,
time
.
gmtime
(
db
.
created_at
),
)))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment