Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 1 | import atexit |
Alex | b151fbe | 2019-04-22 16:53:30 -0500 | [diff] [blame] | 2 | import grp |
| 3 | import os |
| 4 | import pwd |
| 5 | import time |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 6 | import tempfile |
Alex | b151fbe | 2019-04-22 16:53:30 -0500 | [diff] [blame] | 7 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 8 | _default_time_format = "%Y-%m-%d %H:%M:%S.%f%z" |
| 9 | _temp_files = {} |
Alex | b151fbe | 2019-04-22 16:53:30 -0500 | [diff] [blame] | 10 | |
| 11 | |
| 12 | def remove_file(filename): |
| 13 | os.remove(filename) |
| 14 | # open('filename', 'w').close() |
| 15 | |
| 16 | |
| 17 | def write_str_to_file(filename, _str): |
| 18 | with open(filename, 'w') as fo: |
| 19 | fo.write(_str) |
| 20 | |
| 21 | |
| 22 | def append_str_to_file(filename, _str): |
| 23 | with open(filename, 'a') as fa: |
| 24 | fa.write(_str) |
| 25 | |
| 26 | |
| 27 | def write_lines_to_file(filename, source_list): |
| 28 | with open(filename, 'w') as fw: |
| 29 | fw.write("\n".join(source_list) + "\n") |
| 30 | |
| 31 | |
| 32 | def append_lines_to_file(filename, source_list): |
| 33 | _buf = "\n".join(source_list) |
| 34 | with open(filename, 'a') as fw: |
| 35 | fw.write(_buf + "\n") |
| 36 | |
| 37 | |
| 38 | def append_line_to_file(filename, _str): |
| 39 | with open(filename, 'a') as fa: |
| 40 | fa.write(_str+'\n') |
| 41 | |
| 42 | |
| 43 | def read_file(filename): |
| 44 | _buf = None |
| 45 | with open(filename, 'rb') as fr: |
| 46 | _buf = fr.read() |
| 47 | return _buf |
| 48 | |
| 49 | |
| 50 | def read_file_as_lines(filename): |
| 51 | _list = [] |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 52 | with open(filename, 'rt') as fr: |
Alex | b151fbe | 2019-04-22 16:53:30 -0500 | [diff] [blame] | 53 | for line in fr: |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 54 | _list.append(line.rstrip()) |
Alex | b151fbe | 2019-04-22 16:53:30 -0500 | [diff] [blame] | 55 | return _list |
| 56 | |
| 57 | |
| 58 | def get_file_info_fd(fd, time_format=_default_time_format): |
| 59 | |
| 60 | def format_time(unixtime): |
| 61 | return time.strftime( |
| 62 | time_format, |
| 63 | time.gmtime(unixtime) |
| 64 | ) |
| 65 | |
| 66 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = \ |
| 67 | os.fstat(fd.fileno()) |
| 68 | |
| 69 | _dict = { |
| 70 | 'fd': fd.fileno(), |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 71 | 'mode': oct(mode & 0o777), |
Alex | b151fbe | 2019-04-22 16:53:30 -0500 | [diff] [blame] | 72 | 'device': hex(dev), |
| 73 | 'inode': ino, |
| 74 | 'hard_links': nlink, |
| 75 | 'owner_id': uid, |
| 76 | 'owner_name': pwd.getpwuid(uid).pw_name, |
| 77 | 'owner_group_name': grp.getgrgid(gid).gr_name, |
| 78 | 'owner_group_id': gid, |
| 79 | 'size': size, |
| 80 | 'access_time': format_time(atime), |
| 81 | 'modification_time': format_time(mtime), |
| 82 | 'creation_time': format_time(ctime) |
| 83 | } |
| 84 | |
| 85 | return _dict |
Alex | d9fd85e | 2019-05-16 16:58:24 -0500 | [diff] [blame] | 86 | |
| 87 | |
| 88 | def get_gzipped_file(url): |
| 89 | # imports |
| 90 | from io import BytesIO |
| 91 | from requests import get |
| 92 | import gzip |
| 93 | # download a file |
| 94 | _bytes = BytesIO(get(url).content) |
| 95 | with gzip.GzipFile(fileobj=_bytes) as gz: |
| 96 | return gz.read() |
Alex | 7f69a6a | 2019-05-31 16:53:35 -0500 | [diff] [blame] | 97 | |
| 98 | |
| 99 | def ensure_folder_exists(_folder): |
| 100 | if not os.path.exists(_folder): |
| 101 | # it is not exists, create it |
| 102 | os.mkdir(_folder) |
| 103 | return "... folder '{}' created".format(_folder) |
| 104 | else: |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame^] | 105 | return "... folder exists at '{}'".format(_folder) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 106 | |
| 107 | |
| 108 | def ensure_folder_removed(_folder): |
| 109 | if os.path.exists(_folder): |
| 110 | os.rmdir(_folder) |
| 111 | return "... folder '{}' removed".format(_folder) |
| 112 | else: |
| 113 | return "... folder '{}' not exists".format(_folder) |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 114 | |
| 115 | |
| 116 | def _cleanup_temp_files(): |
| 117 | global _temp_files |
| 118 | for temp_file in _temp_files.values(): |
| 119 | try: |
| 120 | os.remove(temp_file) |
| 121 | except OSError: |
| 122 | pass |
| 123 | _temp_files = {} |
| 124 | |
| 125 | |
| 126 | def create_temp_file_with_content(content, mode=None): |
| 127 | if len(_temp_files) == 0: |
| 128 | atexit.register(_cleanup_temp_files) |
| 129 | # Because we may change context several times, try to remember files we |
| 130 | # created and reuse them at a small memory cost. |
| 131 | content_key = hash(content) |
| 132 | if content_key in _temp_files: |
| 133 | return _temp_files[content_key] |
| 134 | |
| 135 | # new file, create it |
| 136 | _, name = tempfile.mkstemp() |
| 137 | _temp_files[content_key] = name |
| 138 | with open(name, 'wb') as fd: |
| 139 | fd.write(content.encode() if isinstance(content, str) else content) |
| 140 | |
| 141 | # set mode for the file |
| 142 | if mode: |
| 143 | os.chmod(name, mode) |
| 144 | |
| 145 | return name |