@fiss_cmd<EOL>def mop(args):
<EOL>if args.verbose:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>r = fapi.get_workspace(args.project, args.workspace)<EOL>fapi._check_response_code(r, <NUM_LIT:200>)<EOL>workspace = r.json()<EOL>bucket = workspace['<STR_LIT>']['<STR_LIT>']<EOL>bucket_prefix = '<STR_LIT>' + bucket<EOL>workspace_name = workspace['<STR_LIT>']['<STR_LIT:name>']<EOL>if args.verbose:<EOL><INDENT>print("<STR_LIT>".format(workspace_name, bucket_prefix))<EOL><DEDENT>referenced_files = set()<EOL>for value in workspace['<STR_LIT>']['<STR_LIT>'].values():<EOL><INDENT>if isinstance(value, string_types) and value.startswith(bucket_prefix):<EOL><INDENT>referenced_files.add(value)<EOL><DEDENT><DEDENT>try:<EOL><INDENT>gsutil_args = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>' + bucket + '<STR_LIT>']<EOL>if args.verbose:<EOL><INDENT>print('<STR_LIT:U+0020>'.join(gsutil_args))<EOL><DEDENT>bucket_files = subprocess.check_output(gsutil_args, stderr=subprocess.PIPE)<EOL>if type(bucket_files) == bytes:<EOL><INDENT>bucket_files = bucket_files.decode()<EOL><DEDENT><DEDENT>except subprocess.CalledProcessError as e:<EOL><INDENT>eprint("<STR_LIT>" + str(e))<EOL>return <NUM_LIT:1><EOL><DEDENT>bucket_files = set(bucket_files.strip().split('<STR_LIT:\n>'))<EOL>if args.verbose:<EOL><INDENT>num = len(bucket_files)<EOL>if args.verbose:<EOL><INDENT>print("<STR_LIT>".format(num, bucket))<EOL><DEDENT><DEDENT>r = fapi.list_entity_types(args.project, args.workspace)<EOL>fapi._check_response_code(r, <NUM_LIT:200>)<EOL>entity_types = r.json().keys()<EOL>for etype in entity_types:<EOL><INDENT>if args.verbose:<EOL><INDENT>print("<STR_LIT>" + etype + "<STR_LIT>")<EOL><DEDENT>entities = _entity_paginator(args.project, args.workspace, etype,<EOL>page_size=<NUM_LIT:1000>, filter_terms=None,<EOL>sort_direction="<STR_LIT>")<EOL>for entity in entities:<EOL><INDENT>for value in entity['<STR_LIT>'].values():<EOL><INDENT>if isinstance(value, string_types) and value.startswith(bucket_prefix):<EOL><INDENT>referenced_files.add(value)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if args.verbose:<EOL><INDENT>num = len(referenced_files)<EOL>print("<STR_LIT>".format(num, workspace_name))<EOL><DEDENT>unreferenced_files = bucket_files - referenced_files<EOL>def can_delete(f):<EOL><INDENT>'''<STR_LIT>'''<EOL>if f.endswith('<STR_LIT>'):<EOL><INDENT>return False<EOL><DEDENT>if f.endswith('<STR_LIT>'):<EOL><INDENT>return False<EOL><DEDENT>if f.endswith('<STR_LIT>'):<EOL><INDENT>return False<EOL><DEDENT>return True<EOL><DEDENT>deleteable_files = [f for f in unreferenced_files if can_delete(f)]<EOL>if len(deleteable_files) == <NUM_LIT:0>:<EOL><INDENT>if args.verbose:<EOL><INDENT>print("<STR_LIT>" + workspace['<STR_LIT>']['<STR_LIT:name>'])<EOL><DEDENT>return <NUM_LIT:0><EOL><DEDENT>if args.verbose or args.dry_run:<EOL><INDENT>print("<STR_LIT>".format(len(deleteable_files))<EOL>+ "<STR_LIT:\n>".join(deleteable_files ) + '<STR_LIT:\n>')<EOL><DEDENT>message = "<STR_LIT>".format(<EOL>len(deleteable_files), bucket_prefix, workspace['<STR_LIT>']['<STR_LIT:name>'])<EOL>if args.dry_run or (not args.yes and not _confirm_prompt(message)):<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>gsrm_args = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>PIPE = subprocess.PIPE<EOL>STDOUT=subprocess.STDOUT<EOL>if args.verbose:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>gsrm_proc = subprocess.Popen(gsrm_args, stdin=PIPE, stdout=PIPE, stderr=STDOUT)<EOL>result = gsrm_proc.communicate(input='<STR_LIT:\n>'.join(deleteable_files).encode())[<NUM_LIT:0>]<EOL>if args.verbose:<EOL><INDENT>if type(result) == bytes:<EOL><INDENT>result = result.decode()<EOL><DEDENT>print(result.rstrip())<EOL><DEDENT>return <NUM_LIT:0><EOL>
Clean up unreferenced data in a workspace