-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
379 lines (315 loc) · 12 KB
/
util.py
File metadata and controls
379 lines (315 loc) · 12 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/env python3
import errno
import glob
import logging
import os
import re
import requests
import time
from multiprocessing.dummy import Pool as ThreadPool
import urllib
# Review later:
# https://github.com/anilmuppalla/python-unix-utils/blob/master/grep.py
# https://github.com/yennanliu/web_scraping
# https://github.com/yennanliu/CS_basics
# See about converting this for use in virtual environments:
# https://github.com/LonamiWebs/Py-Utils/blob/master/misc/pipu
# Ref: https://github.com/shpala/pybuild_utils/blob/master/base/system_info.py
# TODO: Figure out how to identify if in WLS and implement in code
import platform
def get_os() -> str:
"""
Identify OS family
:param questionablestring: NONE
:return: os_name_string
"""
uname_str = platform.system()
if 'MINGW' in uname_str:
return 'windows'
elif uname_str == 'Windows':
return 'windows'
elif uname_str == 'Linux':
return 'linux'
elif uname_str == 'Darwin':
return 'macosx'
elif uname_str == 'FreeBSD':
return 'freebsd'
elif uname_str == 'Android':
return 'android'
else:
return 'unknown'
# Ref: https://github.com/shpala/pybuild_utils/blob/master/base/system_info.py
# Then if Linux
# TODO: Are there other distributions that I need to catch?
def linux_get_dist():
"""
Return the running distribution group
RHEL == RHEL, CENTOS, FEDORA
DEBIAN == UBUNTU, DEBIAN
:param: NONE
:return: distribution_name_string
"""
linux_tuple = platform.linux_distribution()
dist_name = linux_tuple[0]
dist_name_upper = dist_name.upper()
if dist_name_upper in ["RHEL", "CENTOS LINUX", "FEDORA"]:
return "RHEL"
elif dist_name_upper in ["DEBIAN", "UBUNTU"]:
return "DEBIAN"
raise NotImplemented("Unknown platform '%s'" % dist_name)
# Work on a cross-OS safe 'fetch environment var' function.
# It needs to fail/recover sanely.
# This is still only the most spare skeleton
import os
import pathlib
def get_env_var(str: environment_variable) -> str:
env_var = os.environ.get(environment_variable)
# print(f"{env_var}")
return env_var
# Work on a cross-OS safe 'fetch path from environment var' function.
# It needs to fail/recover sanely.
# This is still only the most spare skeleton
import os
import pathlib
def get_path_from_env_var(str: environment_variable) -> object:
path_from_env_var = pathlib.Path(os.environ.get(environment_variable, pathlib.Path.cwd()))
# print(f"{path_from_env_var}")
return path_from_env_var
# Work on a cross-OS safe directory/file test and create function
# This is still only the most spare skeleton
def create_dir(dir_full_path):
# TODO: sanity check the input - safe for directory?
# Safe location? Safe characters?
# TODO: set the OS-specific path construction
# TODO: wrap each step in a try / catch
if os.path.isdir(dir_full_path):
return ("%s already exists" % dir_full_path)
else:
os.mkdir(dir_full_path)
# ?? os.chdir(dir_full_path)
return dir_full_path
# Ref: https://github.com/vered1986/PythonUtils/blob/master/academic_tools/references/common.py
def normalize_tainted_string(questionablestring):
"""
Trim, remove any non alphanumeric character and lower-case a questionable string
:param questionablestring: tainted string
:return: the normalized string
"""
return re.sub('\s+', ' ', re.sub('[\W_]+', ' ', questionablestring.lower()))
# Ref: Started with https://github.com/shpala/pybuild_utils/blob/master/base/utils.py
def read_file_line_by_line_to_list(text_file) -> list:
"""
Read file into an array
:param file: input file
:return: file lines in an array
"""
if not os.path.exists(text_file):
raise Exception('file path: {0} does not exist'.format(text_file))
file_array = []
with open(text_file, "r") as text_file_input:
for line in text_file_input:
file_array.append(line.strip())
return file_array
# Ref: Started with https://github.com/shpala/pybuild_utils/blob/master/base/utils.py
def read_file_line_by_line_to_set(file) -> set:
"""
Read file into a set
:param file: input file
:return: file lines in a set
"""
if not os.path.exists(file):
raise Exception('file path: {0} does not exist'.format(file))
file_set = set()
with open(file, "r") as text_file:
for line in text_file:
file_set.add(line.strip())
return file_set
# Read lines in a way that is ready for pipelines
# Though unsophisticated, this seems like a common
# and useful idiom.
# Under many circumstances input/output sanity checking
# or other *safety* measures will be needed in that while loop.
import sys
# ...your functions
if __name__ == "__main__":
line = sys.stdin.readline()
while line:
what_I_need = do_something_useful(line)
# do whatever is needed, I'll just print
print(f"{what_I_need}")
# re-fill 'line'
line = sys.stdin.readline()
def remove_commas(string_input: str) -> str:
import re
# Input: A collection of "strings" that may be comma separated.
# Output: A collection of "strings" that are now space separated.
no_commas = re.sub(r",", " ", string_input)
return no_commas
def convert_strings_to_floats(strings: str) -> list:
import statistics
# Input: The "strings" must be a list of "numbers" in string format,
# Output: A Python `list` of floats composed of the numbers in the Input.
#
# Depends upon: remove_commas(string_input: str)
#
# Remove commas - because it receives lists...
strings_no_commas: str = remove_commas(strings)
# Convert string input to a list of floats.
# My original use case was receiving a list on the command line.
# This approach was outlined at:
# https://www.geeksforgeeks.org/python/python-converting-all-strings-in-list-to-integers/
list_integers: list = list(map(float, strings_no_commas.split()))
return list_integers
def get_median(list_items: list) -> float:
import statistics
# Input: A Python list of numbers ("strings").
# Output: A float.
median_number = statistics.median(list_items)
return median_number
def get_mean(list_items: list) -> float:
import statistics
# Input: A Python list of numbers ("strings").
# Output: A float.
mean_number = statistics.mean(list_items)
return mean_number
# Ref: https://github.com/shpala/pybuild_utils/blob/master/base/utils.py
# TODO: Work on this download_file() function - make it more general & support proxies
from urllib.request import urlopen
# class for special err handling
class DownloadError(Exception):
def __init__(self, value):
self.value_ = value
def __str__(self):
return self.value_
# TODO: Sanity check the buffer sizing an file write; add try/catch
def download_file(url, current_dir):
"""
Attempt to download file from URL using sane buffer and write to file
:param file: input URL
:return: full path to downloaded file
"""
file_name = url.split('/')[-1]
response = urlopen(url)
if response.status != 200:
raise DownloadError(
"Can't download url: {0}, status: {1}, response: {2}".format(url, response.status, response.reason))
f = open(file_name, 'wb')
file_size = 0
header = response.getheader("Content-Length")
if header:
file_size = int(header)
print("Downloading: {0} Bytes: {1}".format(file_name, file_size))
file_size_dl = 0
block_sz = 8192
while True:
buffer = response.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
percent = 0 if not file_size else file_size_dl * 100. / file_size
status = r"%10d [%3.2f%%]" % (file_size_dl, percent)
status += chr(8) * (len(status) + 1)
print(status, end='\r')
f.close()
return os.path.join(current_dir, file_name)
# https://github.com/daveoncode/python-string-utils
# Installation
# pip install python-string-utils
import string_utils
# TODO: Build this out into a 'safer' utility function.
# Also use this as a reminder of how terse you can be in a hurry.
# Add input/output sanity checking and error handling
# See 'download_file()' above.
def get_content_via_http(url):
returned = requests.get(url)
return returned.content
# TODO: Build this out into a 'safe' utility function.
# Add input/output sanity checking and error handling
import urllib.parse
def url_encode_parms(url, *kwargs):
the_parameters = {"name": "nameString大", "age": 30}
print('&'.join('%s=%s' % (k, v) for k, v in the_parameters.items()))
# age=30&name=nameString大
print('&'.join('%s=%s' % (k, urllib.parse.quote(str(v))) for k, v in the_parameters.items()))
# name=nameString%E5%A4%A7&age=30
base_url = 'null.net/'
url = 'http://%s?%s' % (base_url, '&'.join('%s=%s' % (k, urllib.parse.quote(str(v))) for k, v in the_parameters.items()))
print(url)
# http://null.net/?name=nameString%E5%A4%A7&age=30
# TODO: Build this out into a 'safe' utility function.
# Add input/output sanity checking and error handling
def multi_process_stress_test(url1, url2):
"""
Attempt to download file from URL using sane buffer and write to file
Start 4 threads, send 100 http requests to the target server
Report on timing when finished
:param url1, url2: input full URL1 and URL2
:return:
"""
start = time.time()
targeturl_1 = """url1"""
targeturl_2 = """url2"""
urls = [targeturl_1, targeturl_2]*50
pool = ThreadPool(5)
ret = pool.map(get_content_via_http, urls)
pool.close()
pool.join()
print ('Test duration: %s' % (time.time() - start))
# FROM:
# https://github.com/oVirt/ovirt-hosted-engine-ha/blob/master/ovirt_hosted_engine_ha/lib/util.py
def to_bool(string):
bool_var = str(string).lower()[:1]
if bool_var in ('t', 'y', '1'):
return True
elif bool_var in ('f', 'n', '0'):
return False
else:
raise ValueError("Invalid value for boolean: {0}".format(string))
# FROM:
# https://github.com/oVirt/ovirt-hosted-engine-ha/blob/master/ovirt_hosted_engine_ha/lib/util.py
def __log_debug(logger, *args, **kwargs):
if logger:
logger.debug(*args, **kwargs)
# FROM:
# https://github.com/oVirt/ovirt-hosted-engine-ha/blob/master/ovirt_hosted_engine_ha/lib/util.py
def mkdir_recursive(path):
try:
os.makedirs(path)
except OSError as e:
if e.errno != errno.EEXIST or not os.path.isdir(path):
raise
# FROM:
# https://github.com/oVirt/ovirt-hosted-engine-ha/blob/master/ovirt_hosted_engine_ha/env/path.py
def escape_remote_path(path):
return path.replace("_", "__").replace("/", "_")
# FROM: https://github.com/shpala/pybuild_utils/blob/master/base/utils.py
# This is an interesting idea.
# TODO: Review the code and think about real-world applications
import subprocess
import shutil
def git_clone(url: str, current_dir: str, remove_dot_git=True):
common_git_clone_line = ['git', 'clone', '--depth=1', url]
cloned_dir = os.path.splitext(url.rsplit('/', 1)[-1])[0]
common_git_clone_line.append(cloned_dir)
subprocess.call(common_git_clone_line)
os.chdir(cloned_dir)
common_git_clone_init_line = ['git', 'submodule', 'update', '--init', '--recursive']
subprocess.call(common_git_clone_init_line)
directory = os.path.join(current_dir, cloned_dir)
if remove_dot_git:
shutil.rmtree(os.path.join(directory, '.git'))
return directory
# FROM: https://github.com/shpala/pybuild_utils/blob/master/base/utils.py
# This is another interesting idea and has many real-world applications.
# TODO: Review the code.
def is_role_based_email(email: str) -> bool:
r = re.compile('([^@]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$')
match = r.match(email)
if not match:
return False
start = match.group(1)
for x in ['noreply', 'support', 'admin', 'postmaster']:
if start == x:
return True
return False