2013-10-30 23:58:17 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2015-01-14 02:05:26 +02:00
|
|
|
# Copyright (C) 2015 GNS3 Technologies Inc.
|
2013-10-30 23:58:17 +02:00
|
|
|
#
|
|
|
|
# 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-04-03 01:10:59 +03:00
|
|
|
import os
|
2013-10-30 23:58:17 +02:00
|
|
|
import datetime
|
|
|
|
import sys
|
2014-04-25 00:59:34 +03:00
|
|
|
import locale
|
2015-01-14 02:05:26 +02:00
|
|
|
|
2014-04-06 20:37:34 +03:00
|
|
|
from gns3server.server import Server
|
2015-01-20 15:59:19 +02:00
|
|
|
from gns3server.web.logger import init_logger
|
2014-04-06 20:37:34 +03:00
|
|
|
from gns3server.version import __version__
|
2013-10-30 23:58:17 +02:00
|
|
|
|
2014-04-25 00:59:34 +03:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2015-01-20 14:24:00 +02:00
|
|
|
|
2014-04-25 00:59:34 +03:00
|
|
|
def locale_check():
|
|
|
|
"""
|
|
|
|
Checks if this application runs with a correct locale (i.e. supports UTF-8 encoding) and attempt to fix
|
|
|
|
if this is not the case.
|
|
|
|
|
|
|
|
This is to prevent UnicodeEncodeError with unicode paths when using standard library I/O operation
|
|
|
|
methods (e.g. os.stat() or os.path.*) which rely on the system or user locale.
|
2014-04-25 04:50:58 +03:00
|
|
|
|
|
|
|
More information can be found there: http://seasonofcode.com/posts/unicode-i-o-and-locales-in-python.html
|
|
|
|
or there: http://robjwells.com/post/61198832297/get-your-us-ascii-out-of-my-face
|
2014-04-25 00:59:34 +03:00
|
|
|
"""
|
|
|
|
|
2015-01-14 02:05:26 +02:00
|
|
|
# no need to check on Windows or when this application is frozen
|
2014-05-21 02:21:45 +03:00
|
|
|
if sys.platform.startswith("win") or hasattr(sys, "frozen"):
|
2014-04-25 00:59:34 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
language = encoding = None
|
|
|
|
try:
|
|
|
|
language, encoding = locale.getlocale()
|
|
|
|
except ValueError as e:
|
|
|
|
log.error("could not determine the current locale: {}".format(e))
|
|
|
|
if not language and not encoding:
|
|
|
|
try:
|
|
|
|
log.warn("could not find a default locale, switching to C.UTF-8...")
|
|
|
|
locale.setlocale(locale.LC_ALL, ("C", "UTF-8"))
|
|
|
|
except locale.Error as e:
|
|
|
|
log.error("could not switch to the C.UTF-8 locale: {}".format(e))
|
|
|
|
raise SystemExit
|
|
|
|
elif encoding != "UTF-8":
|
|
|
|
log.warn("your locale {}.{} encoding is not UTF-8, switching to the UTF-8 version...".format(language, encoding))
|
|
|
|
try:
|
|
|
|
locale.setlocale(locale.LC_ALL, (language, "UTF-8"))
|
|
|
|
except locale.Error as e:
|
|
|
|
log.error("could not set an UTF-8 encoding for the {} locale: {}".format(language, e))
|
2014-06-21 15:53:47 +03:00
|
|
|
raise SystemExit
|
2014-04-25 00:59:34 +03:00
|
|
|
else:
|
|
|
|
log.info("current locale is {}.{}".format(language, encoding))
|
|
|
|
|
|
|
|
|
2013-10-30 23:58:17 +02:00
|
|
|
def main():
|
2013-12-05 09:21:06 +02:00
|
|
|
"""
|
|
|
|
Entry point for GNS3 server
|
|
|
|
"""
|
2013-10-30 23:58:17 +02:00
|
|
|
|
2015-01-20 14:24:00 +02:00
|
|
|
# TODO: migrate command line options to argparse (don't forget the quiet mode).
|
2014-05-02 04:34:58 +03:00
|
|
|
|
2013-10-30 23:58:17 +02:00
|
|
|
current_year = datetime.date.today().year
|
2014-09-30 00:56:01 +03:00
|
|
|
|
2015-01-19 16:05:44 +02:00
|
|
|
# TODO: Renable the test when we will have command line
|
|
|
|
# user_log = logging.getLogger('user_facing')
|
|
|
|
# if not options.quiet:
|
|
|
|
# # Send user facing messages to stdout.
|
|
|
|
# stream_handler = logging.StreamHandler(sys.stdout)
|
|
|
|
# stream_handler.addFilter(logging.Filter(name='user_facing'))
|
|
|
|
# user_log.addHandler(stream_handler)
|
|
|
|
# user_log.propagate = False
|
|
|
|
# END OLD LOG CODE
|
2015-01-20 15:59:19 +02:00
|
|
|
user_log = init_logger(logging.DEBUG, quiet=False)
|
2015-01-19 16:05:44 +02:00
|
|
|
# FIXME END Temporary
|
2014-09-30 00:56:01 +03:00
|
|
|
|
|
|
|
user_log.info("GNS3 server version {}".format(__version__))
|
|
|
|
user_log.info("Copyright (c) 2007-{} GNS3 Technologies Inc.".format(current_year))
|
2015-01-20 14:24:00 +02:00
|
|
|
# TODO: end todo
|
2013-10-30 23:58:17 +02:00
|
|
|
|
2014-04-03 01:10:59 +03:00
|
|
|
# we only support Python 3 version >= 3.3
|
|
|
|
if sys.version_info < (3, 3):
|
2013-10-30 23:58:17 +02:00
|
|
|
raise RuntimeError("Python 3.3 or higher is required")
|
|
|
|
|
2014-09-30 00:56:01 +03:00
|
|
|
user_log.info("Running with Python {major}.{minor}.{micro} and has PID {pid}".format(
|
|
|
|
major=sys.version_info[0], minor=sys.version_info[1],
|
|
|
|
micro=sys.version_info[2], pid=os.getpid()))
|
2014-04-03 01:10:59 +03:00
|
|
|
|
2015-01-14 02:05:26 +02:00
|
|
|
# check for the correct locale (UNIX/Linux only)
|
2014-04-25 00:59:34 +03:00
|
|
|
locale_check()
|
|
|
|
|
2014-05-07 01:58:22 +03:00
|
|
|
try:
|
|
|
|
os.getcwd()
|
|
|
|
except FileNotFoundError:
|
2015-01-20 15:59:19 +02:00
|
|
|
log.critical("The current working directory doesn't exist")
|
2014-05-07 01:58:22 +03:00
|
|
|
return
|
|
|
|
|
2015-01-19 16:05:44 +02:00
|
|
|
# TODO: Renable console_bind_to_any when we will have command line parsing
|
2015-01-20 14:24:00 +02:00
|
|
|
# server = Server(options.host, options.port, options.console_bind_to_any)
|
2015-01-19 16:05:44 +02:00
|
|
|
server = Server("127.0.0.1", 8000, False)
|
2013-10-30 23:58:17 +02:00
|
|
|
server.run()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|