1
- from __future__ import annotations
2
-
3
1
import logging
4
2
from functools import reduce
5
3
import getpass
39
37
40
38
class Init (object ):
41
39
def __init__ (self ):
42
- pass
43
-
44
- @staticmethod
45
- def create () -> Init :
46
- SELF = __class__ ()
47
-
48
40
if '-v' in sys .argv or '--verbose' in sys .argv :
49
- SELF .verbose = True
41
+ self .verbose = True
50
42
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' )
60
52
parts = [* version .split (' ' )[1 ].split ('.' ), '0' , '0' ][:3 ]
61
53
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 )
63
55
64
56
os .environ ['LANGUAGE' ] = 'en' # set default locale language to en. All messages will use this locale
65
57
test_env = os .environ .copy ()
@@ -83,31 +75,31 @@ def create() -> Init:
83
75
84
76
test_env ['LC_MESSAGES' ] = 'C'
85
77
test_env ['LC_TIME' ] = 'C'
86
- SELF ._test_env = test_env
78
+ self ._test_env = test_env
87
79
88
80
# Get the directory from which the script was executed
89
- SELF .source_path = os .getcwd ()
81
+ self .source_path = os .getcwd ()
90
82
tmp_path = test_env .get ('PGPROBACKUP_TMP_DIR' )
91
83
if tmp_path and os .path .isabs (tmp_path ):
92
- SELF .tmp_path = tmp_path
84
+ self .tmp_path = tmp_path
93
85
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' ))
96
88
)
97
89
98
- os .makedirs (SELF .tmp_path , exist_ok = True )
90
+ os .makedirs (self .tmp_path , exist_ok = True )
99
91
100
- SELF .username = getpass .getuser ()
92
+ self .username = getpass .getuser ()
101
93
102
- SELF .probackup_path = None
94
+ self .probackup_path = None
103
95
if 'PGPROBACKUPBIN' in test_env :
104
96
if shutil .which (test_env ["PGPROBACKUPBIN" ]):
105
- SELF .probackup_path = test_env ["PGPROBACKUPBIN" ]
97
+ self .probackup_path = test_env ["PGPROBACKUPBIN" ]
106
98
else :
107
- if SELF .verbose :
99
+ if self .verbose :
108
100
print ('PGPROBACKUPBIN is not an executable file' )
109
101
110
- if not SELF .probackup_path :
102
+ if not self .probackup_path :
111
103
probackup_path_tmp = os .path .join (
112
104
testgres .get_pg_config ()['BINDIR' ], 'pg_probackup' )
113
105
@@ -116,118 +108,119 @@ def create() -> Init:
116
108
logging .warning ('{0} is not an executable file' .format (
117
109
probackup_path_tmp ))
118
110
else :
119
- SELF .probackup_path = probackup_path_tmp
111
+ self .probackup_path = probackup_path_tmp
120
112
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
123
115
124
116
if os .path .isfile (probackup_path_tmp ):
125
117
if not os .access (probackup_path_tmp , os .X_OK ):
126
118
logging .warning ('{0} is not an executable file' .format (
127
119
probackup_path_tmp ))
128
120
else :
129
- SELF .probackup_path = probackup_path_tmp
121
+ self .probackup_path = probackup_path_tmp
130
122
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' )
134
125
135
126
if os .name == 'posix' :
136
- SELF .EXTERNAL_DIRECTORY_DELIMITER = ':'
127
+ self .EXTERNAL_DIRECTORY_DELIMITER = ':'
137
128
os .environ ['PATH' ] = os .path .dirname (
138
- SELF .probackup_path ) + ':' + os .environ ['PATH' ]
129
+ self .probackup_path ) + ':' + os .environ ['PATH' ]
139
130
140
131
elif os .name == 'nt' :
141
- SELF .EXTERNAL_DIRECTORY_DELIMITER = ';'
132
+ self .EXTERNAL_DIRECTORY_DELIMITER = ';'
142
133
os .environ ['PATH' ] = os .path .dirname (
143
- SELF .probackup_path ) + ';' + os .environ ['PATH' ]
134
+ self .probackup_path ) + ';' + os .environ ['PATH' ]
144
135
145
- SELF .probackup_old_path = None
136
+ self .probackup_old_path = None
146
137
if 'PGPROBACKUPBIN_OLD' in test_env :
147
138
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' ]
149
140
else :
150
- if SELF .verbose :
141
+ if self .verbose :
151
142
print ('PGPROBACKUPBIN_OLD is not an executable file' )
152
143
153
- SELF .probackup_version = None
154
- SELF .old_probackup_version = None
144
+ self .probackup_version = None
145
+ self .old_probackup_version = None
155
146
156
147
probackup_version_output = subprocess .check_output (
157
- [SELF .probackup_path , "--version" ],
148
+ [self .probackup_path , "--version" ],
158
149
stderr = subprocess .STDOUT ,
159
150
).decode ('utf-8' )
160
151
match = re .search (r"\d+\.\d+\.\d+" ,
161
152
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
163
154
match = re .search (r"\(compressions: ([^)]*)\)" , probackup_version_output )
164
155
compressions = match .group (1 ) if match else None
165
156
if compressions :
166
- SELF .probackup_compressions = {s .strip () for s in compressions .split (',' )}
157
+ self .probackup_compressions = {s .strip () for s in compressions .split (',' )}
167
158
else :
168
- SELF .probackup_compressions = []
159
+ self .probackup_compressions = []
169
160
170
- if SELF .probackup_old_path :
161
+ if self .probackup_old_path :
171
162
old_probackup_version_output = subprocess .check_output (
172
- [SELF .probackup_old_path , "--version" ],
163
+ [self .probackup_old_path , "--version" ],
173
164
stderr = subprocess .STDOUT ,
174
165
).decode ('utf-8' )
175
166
match = re .search (r"\d+\.\d+\.\d+" ,
176
167
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
178
169
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'
182
173
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' ):
185
176
raise Exception ("Wrong PG_PROBACKUP_SOURCE value. Available options: base|direct|pro" )
186
177
187
- SELF .paranoia = test_env .get ('PG_PROBACKUP_PARANOIA' , None ) == 'ON'
178
+ self .paranoia = test_env .get ('PG_PROBACKUP_PARANOIA' , None ) == 'ON'
188
179
env_compress = test_env .get ('ARCHIVE_COMPRESSION' , None )
189
180
if env_compress :
190
181
env_compress = env_compress .lower ()
191
182
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'
194
185
elif env_compress == 'lz4' :
195
186
if not HAVE_LZ4 :
196
187
raise LZ4_error
197
- if 'lz4' not in SELF .probackup_compressions :
188
+ if 'lz4' not in self .probackup_compressions :
198
189
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'
201
192
elif env_compress == 'zstd' :
202
193
if not HAVE_ZSTD :
203
194
raise ZSTD_error
204
- if 'zstd' not in SELF .probackup_compressions :
195
+ if 'zstd' not in self .probackup_compressions :
205
196
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'
208
199
else :
209
- SELF .compress_suffix = ''
210
- SELF .archive_compress = False
200
+ self .compress_suffix = ''
201
+ self .archive_compress = False
211
202
212
203
cfs_compress = test_env .get ('PG_PROBACKUP_CFS_COMPRESS' , None )
213
204
if cfs_compress :
214
- SELF .cfs_compress = cfs_compress .lower ()
205
+ self .cfs_compress = cfs_compress .lower ()
215
206
else :
216
- SELF .cfs_compress = SELF .archive_compress
207
+ self .cfs_compress = self .archive_compress
217
208
218
209
os .environ ["PGAPPNAME" ] = "pg_probackup"
219
- SELF .delete_logs = delete_logs
210
+ self .delete_logs = delete_logs
220
211
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 ])
223
214
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 ))
228
216
229
217
def test_env (self ):
230
218
return self ._test_env .copy ()
231
219
232
220
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