]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
check for old cassette version
authorKevin McCarthy <me@kevinmccarthy.org>
Sat, 10 May 2014 21:15:36 +0000 (11:15 -1000)
committerKevin McCarthy <me@kevinmccarthy.org>
Sat, 10 May 2014 21:52:36 +0000 (11:52 -1000)
vcr/migration.py

index 8dd4a51ae76b8f346fd08854eeaa7f84735f88f7..a269ad887fc6c1c23966e79566d8e3c447d078e2 100644 (file)
@@ -74,20 +74,33 @@ def _migrate(data):
 
 def migrate_json(in_fp, out_fp):
     data = json.load(in_fp)
+    if _already_migrated(data):
+        return False
     interactions = _migrate(data)
     out_fp.write(serialize(interactions, jsonserializer))
+    return True
 
 
 def _list_of_tuples_to_dict(fs):
     return dict((k, v) for k, v in fs[0])
 
+def _already_migrated(data):
+    try:
+        if data.get('version'):
+            return True
+    except AttributeError:
+        return False
+
 
 def migrate_yml(in_fp, out_fp):
     data = yaml.load(preprocess_yaml(in_fp.read()), Loader=Loader)
+    if _already_migrated(data):
+        return False
     for i in range(len(data)):
         data[i]['request']['headers'] = _list_of_tuples_to_dict(data[i]['request']['headers'])
     interactions = _migrate(data)
     out_fp.write(serialize(interactions, yamlserializer))
+    return True
 
 
 def migrate(file_path, migration_fn):
@@ -95,19 +108,19 @@ def migrate(file_path, migration_fn):
     # we will try to copy the content. (os.rename not needed)
     with tempfile.TemporaryFile(mode='w+') as out_fp:
         with open(file_path, 'r') as in_fp:
-            migration_fn(in_fp, out_fp)
+            if not migration_fn(in_fp, out_fp):
+                return False
         with open(file_path, 'w') as in_fp:
             out_fp.seek(0)
             shutil.copyfileobj(out_fp, in_fp)
+        return True
 
 
 def try_migrate(path):
     if path.endswith('.json'):
-        migrate(path, migrate_json)
-        return True
+        return migrate(path, migrate_json)
     elif path.endswith('.yaml') or path.endswith('.yml'):
-        migrate(path, migrate_yml)
-        return True
+        return migrate(path, migrate_yml)
     return False