11
11
import gzip
12
12
from glob import glob
13
13
import sys
14
+ import requests
14
15
15
16
def main ():
16
17
parser = argparse .ArgumentParser (
@@ -131,7 +132,7 @@ def process_docker(datasets):
131
132
datasets_to_build .extend (dataset_map .get (dataset , []))
132
133
133
134
# Build the docker-compose command, adding specific datasets
134
- compose_command = ['docker- compose' , '-f' , compose_file , 'build' , '--parallel' ] + datasets_to_build
135
+ compose_command = ['docker' , ' compose' , '-f' , compose_file , 'build' , '--parallel' ] + datasets_to_build
135
136
136
137
log_file_path = 'local/docker.log'
137
138
env = os .environ .copy ()
@@ -266,9 +267,11 @@ def run_docker_upload_cmd(cmd_arr, all_files_dir, name, version):
266
267
docker_run = ['docker' , 'run' , '--rm' , '-v' , f"{ env ['PWD' ]} /local/{ all_files_dir } :/tmp" , '-e' , f"VERSION={ version } " ]
267
268
268
269
# Add Appropriate Environment Variables
270
+ if name == "validate" :
271
+ docker_run .extend (['upload' ])
269
272
if 'FIGSHARE_TOKEN' in env and name == 'Figshare' :
270
273
docker_run .extend (['-e' , f"FIGSHARE_TOKEN={ env ['FIGSHARE_TOKEN' ]} " , 'upload' ])
271
- if name == "validate " :
274
+ if name == "Map_Drugs" or name == "Map_Samples " :
272
275
docker_run .extend (['upload' ])
273
276
if 'GITHUB_TOKEN' in env and name == "GitHub" :
274
277
docker_run .extend (['-e' , f"GITHUB_TOKEN={ env ['GITHUB_TOKEN' ]} " , 'upload' ])
@@ -300,6 +303,18 @@ def compress_file(file_path):
300
303
with gzip .open (compressed_file_path , 'wb' ) as f_out :
301
304
shutil .copyfileobj (f_in , f_out )
302
305
os .remove (file_path )
306
+
307
+ def get_latest_commit_hash (owner , repo , branch = 'main' ):
308
+ """
309
+ Returns the SHA of the latest commit on the specified branch.
310
+ """
311
+ url = f"https://api.github.com/repos/{ owner } /{ repo } /commits/{ branch } "
312
+ response = requests .get (url )
313
+ response .raise_for_status ()
314
+
315
+ # The commit data is in JSON format; the 'sha' field is the full commit hash.
316
+ commit_data = response .json ()
317
+ return commit_data ['sha' ]
303
318
304
319
######
305
320
### Pre-Build Environment Token Check
@@ -388,17 +403,17 @@ def compress_file(file_path):
388
403
######
389
404
### Begin Upload and/or validation
390
405
#####
391
-
392
- if args .figshare or args .validate :
406
+ if args . figshare or args . validate or github_token :
407
+ # if args.figshare or args.validate:
393
408
# FigShare File Prefixes:
409
+
394
410
prefixes = ['beataml' , 'hcmi' , 'cptac' , 'mpnst' , 'genes' , 'drugs' ]
395
411
broad_sanger_datasets = ["ccle" ,"ctrpv2" ,"fimm" ,"gdscv1" ,"gdscv2" ,"gcsi" ,"prism" ,"nci60" ]
396
412
if "broad_sanger" in datasets :
397
413
prefixes .extend (broad_sanger_datasets )
398
414
datasets .extend (broad_sanger_datasets )
399
415
datasets .remove ("broad_sanger" )
400
416
401
-
402
417
figshare_token = os .getenv ('FIGSHARE_TOKEN' )
403
418
404
419
all_files_dir = 'local/all_files_dir'
@@ -422,6 +437,13 @@ def compress_file(file_path):
422
437
for file in glob (os .path .join (all_files_dir , '*.gz' )):
423
438
decompress_file (file )
424
439
440
+ ### These should be done before schema checking.
441
+ sample_mapping_command = ['python3' , 'scripts/map_improve_sample_ids.py' , '--local_dir' , "/tmp" , '--version' , args .version ]
442
+ run_docker_upload_cmd (sample_mapping_command , 'all_files_dir' , 'Map_Samples' , args .version )
443
+
444
+ drug_mapping_command = ['python3' , 'scripts/map_improve_drug_ids.py' , '--local_dir' , "/tmp" , '--version' , args .version ]
445
+ run_docker_upload_cmd (drug_mapping_command , 'all_files_dir' , 'Map_Drugs' , args .version )
446
+
425
447
# Run schema checker - This will always run if uploading data.
426
448
schema_check_command = ['python3' , 'scripts/check_schema.py' , '--datasets' ] + datasets
427
449
run_docker_upload_cmd (schema_check_command , 'all_files_dir' , 'validate' , args .version )
@@ -438,28 +460,47 @@ def compress_file(file_path):
438
460
439
461
print ("File compression and decompression adjustments are complete." )
440
462
441
- # Upload to Figshare using Docker
463
+ ## # Upload to Figshare using Docker
442
464
if args .figshare and args .version and figshare_token :
443
- figshare_command = ['python3' , 'scripts/push_to_figshare.py' , '--directory' , "/tmp" , '--title' , f"CODERData{ args .version } " , '--token' , os .getenv ('FIGSHARE_TOKEN' ), '--project_id' , '189342' , '--publish' ]
465
+ figshare_command = ['python3' , 'scripts/push_to_figshare.py' , '--directory' , "/tmp" , '--title' , f"CODERData{ args .version } " , '--token' , os .getenv ('FIGSHARE_TOKEN' ), '--project_id' , '189342' , '--version' , args . version , '-- publish' ]
444
466
run_docker_upload_cmd (figshare_command , 'all_files_dir' , 'Figshare' , args .version )
445
467
468
+ ### Push changes to GitHub using Docker
469
+ # if args.version and args.figshare and figshare_token and github_token and args.github_username and args.github_email:
446
470
447
- # Push changes to GitHub using Docker
448
- if args .version and args .figshare and figshare_token and github_token and args .github_username and args .github_email :
449
- git_command = [
450
- 'bash' , '-c' , (
451
- f'git config --global user.name "{ args .github_username } " '
452
- f'&& git config --global user.email "{ args .github_email } " '
453
- f'&& cp /tmp/figshare_latest.yml /usr/src/app/coderdata/docs/_data/figshare_latest.yml '
454
- f'&& git add docs/_data/figshare_latest.yml '
455
- f'&& git commit -m "Data Built and Uploaded. New Tag: { args .version } " '
456
- f'&& git tag { args .version } '
457
- f'&& git push https://{ args .github_username } :{ github_token } @github.com/PNNL-CompBio/coderdata.git main '
458
- f'&& git push https://{ args .github_username } :{ github_token } @github.com/PNNL-CompBio/coderdata.git --tags'
459
- )
460
- ]
461
- run_docker_upload_cmd (git_command , 'all_files_dir' , 'GitHub' , args .version )
471
+ # You can only upload to Github after Figshare upload is completed - otherwise figshare_latest.yml and dataset.yml won't be available.
472
+ if args .version and github_token and args .github_username and args .github_email :
473
+
474
+ git_command = [
475
+ 'bash' , '-c' , (
476
+ f'git config --global user.name "{ args .github_username } " '
477
+ f'&& git config --global user.email "{ args .github_email } " '
478
+
479
+ # Checkout a new branch
480
+ f'&& git checkout -b testing-auto-build-pr-{ args .version } '
481
+
482
+ # Copy and add the necessary files
483
+ f'&& cp /tmp/improve_sample_mapping.json.gz /usr/src/app/coderdata/build/improve_sample_mapping.json.gz '
484
+ f'&& cp /tmp/improve_drug_mapping.json.gz /usr/src/app/coderdata/build/improve_drug_mapping.json.gz '
485
+ f'&& gunzip /usr/src/app/coderdata/build/*.gz '
486
+ f'&& git add -f build/improve_sample_mapping.json build/improve_drug_mapping.json '
487
+ f'&& cp /tmp/figshare_latest.yml /usr/src/app/coderdata/docs/_data/figshare_latest.yml '
488
+ f'&& cp /tmp/dataset.yml /usr/src/app/coderdata/coderdata/dataset.yml '
489
+ f'&& git add -f docs/_data/figshare_latest.yml coderdata/dataset.yml'
490
+
491
+ # Tag and push
492
+ f'&& git commit -m "Data Built and Uploaded. New Tag: { args .version } " '
493
+ f'&& git tag { args .version } '
494
+ f'&& git push https://{ args .github_username } :{ github_token } @github.com/PNNL-CompBio/coderdata.git testing-auto-build-pr-{ args .version } '
495
+
496
+ # Create a PR using GitHub CLI
497
+ f'&& gh pr create --title "Testing Auto PR instead of auto Merge { args .version } " '
498
+ f'--body "This PR was automatically generated by the build process." '
499
+ f'--base main --head testing-auto-build-pr-{ args .version } '
500
+ )
501
+ ]
502
+
503
+ run_docker_upload_cmd (git_command , 'all_files_dir' , 'GitHub' , args .version )
462
504
463
-
464
505
if __name__ == '__main__' :
465
506
main ()
0 commit comments