Advertisement
Guest User

Convert OSSN to YAML

a guest
Mar 20th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. import os
  2. import yaml
  3.  
  4. def main():
  5. dir_orig_ossn = "security-notes"
  6. dir_yaml_ossn = "yaml-notes"
  7.  
  8.  
  9. test_note = "security-notes/OSSN-0001"
  10. out_file = "yaml-notes/OSSN-0001.yaml"
  11.  
  12. for root, dirs, files in os.walk(dir_orig_ossn):
  13. for f in files:
  14. path = root + "/" + f
  15. print "Converting: { " + path + " }"
  16.  
  17. note_file = open(path, 'r')
  18. note_text = note_file.read()
  19. note_file.close()
  20.  
  21. title = parse_title(note_text)
  22.  
  23. summary = find_text_between_markers(note_text,
  24. '### Summary###',
  25. '### Affected Services / Software ###')
  26.  
  27. affected_services = find_text_between_markers(note_text,
  28. '### Affected Services / Software ###',
  29. '### Discussion ###')
  30.  
  31. discussion = find_text_between_markers(note_text,
  32. '### Discussion ###',
  33. '### Recommended Actions ###')
  34.  
  35. recommended_actions = find_text_between_markers(note_text,
  36. '### Recommended Actions ###',
  37. '### Contacts / References ###')
  38.  
  39. contacts_and_refs = find_text_between_markers(note_text,
  40. '### Contacts / References ###',
  41. None)
  42.  
  43. output_file = dir_yaml_ossn + "/" + f + ".yaml"
  44. yaml_doc = open(output_file, 'w')
  45. yaml_doc.write(create_yaml_section('title: ', title))
  46. yaml_doc.write(create_yaml_section('summary: ', summary))
  47. yaml_doc.write(create_yaml_section('affected_services: ', affected_services))
  48. yaml_doc.write(create_yaml_section('discussion: ', discussion))
  49. yaml_doc.write(create_yaml_section('recommended_actions: ', recommended_actions))
  50. yaml_doc.write(create_yaml_section('contacts_and_refs:', contacts_and_refs, strip_newlines=False))
  51. yaml_doc.close()
  52.  
  53. print "... done"
  54.  
  55.  
  56.  
  57. def parse_title(note_text):
  58. end_loc = note_text.find('---')
  59. return note_text[:end_loc].replace('\n','').replace('\r','')
  60.  
  61. def find_text_between_markers(text, start_marker, end_marker):
  62. # gets all of the text in between two marker strings
  63. starting_loc = text.find(start_marker) + len(start_marker)
  64. if end_marker:
  65. end_loc = text.find(end_marker) - 1
  66. else:
  67. end_loc = len(text)
  68. return text[starting_loc:end_loc].strip('\n\r').rstrip('\n\r')
  69.  
  70. def create_yaml_section(section_name, text, trailing_newlines=2, strip_newlines=True):
  71. return_string = ""
  72. return_string += section_name
  73. return_string += "\'"
  74. return_string += _prepare_for_yaml(text, strip_newlines)
  75. return_string += "\'"
  76. return_string += '\n' * trailing_newlines
  77. return return_string
  78.  
  79. def _prepare_for_yaml(orig_text, strip_newlines):
  80. transf_text = orig_text
  81. if strip_newlines:
  82. transf_text = _strip_newlines(transf_text)
  83. transf_text = _escape_quotes(transf_text)
  84. return transf_text
  85.  
  86. def _strip_newlines(text_to_strip):
  87. return text_to_strip.replace('\n', ' ')
  88.  
  89. def _escape_quotes(orig_str):
  90. return orig_str.replace("'", '"')
  91.  
  92.  
  93. if __name__ == "__main__":
  94. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement