tools: fix for Django 1.8

Fix the transaction handling code to work with Django 1.8 in most of the
tools scripts. (Some of these are no longer used, but still serve as
examples of how to import data and update the database.)

Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
This commit is contained in:
Paul Eggleton 2017-01-16 11:35:57 +13:00
parent 072c7d6656
commit 45d307369f
4 changed files with 179 additions and 188 deletions

View File

@ -27,6 +27,10 @@ import recipeparse
logger = utils.logger_create('LayerIndexUpdate') logger = utils.logger_create('LayerIndexUpdate')
class DryRunRollbackException(Exception):
pass
def update_recipe_file(tinfoil, data, path, recipe, layerdir_start, repodir): def update_recipe_file(tinfoil, data, path, recipe, layerdir_start, repodir):
fn = str(os.path.join(path, recipe.filename)) fn = str(os.path.join(path, recipe.filename))
try: try:
@ -154,9 +158,8 @@ def main():
# Clear the default value of HOMEPAGE ('unknown') # Clear the default value of HOMEPAGE ('unknown')
tinfoil.config_data.setVar('HOMEPAGE', '') tinfoil.config_data.setVar('HOMEPAGE', '')
transaction.enter_transaction_management()
transaction.managed(True)
try: try:
with transaction.atomic():
layerdir_start = os.path.normpath(oeclassicpath) + os.sep layerdir_start = os.path.normpath(oeclassicpath) + os.sep
layerrecipes = Recipe.objects.filter(layerbranch=layerbranch) layerrecipes = Recipe.objects.filter(layerbranch=layerbranch)
layermachines = Machine.objects.filter(layerbranch=layerbranch) layermachines = Machine.objects.filter(layerbranch=layerbranch)
@ -168,7 +171,6 @@ def main():
config_data_copy = recipeparse.setup_layer(tinfoil.config_data, fetchdir, oeclassicpath, layer, layerbranch) config_data_copy = recipeparse.setup_layer(tinfoil.config_data, fetchdir, oeclassicpath, layer, layerbranch)
except recipeparse.RecipeParseError as e: except recipeparse.RecipeParseError as e:
logger.error(str(e)) logger.error(str(e))
transaction.rollback()
sys.exit(1) sys.exit(1)
layerrecipes.delete() layerrecipes.delete()
@ -194,15 +196,13 @@ def main():
layerbranch.save() layerbranch.save()
if options.dryrun: if options.dryrun:
transaction.rollback() raise DryRunRollbackException()
else: except DryRunRollbackException:
transaction.commit() pass
except: except:
import traceback import traceback
traceback.print_exc() traceback.print_exc()
transaction.rollback()
finally: finally:
transaction.leave_transaction_management()
tinfoil.shutdown() tinfoil.shutdown()
shutil.rmtree(tempdir) shutil.rmtree(tempdir)

View File

@ -21,6 +21,10 @@ import logging
logger = utils.logger_create('LayerIndexImport') logger = utils.logger_create('LayerIndexImport')
class DryRunRollbackException(Exception):
pass
def read_page(site, path): def read_page(site, path):
ret = {} ret = {}
import httplib import httplib
@ -98,9 +102,8 @@ def main():
recipes_ai = read_page("www.openembedded.org", "/wiki/OE-Classic_Recipes_A-I?action=raw") recipes_ai = read_page("www.openembedded.org", "/wiki/OE-Classic_Recipes_A-I?action=raw")
recipes_jz = read_page("www.openembedded.org", "/wiki/OE-Classic_Recipes_J-Z?action=raw") recipes_jz = read_page("www.openembedded.org", "/wiki/OE-Classic_Recipes_J-Z?action=raw")
transaction.enter_transaction_management()
transaction.managed(True)
try: try:
with transaction.atomic():
recipes = dict(list(recipes_ai.items()) + list(recipes_jz.items())) recipes = dict(list(recipes_ai.items()) + list(recipes_jz.items()))
for pn, comment in recipes.items(): for pn, comment in recipes.items():
newpn = '' newpn = ''
@ -187,14 +190,9 @@ def main():
sys.exit(1) sys.exit(1)
if options.dryrun: if options.dryrun:
transaction.rollback() raise DryRunRollbackException()
else: except DryRunRollbackException:
transaction.commit() pass
except:
transaction.rollback()
raise
finally:
transaction.leave_transaction_management()
sys.exit(0) sys.exit(0)

View File

@ -20,6 +20,10 @@ import utils
logger = utils.logger_create('LayerIndexImport') logger = utils.logger_create('LayerIndexImport')
class DryRunRollbackException(Exception):
pass
def main(): def main():
parser = optparse.OptionParser( parser = optparse.OptionParser(
@ -45,9 +49,7 @@ def main():
readme_re = re.compile(r';f=[a-zA-Z0-9/-]*README;') readme_re = re.compile(r';f=[a-zA-Z0-9/-]*README;')
master_branch = utils.get_branch('master') master_branch = utils.get_branch('master')
core_layer = None core_layer = None
transaction.enter_transaction_management() with transaction.atomic():
transaction.managed(True)
try:
for line in data.splitlines(): for line in data.splitlines():
if line.startswith('{|'): if line.startswith('{|'):
in_table = True in_table = True
@ -140,12 +142,6 @@ def main():
layerdep.layerbranch = layerbranch layerdep.layerbranch = layerbranch
layerdep.dependency = core_layer layerdep.dependency = core_layer
layerdep.save() layerdep.save()
transaction.commit()
except:
transaction.rollback()
raise
finally:
transaction.leave_transaction_management()
else: else:
logger.error('Fetch failed: %d: %s' % (resp.status, resp.reason)) logger.error('Fetch failed: %d: %s' % (resp.status, resp.reason))

View File

@ -19,6 +19,9 @@ import logging
logger = utils.logger_create('LayerIndexClassicUpdate') logger = utils.logger_create('LayerIndexClassicUpdate')
class DryRunRollbackException(Exception):
pass
def main(): def main():
@ -62,9 +65,8 @@ def main():
logger.error("Specified branch %s does not exist in database" % options.branch) logger.error("Specified branch %s does not exist in database" % options.branch)
sys.exit(1) sys.exit(1)
transaction.enter_transaction_management()
transaction.managed(True)
try: try:
with transaction.atomic():
def recipe_pn_query(pn): def recipe_pn_query(pn):
return Recipe.objects.filter(layerbranch__branch__name='master').filter(pn=pn).order_by('layerbranch__layer__index_preference') return Recipe.objects.filter(layerbranch__branch__name='master').filter(pn=pn).order_by('layerbranch__layer__index_preference')
@ -109,14 +111,9 @@ def main():
if options.dryrun: if options.dryrun:
transaction.rollback() raise DryRunRollbackException()
else: except DryRunRollbackException:
transaction.commit() pass
except:
transaction.rollback()
raise
finally:
transaction.leave_transaction_management()
sys.exit(0) sys.exit(0)