diff --git a/python/google/registry/scripts/BUILD b/python/google/registry/scripts/BUILD new file mode 100644 index 000000000..a50937b34 --- /dev/null +++ b/python/google/registry/scripts/BUILD @@ -0,0 +1,20 @@ +package(default_visibility = ["//java/google/registry:registry_project"]) + +licenses(["notice"]) # Apache 2.0 + +py_binary( + name = "xml_to_index_yaml_translator", + srcs = ["xml_to_index_yaml_translator.py"], + deps = ["//python:python_directory_import"], +) + +py_test( + name = "xml_to_index_yaml_translator_test", + size = "small", + srcs = ["xml_to_index_yaml_translator_test.py"], + data = [ + "testdata/datastore-indexes.xml", + "testdata/index.yaml", + ], + deps = [":xml_to_index_yaml_translator"], +) diff --git a/python/google/registry/scripts/testdata/datastore-indexes.xml b/python/google/registry/scripts/testdata/datastore-indexes.xml new file mode 100644 index 000000000..fee7c0dfe --- /dev/null +++ b/python/google/registry/scripts/testdata/datastore-indexes.xml @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/python/google/registry/scripts/testdata/index.yaml b/python/google/registry/scripts/testdata/index.yaml new file mode 100644 index 000000000..e7e2ac7f5 --- /dev/null +++ b/python/google/registry/scripts/testdata/index.yaml @@ -0,0 +1,93 @@ +indexes: + +- kind: ContactResource + properties: + - name: currentSponsorClientId + - name: deletionTime + +- kind: DomainBase + properties: + - name: ^i + - name: currentSponsorClientId + - name: deletionTime + +- kind: DomainBase + properties: + - name: ^i + - name: tld + - name: deletionTime + +- kind: DomainBase + properties: + - name: currentSponsorClientId + - name: deletionTime + +- kind: HostResource + properties: + - name: currentSponsorClientId + - name: deletionTime + +- kind: RegistrarBillingEntry + ancestor: yes + properties: + - name: currency + - name: created + direction: desc + +- kind: DomainBase + properties: + - name: allContacts.contact + - name: deletionTime + +- kind: DomainBase + properties: + - name: nsHosts + - name: deletionTime + +- kind: DomainBase + properties: + - name: ^i + - name: nsHosts + - name: deletionTime + +- kind: HostResource + properties: + - name: inetAddresses + - name: deletionTime + +- kind: PollMessage + properties: + - name: clientId + - name: eventTime + +- kind: PollMessage + ancestor: yes + properties: + - name: clientId + - name: eventTime + +- kind: HistoryEntry + ancestor: yes + properties: + - name: modificationTime + +- kind: DomainBase + properties: + - name: ^i + - name: fullyQualifiedDomainName + +- kind: DomainBase + properties: + - name: ^i + - name: tld + - name: fullyQualifiedDomainName + +- kind: HostResource + properties: + - name: deletionTime + - name: fullyQualifiedHostName + +- kind: ContactResource + properties: + - name: deletionTime + - name: searchName diff --git a/python/google/registry/scripts/xml_to_index_yaml_translator.py b/python/google/registry/scripts/xml_to_index_yaml_translator.py new file mode 100644 index 000000000..39f959730 --- /dev/null +++ b/python/google/registry/scripts/xml_to_index_yaml_translator.py @@ -0,0 +1,51 @@ +# Copyright 2017 The Nomulus Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Convert App Engine Java datastore-indexes.xml file to index.yaml format. + +Pass the name of a datastore-indexes.xml file. The output will be dumped to +stdout. + +The resulting file can be used to call the gcloud cleanup-indexes command, +which interactively removes indexes which are defined on an App Engine +project but not present in the file. The syntax for that command is: + + gcloud datastore cleanup-indexes {index.yaml file} --project={project-id} +""" + +import sys +from xml.etree import ElementTree + + +def main(argv): + if len(argv) < 2: + print 'Usage: command {datastore-indexes.xml file}' + return 1 + + root = ElementTree.parse(argv[1]).getroot() + print 'indexes:' + for index in root: + print '' + print '- kind: %s' % index.attrib['kind'] + if index.attrib['ancestor'] != 'false': + print ' ancestor: %s' % ('yes' if (index.attrib['ancestor'] == 'true') + else 'no') + print ' properties:' + for index_property in index: + print ' - name: %s' % index_property.attrib['name'] + if index_property.attrib['direction'] != 'asc': + print ' direction: %s' % index_property.attrib['direction'] + +if __name__ == '__main__': + main(sys.argv) diff --git a/python/google/registry/scripts/xml_to_index_yaml_translator_test.py b/python/google/registry/scripts/xml_to_index_yaml_translator_test.py new file mode 100644 index 000000000..15c9a84f7 --- /dev/null +++ b/python/google/registry/scripts/xml_to_index_yaml_translator_test.py @@ -0,0 +1,51 @@ +# Copyright 2017 The Nomulus Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Tests for xml_to_index_yaml_translator.py.""" + +import contextlib +import StringIO +import sys +import unittest +from google.registry.scripts import xml_to_index_yaml_translator + + +@contextlib.contextmanager +def _RedirectStdout(): + orig_stdout = sys.stdout + sio = StringIO.StringIO() + sys.stdout = sio + try: + yield sio + finally: + sys.stdout = orig_stdout + + +class XmlToIndexYamlTranslatorTest(unittest.TestCase): + + def testSuccess(self): + with _RedirectStdout() as sio: + xml_to_index_yaml_translator.main(['test', + 'python/' + 'google/registry/scripts/testdata/' + 'datastore-indexes.xml']) + actual = sio.getvalue() + expectedfile = open('python/google/registry/' + 'scripts/testdata/index.yaml') + expected = expectedfile.read() + self.assertEqual(actual, expected) + + +if __name__ == '__main__': + unittest.main()