Skip to content

Commit

Permalink
ID-2: add "restorefilenames" tool
Browse files Browse the repository at this point in the history
  • Loading branch information
mlookaxw committed Jan 6, 2021
1 parent 0414d5d commit f711c03
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/.vscode
5 changes: 4 additions & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
|https://github.com/Axway-API-Management-Plus/apigw-tools/issues/1[#1]
|Enhancement
|Add tool to export/import KPS tables.
|===

|https://github.com/Axway-API-Management-Plus/apigw-tools/issues/2[#2]
|Enhancement
|Add tool to restore project file names.
|===
4 changes: 4 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ This repository provides a collection of tiny tools to operate Axway API Gateway
|Name|Description
|kpsexport
|Add tool to export/import KPS tables in a convenient way.

|restorefilenames
|Restores the original names of entity store files after running `projupgrade` tool.
|===

== Tools

include::docs/_kpsexport.adoc[leveloffset=+2]
include::docs/_restorefilenames.adoc[leveloffset=+2]

== Contributing
Please read https://github.com/Axway-API-Management-Plus/Common/blob/master/Contributing.md[Contributing] for details on our code of conduct, and the process for submitting pull requests to us.
Expand Down
54 changes: 54 additions & 0 deletions docs/_restorefilenames.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
= Restore Project File Names
:source-highlighter: prettify
ifdef::env-github[]
:outfilesuffix: .adoc
:!toc-title:
:caution-caption: :fire:
:important-caption: :exclamation:
:note-caption: :paperclip:
:tip-caption: :bulb:
:warning-caption: :warning:
endif::[]

== Abstract

Restores the original names of entity store files after running `projupgrade` tool.

== Background

When a new release of an Axway API Gateway is used, all policy projects have to be upgraded to the new version.
Local projects can be upgraded with the `projupgrade` tool provided by the client development tools.

Unfortunately after using this tool all the file names of the XML entity store are changed.
This has impacts if the entity store is managed by an SCM (e.g. Git).
From the perspective of the SCM the files are deleted and new files are created.
Or the SCM is able to automatically detect the renaming.
In both cases it is confusing.

This tool restores the original file names for the XML entity store.
It only affect the names of the files, no content is changed.

== Usage

To restore the file names of a policy project in the folder `src/policies`, just invoke the tool with the parameter `--projdir` pointing to the project folder.

.Example: invoke with local Python interpreter
[source,shell]
----
$ python restorefilenames.py --projdir=src/policies
----


.Example: invoke with Axway Jython interpreter (Unix)
[source,shell]
----
$ restorefilenames.sh --projdir=src/policies
----

.Example: invoke with Axway Jython interpreter (Windows)
[source,cmd]
----
> restorefilenames.cmd --projdir=src/policies
----

IMPORTANT: For usage of the Jython interpreter the `AXWAY_HOME` environment variable has to be defined.
75 changes: 50 additions & 25 deletions src/main/assembly/assembly.xml
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
<id>dist</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<!-- KPS export/import tool -->
<fileSet>
<directory>${basedir}/src/main/resources/kpsexport</directory>
<outputDirectory>kpsexport</outputDirectory>
<excludes>
<exclude>_*</exclude>
</excludes>
<fileMode>0500</fileMode>
<lineEnding>unix</lineEnding>
</fileSet>
<fileSet>
<directory>${basedir}/src/main/resources/kpsexport</directory>
<outputDirectory>kpsexport</outputDirectory>
<includes>
<include>_*</include>
</includes>
<fileMode>0400</fileMode>
<lineEnding>unix</lineEnding>
</fileSet>
</fileSets>
<id>dist</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<!-- Unix shell scripts -->
<fileSet>
<directory>${basedir}/src/main/resources</directory>
<outputDirectory>.</outputDirectory>
<includes>
<include>**/*.sh</include>
</includes>
<excludes>
<exclude>**/_*.sh</exclude>
</excludes>
<fileMode>0500</fileMode>
<lineEnding>unix</lineEnding>
</fileSet>
<fileSet>
<directory>${basedir}/src/main/resources</directory>
<outputDirectory>.</outputDirectory>
<includes>
<include>**/_*.sh</include>
</includes>
<fileMode>0400</fileMode>
<lineEnding>unix</lineEnding>
</fileSet>

<!-- Windows command scripts -->
<fileSet>
<directory>${basedir}/src/main/resources</directory>
<outputDirectory>.</outputDirectory>
<includes>
<include>**/*.cmd</include>
</includes>
<fileMode>0400</fileMode>
<lineEnding>windows</lineEnding>
</fileSet>

<!-- All other files -->
<fileSet>
<directory>${basedir}/src/main/resources</directory>
<outputDirectory>.</outputDirectory>
<excludes>
<exclude>**/*.cmd</exclude>
<exclude>**/*.sh</exclude>
</excludes>
<fileMode>0400</fileMode>
</fileSet>
</fileSets>
</assembly>
21 changes: 21 additions & 0 deletions src/main/resources/restorefilenames/restorefilenames.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@ECHO off
SETLOCAL
IF "%AXWAY_HOME%" == "" GOTO no_axway_home

SET JYTHON=%AXWAY_HOME%\apigateway\Win32\bin\jython.bat
IF NOT EXIST %JYTHON% GOTO no_jython

SET CMD_HOME=%~dp0
CALL %JYTHON% "%CMD_HOME%/restorefilenames.py" %*

GOTO exit

:no_axway_home
ECHO ERROR: environment variable AXWAY_HOME not set
GOTO exit

:no_jython
ECHO ERROR: Jython interpreter not found: %JYTHON%

:exit
ENDLOCAL
97 changes: 97 additions & 0 deletions src/main/resources/restorefilenames/restorefilenames.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import sys
import os
import logging

from optparse import OptionParser
import xml.etree.ElementTree as ET

class ProjectFiles:
def __init__(self, projdir):
self.__projdir = projdir
self.__config_file = os.path.join(self.__projdir, "configs.xml")

# Check for valid project directory
logging.info("Restore file names for project '%s'" % (self.__projdir))
if not os.path.isfile(self.__config_file):
raise ValueError("Directory is not a valid project directory (missing 'configs.xml'): %s" % (self.__config_file))

# Read configuration
logging.info("Reading configuration file: %s" % (self.__config_file))
self.__config_tree = ET.parse(self.__config_file)

def __rename_file(self, current_name, new_name):
logging.info("Rename: %s --> %s" % (current_name, new_name))
current_path = os.path.join(self.__projdir, current_name)
new_path = os.path.join(self.__projdir, new_name)
os.rename(current_path, new_path)

def __rename_store_files(self, current_base_name, new_base_name):
current_xml = current_base_name + ".xml"
current_md5 = current_base_name + ".md5"
new_xml = new_base_name + ".xml"
new_md5 = new_base_name + ".md5"

if current_base_name != new_base_name:
self.__rename_file(current_xml, new_xml)
self.__rename_file(current_md5, new_md5)
else:
logging.info("%s already restored" % (new_base_name))
return new_xml

def __restore_store_file(self, id):
store = self.__config_tree.getroot().findall("./store[@id='%s']" % (id))[0]
store_type = store.get('type')
logging.debug("Restore file of store '%s' of type '%s'" % (id, store_type))

url = store.findall("./connectionCredentials/credential[@name='url']")[0]
url.text = self.__rename_store_files(url.text[:-len('.xml')], store_type)

def restore_names(self):
root = self.__config_tree.getroot()
assembly_name = root.get('activeAssembly')
logging.debug("Using assembly '%s'" % (assembly_name))
for component in root.findall("assembly[@id='%s']/component" % (assembly_name)):
self.__restore_store_file(component.get('id'))

self.__config_tree.write(self.__config_file)
logging.info("configs.xml updated")

def main():
prog = sys.argv[0]
version = "%prog 1.0.0"
usage = "%prog OPTIONS"
epilog = "Restores the original names of entity store files after running `projupgrade` tool."

parser = OptionParser(usage=usage, version=version, epilog=epilog)
parser.add_option("-v", "--verbose", dest="verbose", help="Enable verbose messages [optional]", action="store_true")
parser.add_option("--projdir", dest="projdir", help="Project directory (contains 'configs.xml' file)", metavar="PROJDIR")

(options, args) = parser.parse_args()

if not options.projdir:
parser.error("Project directory is missing!")

try:
# Configure logging
logging.basicConfig(format='%(levelname)s: %(message)s')
if options.verbose:
logging.getLogger().setLevel(logging.DEBUG)
else:
logging.getLogger().setLevel(logging.INFO)

# Restore file names
proj = ProjectFiles(options.projdir)
proj.restore_names()

except Exception as e:
if options.verbose:
logging.error("Error occurred, check details:", exc_info=True)
else:
logging.error("%r" % (e))
sys.exit(1)

sys.exit(0)


if __name__ == "__main__":
main()
19 changes: 19 additions & 0 deletions src/main/resources/restorefilenames/restorefilenames.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh
if [ "${AXWAY_HOME}" != "" ]
then
JYTHON=${AXWAY_HOME}/apigateway/posix/bin/jython
if [ $(uname -o) == "Msys" ]
then
JYTHON=${AXWAY_HOME}/apigateway/Win32/bin/jython.bat
fi

if [ -f "${JYTHON}" ]
then
CMD_HOME=`dirname $0`
"${JYTHON}" "${CMD_HOME}/restorefilenames.py" $*
else
echo "ERROR: Jython interpreter not found: ${JYTHON}"
fi
else
echo "ERROR: environment variable AXWAY_HOME not set"
fi

0 comments on commit f711c03

Please sign in to comment.