def _get_builds(request, domain_id=None): projects = Project.objects.all() if domain_id: # filter on domain, if it's set try: domain = Domain.objects.get(id=domain_id) except Domain.DoesNotExist: logging.error("Domain with id %s could not found." % domain_id) return HttpResponseBadRequest("Domain with id %s could not found." % domain_id) projects = projects.filter(domain=domain) if 'export_path' not in settings.RAPIDSMS_APPS['buildmanager']: logging.error("Please set 'export_path' in your hq buildmanager settings.") return HttpResponseBadRequest("Please set 'export_path' in your hq buildmanager settings.") export_dir = settings.RAPIDSMS_APPS['buildmanager']['export_path'] # For now this is RESTful, and de-facto returns all projects and builds. # At some point we may require this to take in a list of guids or # checksums much like the receiver does. if projects.count() == 0: logging.info("No projects could be found.") return HttpResponse("No projects could be found.") builds = ProjectBuild.objects.filter(project__in=projects) if builds.count() == 0: logging.info("No builds could be found.") return HttpResponse("No builds could be found.") compressor = TarCompressor() export_path = os.path.join( export_dir, "commcarehq-builds.tar") compressor.open(name=export_path) # add the root project summaries to the compressor _add_to_compressor(compressor, _get_project_summary(projects), "projects.json") tars = [] for build in builds: try: summary_tar = _get_build_summary(build) tars.append(summary_tar) compressor.add_file(summary_tar.name) except Exception, e: logging.error("Unable to export build: %s. Error is %s." % (build, e)) raise
# to match will still POST some junk character if len(request.raw_post_data) > 1: try: stack_received = util.bz2_to_list(request.raw_post_data) except Exception, e: logging.error("Poorly formatted MD5 file. Expecting a bz2-compressed file of md5 values.") return HttpResponseBadRequest("Poorly formatted MD5 file. Expecting a bz2-compressed file of md5 values.") stack_local = Submission.objects.all().distinct('checksum').order_by('checksum').values_list('checksum', flat=True) results = util.get_stack_diff(stack_received, stack_local) # this could get really large.... O_O submissions = Submission.objects.filter(checksum__in=results).distinct("checksum").order_by("checksum") if submissions.count() == 0: logging.info("No submissions could be found.") return HttpResponse("No submissions could be found.") compressor = TarCompressor() export_path = os.path.join( export_dir, "commcarehq-submissions.tar") compressor.open(name=export_path) file_added = False for submission in submissions: # use StringIO for now, since tarfile requires a stream object # Revisit if xforms get super long try: export = submission.export() # the following error types shouldn't halt submission export except Submission.DoesNotExist: logging.error("%s could not be found. Data export failed." \ % submission.pk) continue except TypeError, e: logging.error("Poorly formatted submission: %s. Data export failed." \