summaryrefslogtreecommitdiff
path: root/migrations/001-empty-build-depends.py
diff options
context:
space:
mode:
authorSam Thursfield <sam.thursfield@codethink.co.uk>2015-07-14 13:27:10 +0100
committerBaserock Gerrit <gerrit@baserock.org>2015-07-28 09:46:13 +0000
commitfabcc63e5dd0ad098dab3508f8c6cc78123517d0 (patch)
tree9b7f452481799a9d2cbab74638e9b32493d87598 /migrations/001-empty-build-depends.py
parent8e4a341db6226deff45003f55afb57aa53c8fada (diff)
downloaddefinitions-fabcc63e5dd0ad098dab3508f8c6cc78123517d0.tar.gz
Add migration scripts for historical versions of the definitions format
See README for more information on how the migrations are intended work. These migrations are probably not widely useful, as our definitions have already been migrated manually. However, I want to come up with a good pattern for writing migration scripts, and actually doing it seems like the best way. There is a 'migrations/indent' tool, which reformats a set of definitions according to how the ruamel.yaml program writes them out. This tool is nice if you like everything to have consistent indent and line wrapping, and you can run it before running the migrations to ensure that the migrations don't do any reformatting when writing the .moprh files back to disk. The migration scripts require the ruamel.yaml Python library. I have sent a separate change to add this to the 'build' and 'devel' reference systems. Change-Id: Ibd62ba140d3f7e8e638beed6d714f671405bdc79
Diffstat (limited to 'migrations/001-empty-build-depends.py')
-rwxr-xr-xmigrations/001-empty-build-depends.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/migrations/001-empty-build-depends.py b/migrations/001-empty-build-depends.py
new file mode 100755
index 00000000..5d4296d6
--- /dev/null
+++ b/migrations/001-empty-build-depends.py
@@ -0,0 +1,82 @@
+#!/usr/bin/env python
+# Copyright (C) 2015 Codethink Limited
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+'''Migration to Baserock Definitions format version 1.
+
+In version 1, the 'build-depends' parameter was made optional. It was
+previously mandatory to specify 'build-depends' for a chunk, even if it was an
+empty list.
+
+'''
+
+
+import sys
+import warnings
+
+import migrations
+
+
+TO_VERSION = 1
+
+
+def check_empty_build_depends(contents, filename):
+ assert contents['kind'] == 'stratum'
+
+ valid = True
+ for chunk_ref in contents.get('chunks', []):
+ if 'build-depends' not in chunk_ref:
+ chunk_ref_name = chunk_ref.get('name', chunk_ref.get('morph'))
+ warnings.warn(
+ "%s:%s has no build-depends field, which "
+ "is invalid in definitions version 0." %
+ (contents['name'], chunk_ref_name))
+ valid = False
+
+ return valid
+
+
+def remove_empty_build_depends(contents, filename):
+ assert contents['kind'] == 'stratum'
+
+ changed = False
+ for chunk_ref in contents.get('chunks', []):
+ if 'build-depends' in chunk_ref:
+ if len(chunk_ref['build-depends']) == 0:
+ del chunk_ref['build-depends']
+ changed = True
+
+ return changed
+
+
+try:
+ if migrations.check_definitions_version(TO_VERSION - 1):
+ success = migrations.process_definitions(
+ path='.', kinds=['stratum'],
+ validate_cb=check_empty_build_depends,
+ modify_cb=remove_empty_build_depends)
+ if success:
+ migrations.set_definitions_version(TO_VERSION)
+ sys.stdout.write("Migration completed successfully.\n")
+ sys.exit(0)
+ else:
+ sys.stderr.write("Migration failed due to warnings.\n")
+ sys.exit(1)
+ else:
+ sys.stdout.write("Nothing to do.\n")
+ sys.exit(0)
+except RuntimeError as e:
+ sys.stderr.write("Error: %s\n" % e.message)
+ sys.exit(1)