Start cleanup

This commit is contained in:
Peter Eckersley
2016-06-15 17:26:38 -07:00
parent c867e1e0d5
commit 4f46289c1b
3 changed files with 7 additions and 59 deletions
+6 -38
View File
@@ -16,12 +16,10 @@ class RawNginxParser(object):
# constants
space = Optional(White())
nspace = Optional(Regex(r"[ \t]+"))
left_bracket = Literal("{").suppress()
right_bracket = space.leaveWhitespace() + Literal("}").suppress()
semicolon = Literal(";").suppress()
key = Word(alphanums + "_/+-.")
blankLine = ZeroOrMore(Regex(r"^\S$"))
# Matches anything that is not a special character AND any chars in single
# or double quotes
value = Regex(r"((\".*\")?(\'.*\')?[^\{\};,]?)+")
@@ -40,8 +38,6 @@ class RawNginxParser(object):
block << Group(
# XXX could this "key" be Literal("location")?
# WIP: in order to allow this leaveWhitespace(), we're going to need an explicit
# "whitespaceline" construction...
(Group(space + key + location_statement) ^ Group(if_statement) ^
Group(map_statement)).leaveWhitespace() +
left_bracket +
@@ -51,7 +47,8 @@ class RawNginxParser(object):
script = OneOrMore(Group(comment | assignment) ^ block) + stringEnd
script.parseWithTabs()
def __init__(self, source): self.source = source
def __init__(self, source):
self.source = source
def parse(self):
"""Returns the parsed tree."""
@@ -71,27 +68,16 @@ class RawNginxDumper(object):
def __iter__(self, blocks=None):
"""Iterates the dumped nginx content."""
blocks = blocks or self.blocks
print "iterating", blocks
for b in blocks:
b0 = b
b = copy.deepcopy(b)
indentation = ""
if isinstance(b, str):
yield b
continue
b = copy.deepcopy(b)
indentation = ""
if spacey(b[0]):
try:
indentation = b.pop(0)
except:
import ipdb
ipdb.set_trace()
#indentation = indentation.replace("\n", "", 1)
indentation = b.pop(0)
key = b.pop(0)
values = b.pop(0)
if "worker_processes" in str(b0) and False:
import ipdb
ipdb.set_trace()
if isinstance(key, list):
yield indentation + "".join(key) + '{'
@@ -117,14 +103,9 @@ class RawNginxDumper(object):
def __str__(self):
"""Return the parsed block as a string."""
x = ''.join(self)
print "Merging:\n", repr(x)
return ''.join(self) + '\n'
# Shortcut functions to respect Python's serialization interface
# (like pyyaml, picker or json)
@@ -136,16 +117,7 @@ def loads(source):
:rtype: list
"""
new = UnspacedList(RawNginxParser(source).as_list())
print "\nNew:"
print new
for entry in new:
print len(entry), " ",
print "\nNewspaced:"
for entry in new.spaced:
print str(len(entry))+ " ",
print "\ngo"
return new
return UnspacedList(RawNginxParser(source).as_list())
def load(_file):
@@ -238,7 +210,3 @@ class UnspacedList(list):
idx -= 1
pos += 1
return spaces
def with_spaces(self):
return self.spaced
+1 -16
View File
@@ -215,25 +215,10 @@ class NginxParser(object):
filename = filename + os.path.extsep + ext
try:
out = nginxparser.dumps(tree)
logger.debug('Dumping to %s:\n%s', filename, out)
#logger.debug('Writing nginx conf tree to %s:\n%s', filename, out)
with open(filename, 'w') as _file:
_file.write(out)
if True or "owncloud" in filename:
print "Outputting", filename
print out
a = open("/tmp/nginx/sites-enabled/owncloud.conf").read()
b = open(filename).read()
for linea, lineb in zip(a.split('\n'), b.split('\n')):
if linea != lineb:
print "a", repr(linea)
print "b", repr(lineb)
if a != b:
print "Mismatch!"
if a != out:
print "Double mismatch", len(a), len(out)
else:
print "Match!"
except IOError:
logger.error("Could not open file for writing: %s", filename)
@@ -125,11 +125,6 @@ class NginxTlsSni01(common.TLSSNI01):
with open(self.challenge_conf, "w") as new_conf:
out = nginxparser.dumps(config)
if "mime" in self.challenge_conf:
print "Weird"
print out
#sys.exit(1)
#nginxparser.dump(config, new_conf)
new_conf.write(out)
def _make_server_block(self, achall, addrs):