#! /usr/bin/env python # encoding: utf-8 # vim: se ts=2 sw=2 et ai: license=''' +-----------------------------------------------------------+ | | | Attention, ce logiciel est potentiellement dangereux ! | | Warting ! This software may be dangerous | | | | Veuillez prendre connaissance de la licence d'utilisation | | Please read the following license text | | | +-----------------------------------------------------------+ Software author : Christophe Martin, Schplurtz@laposte•net Copyright (C) Schplurtz le Déboulonné 2017 FRENCH | ENGLISH -------------------------------------+-------------------------------------- Ce logiciel est un programme | This software is a computer program informatique servant à mettre à | whose purpose is to update jour geonetwork. | geonetwork. | Ce logiciel est régi par la licence | This software is governed by the CeCILL soumise au droit français et | CeCILL license under French law and respectant les principes de | abiding by the rules of diffusion des logiciels libres. | distribution of free software. You Vous pouvez utiliser, modifier | can use, modify and/ or et/ou redistribuer ce programme | redistribute the software under the sous les conditions de la licence | terms of the CeCILL license as CeCILL telle que diffusée par le | circulated by CEA, CNRS and INRIA CEA, le CNRS et l'INRIA sur le site | at the following URL "http://www.cecill.info/". | "http://www.cecill.info/". | En contrepartie de l'accessibilité | As a counterpart to the access to au code source et des droits de | the source code and rights to copie, de modification et de | copy, modify and redistribute redistribution accordés par cette | granted by the license, users are licence, il n'est offert aux | provided only with a limited utilisateurs qu'une garantie | warranty and the software's limitée. Pour les mêmes raisons, | author, the holder of the economic seule une responsabilité restreinte | rights, and the successive pèse sur l'auteur du programme, le | licensors have only limited titulaire des droits patrimoniaux | liability. et les concédants successifs. | | A cet égard l'attention de | In this respect, the user's l'utilisateur est attirée sur les | attention is drawn to the risks risques associés au chargement, à | associated with loading, using, l'utilisation, à la modification | modifying and/or developing or et/ou au développement et à la | reproducing the software by the reproduction du logiciel par | user in light of its specific l'utilisateur étant donné sa | status of free software, that may spécificité de logiciel libre, qui | mean that it is complicated to peut le rendre complexe à manipuler | manipulate, and that also et qui le réserve donc à des | therefore means that it is développeurs et des professionnels | reserved for developers and avertis possédant des | experienced professionals having connaissances informatiques | in-depth computer knowledge. Users approfondies. Les utilisateurs | are therefore encouraged to load sont donc invités à charger et | and test the software's suitability tester l'adéquation du logiciel à | as regards their requirements in leurs besoins dans des conditions | conditions enabling the security of permettant d'assurer la sécurité de | their systems and/or data to be leurs systèmes et ou de leurs | ensured and, more generally, to données et, plus généralement, à | use and operate it in the same l'utiliser et l'exploiter dans les | conditions as regards security. mêmes conditions de sécurité. | | Le fait que vous puissiez accéder à | The fact that you are presently cet en-tête signifie que vous avez | reading this means that you have pris connaissance de la licence | had knowledge of the CeCILL license CeCILL, et que vous en avez accepté | and that you accept its terms. les termes. | ''' import glob import zipfile import filecmp import tempfile import atexit import os import sys import subprocess import shutil import re import gettext import base64 def error( *txt ): sys.stderr.write( "\n".join(txt) + "\n") # thanks to stackoverflow def which(program): import os def is_exe(fpath): return os.path.isfile(fpath) and os.access(fpath, os.X_OK) fpath, fname = os.path.split(program) if fpath: if is_exe(program): return program else: for path in os.environ['PATH'].split(os.pathsep): path = path.strip('"') exe_file = os.path.join(path, program) if is_exe(exe_file): return exe_file return None def cleanup(): os.chdir('/') # rmr(tmpdir) # Surtout pas ! Il peut y rester des fichiers importants rmr(os.path.join(tmpdir,'locale')) # C'est le moins qu'on puisse faire. def question( var, txt ): rep=raw_input( '{0} ({1}) '.format(txt, var) ).strip() if rep == 'exit': exit(0) if rep == '': return var return rep def yn(txt): while True: rep='' rep=question( rep, txt ).strip().lower() if re.match( '\A(o|oui|y|yes)\Z', rep ): return True if re.match( '\A(n|non|no)\Z', rep ): return False def iswar( fn ): ''' a good war file is an existing zip file that contains at least META-INF/MANIFEST.MF . ''' ret=True try: zip=zipfile.ZipFile( fn ) zip.getinfo('META-INF/MANIFEST.MF') except: ret=False return ret def rmr( *fname ): for filename in fname: if os.path.islink(filename) or os.path.isfile(filename): os.remove(filename) # print 'rm {0}'.format(filename) else: for root, dirs, files in os.walk(filename, False): for fn in files: os.remove(os.path.join(root,fn)) # print 'rm {0}'.format(os.path.join(root,fn)) for dn in dirs: os.rmdir(os.path.join(root,dn)) # print 'rmdir {0}'.format(os.path.join(root,dn)) os.rmdir(filename) # print 'rmdir {0}'.format(filename) def bindata(fn): lines='' with open(fn) as data: ok=False for line in data: if not ok and line == '# __DATA__\n': ok=True continue if ok: lines += line[1:] return base64.b64decode(lines) # # Main main Main # me=os.path.abspath(__file__.rstrip('co')) tmpdir=tempfile.mkdtemp() os.makedirs(os.path.join(tmpdir,'locale/fr_FR/LC_MESSAGES')) with open(os.path.join(tmpdir,'locale/fr_FR/LC_MESSAGES','cat.mo'),'w',0644) as cat: cat.write(bindata(me)) #gettext.install('enpython', os.path.join(os.path.dirname(__file__), 'locale')) gettext.install('cat', os.path.join(os.path.join(tmpdir,'locale'))) atexit.register( cleanup ) missing=False for cmd in ['less']: if not which( cmd ): error( _('missing ') + cmd ) missing=True if( missing ): error( '', _("There are missing commands"), _(" bye bye") ) exit( 1 ) # # Accept the license. Or not... # fnlic=os.path.join(tmpdir,'license') with open(fnlic,'w',0644) as f: f.write(license) subprocess.call(['less', '-E', '-X', '-K', _('-PSpace for next page'), fnlic ] ) os.remove(fnlic) if not yn( _("Do you accept the risks and the license ?") ): print _('Ok. exiting') exit(0) # # Now the real job # geonetdir='/var/lib/tomcat8/webapps/geonetwork' while True: geonetdir = question( geonetdir, _('Directory containing current geonetwork')) if os.path.isdir( geonetdir ): webinf=os.path.join( geonetdir, 'WEB-INF' ) if os.path.isdir( webinf ): break error( _('{0} not a dir or not a geonetwork dir...').format(geonetdir), _('try again.') ) oldwar=geonetdir + '.war' while True: oldwar = question( oldwar, _('current war file') ) if iswar(oldwar): break error( '{0} Not a valid/redeable war file.'.format(oldwar), 'try again.' ) newwar='geonetwork-3.2.1.war' while 1: newwar=question( newwar, _('New war file') ) if iswar(newwar): break error( '{0} Not a valid/redeable war file.'.format(newwar), 'try again.' ) geonetdir=os.path.abspath(geonetdir) oldwar=os.path.abspath(oldwar) newwar=os.path.abspath(newwar) print '' print _('geonetwork dir : '), geonetdir print _('current war file : '), oldwar print _('new war file : '), newwar oldcontent=os.path.join(tmpdir,'oldcontent') os.mkdir(oldcontent) os.chdir(oldcontent) print _('Extracting current war in temp dir') zipfile.ZipFile(oldwar, 'r').extractall() # Can't easily get what I want with filecmp.dircmp. It does not have # recursive capabilities. too bad. Let's do it the barbarian way : # get 2 full lists and compare. # diff_cur_old=filecmp.dircmp(geonetdir, oldcontent) # diff_cur_old.report_full_closure() #for file in diff_cur_old.diff_files: # print file #print '+++++++++++++++++++++++++++++++++++++++++++++' print _('Finding all files in current war') os.chdir(oldcontent) oldtree=set() for root, dirs, files in os.walk('.'): for name in files: oldtree.add(os.path.join(root,name)[2:]) #print '==============================================' #for x in oldtree: # print x print _('Finding all files in current installation') os.chdir(geonetdir) currenttree=set() for root, dirs, files in os.walk('.'): for name in files: currenttree.add(os.path.join(root,name)[2:]) #for name in dirs: # currenttree.add(os.path.join(root,name)) #for x in currenttree: # print x print _('Computing file diff between current war and current deployed app') currenttreeenplus=currenttree.copy() currenttreeenplus -= oldtree print _('{0} more files installed than files in current war').format(len(currenttreeenplus)) ''' for x in currenttreeenplus: print x ''' communstree=currenttree.copy() communstree -= currenttreeenplus print _('computed {0} files in common (should be {1})').format( len(communstree), len(oldtree)) print _('searching among {0} files, those that have been modified').format(len(communstree)) modifiedtree=set() for file in sorted(communstree): if not filecmp.cmp( os.path.join(geonetdir,file), os.path.join(oldcontent, file)): modifiedtree.add(file) print _('Removing temporary files') rmr(oldcontent) print _('saving {0} modified files for later use').format(len(modifiedtree)) modifiedfiles=os.path.join( tmpdir, 'modified' ) os.mkdir(modifiedfiles) for filename in modifiedtree: dir=os.path.join( modifiedfiles, os.path.dirname( filename )) if not os.path.exists( dir ): os.makedirs( dir ) shutil.copyfile(os.path.join(geonetdir, filename), os.path.join( modifiedfiles, filename )) print _('Analyzing new war') newzip=zipfile.ZipFile(newwar, 'r') newwartree=set() for filename in newzip.namelist(): newwartree.add(filename) print _('Finding obsolete files') obsoletetree=oldtree.copy() obsoletetree -= newwartree print _('Found {0} obsolete files').format(len(obsoletetree)) if yn( _('all checks are done. Start modifying ?') ): print _("Let's go!") else: print _('Bye bye.') exit(0) print _('extracting new war into {0}').format(geonetdir) os.chdir(geonetdir) newzip.extractall() print _('removing obsolete files') for filename in obsoletetree: os.remove(filename) print _('removing cache') rmr(*glob.glob('WEB-INF/data/data/resources/htmlcache/formatter-cache/public/*' )) rmr(*glob.glob('WEB-INF/data/data/resources/htmlcache/formatter-cache/private/*')) rmr(*glob.glob('WEB-INF/data/data/resources/htmlcache/formatter-cache/info-store.*.db')) rmr(*glob.glob('WEB-INF/data/wro4j-cache.*')) rep='all' while True: rep=question( rep, _(''' There are {0} modified files, what do you want to do ? 0 : restore all 1 : stop now and examine yourself the diffs 2 : ask for each file ''').format(len(modifiedtree)) ).strip().lower() if re.match( '\A[012]\Z', rep ): break error( _('did not understand, sorry...') ) os.chdir( modifiedfiles ) if '1' == rep: print _("OK. You have to examine the situation.\nmodified files are in {0}").format(modifiedfiles) elif '0' == rep: for filename in sorted(modifiedtree): print filename shutil.copyfile(filename, os.path.join(geonetdir,filename)) else: for filename in sorted(modifiedtree): if yn( _("reinstall modified this modified file to that location ?\n{0}\n{1}\n ?"). format(os.path.join(modifiedfiles,filename),os.path.join(geonetdir,filename))): shutil.copyfile(filename, os.path.join(geonetdir,filename)) os.remove(filename) for root, dirs, files in os.walk(modifiedfiles, False): for dn in dirs: try: os.rmdir(os.path.join(root,dn)) except: pass print _('you will find remaining files (if any) in {0}').format(modifiedfiles) print 'don des fichiers à tomcat8.' subprocess.call(['chown', '-hR', 'tomcat8:', geonetdir]) # __DATA__ #3hIElQAAAAAlAAAAHAAAAEQBAAA1AAAAbAIAAAAAAABAAwAAiwAAAEEDAAAKAAAAzQMAABUAAADY #AwAAEQAAAO4DAABAAAAAAAQAACcAAABBBAAAKQAAAGkEAAAiAAAAkwQAACkAAAC2BAAAIAAAAOAE #AAAWAAAAAQUAABgAAAAYBQAACQAAADEFAAAMAAAAOwUAAEAAAABIBQAACwAAAIkFAAAYAAAAlQUA #ABoAAACuBQAAJ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