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 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)