@@ -1025,9 +1025,9 @@ def delete_project(self, project_id, root_node="project", chunk_size=200, nuke_p
10251025 try :
10261026 nodes .remove ("project" )
10271027 except :
1028- print ("No 'project' node in list of nodes." )
1028+ print ("\n \n No 'project' node in list of nodes." )
10291029 for node in nodes :
1030- print ("\n Deleting node '{}' from project '{}'." .format (node , project_id ))
1030+ print ("\n \t Deleting node '{}' from project '{}'." .format (node , project_id ))
10311031 # data = self.delete_node(
10321032 # node=node, project_id=project_id, chunk_size=chunk_size
10331033 # )
@@ -1041,8 +1041,9 @@ def delete_project(self, project_id, root_node="project", chunk_size=200, nuke_p
10411041 print ("{}" .format (data ))
10421042 else :
10431043 print ("Successfully deleted the project '{}'" .format (project_id ))
1044+ self .nuked ()
10441045 else :
1045- print ("Successfully deleted all nodes in the project '{}'.\n If you'd like to delete thr project node itself, then add the flag 'nuke_project=True'." .format (project_id ))
1046+ print ("\n \n Successfully deleted all nodes in the project '{}'.\n If you'd like to delete the project node itself, then add the flag 'nuke_project=True'." .format (project_id ))
10461047
10471048
10481049 # Analysis Functions
@@ -2683,7 +2684,7 @@ def query_indexd(self, limit=100, page=0, uploader=None, args=None):
26832684
26842685 return records
26852686
2686- def get_indexd (self , limit = 1000 , page = 0 , format = "TSV " , uploader = None , args = None ):
2687+ def get_indexd (self , limit = 1000 , page = 0 , format = "JSON " , uploader = None , args = None ):
26872688 """get all the records in indexd
26882689 api = "https://icgc.bionimbus.org/"
26892690 args = lambda: None
@@ -2755,6 +2756,60 @@ def get_indexd(self, limit=1000, page=0, format="TSV", uploader=None, args=None)
27552756
27562757 return all_records
27572758
2759+ def delete_indexd_records (self ,irecs ):
2760+ """
2761+ Arguments:
2762+ irecs(list): A list of indexd records. Get with, e.g., function:
2763+ Gen3Expansion.get_indexd(uploader="[email protected] ") 2764+ """
2765+ total ,count = len (irecs ),0
2766+ success ,failure = [],[]
2767+ for irec in irecs :
2768+ count += 1
2769+ guid = irec ['did' ]
2770+ rev = irec ['rev' ]
2771+ index_url = "{}/index/index/{}?rev={}" .format (self ._endpoint ,guid ,rev )
2772+ access_token = self .get_token ()
2773+ headers = {
2774+ 'Content-Type' : 'application/json' ,
2775+ 'Authorization' : 'bearer {}' .format (access_token )
2776+ }
2777+ response = requests .delete (index_url , headers = headers )
2778+ if response .status_code == 200 :
2779+ success .append (guid )
2780+ print ("{}/{} {}: Successfully deleted '{}'." .format (count ,total ,response .status_code ,guid ))
2781+ else :
2782+ failure .append (guid )
2783+ print ("{}/{} {}: Failed to delete '{}'." .format (count ,total ,response .status_code ,guid ))
2784+ return {'success' :success ,'failure' :failure }
2785+
2786+
2787+ def remove_uploader_from_indexd (self , irecs ):
2788+ """
2789+ Arguments:
2790+ irecs(list): A list of indexd records. Get with, e.g., function:
2791+ Gen3Expansion.get_indexd(uploader="[email protected] ") 2792+ """
2793+ total ,count = len (irecs ),0
2794+ success ,failure = [],[]
2795+ for irec in irecs :
2796+ count += 1
2797+ guid = irec ['did' ]
2798+ rev = irec ['rev' ]
2799+ payload = {'uploader' :None }
2800+ index_url = "{}/index/index/{}?rev={}" .format (self ._endpoint ,guid ,rev )
2801+ access_token = self .get_token ()
2802+ headers = {
2803+ 'Content-Type' : 'application/json' ,
2804+ 'Authorization' : 'bearer {}' .format (access_token )
2805+ }
2806+ response = requests .put (index_url , headers = headers , json = payload )
2807+ if response .status_code == 200 :
2808+ success .append (guid )
2809+ else :
2810+ failure .append (guid )
2811+ print ("{}/{} {}: {}" .format (count ,total ,response .status_code ,response .text .encode ('utf8' )))
2812+ return {'success' :success ,'failure' :failure }
27582813
27592814 def get_urls (self , guids ):
27602815 # Get URLs for a list of GUIDs
@@ -3064,8 +3119,10 @@ def delete_uploaded_files(self, guids):
30643119 if not isinstance (guids , list ):
30653120 raise Gen3Error ("Please, supply GUIDs as a list." )
30663121
3122+ count ,total = 0 ,len (guids )
3123+ deleted ,failed = [],[]
30673124 for guid in guids :
3068-
3125+ count += 1
30693126 fence_url = "{}user/data/" .format (self ._endpoint )
30703127
30713128 try :
@@ -3074,10 +3131,13 @@ def delete_uploaded_files(self, guids):
30743131 raise Gen3Error (e )
30753132
30763133 if response .status_code == 204 :
3077- print ("Successfully deleted GUID {}" .format (guid ))
3134+ print ("({}/{}) Successfully deleted GUID {}" .format (count ,total ,guid ))
3135+ deleted .append (guid )
30783136 else :
3079- print ("Error deleting GUID {}:" .format (guid ))
3137+ print ("({}/{}) Error deleting GUID {}:" .format (count , total , guid ))
30803138 print (response .reason )
3139+ failed .append (guid )
3140+ return ({'deleted' :deleted ,'failed' :failed })
30813141
30823142
30833143 # Data commons summary functions
@@ -5070,7 +5130,9 @@ def create_mock_tsv(self,
50705130 print ("File property '{}' is missing from mfiles! \n \t {}" .format (file_prop ,list (mfiles )))
50715131
50725132 for prop in props :
5073- if prop == 'md5sum' :
5133+ if prop == 'file_name' :
5134+ data ['file_name' ] = [sid + ".mock_filename.txt" for sid in data ['submitter_id' ]]
5135+ elif prop == 'md5sum' :
50745136 md5s = []
50755137 for i in range (count ):
50765138 md5 = str (hashlib .md5 (b"test" ).hexdigest ())
@@ -5100,12 +5162,12 @@ def create_mock_tsv(self,
51005162 elif array_type == "integer" :
51015163 array_list = []
51025164 for i in range (count ):
5103- array_list .append ("," .join (map (str ,list (np .random .randint (low = 0 , high = 10 , size = (2 ))))))
5165+ array_list .append ("," .join (map (str ,list (np .random .randint (low = 1 , high = 89 , size = (2 ))))))
51045166 data [prop ] = array_list
51055167 elif array_type == "number" :
51065168 array_list = []
51075169 for i in range (count ):
5108- one_array = list (np .random .uniform (low = 0 , high = 10 , size = (2 )))
5170+ one_array = list (np .random .uniform (low = 1 , high = 89 , size = (2 )))
51095171 formatted_array = [ '%.2f' % elem for elem in one_array ]
51105172 array_list .append ("," .join (map (str ,formatted_array )))
51115173 data [prop ] = array_list
@@ -5117,9 +5179,9 @@ def create_mock_tsv(self,
51175179 available_types = cycle ([True ,False ])
51185180 data [prop ] = [next (available_types )for i in range (count )]
51195181 elif prop_type == "integer" :
5120- data [prop ] = list (np .random .randint (low = 0 , high = 10 , size = (count )))
5182+ data [prop ] = list (np .random .randint (low = 1 , high = 89 , size = (count )))
51215183 elif prop_type == "number" :
5122- data [prop ] = [ '%.2f' % elem for elem in list (np .random .uniform (low = 0 , high = 10 , size = count ))]
5184+ data [prop ] = [ '%.2f' % elem for elem in list (np .random .uniform (low = 1 , high = 89 , size = count ))]
51235185
51245186 elif 'enum' in dd [node ]['properties' ][prop ]:
51255187 enums = dd [node ]['properties' ][prop ]['enum' ]
@@ -5128,8 +5190,6 @@ def create_mock_tsv(self,
51285190 #available_enums = cycle(enum_values)
51295191 data [prop ] = [next (available_enums )for i in range (count )]
51305192
5131-
5132-
51335193 # create a dataframe and save as a TSV
51345194 df = pd .DataFrame (data )
51355195
@@ -5245,20 +5305,54 @@ def create_mock_project(self,
52455305 object_ids = []
52465306 for i in range (len (df )):
52475307 file_name = list (df ['file_name' ])[i ]
5308+ size = list (df ['file_size' ])[i ]
5309+ md5 = list (df ['md5sum' ])[i ]
52485310 try :
5249- irec = self .create_blank_indexd_record (
5250- 5251- file_name = file_name
5252- )
5253- if 'did' in irec :
5254- object_ids .append (irec ['did' ])
5255- else :
5256- print ("No object_id in indexd response:\n \t {}" .format (irec ))
5311+ irec = self .create_mock_indexd_record (
5312+ file_name = file_name ,
5313+ md5 = md5 ,
5314+ size = size ,
5315+ project_id = project_id )
52575316 except :
5258- print ("Couldn't create the indexd record for file:\n \t {}." .format (file_name ))
5317+ print ("Couldn't create the indexd record for file {}:\n \t {}" .format (file_name ,irec ))
5318+ object_id = irec ['did' ]
5319+ object_ids .append (object_id )
52595320 df ['object_id' ] = object_ids
52605321 d = self .submit_df (project_id = project_id , df = df , chunk_size = 250 )
52615322
5323+
5324+ def create_mock_indexd_record (self ,
5325+ file_name ,
5326+ md5 ,
5327+ size ,
5328+ project_id = "DEV-test" ,
5329+ 5330+ """
5331+ Create a blank indexd record}
5332+ """
5333+ prog ,proj = project_id .split ("-" ,1 )
5334+ iurl = "{}index/index" .format (self ._endpoint )
5335+ payload = {'form' : 'object' ,
5336+ 'file_name' :file_name ,
5337+ 'hashes' :{'md5' :md5 },
5338+ 'size' :size ,
5339+ 'authz' :["/programs/{}/projects/{}" .format (prog ,proj )],
5340+ 'acl' :[prog ,proj ],
5341+ 'urls' :['s3://mock/bucket/{}' .format (file_name )],
5342+ #'uploader':uploader
5343+ }
5344+ try :
5345+ res = requests .post (
5346+ iurl ,
5347+ headers = {"content-type" : "application/json" },
5348+ auth = self ._auth_provider ,
5349+ data = json .dumps (payload ),
5350+ )
5351+ except :
5352+ print ("\n \t Error creating indexd record:\n {}\n {}\n " .format (res ,res .text ))
5353+ data = res .json ()
5354+ return data
5355+
52625356 def create_blank_indexd_record (
self ,
uploader = "[email protected] " ,
file_name = None ):
52635357 """
52645358 Create a blank indexd record}
@@ -5272,9 +5366,31 @@ def create_blank_indexd_record(self, uploader="
[email protected] ", file_name=
52725366 data = json .dumps (payload ),
52735367 )
52745368 try :
5275- irec = res .json ()
5276- object_id = irec ['did' ]
5277- return object_id
5369+ data = res .json ()
5370+ return data
52785371 except :
5279- print (res )
5280- return res
5372+ print ("\n \t No json in indexd response:\n {}\n {}\n " .format (res ,res .text ))
5373+ return res .text
5374+
5375+
5376+
5377+
5378+ def nuked (self ,message = "Deleted!" ):
5379+ mushroom_cloud1 = """
5380+ _.-^^---....,,--
5381+ _-- --_
5382+ < >)
5383+ """
5384+ mushroom_cloud2 = """
5385+ | |
5386+ \._ _./
5387+ ```--. . , ; .--'''
5388+ | | |
5389+ .-=|| | |=-.
5390+ `-=#$%&%$#=-'
5391+ | ; :|
5392+ _____.,-#%&$@%#&#~,._____
5393+ """
5394+ print (mushroom_cloud1 )
5395+ print ("\t \t {}" .format (message ))
5396+ print (mushroom_cloud2 )
0 commit comments