def getElement(request, element, team=None, release=None, fetch=None, linkTarget=None): """ Grab an element out of AccuRev by fileName. Convenience function for importProtocol, refreshProtocol """ if team == None or release == None: team, release = getTeamAndReleaseIDFromSession(request.session) # Different settings based on deployment if settings.LOCAL_SERVER: deploy = Repository.DEPLOY_LOCAL_WINDOWS elif settings.DEVELOPMENT_SERVER: deploy = Repository.DEPLOY_DEVELOPMENT elif settings.PRODUCTION_SERVER: deploy = Repository.DEPLOY_PRODUCTION if not fetch: try: fetch = AccuRevFetcher() except Exception as e: msg = "An unknown error occurred while fetching the protocol from the VCS. Error: %s" % e.message objContext = RequestContext(request, {'msg': msg, 'admin':settings.ADMINS}) return render_to_response('protocol_import_list_error.html', objContext) # Get the repository object for this team/release protocols in accurev try: repository = Repository.objects.get(team=team, release=release, repo_type='P', path_type='A') localrepo = Repository.objects.get(team=team, release=release, repo_type='P', path_type='L', deployment=deploy) except Exception as e: if type(e) == Repository.DoesNotExist: msg = "An AccuRev Repository for the given scope (%s/%s) does not exist. Please contact a site administrator." %(release.get_release_name_display(), team.get_team_name_display()) else: msg = "An unexpected error occurred while fetching the protocol from the VCS. Error: %s" % e.message objContext = RequestContext(request, {'file': element, 'msg': [msg, ], 'admin':settings.ADMINS}) raise GetElementError(render_to_response('protocol_import_error.html', objContext)) # Copy to the local repository # Use a different mechanism if it is an elink, should cat instead of pop try: if linkTarget: fetched_protocol = fetch.cat(repository.accurev_stream, str(os.path.join(settings.PROTOCOL_PARSE_BASE_PATH,localrepo.path).replace('\\',os.sep)), linkTarget.replace(r'/',os.sep)) else: fetched_protocol = fetch.pop(repository.accurev_stream, str(os.path.join(settings.PROTOCOL_PARSE_BASE_PATH,localrepo.path).replace('\\',os.sep)), [str(repository.path.replace('\\',os.sep)+os.sep+element), ], override=True) return (fetched_protocol, fetch, repository) except Exception as e: msg = "An unexpected error occurred while fetching the protocol from the VCS. Error: %s" % e.message objContext = RequestContext(request, {'file': element, 'msg': [msg, ], 'admin':settings.ADMINS}) raise GetElementError(render_to_response('protocol_import_error.html', objContext))
def importProtocol(request): """ Imports a protocol from the VCS and inserts it into the database. Returns the protocol detail page if successful. """ msg = [] team, release = getTeamAndReleaseIDFromSession(request.session) element = request.GET.get('file', None) linkTarget = request.GET.get('linkTargetLocation', None) # for accurev elinks if element is None: msg.append("Error in capturing the file name to parse and load into fortyTwo") objContext = RequestContext(request, {'file': 'Unknown', 'msg': msg, 'admin':settings.ADMINS}) return render_to_response('protocol_import_error.html', objContext) try: (fetched_protocol, fetch, repository) = getElement(request, element, team, release, linkTarget=linkTarget) except GetElementError as e: return e.httpResponse protocol_str = open(fetched_protocol[0]['local_path'], 'r').read() try: theParser = ParserInfo.objects.get(team=team, release=release, parser_type=ParserInfo.PARSER_TYPE_TESTPROTOCOL) protocolParseMod = theParser.getModule() Parser = protocolParseMod.__dict__[theParser.name] parser = Parser() except: msg.append("A suitable protocol parser was not found for this team and release.") logger.error(msg) objContext = RequestContext(request, {'file': element, 'msg': msg, 'admin':settings.ADMINS}) return render_to_response('protocol_import_error.html', objContext) # Parse the protocol try: pdict = parser.parse(protocol_str) logging.debug(pprint.pformat(pdict)) msg.append("%s has been successfully retrieved from the repository." % pdict['name']) logging.info(msg) except protocolParseMod.ProtocolParseError as parseError: msg.append("%s could not be successfully parsed after retrieval from the repository. " % element) msg.append("Message from Parser: %s" % parseError.msg) logging.error(msg) objContext = RequestContext(request, {'file': element, 'msg': msg, 'admin':settings.ADMINS}) return render_to_response('protocol_import_error.html', objContext) # Generic Exception except Exception, e: msg.append("%s could not be successfully parsed after retrieval from the repository. " % element) msg.append("Message from Parser: %s" % e.message) objContext = RequestContext(request, {'file': element, 'msg': msg, 'admin':settings.ADMINS}) return render_to_response('protocol_import_error.html', objContext)
def listAllProtocols(request): ''' Displays all protocols that are currently tracked by 42 for a given team/release ''' team, release = getTeamAndReleaseIDFromSession(request.session) protocols = Protocol.active.filter(team=team, release=release) return render_to_response('protocol_list.html',{'protocols': protocols.order_by('name'), 'JIRA_URL_PREFIX': settings.JIRA_URL_PREFIX}, context_instance=RequestContext(request))
def refreshProtocol(request, pid, linkTarget=None): """ Refresh the selected protocol, so that fortyTwo's DB matches the version in VC """ team, release = getTeamAndReleaseIDFromSession(request.session) p = Protocol.objects.get(id=pid) assert team == p.team_id and release == p.release_id, "Attempting to refresh a protocol for another team/release" linkTarget = request.POST.get('linktarget', None) if linkTarget == "None": linkTarget = None logger.info('Protocol Refresh initiated: %s' % p.name) msg = ['Protocol Refresh for %s' % p.name] try: (protocolName, fetch) = getElementName(request, os.path.basename(p.file_name), p.eid, team, release) (fetched_protocol, fetch, repository) = getElement(request, os.path.basename(protocolName), team, release, fetch, linkTarget=linkTarget) except GetElementError as e: return e.httpResponse protocol_str = open(fetched_protocol[0]['local_path'], 'r').read() try: theParser = ParserInfo.objects.get(team_id=team,release_id=release,parser_type=ParserInfo.PARSER_TYPE_TESTPROTOCOL) protocolParseMod = theParser.getModule() Parser = protocolParseMod.__dict__[theParser.name] parser = Parser() except: msg = "A suitable protocol parser was not found for this team and release." logger.error(msg) objContext = RequestContext(request, {'file': element, 'msg': msg, 'admin':settings.ADMINS}) return render_to_response('protocol_import_error.html', objContext) # Parse the protocol try: pdict = parser.parse(protocol_str) logging.debug(pprint.pformat(pdict)) msg.append("%s has been successfully retrieved from the repository." % pdict['name']) except protocolParseMod.ProtocolParseError, parseError: msg.append("%s could not be successfully parsed after retrieval from the repository." % fetched_protocol[0]['depot_path']) msg.append("Message from parseus: %s" % parseError.msg) objContext = RequestContext(request, {'file': fetched_protocol[0]['depot_path'], 'msg': msg, 'admin':settings.ADMINS}) return render_to_response('protocol_import_error.html', objContext)
def getElementName(request, element, eid, team=None, release=None): """ Get file information from an element based on eid """ if team == None or release == None: team, release = getTeamAndReleaseIDFromSession(request.session) # Different settings based on deployment if settings.LOCAL_SERVER: deploy = Repository.DEPLOY_LOCAL_WINDOWS elif settings.DEVELOPMENT_SERVER: deploy = Repository.DEPLOY_DEVELOPMENT elif settings.PRODUCTION_SERVER: deploy = Repository.DEPLOY_PRODUCTION try: fetch = AccuRevFetcher() except Exception as e: msg = "An unknown error occurred while fetching the protocol from the VCS. Error: %s" % e.message objContext = RequestContext(request, {'msg': msg, 'admin':settings.ADMINS}) return render_to_response('protocol_import_list_error.html', objContext) # Get the repository object for this team/release protocols in accurev try: repository = Repository.objects.get(team=team, release=release, repo_type = 'P', path_type = 'A') localrepo = Repository.objects.get(team=team, release=release, repo_type = 'P', path_type = 'L', deployment = deploy) except Exception, error: if type(error) == Repository.DoesNotExist: msg = "An AccuRev Repository for the given scope (%s/%s) does not exist. Please contact a site administrator." %(release.get_release_name_display(), team.get_team_name_display()) else: msg = "An unknown error occurred while fetching the protocol from the VCS." objContext = RequestContext(request, {'file': element, 'msg': msg, 'admin':settings.ADMINS}) raise GetElementError(render_to_response('protocol_import_error.html', objContext))
def listProtocolsInRepository(request): """ Print a list of the files in the repository location for this test/release """ try: f = AccuRevFetcher() except Exception as e: msg = "An unknown error occurred while fetching protocols from the VCS. Error: " + e.message objContext = RequestContext(request, {'msg': msg, 'admin':settings.ADMINS}) return render_to_response('protocol_import_list_error.html', objContext) # Get the repository object for this team/release protocols in accurev try: team, release = getTeamAndReleaseIDFromSession(request.session) repository = Repository.objects.get(team=team, release=release, repo_type='P', path_type='A') # Get the files from the protocol repository protocol_list = f.files(repository.accurev_stream, [repository.path,]) except Exception as e: if type(e) == Repository.DoesNotExist: msg = "An AccuRev Repository for the given scope (%s/%s) does not exist. Please contact a site administrator." % ( team.get_team_name_display(), release.get_release_name_display() ) long_msg = msg else: msg = "An unexpected error occurred while fetching the protocol from the VCS." long_msg = msg + "\n Error from system: %s" % e.message objContext = RequestContext(request, {'msg': msg, 'long_msg': long_msg, 'admin':settings.ADMINS}) return render_to_response('protocol_import_list_error.html', objContext) # Gather the information if the protocol list exists if protocol_list: protocol_list.sort() # Remove any defuncted protocols and directories protocol_list_available = [elem for elem in protocol_list if ("defunct" not in elem['status'] and "no" in elem['dir'])] # Get queryset of the protocols for this team/release all_protocols = Protocol.active.filter(release__exact=release, team__exact=team) # Loop through and see if any are already in the database active_current_count = 0 active_modified_count = 0 importable_count = 0 for accurev_protocol in protocol_list_available: accurev_protocol['status'] = '(%s)' % accurev_protocol['status'] for fortyTwo_protocol in all_protocols: # Protocol is in the database if os.path.basename(fortyTwo_protocol.file_name) == str(accurev_protocol['file']) or \ str(fortyTwo_protocol.eid) == str(accurev_protocol['eid']): # Protocol is the same version if fortyTwo_protocol.virtual_version == accurev_protocol['virtual']: if fortyTwo_protocol.end_date is None: accurev_protocol['import_state'] = 'active_current' else: accurev_protocol['import_state'] = 'inactive_current' accurev_protocol['id'] = str(fortyTwo_protocol.id) accurev_protocol['db_version'] = fortyTwo_protocol.stream_version active_current_count += 1 break # Protocol is out-of-date elif fortyTwo_protocol.virtual_version != accurev_protocol['virtual']: if fortyTwo_protocol.end_date is None: accurev_protocol['import_state'] = 'active_modified' else: accurev_protocol['import_state'] = 'inactive_modified' accurev_protocol['id'] = str(fortyTwo_protocol.id) accurev_protocol['db_version'] = fortyTwo_protocol.stream_version active_modified_count += 1 break # Protocol does not reside in the database yet else: accurev_protocol['import_state'] = 'importable' accurev_protocol['id'] = '' accurev_protocol['db_version'] = '' importable_count += 1 else: protocol_list_available = [] active_current_count = 0 active_modified_count = 0 importable_count = 0 all_protocols = Protocol.active.filter(release__exact=release, team__exact=team) # Pass back the results objContext = RequestContext(request, {'message': 'The files were successfully retrieved from the repository.', 'protocols': protocol_list_available, 'protocol_number': len(protocol_list_available), 'repository': repository, 'active_current_count': active_current_count, 'active_modified_count': active_modified_count, 'importable_count': importable_count, 'active_protocols_count': all_protocols.count()}) return render_to_response('protocol_import_update.html', objContext)