def manage_repository_reviews_of_revision(self, trans, **kwd): # The value of the received id is the encoded repository id. params = util.Params(kwd) message = util.restore_text(params.get('message', '')) status = params.get('status', 'done') repository_id = kwd.get('id', None) changeset_revision = kwd.get('changeset_revision', None) repository = suc.get_repository_in_tool_shed(trans, repository_id) repo_dir = repository.repo_path(trans.app) repo = hg.repository(suc.get_configured_ui(), repo_dir) installable = changeset_revision in [ metadata_revision.changeset_revision for metadata_revision in repository.metadata_revisions ] rev, changeset_revision_label = suc.get_rev_label_from_changeset_revision( repo, changeset_revision) reviews = suc.get_reviews_by_repository_id_changeset_revision( trans, repository_id, changeset_revision) return trans.fill_template( '/webapps/community/repository_review/reviews_of_changeset_revision.mako', repository=repository, changeset_revision=changeset_revision, changeset_revision_label=changeset_revision_label, reviews=reviews, installable=installable, message=message, status=status)
def upload_tar( self, trans, repository, tar, uploaded_file, upload_point, remove_repo_files_not_in_tar, commit_message, new_repo_alert ): # Upload a tar archive of files. repo_dir = repository.repo_path( trans.app ) repo = hg.repository( suc.get_configured_ui(), repo_dir ) undesirable_dirs_removed = 0 undesirable_files_removed = 0 ok, message = self.__check_archive( tar ) if not ok: tar.close() uploaded_file.close() return ok, message, [], '', undesirable_dirs_removed, undesirable_files_removed else: if upload_point is not None: full_path = os.path.abspath( os.path.join( repo_dir, upload_point ) ) else: full_path = os.path.abspath( repo_dir ) filenames_in_archive = [] for tarinfo_obj in tar.getmembers(): ok = os.path.basename( tarinfo_obj.name ) not in undesirable_files if ok: for file_path_item in tarinfo_obj.name.split( '/' ): if file_path_item in undesirable_dirs: undesirable_dirs_removed += 1 ok = False break else: undesirable_files_removed += 1 if ok: filenames_in_archive.append( tarinfo_obj.name ) # Extract the uploaded tar to the load_point within the repository hierarchy. tar.extractall( path=full_path ) tar.close() uploaded_file.close() return self.__handle_directory_changes(trans, repository, full_path, filenames_in_archive, remove_repo_files_not_in_tar, new_repo_alert, commit_message, undesirable_dirs_removed, undesirable_files_removed)
def upload_directory( self, trans, repository, uploaded_directory, upload_point, remove_repo_files_not_in_tar, commit_message, new_repo_alert ): repo_dir = repository.repo_path( trans.app ) repo = hg.repository( suc.get_configured_ui(), repo_dir ) undesirable_dirs_removed = 0 undesirable_files_removed = 0 if upload_point is not None: full_path = os.path.abspath( os.path.join( repo_dir, upload_point ) ) else: full_path = os.path.abspath( repo_dir ) filenames_in_archive = [] for root, dirs, files in os.walk( uploaded_directory ): for uploaded_file in files: relative_path = os.path.normpath(os.path.join(os.path.relpath(root, uploaded_directory), uploaded_file)) ok = os.path.basename( uploaded_file ) not in undesirable_files if ok: for file_path_item in relative_path.split( '/' ): if file_path_item in undesirable_dirs: undesirable_dirs_removed += 1 ok = False break else: undesirable_files_removed += 1 if ok: repo_path = os.path.join(full_path, relative_path) repo_basedir = os.path.normpath(os.path.join(repo_path, os.path.pardir)) if not os.path.exists(repo_basedir): os.makedirs(repo_basedir) if os.path.exists(repo_path): if os.path.isdir(repo_path): shutil.rmtree(repo_path) else: os.remove(repo_path) shutil.move(os.path.join(uploaded_directory, relative_path), repo_path) filenames_in_archive.append( relative_path ) return self.__handle_directory_changes(trans, repository, full_path, filenames_in_archive, remove_repo_files_not_in_tar, new_repo_alert, commit_message, undesirable_dirs_removed, undesirable_files_removed)
def manage_repository_reviews(self, trans, mine=False, **kwd): # The value of the received id is the encoded repository id. params = util.Params(kwd) message = util.restore_text(params.get('message', '')) status = params.get('status', 'done') repository_id = kwd.get('id', None) if repository_id: repository = suc.get_repository_in_tool_shed(trans, repository_id) repo_dir = repository.repo_path(trans.app) repo = hg.repository(suc.get_configured_ui(), repo_dir) metadata_revision_hashes = [ metadata_revision.changeset_revision for metadata_revision in repository.metadata_revisions ] reviewed_revision_hashes = [ review.changeset_revision for review in repository.reviews ] reviews_dict = odict() for changeset in suc.get_reversed_changelog_changesets(repo): ctx = repo.changectx(changeset) changeset_revision = str(ctx) if changeset_revision in metadata_revision_hashes or changeset_revision in reviewed_revision_hashes: rev, changeset_revision_label = suc.get_rev_label_from_changeset_revision( repo, changeset_revision) if changeset_revision in reviewed_revision_hashes: # Find the review for this changeset_revision repository_reviews = suc.get_reviews_by_repository_id_changeset_revision( trans, repository_id, changeset_revision) # Determine if the current user can add a review to this revision. can_add_review = trans.user not in [ repository_review.user for repository_review in repository_reviews ] repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans, repository_id, changeset_revision) if repository_metadata: repository_metadata_reviews = util.listify( repository_metadata.reviews) else: repository_metadata_reviews = [] else: repository_reviews = [] repository_metadata_reviews = [] can_add_review = True installable = changeset_revision in metadata_revision_hashes revision_dict = dict( changeset_revision_label=changeset_revision_label, repository_reviews=repository_reviews, repository_metadata_reviews=repository_metadata_reviews, installable=installable, can_add_review=can_add_review) reviews_dict[changeset_revision] = revision_dict return trans.fill_template( '/webapps/community/repository_review/reviews_of_repository.mako', repository=repository, reviews_dict=reviews_dict, mine=mine, message=message, status=status)
def get_value(self, trans, grid, repository_metadata): repository = repository_metadata.repository repo = hg.repository(suc.get_configured_ui(), repository.repo_path(trans.app)) ctx = suc.get_changectx_for_changeset( repo, repository_metadata.changeset_revision) return "%s:%s" % (str( ctx.rev()), repository_metadata.changeset_revision)
def __handle_directory_changes(self, trans, repository, full_path, filenames_in_archive, remove_repo_files_not_in_tar, new_repo_alert, commit_message, undesirable_dirs_removed, undesirable_files_removed): repo_dir = repository.repo_path(trans.app) repo = hg.repository(suc.get_configured_ui(), repo_dir) content_alert_str = '' files_to_remove = [] filenames_in_archive = [ os.path.join(full_path, name) for name in filenames_in_archive ] if remove_repo_files_not_in_tar and not repository.is_new(trans.app): # We have a repository that is not new (it contains files), so discover # those files that are in the repository, but not in the uploaded archive. for root, dirs, files in os.walk(full_path): if root.find('.hg') < 0 and root.find('hgrc') < 0: for undesirable_dir in undesirable_dirs: if undesirable_dir in dirs: dirs.remove(undesirable_dir) undesirable_dirs_removed += 1 for undesirable_file in undesirable_files: if undesirable_file in files: files.remove(undesirable_file) undesirable_files_removed += 1 for name in files: full_name = os.path.join(root, name) if full_name not in filenames_in_archive: files_to_remove.append(full_name) for repo_file in files_to_remove: # Remove files in the repository (relative to the upload point) that are not in the uploaded archive. try: commands.remove(repo.ui, repo, repo_file, force=True) except Exception, e: log.debug( "Error removing files using the mercurial API, so trying a different approach, the error was: %s" % str(e)) relative_selected_file = selected_file.split( 'repo_%d' % repository.id)[1].lstrip('/') repo.dirstate.remove(relative_selected_file) repo.dirstate.write() absolute_selected_file = os.path.abspath(selected_file) if os.path.isdir(absolute_selected_file): try: os.rmdir(absolute_selected_file) except OSError, e: # The directory is not empty pass elif os.path.isfile(absolute_selected_file): os.remove(absolute_selected_file) dir = os.path.split(absolute_selected_file)[0] try: os.rmdir(dir) except OSError, e: # The directory is not empty pass
def get_value( self, trans, grid, repository ): # Restrict to revisions that have been reviewed. if repository.reviews: rval = '' repo = hg.repository( suc.get_configured_ui(), repository.repo_path( trans.app ) ) for review in repository.reviews: changeset_revision = review.changeset_revision rev, label = suc.get_rev_label_from_changeset_revision( repo, changeset_revision ) rval += '<a href="manage_repository_reviews_of_revision' rval += '?id=%s&changeset_revision=%s">%s</a><br/>' % ( trans.security.encode_id( repository.id ), changeset_revision, label ) return rval return ''
def browse_review( self, trans, **kwd ): params = util.Params( kwd ) message = util.restore_text( params.get( 'message', '' ) ) status = params.get( 'status', 'done' ) review = suc.get_review( trans, kwd[ 'id' ] ) repository = review.repository repo = hg.repository( suc.get_configured_ui(), repository.repo_path( trans.app ) ) rev, changeset_revision_label = suc.get_rev_label_from_changeset_revision( repo, review.changeset_revision ) return trans.fill_template( '/webapps/community/repository_review/browse_review.mako', repository=repository, changeset_revision_label=changeset_revision_label, review=review, message=message, status=status )
def get_value(self, trans, grid, repository): # Restrict to revisions that have been reviewed. if repository.reviews: rval = '' repo = hg.repository(suc.get_configured_ui(), repository.repo_path(trans.app)) for review in repository.reviews: changeset_revision = review.changeset_revision rev, label = suc.get_rev_label_from_changeset_revision( repo, changeset_revision) rval += '<a href="manage_repository_reviews_of_revision' rval += '?id=%s&changeset_revision=%s">%s</a><br/>' % ( trans.security.encode_id( repository.id), changeset_revision, label) return rval return ''
def __handle_directory_changes( self, trans, repository, full_path, filenames_in_archive, remove_repo_files_not_in_tar, new_repo_alert, commit_message, undesirable_dirs_removed, undesirable_files_removed ): repo_dir = repository.repo_path( trans.app ) repo = hg.repository( suc.get_configured_ui(), repo_dir ) content_alert_str = '' files_to_remove = [] filenames_in_archive = [ os.path.join( full_path, name ) for name in filenames_in_archive ] if remove_repo_files_not_in_tar and not repository.is_new( trans.app ): # We have a repository that is not new (it contains files), so discover # those files that are in the repository, but not in the uploaded archive. for root, dirs, files in os.walk( full_path ): if root.find( '.hg' ) < 0 and root.find( 'hgrc' ) < 0: for undesirable_dir in undesirable_dirs: if undesirable_dir in dirs: dirs.remove( undesirable_dir ) undesirable_dirs_removed += 1 for undesirable_file in undesirable_files: if undesirable_file in files: files.remove( undesirable_file ) undesirable_files_removed += 1 for name in files: full_name = os.path.join( root, name ) if full_name not in filenames_in_archive: files_to_remove.append( full_name ) for repo_file in files_to_remove: # Remove files in the repository (relative to the upload point) that are not in the uploaded archive. try: commands.remove( repo.ui, repo, repo_file, force=True ) except Exception, e: log.debug( "Error removing files using the mercurial API, so trying a different approach, the error was: %s" % str( e )) relative_selected_file = selected_file.split( 'repo_%d' % repository.id )[1].lstrip( '/' ) repo.dirstate.remove( relative_selected_file ) repo.dirstate.write() absolute_selected_file = os.path.abspath( selected_file ) if os.path.isdir( absolute_selected_file ): try: os.rmdir( absolute_selected_file ) except OSError, e: # The directory is not empty pass elif os.path.isfile( absolute_selected_file ): os.remove( absolute_selected_file ) dir = os.path.split( absolute_selected_file )[0] try: os.rmdir( dir ) except OSError, e: # The directory is not empty pass
def upload_directory(self, trans, repository, uploaded_directory, upload_point, remove_repo_files_not_in_tar, commit_message, new_repo_alert): repo_dir = repository.repo_path(trans.app) repo = hg.repository(suc.get_configured_ui(), repo_dir) undesirable_dirs_removed = 0 undesirable_files_removed = 0 if upload_point is not None: full_path = os.path.abspath(os.path.join(repo_dir, upload_point)) else: full_path = os.path.abspath(repo_dir) filenames_in_archive = [] for root, dirs, files in os.walk(uploaded_directory): for uploaded_file in files: relative_path = os.path.normpath( os.path.join(os.path.relpath(root, uploaded_directory), uploaded_file)) ok = os.path.basename(uploaded_file) not in undesirable_files if ok: for file_path_item in relative_path.split('/'): if file_path_item in undesirable_dirs: undesirable_dirs_removed += 1 ok = False break else: undesirable_files_removed += 1 if ok: repo_path = os.path.join(full_path, relative_path) repo_basedir = os.path.normpath( os.path.join(repo_path, os.path.pardir)) if not os.path.exists(repo_basedir): os.makedirs(repo_basedir) if os.path.exists(repo_path): if os.path.isdir(repo_path): shutil.rmtree(repo_path) else: os.remove(repo_path) shutil.move( os.path.join(uploaded_directory, relative_path), repo_path) filenames_in_archive.append(relative_path) return self.__handle_directory_changes(trans, repository, full_path, filenames_in_archive, remove_repo_files_not_in_tar, new_repo_alert, commit_message, undesirable_dirs_removed, undesirable_files_removed)
def browse_review(self, trans, **kwd): params = util.Params(kwd) message = util.restore_text(params.get('message', '')) status = params.get('status', 'done') review = suc.get_review(trans, kwd['id']) repository = review.repository repo = hg.repository(suc.get_configured_ui(), repository.repo_path(trans.app)) rev, changeset_revision_label = suc.get_rev_label_from_changeset_revision( repo, review.changeset_revision) return trans.fill_template( '/webapps/community/repository_review/browse_review.mako', repository=repository, changeset_revision_label=changeset_revision_label, review=review, message=message, status=status)
def select_previous_review( self, trans, **kwd ): # The value of the received id is the encoded repository id. params = util.Params( kwd ) message = util.restore_text( params.get( 'message', '' ) ) status = params.get( 'status', 'done' ) repository = suc.get_repository_in_tool_shed( trans, kwd[ 'id' ] ) changeset_revision = kwd.get( 'changeset_revision', None ) repo = hg.repository( suc.get_configured_ui(), repository.repo_path( trans.app ) ) previous_reviews_dict = suc.get_previous_repository_reviews( trans, repository, changeset_revision ) rev, changeset_revision_label = suc.get_rev_label_from_changeset_revision( repo, changeset_revision ) return trans.fill_template( '/webapps/community/repository_review/select_previous_review.mako', repository=repository, changeset_revision=changeset_revision, changeset_revision_label=changeset_revision_label, previous_reviews_dict=previous_reviews_dict, message=message, status=status )
def manage_repository_reviews( self, trans, mine=False, **kwd ): # The value of the received id is the encoded repository id. params = util.Params( kwd ) message = util.restore_text( params.get( 'message', '' ) ) status = params.get( 'status', 'done' ) repository_id = kwd.get( 'id', None ) if repository_id: repository = suc.get_repository_in_tool_shed( trans, repository_id ) repo_dir = repository.repo_path( trans.app ) repo = hg.repository( suc.get_configured_ui(), repo_dir ) metadata_revision_hashes = [ metadata_revision.changeset_revision for metadata_revision in repository.metadata_revisions ] reviewed_revision_hashes = [ review.changeset_revision for review in repository.reviews ] reviews_dict = odict() for changeset in suc.get_reversed_changelog_changesets( repo ): ctx = repo.changectx( changeset ) changeset_revision = str( ctx ) if changeset_revision in metadata_revision_hashes or changeset_revision in reviewed_revision_hashes: rev, changeset_revision_label = suc.get_rev_label_from_changeset_revision( repo, changeset_revision ) if changeset_revision in reviewed_revision_hashes: # Find the review for this changeset_revision repository_reviews = suc.get_reviews_by_repository_id_changeset_revision( trans, repository_id, changeset_revision ) # Determine if the current user can add a review to this revision. can_add_review = trans.user not in [ repository_review.user for repository_review in repository_reviews ] repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans, repository_id, changeset_revision ) if repository_metadata: repository_metadata_reviews = util.listify( repository_metadata.reviews ) else: repository_metadata_reviews = [] else: repository_reviews = [] repository_metadata_reviews = [] can_add_review = True installable = changeset_revision in metadata_revision_hashes revision_dict = dict( changeset_revision_label=changeset_revision_label, repository_reviews=repository_reviews, repository_metadata_reviews=repository_metadata_reviews, installable=installable, can_add_review=can_add_review ) reviews_dict[ changeset_revision ] = revision_dict return trans.fill_template( '/webapps/community/repository_review/reviews_of_repository.mako', repository=repository, reviews_dict=reviews_dict, mine=mine, message=message, status=status )
def upload_tar(self, trans, repository, tar, uploaded_file, upload_point, remove_repo_files_not_in_tar, commit_message, new_repo_alert): # Upload a tar archive of files. repo_dir = repository.repo_path(trans.app) repo = hg.repository(suc.get_configured_ui(), repo_dir) undesirable_dirs_removed = 0 undesirable_files_removed = 0 ok, message = self.__check_archive(tar) if not ok: tar.close() uploaded_file.close() return ok, message, [], '', undesirable_dirs_removed, undesirable_files_removed else: if upload_point is not None: full_path = os.path.abspath( os.path.join(repo_dir, upload_point)) else: full_path = os.path.abspath(repo_dir) filenames_in_archive = [] for tarinfo_obj in tar.getmembers(): ok = os.path.basename( tarinfo_obj.name) not in undesirable_files if ok: for file_path_item in tarinfo_obj.name.split('/'): if file_path_item in undesirable_dirs: undesirable_dirs_removed += 1 ok = False break else: undesirable_files_removed += 1 if ok: filenames_in_archive.append(tarinfo_obj.name) # Extract the uploaded tar to the load_point within the repository hierarchy. tar.extractall(path=full_path) tar.close() uploaded_file.close() return self.__handle_directory_changes( trans, repository, full_path, filenames_in_archive, remove_repo_files_not_in_tar, new_repo_alert, commit_message, undesirable_dirs_removed, undesirable_files_removed)
def manage_repository_reviews_of_revision( self, trans, **kwd ): # The value of the received id is the encoded repository id. params = util.Params( kwd ) message = util.restore_text( params.get( 'message', '' ) ) status = params.get( 'status', 'done' ) repository_id = kwd.get( 'id', None ) changeset_revision = kwd.get( 'changeset_revision', None ) repository = suc.get_repository_in_tool_shed( trans, repository_id ) repo_dir = repository.repo_path( trans.app ) repo = hg.repository( suc.get_configured_ui(), repo_dir ) installable = changeset_revision in [ metadata_revision.changeset_revision for metadata_revision in repository.metadata_revisions ] rev, changeset_revision_label = suc.get_rev_label_from_changeset_revision( repo, changeset_revision ) reviews = suc.get_reviews_by_repository_id_changeset_revision( trans, repository_id, changeset_revision ) return trans.fill_template( '/webapps/community/repository_review/reviews_of_changeset_revision.mako', repository=repository, changeset_revision=changeset_revision, changeset_revision_label=changeset_revision_label, reviews=reviews, installable=installable, message=message, status=status )
def select_previous_review(self, trans, **kwd): # The value of the received id is the encoded repository id. params = util.Params(kwd) message = util.restore_text(params.get('message', '')) status = params.get('status', 'done') repository = suc.get_repository_in_tool_shed(trans, kwd['id']) changeset_revision = kwd.get('changeset_revision', None) repo = hg.repository(suc.get_configured_ui(), repository.repo_path(trans.app)) previous_reviews_dict = suc.get_previous_repository_reviews( trans, repository, changeset_revision) rev, changeset_revision_label = suc.get_rev_label_from_changeset_revision( repo, changeset_revision) return trans.fill_template( '/webapps/community/repository_review/select_previous_review.mako', repository=repository, changeset_revision=changeset_revision, changeset_revision_label=changeset_revision_label, previous_reviews_dict=previous_reviews_dict, message=message, status=status)
def get_value( self, trans, grid, repository_metadata ): repository = repository_metadata.repository repo = hg.repository( suc.get_configured_ui(), repository.repo_path( trans.app ) ) ctx = suc.get_changectx_for_changeset( repo, repository_metadata.changeset_revision ) return "%s:%s" % ( str( ctx.rev() ), repository_metadata.changeset_revision )
def edit_review(self, trans, **kwd): # The value of the received id is the encoded review id. params = util.Params(kwd) message = util.restore_text(params.get('message', '')) status = params.get('status', 'done') review_id = kwd.get('id', None) review = suc.get_review(trans, review_id) components_dict = odict() for component in suc.get_components(trans): components_dict[component.name] = dict(component=component, component_review=None) repository = review.repository repo = hg.repository(suc.get_configured_ui(), repository.repo_path(trans.app)) for component_review in review.component_reviews: if component_review and component_review.component: component_name = component_review.component.name if component_name in components_dict: component_review_dict = components_dict[component_name] component_review_dict[ 'component_review'] = component_review components_dict[component_name] = component_review_dict # Handle a Save button click. save_button_clicked = False save_buttons = [ '%s%sreview_button' % (component_name, STRSEP) for component_name in components_dict.keys() ] save_buttons.append('revision_approved_button') for save_button in save_buttons: if save_button in kwd: save_button_clicked = True break if save_button_clicked: # Handle the revision_approved_select_field value. revision_approved = kwd.get('revision_approved', None) revision_approved_setting_changed = False if revision_approved: revision_approved = str(revision_approved) if review.approved != revision_approved: revision_approved_setting_changed = True review.approved = revision_approved trans.sa_session.add(review) trans.sa_session.flush() saved_component_names = [] for component_name in components_dict.keys(): flushed = False # Retrieve the review information from the form. # The star rating form field is a radio button list, so it will not be received if it was not clicked in the form. # Due to this behavior, default the value to 0. rating = 0 for k, v in kwd.items(): if k.startswith('%s%s' % (component_name, STRSEP)): component_review_attr = k.replace( '%s%s' % (component_name, STRSEP), '') if component_review_attr == 'component_id': component_id = str(v) elif component_review_attr == 'comment': comment = str(v) elif component_review_attr == 'private': private = CheckboxField.is_checked(v) elif component_review_attr == 'approved': approved = str(v) elif component_review_attr == 'rating': rating = int(str(v)) component = suc.get_component(trans, component_id) component_review = suc.get_component_review_by_repository_review_id_component_id( trans, review_id, component_id) if component_review: # See if the existing component review should be updated. if component_review.comment != comment or \ component_review.private != private or \ component_review.approved != approved or \ component_review.rating != rating: component_review.comment = comment component_review.private = private component_review.approved = approved component_review.rating = rating trans.sa_session.add(component_review) trans.sa_session.flush() flushed = True saved_component_names.append(component_name) else: # See if a new component_review should be created. if comment or private or approved != trans.model.ComponentReview.approved_states.NO or rating: component_review = trans.model.ComponentReview( repository_review_id=review.id, component_id=component.id, comment=comment, approved=approved, rating=rating) trans.sa_session.add(component_review) trans.sa_session.flush() flushed = True saved_component_names.append(component_name) if flushed: # Update the repository rating value to be the average of all component review ratings. average_rating = trans.sa_session.query( func.avg( trans.model.ComponentReview.table.c.rating ) ) \ .filter( trans.model.ComponentReview.table.c.repository_review_id == review.id ) \ .scalar() review.rating = int(average_rating) trans.sa_session.add(review) trans.sa_session.flush() # Update the information in components_dict. if component_name in components_dict: component_review_dict = components_dict[component_name] component_review_dict[ 'component_review'] = component_review components_dict[component_name] = component_review_dict if revision_approved_setting_changed: message += 'Approved value <b>%s</b> saved for this revision.<br/>' % review.approved if saved_component_names: message += 'Reviews were saved for components: %s' % ', '.join( saved_component_names) if not revision_approved_setting_changed and not saved_component_names: message += 'No changes were made to this review, so nothing was saved.' if review and review.approved: selected_value = review.approved else: selected_value = trans.model.ComponentReview.approved_states.NO revision_approved_select_field = build_approved_select_field( trans, name='revision_approved', selected_value=selected_value, for_component=False) rev, changeset_revision_label = suc.get_rev_label_from_changeset_revision( repo, review.changeset_revision) return trans.fill_template( '/webapps/community/repository_review/edit_review.mako', repository=repository, review=review, changeset_revision_label=changeset_revision_label, revision_approved_select_field=revision_approved_select_field, components_dict=components_dict, message=message, status=status)
def edit_review( self, trans, **kwd ): # The value of the received id is the encoded review id. params = util.Params( kwd ) message = util.restore_text( params.get( 'message', '' ) ) status = params.get( 'status', 'done' ) review_id = kwd.get( 'id', None ) review = suc.get_review( trans, review_id ) components_dict = odict() for component in suc.get_components( trans ): components_dict[ component.name ] = dict( component=component, component_review=None ) repository = review.repository repo = hg.repository( suc.get_configured_ui(), repository.repo_path( trans.app ) ) for component_review in review.component_reviews: if component_review and component_review.component: component_name = component_review.component.name if component_name in components_dict: component_review_dict = components_dict[ component_name ] component_review_dict[ 'component_review' ] = component_review components_dict[ component_name ] = component_review_dict # Handle a Save button click. save_button_clicked = False save_buttons = [ '%s%sreview_button' % ( component_name, STRSEP ) for component_name in components_dict.keys() ] save_buttons.append( 'revision_approved_button' ) for save_button in save_buttons: if save_button in kwd: save_button_clicked = True break if save_button_clicked: # Handle the revision_approved_select_field value. revision_approved = kwd.get( 'revision_approved', None ) revision_approved_setting_changed = False if revision_approved: revision_approved = str( revision_approved ) if review.approved != revision_approved: revision_approved_setting_changed = True review.approved = revision_approved trans.sa_session.add( review ) trans.sa_session.flush() saved_component_names = [] for component_name in components_dict.keys(): flushed = False # Retrieve the review information from the form. # The star rating form field is a radio button list, so it will not be received if it was not clicked in the form. # Due to this behavior, default the value to 0. rating = 0 for k, v in kwd.items(): if k.startswith( '%s%s' % ( component_name, STRSEP ) ): component_review_attr = k.replace( '%s%s' % ( component_name, STRSEP ), '' ) if component_review_attr == 'component_id': component_id = str( v ) elif component_review_attr == 'comment': comment = str( v ) elif component_review_attr == 'private': private = CheckboxField.is_checked( v ) elif component_review_attr == 'approved': approved = str( v ) elif component_review_attr == 'rating': rating = int( str( v ) ) component = suc.get_component( trans, component_id ) component_review = suc.get_component_review_by_repository_review_id_component_id( trans, review_id, component_id ) if component_review: # See if the existing component review should be updated. if component_review.comment != comment or \ component_review.private != private or \ component_review.approved != approved or \ component_review.rating != rating: component_review.comment = comment component_review.private = private component_review.approved = approved component_review.rating = rating trans.sa_session.add( component_review ) trans.sa_session.flush() flushed = True saved_component_names.append( component_name ) else: # See if a new component_review should be created. if comment or private or approved != trans.model.ComponentReview.approved_states.NO or rating: component_review = trans.model.ComponentReview( repository_review_id=review.id, component_id=component.id, comment=comment, approved=approved, rating=rating ) trans.sa_session.add( component_review ) trans.sa_session.flush() flushed = True saved_component_names.append( component_name ) if flushed: # Update the repository rating value to be the average of all component review ratings. average_rating = trans.sa_session.query( func.avg( trans.model.ComponentReview.table.c.rating ) ) \ .filter( trans.model.ComponentReview.table.c.repository_review_id == review.id ) \ .scalar() review.rating = int( average_rating ) trans.sa_session.add( review ) trans.sa_session.flush() # Update the information in components_dict. if component_name in components_dict: component_review_dict = components_dict[ component_name ] component_review_dict[ 'component_review' ] = component_review components_dict[ component_name ] = component_review_dict if revision_approved_setting_changed: message += 'Approved value <b>%s</b> saved for this revision.<br/>' % review.approved if saved_component_names: message += 'Reviews were saved for components: %s' % ', '.join( saved_component_names ) if not revision_approved_setting_changed and not saved_component_names: message += 'No changes were made to this review, so nothing was saved.' if review and review.approved: selected_value = review.approved else: selected_value = trans.model.ComponentReview.approved_states.NO revision_approved_select_field = build_approved_select_field( trans, name='revision_approved', selected_value=selected_value, for_component=False ) rev, changeset_revision_label = suc.get_rev_label_from_changeset_revision( repo, review.changeset_revision ) return trans.fill_template( '/webapps/community/repository_review/edit_review.mako', repository=repository, review=review, changeset_revision_label=changeset_revision_label, revision_approved_select_field=revision_approved_select_field, components_dict=components_dict, message=message, status=status )
def upload( self, trans, **kwd ): params = util.Params( kwd ) message = util.restore_text( params.get( 'message', '' ) ) status = params.get( 'status', 'done' ) commit_message = util.restore_text( params.get( 'commit_message', 'Uploaded' ) ) category_ids = util.listify( params.get( 'category_id', '' ) ) categories = suc.get_categories( trans ) repository_id = params.get( 'repository_id', '' ) repository = suc.get_repository_in_tool_shed( trans, repository_id ) repo_dir = repository.repo_path( trans.app ) repo = hg.repository( suc.get_configured_ui(), repo_dir ) uncompress_file = util.string_as_bool( params.get( 'uncompress_file', 'true' ) ) remove_repo_files_not_in_tar = util.string_as_bool( params.get( 'remove_repo_files_not_in_tar', 'true' ) ) uploaded_file = None upload_point = self.__get_upload_point( repository, **kwd ) tip = repository.tip( trans.app ) file_data = params.get( 'file_data', '' ) url = params.get( 'url', '' ) # Part of the upload process is sending email notification to those that have registered to # receive them. One scenario occurs when the first change set is produced for the repository. # See the suc.handle_email_alerts() method for the definition of the scenarios. new_repo_alert = repository.is_new( trans.app ) uploaded_directory = None if params.get( 'upload_button', False ): if file_data == '' and url == '': message = 'No files were entered on the upload form.' status = 'error' uploaded_file = None elif url and url.startswith( 'hg' ): # Use mercurial clone to fetch repository, contents will then be copied over. uploaded_directory = tempfile.mkdtemp() repo_url = 'http%s' % url[ len( 'hg' ): ] repo_url = repo_url.encode( 'ascii', 'replace' ) commands.clone( suc.get_configured_ui(), repo_url, uploaded_directory ) elif url: valid_url = True try: stream = urllib.urlopen( url ) except Exception, e: valid_url = False message = 'Error uploading file via http: %s' % str( e ) status = 'error' uploaded_file = None if valid_url: fd, uploaded_file_name = tempfile.mkstemp() uploaded_file = open( uploaded_file_name, 'wb' ) while 1: chunk = stream.read( CHUNK_SIZE ) if not chunk: break uploaded_file.write( chunk ) uploaded_file.flush() uploaded_file_filename = url.split( '/' )[ -1 ] isempty = os.path.getsize( os.path.abspath( uploaded_file_name ) ) == 0 elif file_data not in ( '', None ): uploaded_file = file_data.file uploaded_file_name = uploaded_file.name uploaded_file_filename = os.path.split( file_data.filename )[ -1 ] isempty = os.path.getsize( os.path.abspath( uploaded_file_name ) ) == 0 if uploaded_file or uploaded_directory: ok = True isgzip = False isbz2 = False if uploaded_file: if uncompress_file: isgzip = checkers.is_gzip( uploaded_file_name ) if not isgzip: isbz2 = checkers.is_bz2( uploaded_file_name ) if isempty: tar = None istar = False else: # Determine what we have - a single file or an archive try: if ( isgzip or isbz2 ) and uncompress_file: # Open for reading with transparent compression. tar = tarfile.open( uploaded_file_name, 'r:*' ) else: tar = tarfile.open( uploaded_file_name ) istar = True except tarfile.ReadError, e: tar = None istar = False else: # Uploaded directory istar = False if istar: ok, message, files_to_remove, content_alert_str, undesirable_dirs_removed, undesirable_files_removed = \ self.upload_tar( trans, repository, tar, uploaded_file, upload_point, remove_repo_files_not_in_tar, commit_message, new_repo_alert ) elif uploaded_directory: ok,message, files_to_remove, content_alert_str, undesirable_dirs_removed, undesirable_files_removed = \ self.upload_directory( trans, repository, uploaded_directory, upload_point, remove_repo_files_not_in_tar, commit_message, new_repo_alert ) else: if ( isgzip or isbz2 ) and uncompress_file: uploaded_file_filename = self.uncompress( repository, uploaded_file_name, uploaded_file_filename, isgzip, isbz2 ) if upload_point is not None: full_path = os.path.abspath( os.path.join( repo_dir, upload_point, uploaded_file_filename ) ) else: full_path = os.path.abspath( os.path.join( repo_dir, uploaded_file_filename ) ) # Move the uploaded file to the load_point within the repository hierarchy. shutil.move( uploaded_file_name, full_path ) # See if any admin users have chosen to receive email alerts when a repository is # updated. If so, check every uploaded file to ensure content is appropriate. check_contents = suc.check_file_contents( trans ) if check_contents and os.path.isfile( full_path ): content_alert_str = self.__check_file_content( full_path ) else: content_alert_str = '' commands.add( repo.ui, repo, full_path ) # Convert from unicode to prevent "TypeError: array item must be char" full_path = full_path.encode( 'ascii', 'replace' ) commands.commit( repo.ui, repo, full_path, user=trans.user.username, message=commit_message ) if full_path.endswith( 'tool_data_table_conf.xml.sample' ): # Handle the special case where a tool_data_table_conf.xml.sample file is being uploaded by parsing the file and adding new entries # to the in-memory trans.app.tool_data_tables dictionary. error, error_message = suc.handle_sample_tool_data_table_conf_file( trans.app, full_path ) if error: message = '%s<br/>%s' % ( message, error_message ) # See if the content of the change set was valid. admin_only = len( repository.downloadable_revisions ) != 1 suc.handle_email_alerts( trans, repository, content_alert_str=content_alert_str, new_repo_alert=new_repo_alert, admin_only=admin_only ) if ok: # Update the repository files for browsing. suc.update_repository( repo ) # Get the new repository tip. if tip == repository.tip( trans.app ): message = 'No changes to repository. ' status = 'warning' else: if ( isgzip or isbz2 ) and uncompress_file: uncompress_str = ' uncompressed and ' else: uncompress_str = ' ' if uploaded_directory: source_type = "repository" source = url else: source_type = "file" source = uploaded_file_filename message = "The %s '%s' has been successfully%suploaded to the repository. " % ( source_type, source, uncompress_str ) if istar and ( undesirable_dirs_removed or undesirable_files_removed ): items_removed = undesirable_dirs_removed + undesirable_files_removed message += " %d undesirable items (.hg .svn .git directories, .DS_Store, hgrc files, etc) were removed from the archive. " % items_removed if istar and remove_repo_files_not_in_tar and files_to_remove: if upload_point is not None: message += " %d files were removed from the repository relative to the selected upload point '%s'. " % ( len( files_to_remove ), upload_point ) else: message += " %d files were removed from the repository root. " % len( files_to_remove ) kwd[ 'message' ] = message suc.set_repository_metadata_due_to_new_tip( trans, repository, content_alert_str=content_alert_str, **kwd ) # Provide a warning message if a tool_dependencies.xml file is provided, but tool dependencies weren't loaded due to a requirement tag mismatch # or some other problem. if suc.get_config_from_disk( 'tool_dependencies.xml', repo_dir ): if repository.metadata_revisions: # A repository's metadata revisions are order descending by update_time, so the zeroth revision will be the tip just after an upload. metadata_dict = repository.metadata_revisions[0].metadata else: metadata_dict = {} if suc.has_orphan_tool_dependencies_in_tool_shed( metadata_dict ): message += 'Name, version and type from a tool requirement tag does not match the information in the "tool_dependencies.xml file", ' message += 'so one or more of the defined tool dependencies are considered orphans within this repository.' status = 'warning' # Reset the tool_data_tables by loading the empty tool_data_table_conf.xml file. suc.reset_tool_data_tables( trans.app ) trans.response.send_redirect( web.url_for( controller='repository', action='browse_repository', id=repository_id, commit_message='Deleted selected files', message=message, status=status ) ) else: status = 'error' # Reset the tool_data_tables by loading the empty tool_data_table_conf.xml file. suc.reset_tool_data_tables( trans.app )
def upload(self, trans, **kwd): params = util.Params(kwd) message = util.restore_text(params.get('message', '')) status = params.get('status', 'done') commit_message = util.restore_text( params.get('commit_message', 'Uploaded')) category_ids = util.listify(params.get('category_id', '')) categories = suc.get_categories(trans) repository_id = params.get('repository_id', '') repository = suc.get_repository_in_tool_shed(trans, repository_id) repo_dir = repository.repo_path(trans.app) repo = hg.repository(suc.get_configured_ui(), repo_dir) uncompress_file = util.string_as_bool( params.get('uncompress_file', 'true')) remove_repo_files_not_in_tar = util.string_as_bool( params.get('remove_repo_files_not_in_tar', 'true')) uploaded_file = None upload_point = self.__get_upload_point(repository, **kwd) tip = repository.tip(trans.app) file_data = params.get('file_data', '') url = params.get('url', '') # Part of the upload process is sending email notification to those that have registered to # receive them. One scenario occurs when the first change set is produced for the repository. # See the suc.handle_email_alerts() method for the definition of the scenarios. new_repo_alert = repository.is_new(trans.app) uploaded_directory = None if params.get('upload_button', False): if file_data == '' and url == '': message = 'No files were entered on the upload form.' status = 'error' uploaded_file = None elif url and url.startswith('hg'): # Use mercurial clone to fetch repository, contents will then be copied over. uploaded_directory = tempfile.mkdtemp() repo_url = 'http%s' % url[len('hg'):] repo_url = repo_url.encode('ascii', 'replace') commands.clone(suc.get_configured_ui(), repo_url, uploaded_directory) elif url: valid_url = True try: stream = urllib.urlopen(url) except Exception, e: valid_url = False message = 'Error uploading file via http: %s' % str(e) status = 'error' uploaded_file = None if valid_url: fd, uploaded_file_name = tempfile.mkstemp() uploaded_file = open(uploaded_file_name, 'wb') while 1: chunk = stream.read(CHUNK_SIZE) if not chunk: break uploaded_file.write(chunk) uploaded_file.flush() uploaded_file_filename = url.split('/')[-1] isempty = os.path.getsize( os.path.abspath(uploaded_file_name)) == 0 elif file_data not in ('', None): uploaded_file = file_data.file uploaded_file_name = uploaded_file.name uploaded_file_filename = os.path.split(file_data.filename)[-1] isempty = os.path.getsize( os.path.abspath(uploaded_file_name)) == 0 if uploaded_file or uploaded_directory: ok = True isgzip = False isbz2 = False if uploaded_file: if uncompress_file: isgzip = checkers.is_gzip(uploaded_file_name) if not isgzip: isbz2 = checkers.is_bz2(uploaded_file_name) if isempty: tar = None istar = False else: # Determine what we have - a single file or an archive try: if (isgzip or isbz2) and uncompress_file: # Open for reading with transparent compression. tar = tarfile.open(uploaded_file_name, 'r:*') else: tar = tarfile.open(uploaded_file_name) istar = True except tarfile.ReadError, e: tar = None istar = False else: # Uploaded directory istar = False if istar: ok, message, files_to_remove, content_alert_str, undesirable_dirs_removed, undesirable_files_removed = \ self.upload_tar( trans, repository, tar, uploaded_file, upload_point, remove_repo_files_not_in_tar, commit_message, new_repo_alert ) elif uploaded_directory: ok,message, files_to_remove, content_alert_str, undesirable_dirs_removed, undesirable_files_removed = \ self.upload_directory( trans, repository, uploaded_directory, upload_point, remove_repo_files_not_in_tar, commit_message, new_repo_alert ) else: if (isgzip or isbz2) and uncompress_file: uploaded_file_filename = self.uncompress( repository, uploaded_file_name, uploaded_file_filename, isgzip, isbz2) if upload_point is not None: full_path = os.path.abspath( os.path.join(repo_dir, upload_point, uploaded_file_filename)) else: full_path = os.path.abspath( os.path.join(repo_dir, uploaded_file_filename)) # Move the uploaded file to the load_point within the repository hierarchy. shutil.move(uploaded_file_name, full_path) # See if any admin users have chosen to receive email alerts when a repository is # updated. If so, check every uploaded file to ensure content is appropriate. check_contents = suc.check_file_contents(trans) if check_contents and os.path.isfile(full_path): content_alert_str = self.__check_file_content( full_path) else: content_alert_str = '' commands.add(repo.ui, repo, full_path) # Convert from unicode to prevent "TypeError: array item must be char" full_path = full_path.encode('ascii', 'replace') commands.commit(repo.ui, repo, full_path, user=trans.user.username, message=commit_message) if full_path.endswith('tool_data_table_conf.xml.sample'): # Handle the special case where a tool_data_table_conf.xml.sample file is being uploaded by parsing the file and adding new entries # to the in-memory trans.app.tool_data_tables dictionary. error, error_message = suc.handle_sample_tool_data_table_conf_file( trans.app, full_path) if error: message = '%s<br/>%s' % (message, error_message) # See if the content of the change set was valid. admin_only = len(repository.downloadable_revisions) != 1 suc.handle_email_alerts( trans, repository, content_alert_str=content_alert_str, new_repo_alert=new_repo_alert, admin_only=admin_only) if ok: # Update the repository files for browsing. suc.update_repository(repo) # Get the new repository tip. if tip == repository.tip(trans.app): message = 'No changes to repository. ' status = 'warning' else: if (isgzip or isbz2) and uncompress_file: uncompress_str = ' uncompressed and ' else: uncompress_str = ' ' if uploaded_directory: source_type = "repository" source = url else: source_type = "file" source = uploaded_file_filename message = "The %s '%s' has been successfully%suploaded to the repository. " % ( source_type, source, uncompress_str) if istar and (undesirable_dirs_removed or undesirable_files_removed): items_removed = undesirable_dirs_removed + undesirable_files_removed message += " %d undesirable items (.hg .svn .git directories, .DS_Store, hgrc files, etc) were removed from the archive. " % items_removed if istar and remove_repo_files_not_in_tar and files_to_remove: if upload_point is not None: message += " %d files were removed from the repository relative to the selected upload point '%s'. " % ( len(files_to_remove), upload_point) else: message += " %d files were removed from the repository root. " % len( files_to_remove) kwd['message'] = message suc.set_repository_metadata_due_to_new_tip( trans, repository, content_alert_str=content_alert_str, **kwd) # Provide a warning message if a tool_dependencies.xml file is provided, but tool dependencies weren't loaded due to a requirement tag mismatch # or some other problem. if suc.get_config_from_disk('tool_dependencies.xml', repo_dir): if repository.metadata_revisions: # A repository's metadata revisions are order descending by update_time, so the zeroth revision will be the tip just after an upload. metadata_dict = repository.metadata_revisions[ 0].metadata else: metadata_dict = {} if suc.has_orphan_tool_dependencies_in_tool_shed( metadata_dict): message += 'Name, version and type from a tool requirement tag does not match the information in the "tool_dependencies.xml file", ' message += 'so one or more of the defined tool dependencies are considered orphans within this repository.' status = 'warning' # Reset the tool_data_tables by loading the empty tool_data_table_conf.xml file. suc.reset_tool_data_tables(trans.app) trans.response.send_redirect( web.url_for(controller='repository', action='browse_repository', id=repository_id, commit_message='Deleted selected files', message=message, status=status)) else: status = 'error' # Reset the tool_data_tables by loading the empty tool_data_table_conf.xml file. suc.reset_tool_data_tables(trans.app)