-
Notifications
You must be signed in to change notification settings - Fork 0
/
expand-urls
executable file
·145 lines (124 loc) · 3.01 KB
/
expand-urls
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python3
import os
import re
import sys
import urllib.parse
#
# This is the prefix that will be required of all envvars to be processed
#
PREFIX = "ARKCASE_"
PREFIX_ENVVAR = "EXPAND_URLS_PREFIX"
#
# This is the suffix that will be required of all envvars to be processed
#
SUFFIX = "_URL"
SUFFIX_ENVVAR = "EXPAND_URLS_SUFFIX"
SCHEME_PORTS = {
"ftp" : 21,
"ftps" : 990,
"gopher" : 70,
"http" : 80,
"https" : 443,
"ldap" : 389,
"ldaps" : 636,
"imap" : 143,
"imaps" : 993,
"pop" : 110,
"pops" : 995,
"smtp" : 25,
"smtps" : 465,
"ssh" : 22,
"sftp" : 22,
"telnet" : 23,
"nfs" : 2049,
"nntp" : 119,
}
def resolve_port_from_scheme(scheme):
if (scheme) and (scheme in SCHEME_PORTS):
return int(SCHEME_PORTS[scheme])
return None
def assign_value(value, default=None):
# Allow empty values
if (value != None) and (not value):
return value
# If a non-empty value is given, then make sure
# it's valid as an environment variable name
if re.match("^[a-zA-Z_][a-zA-Z0-9_]*$", value):
return value
# If it's not valid, use the default
return default
if len(sys.argv) > 1:
# If the prefix is given as a parameter, try to use it
PREFIX = assign_value(sys.argv[1], PREFIX)
else:
try:
PREFIX = assign_value(os.environ[PREFIX_ENVVAR], PREFIX)
except KeyError:
pass
if len(sys.argv) > 2:
# If the suffix is given as a parameter, try to use it
SUFFIX = assign_value(sys.argv[2], SUFFIX)
else:
try:
SUFFIX = assign_value(os.environ[SUFFIX_ENVVAR], SUFFIX)
except KeyError:
pass
#
# Make sure the prefix ends with an underscore
#
if PREFIX and (not PREFIX.endswith("_")):
PREFIX = f"{PREFIX}_"
#
# Make sure the suffix starts with an underscore
#
if SUFFIX and (not SUFFIX.startswith("_")):
SUFFIX = f"_{SUFFIX}"
#
# This map links names of fields to the suffixes that will be applied
# when rendering the environment variables
#
parts = {
"scheme" : "scheme",
"path" : "path",
"hostname" : "host",
"port" : "port",
"query" : "query",
"fragment" : "fragment",
}
#
# This tiny function will parse out the URL into
# its parts, but only if the variable name matches
# the pattern ${PREFIX}_*_URL
#
def parse_url(name, value):
if name.startswith(PREFIX) and name.endswith(SUFFIX):
return urllib.parse.urlparse(value)
return None
#
# Ok... now iterate over all envvars, and process
# them all, knowing that the parse_url() function
# will only process the ones we're interested in
#
for name, value in os.environ.items():
url = parse_url(name, value)
if not url:
continue
for field, suffix in parts.items():
partValue = ""
if hasattr(url, field):
v = getattr(url, field)
if v:
partValue = v
#
# Special case: if there's no port, compute it
# from the scheme (if there's a scheme)
#
if (not partValue) and (field == "port"):
partValue = resolve_port_from_scheme(url.scheme)
if (not partValue):
partValue = ""
#
# Spit out stuff that can be eval'd by the shell
#
partValue = str(partValue).replace("'", "'\\''")
print(f"export {name}_{suffix.upper()}='{partValue}';")