File: ESTCommon.py

package info (click to toggle)
libest 3.2.0%2Bds-1.1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,792 kB
  • sloc: ansic: 60,980; java: 12,082; sh: 4,956; python: 4,474; xml: 385; makefile: 290
file content (91 lines) | stat: -rw-r--r-- 2,052 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#! python

import os
import sys
from subprocess import call
est_logging_file = "estserver.scripts.log"
openssl_cmd = "openssl"
prev_code = 0
windows = False
EST_OPENSSL_CACNF = "estExampleCA.cnf"
logandexit_msg = \
    """\
###########..EXIT..##########
SCRIPT $EST_SCRIPTNAME EXIT: %s (%d)
###########^^EXIT^^##########
 \
"""
headers_msg = \
    """\
Headers:
%s
/Headers\
"""

content_msg = \
    """\
Content:
%s
/Content\
"""


def logandexit(msg, code):
    with open(est_logging_file, "w") as file:
        file.write(logandexit_msg % (msg, code))
        sys.exit(code)


def iferrorlogandexit(msg, code):
    if prev_code:
        logandexit(msg, code)


def dumpheadersandcontent(header_file, content_file):
    with open(est_logging_file, "w") as file:
        if header_file:
            with open(header_file, "r") as headers:
                file.write(headers_msg % (headers.read()))
        if content_file:
            with open(content_file, "r") as content:
                file.write(content_msg % (content.read()))


def iferrorheaderslogandexit(search_hdr, hdr_file, log_msg, content_file):
    if not search_hdr in open(hdr_file).read:
        dumpheadersandcontent(hdr_file, content_file)
        logandexit("Header ERROR: %s" % log_msg, 1)


def cert2pkcs72stdout(crt_file):
    print("Content-Type: application/pkcs7-mime")
    print("")
    system("%s crl2pkcs7 -certfile %s -nocrl" %
           (openssl_cmd, crt_file))


def combinefiles(file1, file2, outfile):
    with open(outfile, "w") as fileout:
        with open(file1) as file:
            fileout.write(file.read())
    with open(outfile, "a") as fileout:
        with open(file2) as file:
            fileout.write(file.read())


def touch(fname, times=None):
    with open(fname, 'a'):
        os.utime(fname, times)


def system(cmd):
    if windows:
        cmd = cmd.replace('/', '\\')
    prev_code = call(cmd, shell=True)
    return prev_code


def detectWindowsFlag():
    global windows
    if (len(sys.argv) > 1):
        windows = (sys.argv[1] == "-w")