8000 testgres.plugins.probackup2.Init was restored [thanks to Yuri Sokolov] · postgrespro/testgres@d35ca43 · GitHub
[go: up one dir, main page]

Skip to content

Commit d35ca43

Browse files
testgres.plugins.probackup2.Init was restored [thanks to Yuri Sokolov]
1 parent 8af4a49 commit d35ca43

File tree

1 file changed

+72
-79
lines changed

1 file changed

+72
-79
lines changed
Lines changed: 72 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import annotations
2-
31
import logging
42
from functools import reduce
53
import getpass
@@ -39,27 +37,21 @@
3937

4038
class Init(object):
4139
def __init__(self):
42-
pass
43-
44-
@staticmethod
45-
def create() -> Init:
46-
SELF = __class__()
47-
4840
if '-v' in sys.argv or '--verbose' in sys.argv:
49-
SELF.verbose = True
41+
self.verbose = True
5042
else:
51-
SELF.verbose = False
52-
53-
SELF._pg_config = testgres.get_pg_config()
54-
SELF.is_enterprise = SELF._pg_config.get('PGPRO_EDITION', None) == 'enterprise'
55-
SELF.is_shardman = SELF._pg_config.get('PGPRO_EDITION', None) == 'shardman'
56-
SELF.is_pgpro = 'PGPRO_EDITION' in SELF._pg_config
57-
SELF.is_nls_enabled = 'enable-nls' in SELF._pg_config['CONFIGURE']
58-
SELF.is_lz4_enabled = '-llz4' in SELF._pg_config['LIBS']
59-
version = SELF._pg_config['VERSION'].rstrip('develalphabetapre')
43+
self.verbose = False
44+
45+
self._pg_config = testgres.get_pg_config()
46+
self.is_enterprise = self._pg_config.get('PGPRO_EDITION', None) == 'enterprise'
47+
self.is_shardman = self._pg_config.get('PGPRO_EDITION', None) == 'shardman'
48+
self.is_pgpro = 'PGPRO_EDITION' in self._pg_config
49+
self.is_nls_enabled = 'enable-nls' in self._pg_config['CONFIGURE']
50+
self.is_lz4_enabled = '-llz4' in self._pg_config['LIBS']
51+
version = self._pg_config['VERSION'].rstrip('develalphabetapre')
6052
parts = [*version.split(' ')[1].split('.'), '0', '0'][:3]
6153
parts[0] = re.match(r'\d+', parts[0]).group()
62-
SELF.pg_config_version = reduce(lambda v, x: v * 100 + int(x), parts, 0)
54+
self.pg_config_version = reduce(lambda v, x: v * 100 + int(x), parts, 0)
6355

6456
os.environ['LANGUAGE'] = 'en' # set default locale language to en. All messages will use this locale
6557
test_env = os.environ.copy()
@@ -83,31 +75,31 @@ def create() -> Init:
8375

8476
test_env['LC_MESSAGES'] = 'C'
8577
test_env['LC_TIME'] = 'C'
86-
SELF._test_env = test_env
78+
self._test_env = test_env
8779

8880
# Get the directory from which the script was executed
89-
SELF.source_path = os.getcwd()
81+
self.source_path = os.getcwd()
9082
tmp_path = test_env.get('PGPROBACKUP_TMP_DIR')
9183
if tmp_path and os.path.isabs(tmp_path):
92-
SELF.tmp_path = tmp_path
84+
self.tmp_path = tmp_path
9385
else:
94-
SELF.tmp_path = os.path.abspath(
95-
os.path.join(SELF.source_path, tmp_path or os.path.join('tests', 'tmp_dirs'))
86+
self.tmp_path = os.path.abspath(
87+
os.path.join(self.source_path, tmp_path or os.path.join('tests', 'tmp_dirs'))
9688
)
9789

98-
os.makedirs(SELF.tmp_path, exist_ok=True)
90+
os.makedirs(self.tmp_path, exist_ok=True)
9991

100-
SELF.username = getpass.getuser()
92+
self.username = getpass.getuser()
10193

102-
SELF.probackup_path = None
94+
self.probackup_path = None
10395
if 'PGPROBACKUPBIN' in test_env:
10496
if shutil.which(test_env["PGPROBACKUPBIN"]):
105-
SELF.probackup_path = test_env["PGPROBACKUPBIN"]
97+
self.probackup_path = test_env["PGPROBACKUPBIN"]
10698
else:
107-
if SELF.verbose:
99+
if self.verbose:
108100
print('PGPROBACKUPBIN is not an executable file')
109101

110-
if not SELF.probackup_path:
102+
if not self.probackup_path:
111103
probackup_path_tmp = os.path.join(
112104
testgres.get_pg_config()['BINDIR'], 'pg_probackup')
113105

@@ -116,118 +108,119 @@ def create() -> Init:
116108
logging.warning('{0} is not an executable file'.format(
117109
probackup_path_tmp))
118110
else:
119-
SELF.probackup_path = probackup_path_tmp
111+
self.probackup_path = probackup_path_tmp
120112

121-
if not SELF.probackup_path:
122-
probackup_path_tmp = SELF.source_path
113+
if not self.probackup_path:
114+
probackup_path_tmp = self.source_path
123115

124116
if os.path.isfile(probackup_path_tmp):
125117
if not os.access(probackup_path_tmp, os.X_OK):
126118
logging.warning('{0} is not an executable file'.format(
127119
probackup_path_tmp))
128120
else:
129-
SELF.probackup_path = probackup_path_tmp
121+
self.probackup_path = probackup_path_tmp
130122

131-
if not SELF.probackup_path:
132-
logging.error('pg_probackup binary is not found')
133-
return None
123+
if not self.probackup_path:
124+
raise Exception('pg_probackup binary is not found')
134125

135126
if os.name == 'posix':
136-
SELF.EXTERNAL_DIRECTORY_DELIMITER = ':'
127+
self.EXTERNAL_DIRECTORY_DELIMITER = ':'
137128
os.environ['PATH'] = os.path.dirname(
138-
SELF.probackup_path) + ':' + os.environ['PATH']
129+
self.probackup_path) + ':' + os.environ['PATH']
139130

140131
elif os.name == 'nt':
141-
SELF.EXTERNAL_DIRECTORY_DELIMITER = ';'
132+
self.EXTERNAL_DIRECTORY_DELIMITER = ';'
142133
os.environ['PATH'] = os.path.dirname(
143-
SELF.probackup_path) + ';' + os.environ['PATH']
134+
self.probackup_path) + ';' + os.environ['PATH']
144135

145-
SELF.probackup_old_path = None
136+
self.probackup_old_path = None
146137
if 'PGPROBACKUPBIN_OLD' in test_env:
147138
if (os.path.isfile(test_env['PGPROBACKUPBIN_OLD']) and os.access(test_env['PGPROBACKUPBIN_OLD'], os.X_OK)):
148-
SELF.probackup_old_path = test_env['PGPROBACKUPBIN_OLD']
139+
self.probackup_old_path = test_env['PGPROBACKUPBIN_OLD']
149140
else:
150-
if SELF.verbose:
141+
if self.verbose:
151142
print('PGPROBACKUPBIN_OLD is not an executable file')
152143

153-
SELF.probackup_version = None
154-
SELF.old_probackup_version = None
144+
self.probackup_version = None
145+
self.old_probackup_version = None
155146

156147
probackup_version_output = subprocess.check_output(
157-
[SELF.probackup_path, "--version"],
148+
[self.probackup_path, "--version"],
158149
stderr=subprocess.STDOUT,
159150
).decode('utf-8')
160151
match = re.search(r"\d+\.\d+\.\d+",
161152
probackup_version_output)
162-
SELF.probackup_version = match.group(0) if match else None
153+
self.probackup_version = match.group(0) if match else None
163154
match = re.search(r"\(compressions: ([^)]*)\)", probackup_version_output)
164155
compressions = match.group(1) if match else None
165156
if compressions:
166-
SELF.probackup_compressions = {s.strip() for s in compressions.split(',')}
157+
self.probackup_compressions = {s.strip() for s in compressions.split(',')}
167158
else:
168-
SELF.probackup_compressions = []
159+
self.probackup_compressions = []
169160

170-
if SELF.probackup_old_path:
161+
if self.probackup_old_path:
171162
old_probackup_version_output = subprocess.check_output(
172-
[SELF.probackup_old_path, "--version"],
163+
[self.probackup_old_path, "--version"],
173164
stderr=subprocess.STDOUT,
174165
).decode('utf-8')
175166
match = re.search(r"\d+\.\d+\.\d+",
176167
old_probackup_version_output)
177-
SELF.old_probackup_version = match.group(0) if match else None
168+
self.old_probackup_version = match.group(0) if match else None
178169

179-
SELF.remote = test_env.get('PGPROBACKUP_SSH_REMOTE', None) == 'ON'
180-
SELF.ptrack = test_env.get('PG_PROBACKUP_PTRACK', None) == 'ON' and SELF.pg_config_version >= 110000
181-
SELF.wal_tree_enabled = test_env.get('PG_PROBACKUP_WAL_TREE_ENABLED', None) == 'ON'
170+
self.remote = test_env.get('PGPROBACKUP_SSH_REMOTE', None) == 'ON'
171+
self.ptrack = test_env.get('PG_PROBACKUP_PTRACK', None) == 'ON' and self.pg_config_version >= 110000
172+
self.wal_tree_enabled = test_env.get('PG_PROBACKUP_WAL_TREE_ENABLED', None) == 'ON'
182173

183-
SELF.bckp_source = test_env.get('PG_PROBACKUP_SOURCE', 'pro').lower()
184-
if SELF.bckp_source not in ('base', 'direct', 'pro'):
174+
self.bckp_source = test_env.get('PG_PROBACKUP_SOURCE', 'pro').lower()
175+
if self.bckp_source not in ('base', 'direct', 'pro'):
185176
raise Exception("Wrong PG_PROBACKUP_SOURCE value. Available options: base|direct|pro")
186177

187-
SELF.paranoia = test_env.get('PG_PROBACKUP_PARANOIA', None) == 'ON'
178+
self.paranoia = test_env.get('PG_PROBACKUP_PARANOIA', None) == 'ON'
188179
env_compress = test_env.get('ARCHIVE_COMPRESSION', None)
189180
if env_compress:
190181
env_compress = env_compress.lower()
191182
if env_compress in ('on', 'zlib'):
192-
SELF.compress_suffix = '.gz'
193-
SELF.archive_compress = 'zlib'
183+
self.compress_suffix = '.gz'
184+
self.archive_compress = 'zlib'
194185
elif env_compress == 'lz4':
195186
if not HAVE_LZ4:
196187
raise LZ4_error
197-
if 'lz4' not in SELF.probackup_compressions:
188+
if 'lz4' not in self.probackup_compressions:
198189
raise Exception("pg_probackup is not compiled with lz4 support")
199-
SELF.compress_suffix = '.lz4'
200-
SELF.archive_compress = 'lz4'
190+
self.compress_suffix = '.lz4'
191+
self.archive_compress = 'lz4'
201192
elif env_compress == 'zstd':
202193
if not HAVE_ZSTD:
203194
raise ZSTD_error
204-
if 'zstd' not in SELF.probackup_compressions:
195+
if 'zstd' not in self.probackup_compressions:
205196
raise Exception("pg_probackup is not compiled with zstd support")
206-
SELF.compress_suffix = '.zst'
207-
SELF.archive_compress = 'zstd'
197+
self.compress_suffix = '.zst'
198+
self.archive_compress = 'zstd'
208199
else:
209-
SELF.compress_suffix = ''
210-
SELF.archive_compress = False
200+
self.compress_suffix = ''
201+
self.archive_compress = False
211202

212203
cfs_compress = test_env.get('PG_PROBACKUP_CFS_COMPRESS', None)
213204
if cfs_compress:
214-
SELF.cfs_compress = cfs_compress.lower()
205+
self.cfs_compress = cfs_compress.lower()
215206
else:
216-
SELF.cfs_compress = SELF.archive_compress
207+
self.cfs_compress = self.archive_compress
217208

218209
os.environ["PGAPPNAME"] = "pg_probackup"
219-
SELF.delete_logs = delete_logs
210+
self.delete_logs = delete_logs
220211

221-
if SELF.probackup_version.split('.')[0].isdigit():
222-
SELF.major_version = int(SELF.probackup_version.split('.')[0])
212+
if self.probackup_version.split('.')[0].isdigit():
213+
self.major_version = int(self.probackup_version.split('.')[0])
223214
else:
224-
logging.error('Can\'t process pg_probackup version \"{}\": the major version is expected to be a number'.format(SELF.probackup_version))
225-
return None
226-
227-
return SELF
215+
raise Exception('Can\'t process pg_probackup version \"{}\": the major version is expected to be a number'.format(self.probackup_version))
228216

229217
def test_env(self):
230218
return self._test_env.copy()
231219

232220

233-
init_params = Init.create()
221+
try:
222+
init_params = Init()
223+
except Exception as e:
224+
logging.error(str(e))
225+
logging.warning("testgres.plugins.probackup2.init_params is set to None.")
226+
init_params = None

0 commit comments

Comments
 (0)
0