Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
be0ca219
Commit
be0ca219
authored
Aug 11, 2014
by
James Cammarata
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'leth-optimise_utils_clean_data' into devel
parents
a650421e
25e9d119
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
52 deletions
+43
-52
lib/ansible/utils/__init__.py
+39
-52
test/units/TestUtils.py
+4
-0
No files found.
lib/ansible/utils/__init__.py
View file @
be0ca219
...
@@ -46,6 +46,7 @@ import getpass
...
@@ -46,6 +46,7 @@ import getpass
import
sys
import
sys
import
json
import
json
import
subprocess
import
subprocess
import
contextlib
from
vault
import
VaultLib
from
vault
import
VaultLib
...
@@ -55,7 +56,9 @@ MAX_FILE_SIZE_FOR_DIFF=1*1024*1024
...
@@ -55,7 +56,9 @@ MAX_FILE_SIZE_FOR_DIFF=1*1024*1024
# caching the compilation of the regex used
# caching the compilation of the regex used
# to check for lookup calls within data
# to check for lookup calls within data
LOOKUP_REGEX
=
re
.
compile
(
r'lookup\s*\('
)
LOOKUP_REGEX
=
re
.
compile
(
r'lookup\s*\('
)
PRINT_CODE_REGEX
=
re
.
compile
(
r'(?:{[{
%
]|[
%
}]})'
)
CODE_REGEX
=
re
.
compile
(
r'(?:{
%
|
%
})'
)
try
:
try
:
import
json
import
json
...
@@ -355,64 +358,48 @@ def _clean_data(orig_data, from_remote=False, from_inventory=False):
...
@@ -355,64 +358,48 @@ def _clean_data(orig_data, from_remote=False, from_inventory=False):
if
not
isinstance
(
orig_data
,
basestring
):
if
not
isinstance
(
orig_data
,
basestring
):
return
orig_data
return
orig_data
data
=
StringIO
.
StringIO
(
""
)
# when the data is marked as having come from a remote, we always
# when the data is marked as having come from a remote, we always
# replace any print blocks (ie. {{var}}), however when marked as coming
# replace any print blocks (ie. {{var}}), however when marked as coming
# from inventory we only replace print blocks that contain a call to
# from inventory we only replace print blocks that contain a call to
# a lookup plugin (ie. {{lookup('foo','bar'))}})
# a lookup plugin (ie. {{lookup('foo','bar'))}})
replace_prints
=
from_remote
or
(
from_inventory
and
'{{'
in
orig_data
and
LOOKUP_REGEX
.
search
(
orig_data
)
is
not
None
)
replace_prints
=
from_remote
or
(
from_inventory
and
'{{'
in
orig_data
and
LOOKUP_REGEX
.
search
(
orig_data
)
is
not
None
)
# these variables keep track of opening block locations, as we only
regex
=
PRINT_CODE_REGEX
if
replace_prints
else
CODE_REGEX
# want to replace matched pairs of print/block tags
print_openings
=
[]
with
contextlib
.
closing
(
StringIO
.
StringIO
(
orig_data
))
as
data
:
block_openings
=
[]
# these variables keep track of opening block locations, as we only
# want to replace matched pairs of print/block tags
for
idx
,
c
in
enumerate
(
orig_data
):
print_openings
=
[]
# if the current character is an opening brace, check to
block_openings
=
[]
# see if this is a jinja2 token. Otherwise, if the current
for
mo
in
regex
.
finditer
(
orig_data
):
# character is a closing brace, we backup one character to
token
=
mo
.
group
(
0
)
# see if we have a closing.
token_start
=
mo
.
start
(
0
)
if
c
==
'{'
and
idx
<
len
(
orig_data
)
-
1
:
token
=
orig_data
[
idx
:
idx
+
2
]
if
token
[
0
]
==
'{'
:
# if so, and we want to replace this block, push
if
token
==
'{
%
'
:
# this token's location onto the appropriate array
block_openings
.
append
(
token_start
)
if
token
==
'{{'
and
replace_prints
:
elif
token
==
'{{'
:
print_openings
.
append
(
idx
)
print_openings
.
append
(
token_start
)
elif
token
==
'{
%
'
:
block_openings
.
append
(
idx
)
elif
token
[
1
]
==
'}'
:
# finally we write the data to the buffer and write
prev_idx
=
None
data
.
seek
(
0
,
os
.
SEEK_END
)
if
token
==
'
%
}'
and
block_openings
:
data
.
write
(
c
)
prev_idx
=
block_openings
.
pop
()
elif
c
==
'}'
and
idx
>
0
:
elif
token
==
'}}'
and
print_openings
:
token
=
orig_data
[
idx
-
1
:
idx
+
1
]
prev_idx
=
print_openings
.
pop
()
prev_idx
=
-
1
if
token
==
'
%
}'
and
len
(
block_openings
)
>
0
:
if
prev_idx
is
not
None
:
prev_idx
=
block_openings
.
pop
()
# replace the opening
elif
token
==
'}}'
and
len
(
print_openings
)
>
0
:
data
.
seek
(
prev_idx
,
os
.
SEEK_SET
)
prev_idx
=
print_openings
.
pop
()
data
.
write
(
'{#'
)
# if we have a closing token, and we have previously found
# replace the closing
# the opening to the same kind of block represented by this
data
.
seek
(
token_start
,
os
.
SEEK_SET
)
# token, replace both occurrences, otherwise we just write
data
.
write
(
'#}'
)
# the current character to the buffer
if
prev_idx
!=
-
1
:
# replace the opening
data
.
seek
(
prev_idx
,
os
.
SEEK_SET
)
data
.
write
(
'{#'
)
# replace the closing
data
.
seek
(
-
1
,
os
.
SEEK_END
)
data
.
write
(
'#}'
)
else
:
else
:
data
.
seek
(
0
,
os
.
SEEK_END
)
assert
False
,
'Unhandled regex match'
data
.
write
(
c
)
else
:
return
data
.
getvalue
()
# not a jinja2 token, so we just write the current char
# to the output buffer
data
.
seek
(
0
,
os
.
SEEK_END
)
data
.
write
(
c
)
return_data
=
data
.
getvalue
()
data
.
close
()
return
return_data
def
_clean_data_struct
(
orig_data
,
from_remote
=
False
,
from_inventory
=
False
):
def
_clean_data_struct
(
orig_data
,
from_remote
=
False
,
from_inventory
=
False
):
'''
'''
...
...
test/units/TestUtils.py
View file @
be0ca219
...
@@ -730,6 +730,10 @@ class TestUtils(unittest.TestCase):
...
@@ -730,6 +730,10 @@ class TestUtils(unittest.TestCase):
'this string has a {#variable#}'
'this string has a {#variable#}'
)
)
self
.
assertEqual
(
self
.
assertEqual
(
ansible
.
utils
.
_clean_data
(
'this string {{has}} two {{variables}} in it'
,
from_remote
=
True
),
'this string {#has#} two {#variables#} in it'
)
self
.
assertEqual
(
ansible
.
utils
.
_clean_data
(
'this string has a {{variable with a
\n
newline}}'
,
from_remote
=
True
),
ansible
.
utils
.
_clean_data
(
'this string has a {{variable with a
\n
newline}}'
,
from_remote
=
True
),
'this string has a {#variable with a
\n
newline#}'
'this string has a {#variable with a
\n
newline#}'
)
)
...
...
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