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