def parse(file_or_cmds, onbuild=False, with_container_id=False): """ Getting the structure Dockerfile """ commands = file_or_cmds if onbuild else _to_commands(file_or_cmds) image = _get_dist_name(commands) workdir, first, user, data, onbuild_lines = '/', True, 'root', \ OrderedDict(), [] for command in commands: if with_container_id: if image not in data: data[image] = OrderedDict() if not onbuild: if 'workdir' not in data[image]: data[image]['workdir'] = OrderedDict() if workdir not in data[image]['workdir']: data[image]['workdir'][workdir] = OrderedDict() if user not in data[image]['workdir'][workdir]: data[image]['workdir'][workdir][user] = OrderedDict() struct = data[image] else: if not onbuild: if 'workdir' not in data: data['workdir'] = OrderedDict() if workdir not in data['workdir']: data['workdir'][workdir] = OrderedDict() if user not in data['workdir'][workdir]: data['workdir'][workdir][user] = OrderedDict() struct = data if onbuild: udata = struct else: udata = struct['workdir'][workdir][user] split = [v for v in command.split() if len(v)] instr, value = split[0].upper(), ' '.join(split[1:]).strip() if instr not in _INSTRUCTIONS: break if instr == 'FROM': if with_container_id and not first: image = _hash_image_name(value) data[image] = OrderedDict() data[image]['from'] = filters.from_filter(value) else: first = False struct['from'] = filters.from_filter(value) elif instr == 'MAINTAINER': struct['maintainer'] = filters.maintainer_filter(value) elif instr == 'RUN': if 'run' not in udata: udata['run'] = [] udata['run'] = filters.run_filter(udata['run'], value) elif instr == 'CMD': if 'cmd' not in udata: udata['cmd'] = [] udata['cmd'] = filters.cmd_filter(udata['cmd'], value) elif instr == 'LABEL': if 'label' not in struct: struct['label'] = OrderedDict() struct['label'] = filters.label_filter(struct['label'], value) elif instr == 'EXPOSE': if 'expose' not in struct: struct['expose'] = [] struct['expose'] = filters.expose_filter(struct['expose'], value) elif instr == 'ENV': if 'env' not in struct: struct['env'] = OrderedDict() struct['env'] = filters.env_filter(struct['env'], value) elif instr == 'ADD': if 'add' not in udata: udata['add'] = [] udata['add'] = filters.add_filter(udata['add'], value) elif instr == 'COPY': if 'copy' not in udata: udata['copy'] = [] udata['copy'] = filters.copy_filter(udata['copy'], value) elif instr == 'ENTRYPOINT': udata['entrypoint'] = filters.entrypoint_filter(value) elif instr == 'VOLUME': if 'volume' not in struct: struct['volume'] = [] struct['volume'] = filters.volume_filter(struct['volume'], value) elif instr == 'USER': user = value elif instr == 'WORKDIR': workdir = value elif instr == 'ONBUILD': onbuild_lines.append(value) if len(onbuild_lines): struct['onbuild'] = parse( onbuild_lines, onbuild=True, with_container_id=False) return data
# If message is multi-part and has both html/text parts, # get only the text message elif self.raw_msg.get_content_type() == 'multipart/alternative': for part in self.raw_msg.walk(): if h.contains(part.get_content_type(), 'text/plain'): self.body = part.get_payload(decode=True) self.body = h.html_to_text(self.body) else: # If message is multi-part and encoded with base-64, combine plain # text and html text and strip all html tags for part in self.raw_msg.walk(): if h.contains(part.get_content_type(), 'text'): self.body = self.body + part.get_payload(decode=True) self.body = h.html_to_text(self.body) # Store data into essential variables self.sender = self.headers['From'].lower() self.to = self.headers['To'].lower() self.date_s = self.headers['Date'] self.date_d = h.c_date(self.date_s) self.subject = self.headers['Subject'].lower() if __name__ == '__main__': # read from file/stdin and pipe to feedparser m_instance = mail() m_instance.feed(sys.stdin.readlines()) m_instance.parse() filters.run_filter(m_instance)
# If message is multi-part and has both html/text parts, # get only the text message elif self.raw_msg.get_content_type() == 'multipart/alternative': for part in self.raw_msg.walk(): if h.contains(part.get_content_type(),'text/plain'): self.body = part.get_payload(decode=True) self.body = h.html_to_text(self.body) else: # If message is multi-part and encoded with base-64, combine plain # text and html text and strip all html tags for part in self.raw_msg.walk(): if h.contains(part.get_content_type(), 'text'): self.body = self.body + part.get_payload(decode=True) self.body = h.html_to_text(self.body) # Store data into essential variables self.sender = self.headers['From'].lower() self.to = self.headers['To'].lower() self.date_s = self.headers['Date'] self.date_d = h.c_date(self.date_s) self.subject = self.headers['Subject'].lower() if __name__ == '__main__': # read from file/stdin and pipe to feedparser m_instance = mail() m_instance.feed(sys.stdin.readlines()) m_instance.parse() filters.run_filter(m_instance)