blob: faf7cf06ec974a7f9b71c075f3314de7592bb55a [file] [log] [blame]
Alex9a4ad212020-10-01 18:04:25 -05001import atexit
Alexb151fbe2019-04-22 16:53:30 -05002import grp
3import os
4import pwd
5import time
Alex9a4ad212020-10-01 18:04:25 -05006import tempfile
Alexb151fbe2019-04-22 16:53:30 -05007
Alex9a4ad212020-10-01 18:04:25 -05008_default_time_format = "%Y-%m-%d %H:%M:%S.%f%z"
9_temp_files = {}
Alexb151fbe2019-04-22 16:53:30 -050010
11
12def remove_file(filename):
13 os.remove(filename)
14 # open('filename', 'w').close()
15
16
17def write_str_to_file(filename, _str):
18 with open(filename, 'w') as fo:
19 fo.write(_str)
20
21
22def append_str_to_file(filename, _str):
23 with open(filename, 'a') as fa:
24 fa.write(_str)
25
26
27def write_lines_to_file(filename, source_list):
28 with open(filename, 'w') as fw:
29 fw.write("\n".join(source_list) + "\n")
30
31
32def 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
38def append_line_to_file(filename, _str):
39 with open(filename, 'a') as fa:
40 fa.write(_str+'\n')
41
42
43def read_file(filename):
44 _buf = None
45 with open(filename, 'rb') as fr:
46 _buf = fr.read()
47 return _buf
48
49
50def read_file_as_lines(filename):
51 _list = []
Alex1839bbf2019-08-22 17:17:21 -050052 with open(filename, 'rt') as fr:
Alexb151fbe2019-04-22 16:53:30 -050053 for line in fr:
Alex1839bbf2019-08-22 17:17:21 -050054 _list.append(line.rstrip())
Alexb151fbe2019-04-22 16:53:30 -050055 return _list
56
57
58def 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(),
Alex3bc95f62020-03-05 17:00:04 -060071 'mode': oct(mode & 0o777),
Alexb151fbe2019-04-22 16:53:30 -050072 '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
Alexd9fd85e2019-05-16 16:58:24 -050086
87
88def 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()
Alex7f69a6a2019-05-31 16:53:35 -050097
98
99def 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:
Alexb2129542021-11-23 15:49:42 -0600105 return "... folder exists at '{}'".format(_folder)
Alex3bc95f62020-03-05 17:00:04 -0600106
107
108def 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)
Alex9a4ad212020-10-01 18:04:25 -0500114
115
116def _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
126def 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