Debian Codegraph @

test/UT/CA/mfgCAs/certgen/ESTCommon.py

raw
1 #! python
2
3 import os
4 import sys
5 from subprocess import call
6 est_logging_file = "estserver.scripts.log"
7 openssl_cmd = "openssl"
8 prev_code = 0
9 windows = False
10 EST_OPENSSL_CACNF = "estExampleCA.cnf"
11 logandexit_msg = \
12 """\
13 ###########..EXIT..##########
14 SCRIPT $EST_SCRIPTNAME EXIT: %s (%d)
15 ###########^^EXIT^^##########
16 \
17 """
18 headers_msg = \
19 """\
20 Headers:
21 %s
22 /Headers\
23 """
24
25 content_msg = \
26 """\
27 Content:
28 %s
29 /Content\
30 """
31
32
33 def logandexit(msg, code):
34 with open(est_logging_file, "w") as file:
35 file.write(logandexit_msg % (msg, code))
36 sys.exit(code)
37
38
39 def iferrorlogandexit(msg, code):
40 if prev_code:
41 logandexit(msg, code)
42
43
44 def dumpheadersandcontent(header_file, content_file):
45 with open(est_logging_file, "w") as file:
46 if header_file:
47 with open(header_file, "r") as headers:
48 file.write(headers_msg % (headers.read()))
49 if content_file:
50 with open(content_file, "r") as content:
51 file.write(content_msg % (content.read()))
52
53
54 def iferrorheaderslogandexit(search_hdr, hdr_file, log_msg, content_file):
55 if not search_hdr in open(hdr_file).read:
56 dumpheadersandcontent(hdr_file, content_file)
57 logandexit("Header ERROR: %s" % log_msg, 1)
58
59
60 def cert2pkcs72stdout(crt_file):
61 print("Content-Type: application/pkcs7-mime")
62 print("")
63 system("%s crl2pkcs7 -certfile %s -nocrl" %
64 (openssl_cmd, crt_file))
65
66
67 def combinefiles(file1, file2, outfile):
68 with open(outfile, "w") as fileout:
69 with open(file1) as file:
70 fileout.write(file.read())
71 with open(outfile, "a") as fileout:
72 with open(file2) as file:
73 fileout.write(file.read())
74
75
76 def touch(fname, times=None):
77 with open(fname, 'a'):
78 os.utime(fname, times)
79
80
81 def system(cmd):
82 if windows:
83 cmd = cmd.replace('/', '\\')
84 prev_code = call(cmd, shell=True)
85 return prev_code
86
87
88 def detectWindowsFlag():
89 global windows
90 if (len(sys.argv) > 1):
91 windows = (sys.argv[1] == "-w")