1 | """ |
---|
2 | Script to migrate repository from sqlalchemy <= 0.4.4 to the new |
---|
3 | repository schema. This shouldn't use any other migrate modules, so |
---|
4 | that it can work in any version. |
---|
5 | """ |
---|
6 | |
---|
7 | import os |
---|
8 | import sys |
---|
9 | |
---|
10 | |
---|
11 | def usage(): |
---|
12 | """Gives usage information.""" |
---|
13 | print """Usage: %(prog)s repository-to-migrate |
---|
14 | |
---|
15 | Upgrade your repository to the new flat format. |
---|
16 | |
---|
17 | NOTE: You should probably make a backup before running this. |
---|
18 | """ % {'prog': sys.argv[0]} |
---|
19 | |
---|
20 | sys.exit(1) |
---|
21 | |
---|
22 | |
---|
23 | def delete_file(filepath): |
---|
24 | """Deletes a file and prints a message.""" |
---|
25 | print ' Deleting file: %s' % filepath |
---|
26 | os.remove(filepath) |
---|
27 | |
---|
28 | |
---|
29 | def move_file(src, tgt): |
---|
30 | """Moves a file and prints a message.""" |
---|
31 | print ' Moving file %s to %s' % (src, tgt) |
---|
32 | if os.path.exists(tgt): |
---|
33 | raise Exception( |
---|
34 | 'Cannot move file %s because target %s already exists' % \ |
---|
35 | (src, tgt)) |
---|
36 | os.rename(src, tgt) |
---|
37 | |
---|
38 | |
---|
39 | def delete_directory(dirpath): |
---|
40 | """Delete a directory and print a message.""" |
---|
41 | print ' Deleting directory: %s' % dirpath |
---|
42 | os.rmdir(dirpath) |
---|
43 | |
---|
44 | |
---|
45 | def migrate_repository(repos): |
---|
46 | """Does the actual migration to the new repository format.""" |
---|
47 | print 'Migrating repository at: %s to new format' % repos |
---|
48 | versions = '%s/versions' % repos |
---|
49 | dirs = os.listdir(versions) |
---|
50 | # Only use int's in list. |
---|
51 | numdirs = [int(dirname) for dirname in dirs if dirname.isdigit()] |
---|
52 | numdirs.sort() # Sort list. |
---|
53 | for dirname in numdirs: |
---|
54 | origdir = '%s/%s' % (versions, dirname) |
---|
55 | print ' Working on directory: %s' % origdir |
---|
56 | files = os.listdir(origdir) |
---|
57 | files.sort() |
---|
58 | for filename in files: |
---|
59 | # Delete compiled Python files. |
---|
60 | if filename.endswith('.pyc') or filename.endswith('.pyo'): |
---|
61 | delete_file('%s/%s' % (origdir, filename)) |
---|
62 | |
---|
63 | # Delete empty __init__.py files. |
---|
64 | origfile = '%s/__init__.py' % origdir |
---|
65 | if os.path.exists(origfile) and len(open(origfile).read()) == 0: |
---|
66 | delete_file(origfile) |
---|
67 | |
---|
68 | # Move sql upgrade scripts. |
---|
69 | if filename.endswith('.sql'): |
---|
70 | version, dbms, operation = filename.split('.', 3)[0:3] |
---|
71 | origfile = '%s/%s' % (origdir, filename) |
---|
72 | # For instance: 2.postgres.upgrade.sql -> |
---|
73 | # 002_postgres_upgrade.sql |
---|
74 | tgtfile = '%s/%03d_%s_%s.sql' % ( |
---|
75 | versions, int(version), dbms, operation) |
---|
76 | move_file(origfile, tgtfile) |
---|
77 | |
---|
78 | # Move Python upgrade script. |
---|
79 | pyfile = '%s.py' % dirname |
---|
80 | pyfilepath = '%s/%s' % (origdir, pyfile) |
---|
81 | if os.path.exists(pyfilepath): |
---|
82 | tgtfile = '%s/%03d.py' % (versions, int(dirname)) |
---|
83 | move_file(pyfilepath, tgtfile) |
---|
84 | |
---|
85 | # Try to remove directory. Will fail if it's not empty. |
---|
86 | delete_directory(origdir) |
---|
87 | |
---|
88 | |
---|
89 | def main(): |
---|
90 | """Main function to be called when using this script.""" |
---|
91 | if len(sys.argv) != 2: |
---|
92 | usage() |
---|
93 | migrate_repository(sys.argv[1]) |
---|
94 | |
---|
95 | |
---|
96 | if __name__ == '__main__': |
---|
97 | main() |
---|