1+ from __future__import annotations
2+
13import logging
24from functools import reduce
35import getpass
3739
3840class Init (object ):
3941def __init__ (self ):
42+ pass
43+
44+ @staticmethod
45+ def create ()-> Init :
46+ SELF = __class__ ()
47+
4048if '-v' in sys .argv or '--verbose' in sys .argv :
41- self .verbose = True
49+ SELF .verbose = True
4250else :
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' )
5260parts = [* version .split (' ' )[1 ].split ('.' ),'0' ,'0' ][:3 ]
5361parts [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 )
5563
5664os .environ ['LANGUAGE' ]= 'en' # set default locale language to en. All messages will use this locale
5765test_env = os .environ .copy ()
@@ -75,31 +83,31 @@ def __init__(self):
7583
7684test_env ['LC_MESSAGES' ]= 'C'
7785test_env ['LC_TIME' ]= 'C'
78- self ._test_env = test_env
86+ SELF ._test_env = test_env
7987
8088# Get the directory from which the script was executed
81- self .source_path = os .getcwd ()
89+ SELF .source_path = os .getcwd ()
8290tmp_path = test_env .get ('PGPROBACKUP_TMP_DIR' )
8391if tmp_path and os .path .isabs (tmp_path ):
84- self .tmp_path = tmp_path
92+ SELF .tmp_path = tmp_path
8593else :
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' ))
8896 )
8997
90- os .makedirs (self .tmp_path ,exist_ok = True )
98+ os .makedirs (SELF .tmp_path ,exist_ok = True )
9199
92- self .username = getpass .getuser ()
100+ SELF .username = getpass .getuser ()
93101
94- self .probackup_path = None
102+ SELF .probackup_path = None
95103if 'PGPROBACKUPBIN' in test_env :
96104if shutil .which (test_env ["PGPROBACKUPBIN" ]):
97- self .probackup_path = test_env ["PGPROBACKUPBIN" ]
105+ SELF .probackup_path = test_env ["PGPROBACKUPBIN" ]
98106else :
99- if self .verbose :
107+ if SELF .verbose :
100108print ('PGPROBACKUPBIN is not an executable file' )
101109
102- if not self .probackup_path :
110+ if not SELF .probackup_path :
103111probackup_path_tmp = os .path .join (
104112testgres .get_pg_config ()['BINDIR' ],'pg_probackup' )
105113
@@ -108,116 +116,118 @@ def __init__(self):
108116logging .warning ('{0} is not an executable file' .format (
109117probackup_path_tmp ))
110118else :
111- self .probackup_path = probackup_path_tmp
119+ SELF .probackup_path = probackup_path_tmp
112120
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
115123
116124if os .path .isfile (probackup_path_tmp ):
117125if not os .access (probackup_path_tmp ,os .X_OK ):
118126logging .warning ('{0} is not an executable file' .format (
119127probackup_path_tmp ))
120128else :
121- self .probackup_path = probackup_path_tmp
129+ SELF .probackup_path = probackup_path_tmp
122130
123- if not self .probackup_path :
131+ if not SELF .probackup_path :
124132logging .error ('pg_probackup binary is not found' )
125- exit ( 1 )
133+ return None
126134
127135if os .name == 'posix' :
128- self .EXTERNAL_DIRECTORY_DELIMITER = ':'
136+ SELF .EXTERNAL_DIRECTORY_DELIMITER = ':'
129137os .environ ['PATH' ]= os .path .dirname (
130- self .probackup_path )+ ':' + os .environ ['PATH' ]
138+ SELF .probackup_path )+ ':' + os .environ ['PATH' ]
131139
132140elif os .name == 'nt' :
133- self .EXTERNAL_DIRECTORY_DELIMITER = ';'
141+ SELF .EXTERNAL_DIRECTORY_DELIMITER = ';'
134142os .environ ['PATH' ]= os .path .dirname (
135- self .probackup_path )+ ';' + os .environ ['PATH' ]
143+ SELF .probackup_path )+ ';' + os .environ ['PATH' ]
136144
137- self .probackup_old_path = None
145+ SELF .probackup_old_path = None
138146if 'PGPROBACKUPBIN_OLD' in test_env :
139147if (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' ]
141149else :
142- if self .verbose :
150+ if SELF .verbose :
143151print ('PGPROBACKUPBIN_OLD is not an executable file' )
144152
145- self .probackup_version = None
146- self .old_probackup_version = None
153+ SELF .probackup_version = None
154+ SELF .old_probackup_version = None
147155
148156probackup_version_output = subprocess .check_output (
149- [self .probackup_path ,"--version" ],
157+ [SELF .probackup_path ,"--version" ],
150158stderr = subprocess .STDOUT ,
151159 ).decode ('utf-8' )
152160match = re .search (r"\d+\.\d+\.\d+" ,
153161probackup_version_output )
154- self .probackup_version = match .group (0 )if match else None
162+ SELF .probackup_version = match .group (0 )if match else None
155163match = re .search (r"\(compressions: ([^)]*)\)" ,probackup_version_output )
156164compressions = match .group (1 )if match else None
157165if compressions :
158- self .probackup_compressions = {s .strip ()for s in compressions .split (',' )}
166+ SELF .probackup_compressions = {s .strip ()for s in compressions .split (',' )}
159167else :
160- self .probackup_compressions = []
168+ SELF .probackup_compressions = []
161169
162- if self .probackup_old_path :
170+ if SELF .probackup_old_path :
163171old_probackup_version_output = subprocess .check_output (
164- [self .probackup_old_path ,"--version" ],
172+ [SELF .probackup_old_path ,"--version" ],
165173stderr = subprocess .STDOUT ,
166174 ).decode ('utf-8' )
167175match = re .search (r"\d+\.\d+\.\d+" ,
168176old_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
170178
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'
174182
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' ):
177185raise Exception ("Wrong PG_PROBACKUP_SOURCE value. Available options: base|direct|pro" )
178186
179- self .paranoia = test_env .get ('PG_PROBACKUP_PARANOIA' ,None )== 'ON'
187+ SELF .paranoia = test_env .get ('PG_PROBACKUP_PARANOIA' ,None )== 'ON'
180188env_compress = test_env .get ('ARCHIVE_COMPRESSION' ,None )
181189if env_compress :
182190env_compress = env_compress .lower ()
183191if 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'
186194elif env_compress == 'lz4' :
187195if not HAVE_LZ4 :
188196raise LZ4_error
189- if 'lz4' not in self .probackup_compressions :
197+ if 'lz4' not in SELF .probackup_compressions :
190198raise 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'
193201elif env_compress == 'zstd' :
194202if not HAVE_ZSTD :
195203raise ZSTD_error
196- if 'zstd' not in self .probackup_compressions :
204+ if 'zstd' not in SELF .probackup_compressions :
197205raise 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'
200208else :
201- self .compress_suffix = ''
202- self .archive_compress = False
209+ SELF .compress_suffix = ''
210+ SELF .archive_compress = False
203211
204212cfs_compress = test_env .get ('PG_PROBACKUP_CFS_COMPRESS' ,None )
205213if cfs_compress :
206- self .cfs_compress = cfs_compress .lower ()
214+ SELF .cfs_compress = cfs_compress .lower ()
207215else :
208- self .cfs_compress = self .archive_compress
216+ SELF .cfs_compress = SELF .archive_compress
209217
210218os .environ ["PGAPPNAME" ]= "pg_probackup"
211- self .delete_logs = delete_logs
219+ SELF .delete_logs = delete_logs
212220
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 ])
215223else :
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
218228
219229def test_env (self ):
220230return self ._test_env .copy ()
221231
222232
223- init_params = Init ()
233+ init_params = Init . create ()