Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Merge branch '1.2.6' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Cameron Dawson committed Nov 27, 2012
2 parents 7d2469f + 54616f2 commit c3f098f
Show file tree
Hide file tree
Showing 5 changed files with 418 additions and 25 deletions.
70 changes: 56 additions & 14 deletions moztrap/model/core/management/commands/import.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@

from django.core.management.base import BaseCommand, CommandError

from optparse import make_option
import json
import os.path

from moztrap.model.core.models import Product, ProductVersion
from moztrap.model.library.importer import Importer
Expand All @@ -45,11 +47,24 @@ class Command(BaseCommand):
"Imports the cases from a JSON file into "
"the specified Product Version")

option_list = BaseCommand.option_list + (
make_option(
"-f",
"--force_dupes",
action='store_true',
dest="force_dupes",
default=False,
help="Force importing cases, even if the case name is a"
" duplicate"),

)

def handle(self, *args, **options):
if not len(args) == 3:
raise CommandError("Usage: {0}".format(self.args))

force_dupes = options.get("force_dupes")

try:
product = Product.objects.get(name=args[0])
except Product.DoesNotExist:
Expand All @@ -65,25 +80,52 @@ def handle(self, *args, **options):
)

try:
with open(args[2]) as fh:

# try to import this as JSON
try:
case_data = json.load(fh) # pragma: no branch
except ValueError as e:
raise CommandError(
"Could not parse JSON: {0}".format(str(e)))
files = []
# if this is a directory, import all files in it
if os.path.isdir(args[2]):
for file in os.listdir(args[2]):
if not file.startswith("."):
files.append("{0}/{1}".format(args[2], file))
else:
files.append(args[2])

results_for_files = None
for file in files:
with open(file) as fh:

# try to import this as JSON
try:
case_data = json.load(fh) # pragma: no branch
except ValueError as e:
raise CommandError(
"Could not parse JSON: {0}: {1}".format(
str(e),
fh,
))

# @@@: support importing as CSV. Rather than returning an
# error above, just try CSV import instead.

result = Importer().import_data(
product_version, case_data, force_dupes=force_dupes)

# append this result to those for any of the other files.
if not results_for_files:
results_for_files = result
else:
results_for_files.append(result) # pragma: no branch

if results_for_files:
result_list = results_for_files.get_as_list()
result_list.append("")
self.stdout.write("\n".join(result_list))
else:
self.stdout.write("No files found to import.\n")

# @@@: support importing as CSV. Rather than returning an
# error above, just try CSV import instead.

except IOError as (errno, strerror):
raise CommandError(
'Could not open "{0}", I/O error {1}: {2}'.format(
args[2], errno, strerror)
)

result_list = Importer().import_data(
product_version, case_data).get_as_list()
result_list.append("")
self.stdout.write("\n".join(result_list))
12 changes: 8 additions & 4 deletions moztrap/model/library/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Importer(object):
"""

@transaction.commit_on_success
def import_data(self, productversion, case_data):
def import_data(self, productversion, case_data, force_dupes=False):
"""
Import the top-level dictionary of cases and suites.
Expand All @@ -67,6 +67,8 @@ def import_data(self, productversion, case_data):
* productversion -- The ProductVersion model object for which case_data
will be imported
* case_data -- a dictionary of cases and/or suites to be imported
* force_dupes -- if True, will import cases with duplicate names. If
False, they will be skipped.
"""

Expand All @@ -84,7 +86,9 @@ def import_data(self, productversion, case_data):
# gracefully if no cases.
if "cases" in case_data:
case_importer = CaseImporter(productversion, suite_importer)
result.append(case_importer.import_cases(case_data["cases"]))
result.append(case_importer.import_cases(
case_data["cases"],
force_dupes=force_dupes))

# now create the suites and add cases to them
if suite_importer:
Expand Down Expand Up @@ -125,7 +129,7 @@ def __init__(self, productversion, suite_importer=None):
# cache of user emails
self.user_cache = UserCache()

def import_cases(self, case_dict_list):
def import_cases(self, case_dict_list, force_dupes=False):
"""
Import the test cases in the data.
Expand Down Expand Up @@ -167,7 +171,7 @@ def import_cases(self, case_dict_list):
continue

# Don't re-import if we have the same case name and Product Version
if CaseVersion.objects.filter(
if not force_dupes and CaseVersion.objects.filter(
name=new_case["name"],
productversion=self.productversion,
).exists():
Expand Down
Loading

0 comments on commit c3f098f

Please sign in to comment.