Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
The Tor Project
Anti-censorship
GetTor Project
gettor
Commits
f377c24d
Commit
f377c24d
authored
Aug 25, 2014
by
ilv
Browse files
Seems like I messed up with changes for cleaning the code and comments. Fixed
parent
02435673
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/gettor/core.py
View file @
f377c24d
...
...
@@ -72,13 +72,12 @@ class Core(object):
def
__init__
(
self
,
cfg
=
None
):
"""Create a new core object by reading a configuration file.
Raises: ConfigurationError if the configuration file doesn't exists
:param: cfg (string) the path of the configuration file.
:raise: ConfigurationError if the configuration file doesn't exists
or if something goes wrong while reading options from it.
Params: cfg - path of the configuration file.
"""
#
D
efine a set of default values
#
d
efine a set of default values
DEFAULT_CONFIG_FILE
=
'core.cfg'
logging
.
basicConfig
(
format
=
'[%(levelname)s] %(asctime)s - %(message)s'
,
...
...
@@ -115,7 +114,7 @@ class Core(object):
raise
ConfigurationError
(
"Error with conf. See log file."
)
try
:
self
.
supported_l
ocales
=
config
.
get
(
'links'
,
'locales'
)
self
.
supported_l
c
=
config
.
get
(
'links'
,
'locales'
)
except
ConfigParser
.
Error
as
e
:
logger
.
warning
(
"Couldn't read 'locales' from 'links' (%s)"
%
cfg
)
raise
ConfigurationError
(
"Error with conf. See log file."
)
...
...
@@ -139,79 +138,74 @@ class Core(object):
logger
.
warning
(
"Couldn't read 'dir' from 'log' %s)"
%
cfg
)
raise
ConfigurationError
(
"Error with conf. See log file."
)
#
K
eep log levels separated
self
.
log
ger
=
utils
.
filter_logging
(
logger
,
self
.
logdir
,
self
.
loglevel
)
# self.log
ger
.setLevel(logging.getLevelName(self.loglevel))
self
.
log
ger
.
info
(
'Redirecting logging to %s'
%
self
.
logdir
)
#
k
eep log levels separated
self
.
log
=
utils
.
filter_logging
(
logger
,
self
.
logdir
,
self
.
loglevel
)
# self.log.setLevel(logging.getLevelName(self.loglevel))
self
.
log
.
info
(
'Redirecting logging to %s'
%
self
.
logdir
)
#
S
top logging on stdout from now on
self
.
log
ger
.
propagate
=
False
self
.
log
ger
.
debug
(
"New core object created"
)
#
s
top logging on stdout from now on
self
.
log
.
propagate
=
False
self
.
log
.
debug
(
"New core object created"
)
def
get_links
(
self
,
service
,
o
perating_system
,
locale
):
def
get_links
(
self
,
service
,
o
s
,
lc
):
"""Get links for OS in locale.
This method should be called from the services modules of
GetTor (e.g. SMTP). To make it easy we let the module calling us
specify the name of the service (for stats purpose).
Raises: UnsupportedOSError: if the operating system is not supported
.
UnsupportedLocaleError: if the locale is not suppor
te
d
.
InternalError: if something goes wrong while internally
.
:param: service (string) the service trying to get the links
.
:param: os (string) the operating sys
te
m
.
:param: lc (string) tthe locale
.
Params: service - the name of the service trying to get the links
.
operating_system - the name of the operating sys
te
m
.
locale - two-character string representing the locale
.
:raise: UnsupportedOSError if the operating system is not supported
.
:raise: UnsupportedLocaleError if the locale is not suppor
te
d
.
:raise: InternalError if something goes wrong while internally
.
R
eturn
s
:
S
tring
wi
th links.
:r
eturn:
(s
tring
)
th
e
links.
"""
# Which module called us and what was asking for?
self
.
logger
.
info
(
"%s did a request for %s, %s."
%
(
service
,
operating_system
,
locale
))
self
.
log
.
info
(
"%s did a request for %s, %s."
%
(
service
,
os
,
lc
))
if
locale
not
in
self
.
supported_locales
:
self
.
logger
.
warning
(
"Request for unsupported locale: %s"
%
locale
)
raise
UnsupportedLocaleError
(
"Locale %s not supported at the "
"moment"
%
locale
)
if
lc
not
in
self
.
supported_lc
:
self
.
log
.
warning
(
"Request for unsupported locale: %s"
%
lc
)
raise
UnsupportedLocaleError
(
"Locale %s not supported"
%
lc
)
if
operating_system
not
in
self
.
supported_os
:
self
.
logger
.
warning
(
"Request for unsupported operating system: %s"
%
operating_system
)
raise
UnsupportedOSError
(
"Operating system %s not supported at the"
"moment"
%
operating_system
)
if
os
not
in
self
.
supported_os
:
self
.
log
.
warning
(
"Request for unsupported OS: %s"
%
os
)
raise
UnsupportedOSError
(
"OS %s not supported "
%
os
)
#
T
his could change in the future, let's leave it isolated.
links
=
self
.
_get_links
(
o
perating_system
,
locale
)
#
t
his could change in the future, let's leave it isolated.
links
=
self
.
_get_links
(
o
s
,
lc
)
if
links
is
None
:
self
.
log
ger
.
error
(
"Couldn't get the links"
,
exc_info
=
True
)
self
.
log
.
error
(
"Couldn't get the links"
,
exc_info
=
True
)
raise
InternalError
(
"Something went wrong internally. See logs for"
" detailed info."
)
self
.
log
ger
.
info
(
"Returning the links"
)
self
.
log
.
info
(
"Returning the links"
)
return
links
def
_get_links
(
self
,
o
perating_system
,
locale
):
def
_get_links
(
self
,
o
s
,
lc
):
"""Internal method to get the links.
Looks for the links inside each provider file. This should only be
called from get_links() method.
Returns: String with the links on success
.
None on failur
e.
:param: os (string) the operating system
.
:param: lc (string) the local
e.
Params: operating_system - name of the operating system
locale: two-character string representing the locale.
:return: (string/None) links on success, None otherwise.
"""
#
R
ead the links files using ConfigParser
#
S
ee the README for more details on the format used
#
r
ead the links files using ConfigParser
#
s
ee the README for more details on the format used
links
=
[]
#
L
ook for files ending with .links
#
l
ook for files ending with .links
p
=
re
.
compile
(
'.*\.links$'
)
for
name
in
os
.
listdir
(
self
.
linksdir
):
...
...
@@ -219,14 +213,14 @@ class Core(object):
if
os
.
path
.
isfile
(
path
)
and
p
.
match
(
path
):
links
.
append
(
path
)
#
L
et's create a dictionary linking each provider with the links
# found for o
perating_system
and l
ocale
. This way makes it easy
#
to check if no
links were found
#
l
et's create a dictionary linking each provider with the links
# found for o
s
and l
c
. This way makes it easy
to check if no
# links were found
providers
=
{}
self
.
log
ger
.
info
(
"Reading links from providers directory"
)
self
.
log
.
info
(
"Reading links from providers directory"
)
for
name
in
links
:
self
.
log
ger
.
debug
(
"Reading %s"
%
name
)
self
.
log
.
debug
(
"Reading %s"
%
name
)
# We're reading files listed on linksdir, so they must exist!
config
=
ConfigParser
.
ConfigParser
()
config
.
read
(
name
)
...
...
@@ -234,41 +228,40 @@ class Core(object):
try
:
pname
=
config
.
get
(
'provider'
,
'name'
)
except
ConfigParser
.
Error
as
e
:
self
.
log
ger
.
warning
(
"Couldn't get 'name' from 'provider' (%s)"
%
name
)
self
.
log
.
warning
(
"Couldn't get 'name' from 'provider' (%s)"
%
name
)
raise
InternalError
(
"Error while reading %s links file. See "
"log file"
%
name
)
self
.
log
ger
.
debug
(
"Checking if %s has links for %s in %s"
%
(
pname
,
o
perating_system
,
locale
))
self
.
log
.
debug
(
"Checking if %s has links for %s in %s"
%
(
pname
,
o
s
,
lc
))
try
:
providers
[
pname
]
=
config
.
get
(
o
perating_system
,
locale
)
providers
[
pname
]
=
config
.
get
(
o
s
,
lc
)
except
ConfigParser
.
Error
as
e
:
self
.
log
ger
.
warning
(
"Couldn't get %s from %s (%s)"
%
(
locale
,
operating_system
,
name
))
self
.
log
.
warning
(
"Couldn't get %s from %s (%s)"
%
(
lc
,
os
,
name
))
raise
InternalError
(
"Error while reading %s links file. See "
"log file"
%
name
)
#
E
ach provider must have a fingerprint of the key used to
#
e
ach provider must have a fingerprint of the key used to
# sign the uploaded packages
try
:
self
.
log
ger
.
debug
(
"Trying to get fingerprint from %s"
,
pname
)
self
.
log
.
debug
(
"Trying to get fingerprint from %s"
,
pname
)
fingerprint
=
config
.
get
(
'key'
,
'fingerprint'
)
providers
[
pname
]
=
providers
[
pname
]
+
"
\n
Fingerprint: "
providers
[
pname
]
=
providers
[
pname
]
+
fingerprint
self
.
log
ger
.
debug
(
"Fingerprint added %s"
,
fingerprint
)
self
.
log
.
debug
(
"Fingerprint added %s"
,
fingerprint
)
except
ConfigParser
.
Error
as
e
:
self
.
log
ger
.
warning
(
"Couldn't get 'fingerprint' from 'key' "
"(%s)"
%
name
)
self
.
log
.
warning
(
"Couldn't get 'fingerprint' from 'key' "
"(%s)"
%
name
)
raise
InternalError
(
"Error while reading %s links file. See "
"log file"
%
name
)
#
C
reate the final links list with all providers
#
c
reate the final links list with all providers
all_links
=
[]
self
.
logger
.
debug
(
"Joining all links found for %s in %s"
%
(
operating_system
,
locale
))
self
.
log
.
debug
(
"Joining all links found for %s in %s"
%
(
os
,
lc
))
for
key
in
providers
.
keys
():
all_links
.
append
(
"
\n
%s
\n
%s
\n
"
%
(
key
,
''
.
join
(
providers
[
key
]))
...
...
@@ -277,14 +270,14 @@ class Core(object):
if
all_links
:
return
""
.
join
(
all_links
)
else
:
self
.
log
ger
.
warning
(
"Trying to get supported os and locales, but"
" no links were found"
)
self
.
log
.
warning
(
"Trying to get supported os and locales, but"
" no links were found"
)
return
None
def
get_supported_os
(
self
):
"""Public method to get the list of supported operating systems.
R
eturn
s
:
L
ist
of string
s.
:r
eturn:
(l
ist
) the supported operating system
s.
"""
return
self
.
supported_os
.
split
(
','
)
...
...
@@ -292,10 +285,10 @@ class Core(object):
def
get_supported_lc
(
self
):
"""Public method to get the list of supported locales.
R
eturn
s
:
L
ist
of string
s.
:r
eturn:
(l
ist
) the supported locale
s.
"""
return
self
.
supported_l
ocales
.
split
(
','
)
return
self
.
supported_l
c
.
split
(
','
)
def
create_links_file
(
self
,
provider
,
fingerprint
):
"""Public method to create a links file for a provider.
...
...
@@ -304,116 +297,107 @@ class Core(object):
file with the proper format. It backs up the old links file
(if exists) and creates a new one.
Params: provider - provider's name (links file will use this
name in lower case).
fingerprint: fingerprint of the key that signed the packages
to be uploaded to the provider.
:param: provider (string) the provider (links file will use this
name in slower case).
:param: fingerprint (string) the fingerprint of the key that signed
the packages to be uploaded to the provider.
"""
linksfile
=
os
.
path
.
join
(
self
.
linksdir
,
provider
.
lower
()
+
'.links'
)
linksfile_backup
=
""
self
.
log
ger
.
info
(
"Request to create new %s"
%
linksfile
)
self
.
log
.
info
(
"Request to create new %s"
%
linksfile
)
if
os
.
path
.
isfile
(
linksfile
):
#
B
ackup the old file in case something fails
#
b
ackup the old file in case something fails
linksfile_backup
=
linksfile
+
'.backup'
self
.
logger
.
info
(
"Backing up %s to %s"
%
(
linksfile
,
linksfile_backup
))
self
.
log
.
info
(
"Backing up %s (%s)"
%
(
linksfile
,
linksfile_backup
))
os
.
rename
(
linksfile
,
linksfile_backup
)
try
:
#
T
his creates an empty links file (with no links)
#
t
his creates an empty links file (with no links)
content
=
ConfigParser
.
RawConfigParser
()
content
.
add_section
(
'provider'
)
content
.
set
(
'provider'
,
'name'
,
provider
)
content
.
add_section
(
'key'
)
content
.
set
(
'key'
,
'fingerprint'
,
fingerprint
)
content
.
add_section
(
'linux'
)
content
.
add_section
(
'windows'
)
content
.
add_section
(
'osx'
)
for
os
in
self
.
supported_os
:
content
.
add_section
(
os
)
with
open
(
linksfile
,
'w+'
)
as
f
:
content
.
write
(
f
)
self
.
log
ger
.
info
(
"New %s created"
%
linksfile
)
self
.
log
.
info
(
"New %s created"
%
linksfile
)
except
Exception
as
e
:
if
linksfile_backup
:
os
.
rename
(
linksfile_backup
,
linksfile
)
raise
LinkFileError
(
"Error while trying to create new links file."
)
def
add_link
(
self
,
provider
,
o
perating_system
,
locale
,
link
):
def
add_link
(
self
,
provider
,
o
s
,
lc
,
link
):
"""Public method to add a link to a provider's links file.
Use ConfigParser to add a link into the o
perating_system
section, under the locale
option. It checks for valid format;
the provider's script should
use the right format (see design).
Use ConfigParser to add a link into the o
s section, under the lc
option. It checks for valid format;
the provider's script should
use the right format (see design).
Raises: UnsupportedOSError: if the operating system is not supported.
UnsupportedLocaleError: if the locale is not supported.
LinkFileError: if there is no links file for the provider.
LinkFormatError: if the link format doesn't seem legit.
InternalError: if the links file doesn't have a section for the
OS requested. This *shouldn't* happen because
it means the file wasn't created correctly.
Params: provider - name of the provider.
operating_system - name of the operating system.
locale - two-character string representing the locale.
link - string to be added. The format should be as follows:
:param: provider (string) the provider.
:param: os (string) the operating system.
:param: lc (string) the locale.
:param: link (string) link to be added. The format should be as
follows:
https://pkg_url https://asc_url
where pkg_url is the url for the bundle and asc_url is the
url for the asc of the bundle.
:raise: UnsupportedOSError if the operating system is not supported.
:raise: UnsupportedLocaleError if the locale is not supported.
:raise: LinkFileError if there is no links file for the provider.
:raise: LinkFormatError if the link format doesn't seem legit.
:raise: InternalError if the links file doesn't have a section for
the OS requested. This *shouldn't* happen because it means
the file wasn't created correctly.
"""
linksfile
=
os
.
path
.
join
(
self
.
linksdir
,
provider
.
lower
()
+
'.links'
)
# Don't try to add unsupported stuff
if
locale
not
in
self
.
supported_locales
:
self
.
logger
.
warning
(
"Trying to add link for unsupported locale: %s"
%
locale
)
raise
UnsupportedLocaleError
(
"Locale %s not supported at the "
"moment"
%
locale
)
# don't try to add unsupported stuff
if
lc
not
in
self
.
supported_lc
:
self
.
log
.
warning
(
"Can't add link for unsupported lc: %s"
%
lc
)
raise
UnsupportedLocaleError
(
"Locale %s not supported"
%
lc
)
if
operating_system
not
in
self
.
supported_os
:
self
.
logger
.
warning
(
"Trying to add link for unsupported operating "
"system: %s"
%
operating_system
)
raise
UnsupportedOSError
(
"Operating system %s not supported at the"
" moment"
%
operating_system
)
if
os
not
in
self
.
supported_os
:
self
.
log
.
warning
(
"Can't add link for unsupported os: %s"
%
os
)
raise
UnsupportedOSError
(
"OS %s not supported"
%
os
)
#
C
heck if the link has a legit format
#
c
heck if the link has a legit format
# e.g. https://db.tt/JjfUTb04 https://db.tt/MEfUTb04
p
=
re
.
compile
(
'^https://.+\shttps://.+$'
)
if
not
p
.
match
(
link
):
self
.
logger
.
warning
(
"Trying to add an invalid link: %s"
%
link
)
raise
LinkFormatError
(
"Link '%s' doesn't seem to have a valid "
"format"
%
link
)
self
.
log
.
warning
(
"Can't add an invalid link: %s"
%
link
)
raise
LinkFormatError
(
"Link '%s' doesn't seem legit"
%
link
)
if
os
.
path
.
isfile
(
linksfile
):
content
=
ConfigParser
.
RawConfigParser
()
content
.
readfp
(
open
(
linksfile
))
#
C
heck if exists and entry for locale; if not, create it
#
c
heck if exists and entry for locale; if not, create it
try
:
links
=
content
.
get
(
o
perating_system
,
locale
)
links
=
content
.
get
(
o
s
,
lc
)
links
=
links
+
",
\n
"
+
link
content
.
set
(
o
perating_system
,
locale
,
links
)
content
.
set
(
o
c
,
lc
,
links
)
with
open
(
linksfile
,
'w'
)
as
f
:
content
.
write
(
f
)
self
.
log
ger
.
info
(
"Link %s added to %s %s in %s"
%
(
link
,
o
perating_system
,
locale
,
provider
))
self
.
log
.
info
(
"Link %s added to %s %s in %s"
%
(
link
,
o
s
,
lc
,
provider
))
except
ConfigParser
.
NoOptionError
:
content
.
set
(
o
perating_system
,
locale
,
link
)
content
.
set
(
o
s
,
lc
,
link
)
with
open
(
linksfile
,
'w'
)
as
f
:
content
.
write
(
f
)
self
.
log
ger
.
info
(
"Link %s added to %s-%s in %s"
%
(
link
,
o
perating_system
,
locale
,
provider
))
self
.
log
.
info
(
"Link %s added to %s-%s in %s"
%
(
link
,
o
s
,
lc
,
provider
))
except
ConfigParser
.
NoSectionError
:
# This shouldn't happen, but just in case
self
.
logger
.
error
(
"Unknown section %s in links file"
)
raise
InternalError
(
"Unknown %s section in links file"
%
operating_system
)
# this shouldn't happen, but just in case
self
.
log
.
error
(
"Unknown section %s in links file"
)
raise
InternalError
(
"Unknown %s section in links file"
%
os
)
else
:
raise
LinkFileError
(
"There is no links file for %s"
%
provider
)
...
...
Write
Preview
Supports
Markdown
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