1
|
|
|
import os |
2
|
|
|
import random |
3
|
|
|
import shutil |
4
|
|
|
from datetime import datetime |
5
|
|
|
from uuid import UUID |
6
|
|
|
|
7
|
|
|
from satcfdi import Signer, DatePeriod |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
def parse_date_period(periodo): |
11
|
|
|
fmt, period = try_parsing_date(periodo) |
12
|
|
|
if fmt == '%Y': |
13
|
|
|
return DatePeriod(period.year) |
14
|
|
|
if fmt == '%Y-%m': |
15
|
|
|
return DatePeriod(period.year, period.month) |
16
|
|
|
if fmt == '%Y-%m-%d': |
17
|
|
|
return DatePeriod(period.year, period.month, period.day) |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
def try_parsing_date(text): |
21
|
|
|
for fmt in ('%Y', '%Y-%m', '%Y-%m-%d'): |
22
|
|
|
try: |
23
|
|
|
return fmt, datetime.strptime(text, fmt) |
24
|
|
|
except ValueError: |
25
|
|
|
pass |
26
|
|
|
raise ValueError('no valid date format found') |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def to_uuid(s): |
30
|
|
|
try: |
31
|
|
|
return UUID(s) |
32
|
|
|
except ValueError: |
33
|
|
|
return None |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def to_int(s): |
37
|
|
|
try: |
38
|
|
|
return int(s) |
39
|
|
|
except ValueError: |
40
|
|
|
return None |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def random_string(): |
44
|
|
|
chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
45
|
|
|
return "".join(random.choice(chars) for _ in range(32)) |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
def convert_ans1_date(ans1_date): |
49
|
|
|
return datetime.strptime(ans1_date.decode('utf-8'), '%Y%m%d%H%M%SZ') |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def cert_info(signer: Signer): |
53
|
|
|
if signer: |
54
|
|
|
return { |
55
|
|
|
"NoCertificado": signer.certificate_number, |
56
|
|
|
# "Tipo": str(signer.type), |
57
|
|
|
# |
58
|
|
|
# "organizationName": signer.certificate.get_subject().O, |
59
|
|
|
# "x500UniqueIdentifier": signer.certificate.get_subject().x500UniqueIdentifier, |
60
|
|
|
# "serialNumber": signer.certificate.get_subject().serialNumber, |
61
|
|
|
# "organizationUnitName": signer.certificate.get_subject().OU, |
62
|
|
|
# "emailAddress": signer.certificate.get_subject().emailAddress, |
63
|
|
|
# |
64
|
|
|
"Expira": convert_ans1_date(signer.certificate.get_notAfter()), |
65
|
|
|
"Creado": convert_ans1_date(signer.certificate.get_notBefore()), |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
def find_best_match(cases, dp: DatePeriod): |
70
|
|
|
fk, fv = (None, None) |
71
|
|
|
for k, v in cases.items(): |
72
|
|
|
k = datetime.strptime(k, '%Y-%m') |
73
|
|
|
if k <= dp: |
74
|
|
|
if fk is None or k > fk: |
75
|
|
|
fk, fv = k, v |
76
|
|
|
return fk, fv |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
def clear_directory(directory): |
80
|
|
|
shutil.rmtree(directory, ignore_errors=True) |
81
|
|
|
os.makedirs(directory, exist_ok=True) |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
# calculate the number of months between two dates |
85
|
|
|
def months_between(d1, d2): |
86
|
|
|
return (d1.year - d2.year) * 12 + d1.month - d2.month |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
def add_month(dp: DatePeriod, months): |
90
|
|
|
year, month = divmod(dp.year * 12 + dp.month + months - 1, 12) |
91
|
|
|
month += 1 |
92
|
|
|
return DatePeriod(year, month) |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
def load_certificate(data): |
96
|
|
|
if 'data' in data: |
97
|
|
|
return Signer.load_pkcs12( |
98
|
|
|
**data, |
99
|
|
|
) |
100
|
|
|
else: |
101
|
|
|
return Signer.load( |
102
|
|
|
**data, |
103
|
|
|
) |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
def first_duplicate(seq): |
107
|
|
|
seen = set() |
108
|
|
|
for x in seq: |
109
|
|
|
if x in seen: |
110
|
|
|
return x |
111
|
|
|
seen.add(x) |
112
|
|
|
return None |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
def parse_rango(rango): |
116
|
|
|
if rango == "": |
117
|
|
|
return 1, None |
118
|
|
|
if rango.isdigit(): |
119
|
|
|
return int(rango), int(rango) |
120
|
|
|
if "-" in rango: |
121
|
|
|
start, end = rango.split("-") |
122
|
|
|
return int(start), int(end) |
123
|
|
|
|