2727import os
2828import sys
2929import tempfile
30+ from zipfile import ZipFile
3031from shutil import copyfile
3132
32- from .utils .command import Command
33- from .utils .utils import get_command
34-
3533"""
3634 deploy war file
3735
@@ -44,70 +42,18 @@ def repack_war(logger, sourceWar, targetWar, configFile, defaultConfigFile):
4442 in the process.
4543 """
4644
47- jar_cmd = get_command (logger , None , 'jar' )
48- if jar_cmd :
49- extract_cmd = [jar_cmd , '-xf' ]
50- compress_cmd = [jar_cmd , '-uf' ]
51- else :
52- zip_cmd = get_command (logger , None , 'zip' )
53- unzip_cmd = get_command (logger , None , 'unzip' )
54- if not zip_cmd :
55- raise Exception ("zip not found" )
56- if not unzip_cmd :
57- raise Exception ("unzip not found" )
58-
59- extract_cmd = [unzip_cmd ]
60- compress_cmd = [zip_cmd , '-rf' ]
61-
62- # Resolve the full path. This is needed if sourceWar is specified as
63- # relative path because the process will switch working directory below.
64- sourceWar = os .path .realpath (sourceWar )
65- logger .debug ('source war path = {}' .format (sourceWar ))
66-
67- # Create temporary directory and switch to it.
68- with tempfile .TemporaryDirectory (prefix = 'OpenGrokWarRepack' ) as tmpDirName :
69- logger .debug ('Changing working directory to {}' .format (tmpDirName ))
70- origWorkingDir = os .getcwd ()
71- os .chdir (tmpDirName )
72-
73- # Extract the web.xml file from the source archive.
74- WEB_INF = 'WEB-INF'
75- WEB_XML = os .path .join (WEB_INF , 'web.xml' )
76- logger .debug ('Decompressing {} from {} into {}' .
77- format (WEB_XML , sourceWar , tmpDirName ))
78- extract_cmd .append (sourceWar )
79- extract_cmd .append (WEB_XML )
80- cmd = Command (extract_cmd , logger = logger )
81- cmd .execute ()
82- ret = cmd .getretcode ()
83- if ret is None or ret != 0 :
84- raise Exception ("Cannot decompress war file {}, command '{}' "
85- "ended with {}" .format (sourceWar , cmd , ret ))
86-
87- # Substitute the configuration path in the web.xml file.
88- logger .debug ("Performing substitution of '{}' with '{}'" .
89- format (defaultConfigFile , configFile ))
90- with open (WEB_XML , 'r' ) as f :
91- data = f .read ().replace (defaultConfigFile , configFile )
92- with open (WEB_XML , 'w' ) as f :
93- f .write (data )
94-
95- # Refresh the target archive with the modified file.
96- logger .debug ('Copying {} to {}' .format (sourceWar , targetWar ))
97- copyfile (sourceWar , targetWar )
98- logger .debug ('Refreshing target archive {}' .format (targetWar ))
99- compress_cmd .append (targetWar )
100- compress_cmd .append (WEB_XML )
101- cmd = Command (compress_cmd , logger = logger )
102- cmd .execute ()
103- ret = cmd .getretcode ()
104- if ret is None or ret != 0 :
105- raise Exception ("Cannot re-compress war file {}, command '{}' "
106- "ended with {}" .format (targetWar , cmd , ret ))
107-
108- # Need to switch back to original working directory, so that the
109- # temporary directory can be deleted.
110- os .chdir (origWorkingDir )
45+ WEB_XML = 'WEB-INF/web.xml'
46+
47+ with ZipFile (sourceWar , 'r' ) as infile , ZipFile (targetWar , 'w' ) as outfile :
48+ for item in infile .infolist ():
49+ data = infile .read (item .filename )
50+ if item .filename == WEB_XML :
51+ logger .debug ("Performing substitution of '{}' with '{}'" .
52+ format (defaultConfigFile , configFile ))
53+ defaultConfigFile = defaultConfigFile .encode ()
54+ configFile = configFile .encode ()
55+ data = data .replace (defaultConfigFile , configFile )
56+ outfile .writestr (item , data )
11157
11258
11359def deploy_war (logger , sourceWar , targetWar , configFile = None ):
0 commit comments