def connectToPerforce(self, retry=3): # if ( # not hasattr(self, "p4") # or not hasattr(self, "sgPrjId") # or (user and not hasattr(self, "sgUserId")) # ): is_connected_p4 = False try: self.p4.disconnect() except: pass self.p4.port = self.core.getConfig("perforce", "port", configPath = self.core.prismIni) self.p4.user = self.core.getConfig("perforce", "p4username") self.p4.client = self.core.getConfig("perforce", "p4userworkspacename") self.p4.password = self.core.getConfig("perforce", "p4userpassword") try: self.p4.connect() except P4.P4Exception as why: raise P4.P4Exception("Failed to connect to p4. {}".format(why)) try: self.p4.run_login('-s') is_connected_p4 = True except P4.P4Exception as why: try: self.p4.run_login() is_connected_p4 = True except P4.P4Exception as why: raise P4.P4Exception("Failed to login to p4. {}".format(why)) while not is_connected_p4 and retry != 0: is_connected_p4 = self.connectToPerforce(retry-1) return is_connected_p4
def raise_if_homedir(homedir, view_name, rm_list): """If any path in rm_list is user's home directory, fail with error rather than delete the home directory.""" for e in rm_list: if e == homedir: raise P4.P4Exception(("One of view {}'s directories is" + " user's home directory!").format(view_name))
def convert_client(args, p4, client_name): """Convert the named Perforce client and its workspace. Raises P4Exception if the client is not present, or the client configuration is not set up as expected. Keyword arguments: args -- parsed command line arguments p4 -- Git user's Perforce client client_name -- name of client to be deleted """ group_list = [ p4gf_const.P4GF_GROUP_VIEW_PULL, p4gf_const.P4GF_GROUP_VIEW_PUSH ] p4.user = p4gf_const.P4GF_USER old_client = p4.client p4.client = client_name print(" Processing client {}...".format(client_name)) if not p4gf_util.spec_exists(p4, 'client', client_name): raise P4.P4Exception('No such client "{}" defined'.format(client_name)) view_name = client_name[len(p4gf_const.P4GF_CLIENT_PREFIX):] client = p4.fetch_client() command_path = client["Root"] if not args.convert: print(" Removing client files for {}...".format(client_name)) print(" p4 sync -fqk {}/...#none".format(command_path)) print(" Create config for client {}...".format(client_name)) p4.client = old_client for group_template in group_list: group = group_template.format(view=view_name) print(" Leaving existing group {}".format(group)) print(" Remove client lock counter") print(" p4 counter -u -d {}".format( p4gf_lock.view_lock_name(view_name))) else: LOG_FILE.write("Processing client {}\n".format(client_name)) print(" Removing client files for {}...".format(client_name)) print(" p4 sync -fqk {}/...#none".format(command_path)) LOG_FILE.write("p4 sync -fqk {}/...#none\n".format(command_path)) p4.run('sync', '-fqk', command_path + '/...#none') print(" Creating config for client {}...".format(client_name)) LOG_FILE.write( "Creating config for client {}...\n".format(client_name)) p4.client = old_client create_from_12x_gf_client_name(p4, client_name) for group_template in group_list: group = group_template.format(view=view_name) print(" Leaving existing group {}".format(group)) print(" Remove client lock counter") print(" p4 counter -u -d {}".format( p4gf_lock.view_lock_name(view_name))) LOG_FILE.write("p4 counter -u -d {}\n".format( p4gf_lock.view_lock_name(view_name))) _delete_counter(p4, p4gf_lock.view_lock_name(view_name))
def connectToPerforce(self, retry=3, show_message=False): # if ( # not hasattr(self, "p4") # or not hasattr(self, "sgPrjId") # or (user and not hasattr(self, "sgUserId")) # ): is_connected_p4 = False try: self.p4.disconnect() except: pass try: self.p4.port = self.core.getConfig("perforce", "port", configPath=self.core.prismIni) self.p4.user = self.core.getConfig("perforce", "p4username") self.p4.client = self.core.getConfig("perforce", "p4userworkspacename") self.p4.password = self.core.getConfig("perforce", "p4userpassword") except AttributeError as why: logger.error(why) return try: self.p4.connect() except P4.P4Exception as why: raise P4.P4Exception("Failed to connect to p4. {}".format(why)) try: self.p4.run_login('-s') is_connected_p4 = True except P4.P4Exception as why: try: self.p4.run_login() is_connected_p4 = True except P4.P4Exception as why: if show_message and retry == 0: QMessageBox.critical( self.core.messageParent, "Perforce Error", "Failed to login to p4. {}".format(why)) while not is_connected_p4 and retry != 0: time.sleep(1) is_connected_p4 = self.connectToPerforce(retry - 1) if show_message and is_connected_p4: QMessageBox.information( self.core.messageParent, "Perforce", "Successfully connect to {} with user {}".format( self.p4.port, self.p4.user)) if hasattr(self, "connectStatusWidget"): self.connectStatusWidget = is_connected_p4 return is_connected_p4
def delete_client(args, p4, client_name): """Delete the named Perforce client and its workspace. Raises P4Exception if the client is not present, or the client configuration is not set up as expected. Keyword arguments: args -- parsed command line arguments p4 -- Git user's Perforce client client_name -- name of client to be deleted """ group_list = [ p4gf_const.P4GF_GROUP_VIEW_PULL, p4gf_const.P4GF_GROUP_VIEW_PUSH ] p4.user = p4gf_const.P4GF_USER print_verbose(args, "Checking for client {}...".format(client_name)) if not p4gf_util.spec_exists(p4, 'client', client_name): raise P4.P4Exception('No such client "{}" defined'.format(client_name)) view_name = client_name[len(p4gf_const.P4GF_CLIENT_PREFIX):] view_lock = None # We're clobbering and deleting. Overrule locks. try: ctx = p4gf_context.create_context(view_name, view_lock) except RuntimeError: # not a conforming Git Fusion client, ignore it return command_path = ctx.client_view_path() p4gf_dir = p4gf_util.p4_to_p4gf_dir(p4) view_dirs = p4gf_view_dirs.from_p4gf_dir(p4gf_dir, view_name) rm_list = [view_dirs.view_container] homedir = os.path.expanduser('~') raise_if_homedir(homedir, view_name, rm_list) # Scan for objects associated only with this view so we can either remove # them completely or update their 'views' attribute appropriately. p4.handler = FilterViewFstatHandler(view_name) p4.run("fstat", "-Oa", "-T", "depotFile, attr-views", "//.git-fusion/objects/...") objects_to_delete = p4.handler.files_to_delete objects_to_modify = p4.handler.files_to_modify p4.handler = None if not args.delete: print("p4 sync -f {}#none".format(command_path)) print("p4 client -f -d {}".format(client_name)) for d in rm_list: print("rm -rf {}".format(d)) for to_delete in objects_to_delete: print("p4 obliterate -y {}".format(to_delete)) if objects_to_modify: for (fname, views) in objects_to_modify: print("attribute -p -n views -v {} {}".format(views, fname)) for group_template in group_list: group = group_template.format(view=view_name) print("p4 group -a -d {}".format(group)) print('p4 counter -u -d {}'.format( p4gf_lock.view_lock_name(view_name))) else: print_verbose(args, "Removing client files for {}...".format(client_name)) ctx.p4.run('sync', '-fq', command_path + '#none') print_verbose(args, "Deleting client {}...".format(client_name)) p4.run('client', '-df', client_name) for d in rm_list: remove_file_or_dir(args, view_name, d) bite_size = 1000 while len(objects_to_delete): to_delete = objects_to_delete[:bite_size] objects_to_delete = objects_to_delete[bite_size:] p4.run("obliterate", "-y", to_delete) if objects_to_modify: for (fname, views) in objects_to_modify: p4.run("edit", fname) p4.run("attribute", "-p", "-n", "views", "-v", views, fname) p4.run("submit", "-d", "'Removing {} from views attribute'".format(view_name)) for group_template in group_list: delete_group(args, p4, group_template.format(view=view_name)) _delete_counter(p4, p4gf_lock.view_lock_name(view_name))
def getOrThrow(self, name): setting = self._settings.get(name) if setting is None: raise P4.P4Exception("Subforce: You must set the {} setting!".format(name)) return setting
def delete_client(args, p4, client_name, metrics, prune_objs=True): """Delete the named Perforce client and its workspace. Raises P4Exception if the client is not present, or the client configuration is not set up as expected. Keyword arguments: args -- parsed command line arguments p4 -- Git user's Perforce client client_name -- name of client to be deleted metrics -- DeletionMetrics for collecting resulting metrics prune_objs -- if True, delete associated objects from cache """ # pylint: disable=R0912,R0915 group_list = [ p4gf_const.P4GF_GROUP_VIEW_PULL, p4gf_const.P4GF_GROUP_VIEW_PUSH ] p4.user = p4gf_const.P4GF_USER print_verbose(args, _("Checking for client '{}'...").format(client_name)) if not p4gf_util.spec_exists(p4, 'client', client_name): raise P4.P4Exception( _("No such client '{}' defined").format(client_name)) view_name = p4gf_util.client_to_view_name(client_name) p4gf_dir = p4gf_util.p4_to_p4gf_dir(p4) view_dirs = p4gf_view_dirs.from_p4gf_dir(p4gf_dir, view_name) p4gf_util.ensure_spec_values(p4, 'client', client_name, {'Root': view_dirs.p4root}) view_lock = None # We're clobbering and deleting. Overrule locks. with p4gf_context.create_context(view_name, view_lock) as ctx: command_path = ctx.client_view_path() homedir = os.path.expanduser('~') raise_if_homedir(homedir, view_name, view_dirs.view_container) # Scan for objects associated only with this view so we can remove them. objects_to_delete = [] if prune_objs: objects_to_delete = _find_client_commit_objects( args, p4, view_name) # Do we have a repo config file to delete? config_file = p4gf_config.depot_path_repo(view_name) + '*' config_file_exists = p4gf_util.depot_file_exists(p4, config_file) # What counters shall we delete? counter_list = [] counter_list.append( p4gf_context.calc_last_copied_change_counter_name( view_name, p4gf_util.get_server_id())) for spec in p4.run('counters', '-u', '-e', "git-fusion-index-last-{},*".format(view_name)): counter_list.append(spec['counter']) for spec in p4.run('counters', '-u', '-e', "git-fusion-index-branch-{},*".format(view_name)): counter_list.append(spec['counter']) if not args.delete: print(NTR('p4 sync -f {}#none').format(command_path)) print(NTR('p4 client -f -d {}').format(client_name)) print(NTR('rm -rf {}').format(view_dirs.view_container)) print( NTR('Deleting {} objects from //{}/objects/...').format( len(objects_to_delete), p4gf_const.P4GF_DEPOT)) for group_template in group_list: group = group_template.format(view=view_name) print(NTR('p4 group -a -d {}').format(group)) for c in counter_list: print(NTR('p4 counter -u -d {}').format(c)) if config_file_exists: print(NTR('p4 sync -f {}').format(config_file)) print(NTR('p4 delete {}').format(config_file)) print( NTR('p4 submit -d "Delete repo config for {view_name}" {config_file}' ).format(view_name=view_name, config_file=config_file)) else: print_verbose( args, NTR('Removing client files for {}...').format(client_name)) ctx.p4.run('sync', '-fq', command_path + '#none') print_verbose(args, NTR('Deleting client {}...').format(client_name)) p4.run('client', '-df', client_name) metrics.clients += 1 print_verbose( args, NTR("Deleting repo {0}'s directory {1}...").format( view_name, view_dirs.view_container)) _remove_tree(view_dirs.view_container, contents_only=False) metrics.files += _delete_files(p4, objects_to_delete, view_name) for group_template in group_list: _delete_group(args, p4, group_template.format(view=view_name), metrics) for c in counter_list: _delete_counter(p4, c, metrics) if config_file_exists: p4gf_util.p4run_logged(p4, ['sync', '-fq', config_file]) with p4gf_util.NumberedChangelist( p4=p4, description=_("Delete repo config for '{}'").format( view_name)) as nc: nc.p4run(["delete", config_file]) nc.submit()