Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.core
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
Roque
slapos.core
Commits
fa8f6d8a
Commit
fa8f6d8a
authored
Jun 11, 2022
by
Jérome Perrin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
console: set __file__ like python interpreter would do
parent
b9904d9f
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
8 deletions
+43
-8
slapos/cli/console.py
slapos/cli/console.py
+1
-0
slapos/client.py
slapos/client.py
+1
-0
slapos/tests/test_cli.py
slapos/tests/test_cli.py
+41
-8
No files found.
slapos/cli/console.py
View file @
fa8f6d8a
...
...
@@ -94,6 +94,7 @@ class ConsoleCommand(ClientConfigCommand):
if
args
.
script_file
:
with
open
(
args
.
script_file
)
as
f
:
code
=
compile
(
f
.
read
(),
args
.
script_file
,
'exec'
)
local
[
'__file__'
]
=
args
.
script_file
return
exec_
(
code
,
globals
(),
local
)
if
not
any
([
args
.
python
,
args
.
ipython
,
args
.
bpython
]):
...
...
slapos/client.py
View file @
fa8f6d8a
...
...
@@ -93,6 +93,7 @@ def init(conf, logger):
key_file
=
conf
.
key_file
,
cert_file
=
conf
.
cert_file
,
slapgrid_rest_uri
=
conf
.
master_rest_url
)
local
=
globals
().
copy
()
local
.
pop
(
'__file__'
,
None
)
local
[
'slap'
]
=
slap
# Create global shortcut functions to request instance and software
def
shorthandRequest
(
*
args
,
**
kwargs
):
...
...
slapos/tests/test_cli.py
View file @
fa8f6d8a
...
...
@@ -718,7 +718,7 @@ class TestCliSupervisorctl(CliMixin):
class
TestCliConsole
(
unittest
.
TestCase
):
script
=
"""
\
default_
script
=
"""
\
print(request('software_release', 'instance').getInstanceParameterDict()['parameter_name'])
"""
...
...
@@ -732,22 +732,55 @@ print(request('software_release', 'instance').getInstanceParameterDict()['parame
tempfile
.
NamedTemporaryFile
()
as
config_file
:
config_file
.
write
(
b'[slapos]
\
n
master_url=null
\
n
'
)
config_file
.
flush
()
yield
slapos
.
cli
.
entry
.
SlapOSApp
(),
config_file
.
name
yield
slapos
.
cli
.
entry
.
SlapOSApp
(),
\
config_file
.
name
,
\
mock_request
,
\
app_stdout
def
test_console_interactive
(
self
):
with
self
.
_test_console
()
as
(
app
,
config_file
,
mock_request
,
stdout
),
\
patch
.
object
(
sys
,
'stdin'
,
StringIO
(
self
.
default_script
)):
app
.
run
((
'console'
,
'--cfg'
,
config_file
))
self
.
assertIn
(
'parameter_value'
,
stdout
.
getvalue
())
mock_request
.
assert_called_once_with
(
'software_release'
,
'instance'
)
self
.
assertIn
(
'parameter_value'
,
app_stdout
.
getvalue
())
def
test_console_interactive
(
self
):
with
self
.
_test_console
()
as
(
app
,
config_file
),
\
patch
.
object
(
sys
,
'stdin'
,
StringIO
(
self
.
script
)):
def
test_console_interactive_no__file__
(
self
):
script
=
'''if 1:
try:
print('FAIL __file__ is set to', __file__)
except NameError:
print('OK __file__ is not set')
'''
with
self
.
_test_console
()
as
(
app
,
config_file
,
_
,
stdout
),
\
patch
.
object
(
sys
,
'stdin'
,
StringIO
(
script
)):
app
.
run
((
'console'
,
'--cfg'
,
config_file
))
self
.
assertIn
(
'OK __file__ is not set'
,
stdout
.
getvalue
())
def
test_console_script
(
self
):
with
self
.
_test_console
()
as
(
app
,
config_file
),
\
with
self
.
_test_console
()
as
(
app
,
config_file
,
mock_request
,
stdout
),
\
tempfile
.
NamedTemporaryFile
(
'w'
)
as
script
:
script
.
write
(
self
.
default_script
)
script
.
flush
()
app
.
run
((
'console'
,
'--cfg'
,
config_file
,
script
.
name
))
self
.
assertIn
(
'parameter_value'
,
stdout
.
getvalue
())
mock_request
.
assert_called_once_with
(
'software_release'
,
'instance'
)
def
test_console_script__file__
(
self
):
with
self
.
_test_console
()
as
(
app
,
config_file
,
_
,
stdout
),
\
tempfile
.
NamedTemporaryFile
(
'w'
)
as
script
:
script
.
write
(
self
.
script
)
script
.
write
(
'''if 1:
if __file__ == %r:
print('OK __file__ is set to script')
else:
print('FAIL __file__ is set to', __file__)
'''
%
script
.
name
)
script
.
flush
()
app
.
run
((
'console'
,
'--cfg'
,
config_file
,
script
.
name
))
self
.
assertIn
(
'OK __file__ is set to script'
,
stdout
.
getvalue
())
class
TestCliComplete
(
CliMixin
):
...
...
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