1+ """Update the rattler-build recipe.yaml with the current git remote and branch."""
2+
13import sys
24import os
35import subprocess
46
5- # Get the name of the script.
67script = os .path .abspath (sys .argv [0 ])
78
8- # we want to import the 'get_requirements' package from this directory
9- sys .path .insert (0 , os .path .dirname (script ))
10-
11- # go up one directories to get the source directory
12- # (this script is in BioSimSpace/actions/)
9+ # Go up one directory to get the source directory.
1310srcdir = os .path .dirname (os .path .dirname (script ))
1411
1512condadir = os .path .join (srcdir , "recipes" , "ghostly" )
1613
17- print (f"conda recipe in { condadir } " )
14+ print (f"Recipe in { condadir } " )
1815
19- # Store the name of the recipe and template YAML files.
20- recipe = os .path .join (condadir , "meta.yaml" )
21- template = os .path .join (condadir , "template.yaml" )
16+ recipe = os .path .join (condadir , "recipe.yaml" )
2217
2318gitdir = os .path .join (srcdir , ".git" )
2419
@@ -32,19 +27,32 @@ def run_cmd(cmd):
3227remote = run_cmd (
3328 f"git --git-dir={ gitdir } --work-tree={ srcdir } config --get remote.origin.url"
3429)
35- print (remote )
30+ if not remote .endswith (".git" ):
31+ remote += ".git"
32+ print (f"Remote: { remote } " )
3633
3734# Get the branch.
3835branch = run_cmd (
3936 f"git --git-dir={ gitdir } --work-tree={ srcdir } rev-parse --abbrev-ref HEAD"
4037)
41- print (branch )
4238
43- lines = open (template , "r" ).readlines ()
39+ # Handle detached HEAD (e.g. in GitHub Actions PR checkouts).
40+ if branch == "HEAD" :
41+ # GITHUB_HEAD_REF is set for pull_request events.
42+ # GITHUB_REF_NAME is set for push/workflow_dispatch events.
43+ branch = os .environ .get ("GITHUB_HEAD_REF" ) or os .environ .get (
44+ "GITHUB_REF_NAME" , "HEAD"
45+ )
46+ print (f"Branch: { branch } " )
47+
48+ # Read and update the recipe.
49+ with open (recipe , "r" ) as f :
50+ content = f .read ()
51+
52+ content = content .replace ("GHOSTLY_REMOTE" , remote )
53+ content = content .replace ("GHOSTLY_BRANCH" , branch )
4454
45- with open (recipe , "w" ) as FILE :
46- for line in lines :
47- line = line .replace ("GHOSTLY_REMOTE" , remote )
48- line = line .replace ("GHOSTLY_BRANCH" , branch )
55+ with open (recipe , "w" ) as f :
56+ f .write (content )
4957
50- FILE . write ( line )
58+ print ( f"Recipe updated: { recipe } " )
0 commit comments