feisty meow concerns codebase  2.140
one_line_cert_to_pem.py
Go to the documentation of this file.
1 #! /usr/bin/env python3
2 """
3 
4 Takes a certificate from the awful, often used form of a single line of text and turns
5 it into a real PEM file.
6 This assumes the input file has one single line of text that needs to be formatted
7 appropriately.
8 
9 author: chris koeritz
10 
11 """
12 
13 import os
14 import sys
15 
16 
17 
18 def split_lines(unsplit_line: str) -> None:
19  """
20  Takes text as input and breaks it up at the prescribed 64 character boundaries
21  used by PEM files.
22  """
23 
24  size = 64
25  splits = [(unsplit_line[i : i + size]) for i in range(0, len(unsplit_line), size)]
26 
27  #print("splits:", splits)
28  for split_line in splits:
29  if len(split_line) > 0:
30  print(split_line)
31 
32 
33 
34 def main() -> None:
35  """Main function"""
36 
37  # make sure they gave us a filename.
38  args = len(sys.argv)
39  if args < 2:
40  print("\
41 This script needs a filename to operate on. The file is expected to contain\n\
42 one line of certificate data, which this script will reformat into a standard\n\
43 PEM file format. The PEM file will be output on the console.")
44  exit(1)
45 
46  filename = sys.argv[1]
47 
48  # make sure the filename is valid.
49  if not os.path.isfile(filename):
50  print("The filename provided does not seem to be a readable file:", filename)
51  exit(1)
52 
53  file = open(filename, "r")
54 
55  cert_line = file.readline()
56  cert_line = cert_line.strip('\r\n')
57 
58  #ugh, no extra noise needed.
59  #print()
60  #print("below is the properly formatted output sourced from:", filename)
61  #print()
62 
63  print("-----BEGIN CERTIFICATE-----")
64  split_lines(cert_line)
65  print("-----END CERTIFICATE-----")
66 
67 
68 
69 if __name__ == "__main__":
70  main()
71 
72 
#define open
Definition: Xos2defs.h:36
None split_lines(str unsplit_line)