Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
ardublockly
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
ardublockly
Commits
867e7b56
Commit
867e7b56
authored
Jun 18, 2015
by
carlosperate
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Start script: improve app localiser and cli args.
parent
8e942f9f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
126 additions
and
75 deletions
+126
-75
ardublocklyserver/server.py
ardublocklyserver/server.py
+1
-1
start.py
start.py
+125
-74
No files found.
ardublocklyserver/server.py
View file @
867e7b56
...
@@ -15,7 +15,7 @@ PORT = 8000
...
@@ -15,7 +15,7 @@ PORT = 8000
def
start_server
(
document_root
):
def
start_server
(
document_root
):
""" Start the server with the document root indicated by argument """
""" Start the server with the document root indicated by argument """
print
(
'
\n
Setting HTTP Server Document Root to:
\n\t
'
+
document_root
+
"
\n
"
)
print
(
'Setting HTTP Server Document Root to:
\n\t
'
+
document_root
+
"
\n
"
)
os
.
chdir
(
document_root
)
os
.
chdir
(
document_root
)
server_address
=
(
ADDRESS
,
PORT
)
server_address
=
(
ADDRESS
,
PORT
)
server
=
BaseHTTPServer
.
HTTPServer
(
server
=
BaseHTTPServer
.
HTTPServer
(
...
...
start.py
View file @
867e7b56
...
@@ -11,6 +11,7 @@ from __future__ import unicode_literals, absolute_import
...
@@ -11,6 +11,7 @@ from __future__ import unicode_literals, absolute_import
import
os
import
os
import
re
import
re
import
sys
import
sys
import
struct
import
getopt
import
getopt
import
platform
import
platform
import
threading
import
threading
...
@@ -25,99 +26,149 @@ def open_browser(open_file):
...
@@ -25,99 +26,149 @@ def open_browser(open_file):
Start a browser in a separate thread after waiting for half a second.
Start a browser in a separate thread after waiting for half a second.
"""
"""
def
_open_browser
():
def
_open_browser
():
webbrowser
.
open
(
'http://localhost:%s/%s'
%
webbrowser
.
get
().
open
(
'http://localhost:%s/%s'
%
(
ardublocklyserver
.
server
.
PORT
,
open_file
))
(
ardublocklyserver
.
server
.
PORT
,
open_file
))
thread
=
threading
.
Timer
(
0.5
,
_open_browser
)
thread
=
threading
.
Timer
(
0.5
,
_open_browser
)
thread
.
start
()
thread
.
start
()
def
parsing_args
(
argv
):
def
find_ardublockly_dir
(
search_path
):
"""
Navigates within each node of a given path and tries to find the Ardublockly
project root directory. It assumes that the project root with have an folder
name ardublockly with an index.html file inside.
This function is required because this script can end up in executable form
in different locations of the project folder, and a literal relative path
should not be assumed.
:param search_path: Path in which to find the Ardublockly root project
folder.
:return: Path to the Ardublockly root folder.
If not found returns None.
"""
path_to_navigate
=
os
.
path
.
normpath
(
search_path
)
# Navigate through each path node starting at the bottom until there are
# no folders left
while
path_to_navigate
:
# Check if file /ardublokly/index.html exists within current path
if
os
.
path
.
isfile
(
os
.
path
.
join
(
path_to_navigate
,
'ardublockly'
,
'index.html'
)):
# Found the right folder, return it
return
path_to_navigate
path_to_navigate
=
os
.
path
.
dirname
(
path_to_navigate
)
# The right folder wasn't found, so return input path
return
None
def
parsing_cl_args
():
"""
"""
Processes the command line arguments. Arguments supported:
Processes the command line arguments. Arguments supported:
-h / --help
-h / --help
-s / --serverroot <working directory>
-s / --serverroot <working directory>
:return: dictionary with available options(keys) and value(value)
:return: Dictionary with available options(keys) and value(value).
"""
"""
option_dict
=
{}
# Set option defaults
try
:
launch_browser
=
True
opts
,
args
=
getopt
.
getopt
(
argv
,
"hs:"
,
[
"help"
,
"serverroot="
])
server_root
=
None
except
getopt
.
GetoptError
as
e
:
find_project_root
=
False
print
(
'There was a problem parsing the command line arguments:'
)
print
(
'
\t
%s'
%
e
)
sys
.
exit
(
1
)
for
opt
,
arg
in
opts
:
if
len
(
sys
.
argv
)
==
1
:
if
opt
in
(
"-h"
,
"--help"
):
print
(
"No command line arguments found."
)
print
(
'Include a server working directory using the flag:'
)
else
:
print
(
'
\t
-s <folder>'
)
try
:
sys
.
exit
(
0
)
opts
,
args
=
getopt
.
getopt
(
if
opt
in
(
"-s"
,
"--serverroot"
):
sys
.
argv
[
1
:],
# Windows only issue: In BlocklyRequestHandler, if chdir is fed
'hs:bf'
,
# an 'incorrect' path like 'C:' instead of 'C:\' or C:/' it
[
'help'
,
'serverroot='
,
'nobrowser'
,
'findprojectroot'
])
# fails silently maintaining the current working directory.
except
getopt
.
GetoptError
as
e
:
# Use regular expressions to catch this corner case.
print
(
'There was a problem parsing the command line arguments:'
)
if
re
.
match
(
"^[a-zA-Z]:$"
,
arg
):
print
(
'
\t
%s'
%
e
)
print
(
'The windows drive letter needs to end in a slash, '
+
sys
.
exit
(
1
)
'eg. %s
\\
'
%
arg
)
sys
.
exit
(
1
)
for
opt
,
arg
in
opts
:
# Check if the value is a valid directory
if
opt
in
(
'-h'
,
'--help'
):
if
os
.
path
.
isdir
(
arg
):
print
(
'Help flag parsed, these are the current options:
\n
'
)
option_dict
[
'serverroot'
]
=
arg
print
(
'
\t
-s <folder>
\t
Sets the server working directory.'
)
print
(
'Parsed "'
+
opt
+
'" flag with "'
+
arg
+
'" value.'
)
print
(
'
\t
-b
\t\t
Suppresses opening the local browser.'
)
sys
.
exit
(
0
)
if
opt
in
(
'-s'
,
'--serverroot'
):
# Windows only issue: In BlocklyRequestHandler, if chdir is fed
# an 'incorrect' path like 'C:' instead of 'C:\' or C:/' it
# fails silently maintaining the current working directory.
# Use regular expressions to catch this corner case.
if
re
.
match
(
"^[a-zA-Z]:$"
,
arg
):
print
(
'The windows drive letter needs to end in a slash, '
+
'eg. %s
\\
'
%
arg
)
sys
.
exit
(
1
)
# Check if the value is a valid directory
arg
=
os
.
path
.
normpath
(
arg
)
if
os
.
path
.
isdir
(
arg
):
server_root
=
arg
print
(
'Parsed "%s" flag with "%s" value.'
%
(
opt
,
arg
))
else
:
print
(
'Invalid directory "'
+
arg
+
'".'
)
sys
.
exit
(
1
)
elif
opt
in
(
'-b'
,
'--nobrowser'
):
launch_browser
=
False
print
(
'Parsed "%s" flag. No browser will be opened.'
%
opt
)
elif
opt
in
(
'-f'
,
'--findprojectroot'
):
find_project_root
=
True
print
(
'Parsed "%s" flag. The ardublockly project root will be '
'set as the server root directory.'
%
opt
)
else
:
else
:
print
(
'Invalid directory "'
+
arg
+
'".'
)
print
(
'Flag '
+
opt
+
' not recognised.'
)
sys
.
exit
(
1
)
else
:
return
find_project_root
,
launch_browser
,
server_root
print
(
'Flag '
+
opt
+
' not recognised.'
)
return
option_dict
def
main
(
argv
):
def
main
():
"""
"""
Initialises the Settings singleton
and starts the HTTP Server
Initialises the Settings singleton
, resolves paths, and starts the server.
"""
"""
print
(
'Running Python version %s'
%
platform
.
python_version
())
print
(
'Running Python %s (%s bit) on %s'
%
(
platform
.
python_version
(),
(
struct
.
calcsize
(
'P'
)
*
8
),
platform
.
platform
()))
# Checking command line arguments
if
len
(
argv
)
>
0
:
print
(
'
\n\n
======= Parsing Command line arguments =======
\n
'
)
print
(
"
\n
======= Parsing Command line arguments ======="
)
find_project_root
,
launch_browser
,
server_root
=
parsing_cl_args
()
arguments
=
parsing_args
(
argv
)
if
'serverroot'
in
arguments
:
print
(
'
\n\n
======= Loading Settings ======='
)
# Overwrite server working directory if valid
server_root
=
arguments
[
'serverroot'
]
# Loading the settings
print
(
"
\n
======= Loading Settings ======="
)
ardublocklyserver
.
compilersettings
.
ServerCompilerSettings
()
ardublocklyserver
.
compilersettings
.
ServerCompilerSettings
()
# Loading the server with the argument working root directory, or by default
print
(
'
\n\n
======= Resolving server and project paths =======
\n
'
)
# with the parent folder of where this script is executed, done to be able
# Based on command line options, set the server root to the ardublockly
# to find the closure lib directory on the same level as the project folder
# project root directory, a directory specified in the arguments, or by
print
(
"
\n
======= Starting Server ======="
)
# default to the project root directory.
this_file_parent_dir
=
\
this_file_dir
=
os
.
path
.
dirname
(
os
.
path
.
realpath
(
sys
.
argv
[
0
]))
os
.
path
.
dirname
(
os
.
path
.
realpath
(
sys
.
argv
[
0
]))
ardublockly_root_dir
=
find_ardublockly_dir
(
this_file_dir
)
try
:
if
ardublockly_root_dir
is
None
:
server_root
print
(
'The Ardublockly project root folder could not be found within '
except
NameError
:
'the %s directory !'
%
this_file_dir
)
server_root
=
this_file_parent_dir
# Opening the browser to the web-app
paths
=
[
server_root
,
this_file_parent_dir
]
common_path
=
os
.
path
.
commonprefix
(
paths
)
if
not
common_path
:
print
(
'The server working directory and Ardublockly project need to '
+
'be in the same root directory.!'
+
'The server root also needs to be at least one directory up '
+
'from the Ardublockly folder!'
)
sys
.
exit
(
1
)
sys
.
exit
(
1
)
relative_path
=
[
os
.
path
.
relpath
(
this_file_parent_dir
,
common_path
)]
print
(
"Ardublockly root directory: %s"
%
ardublockly_root_dir
)
app_index
=
os
.
path
.
normpath
(
os
.
path
.
join
(
relative_path
[
0
],
'ardublockly'
))
if
find_project_root
is
True
or
server_root
is
None
:
#print('Root & script parent: %s\nCommon & relative path: %s; %s\nIndex: %s'
server_root
=
ardublockly_root_dir
# % (paths, common_path, relative_path, app_index))
else
:
open_browser
(
app_index
)
# Arguments have set a server root, and to not find ardublockly dir
if
not
os
.
path
.
commonprefix
([
server_root
,
ardublockly_root_dir
]):
print
(
'The Ardublockly project folder needs to be accessible from '
'the server root directory !'
)
print
(
"Selected server root: %s"
%
server_root
)
if
launch_browser
:
# Find the relative path from server root to ardublockly html
ardublockly_html_dir
=
os
.
path
.
join
(
ardublockly_root_dir
,
'ardublockly'
)
relative_path
=
os
.
path
.
relpath
(
ardublockly_html_dir
,
server_root
)
print
(
"Ardublockly page relative path from server root: %s"
%
relative_path
)
open_browser
(
relative_path
)
print
(
'
\n\n
======= Starting Server =======
\n
'
)
ardublocklyserver
.
server
.
start_server
(
server_root
)
ardublocklyserver
.
server
.
start_server
(
server_root
)
if
__name__
==
"__main__"
:
if
__name__
==
'__main__'
:
main
(
sys
.
argv
[
1
:]
)
main
()
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