2222import os
2323import platform
2424import re
25+ import shutil
2526import sys
2627import tarfile
2728import urllib .error
@@ -426,6 +427,9 @@ def install_version(
426427
427428
428429def is_version_available (version : str ) -> bool :
430+ if 'git:' in version :
431+ return True # no good way in general to check if a git tag exists
432+
429433 is_available = True
430434 url = get_download_url (version )
431435 for i in range (6 ):
@@ -455,6 +459,27 @@ def retrieve_version(version: str, progress: bool = True) -> None:
455459 """Download specified CmdStan version."""
456460 if version is None or version == '' :
457461 raise ValueError ('Argument "version" unspecified.' )
462+
463+ if 'git:' in version :
464+ tag = version .split (':' )[1 ]
465+ tag_folder = version .replace (':' , '-' ).replace ('/' , '_' )
466+ print (f"Cloning CmdStan branch '{ tag } ' from stan-dev/cmdstan on GitHub" )
467+ do_command (
468+ [
469+ 'git' ,
470+ 'clone' ,
471+ '--depth' ,
472+ '1' ,
473+ '--branch' ,
474+ tag ,
475+ '--recursive' ,
476+ '--shallow-submodules' ,
477+ 'https://github.com/stan-dev/cmdstan.git' ,
478+ f'cmdstan-{ tag_folder } ' ,
479+ ]
480+ )
481+ return
482+
458483 print ('Downloading CmdStan version {}' .format (version ))
459484 url = get_download_url (version )
460485 for i in range (6 ): # always retry to allow for transient URLErrors
@@ -578,9 +603,12 @@ def run_install(args: Union[InteractiveSettings, InstallationSettings]) -> None:
578603 if args .compiler :
579604 run_compiler_install (args .dir , args .verbose , args .progress )
580605
581- cmdstan_version = f'cmdstan-{ args .version } '
606+ if 'git:' in args .version :
607+ tag = args .version .replace (':' , '-' ).replace ('/' , '_' )
608+ cmdstan_version = f'cmdstan-{ tag } '
609+ else :
610+ cmdstan_version = f'cmdstan-{ args .version } '
582611 with pushd (args .dir ):
583-
584612 already_installed = os .path .exists (cmdstan_version ) and os .path .exists (
585613 os .path .join (
586614 cmdstan_version ,
@@ -598,6 +626,7 @@ def run_install(args: Union[InteractiveSettings, InstallationSettings]) -> None:
598626 'Connection to GitHub failed. '
599627 'Check firewall settings or ensure this version exists.'
600628 )
629+ shutil .rmtree (cmdstan_version , ignore_errors = True )
601630 retrieve_version (args .version , args .progress )
602631 install_version (
603632 cmdstan_version = cmdstan_version ,
@@ -620,7 +649,11 @@ def parse_cmdline_args() -> Dict[str, Any]:
620649 + "interactive mode" ,
621650 )
622651 parser .add_argument (
623- '--version' , '-v' , help = "version, defaults to latest release version"
652+ '--version' ,
653+ '-v' ,
654+ help = "version, defaults to latest release version. "
655+ "If git is installed, you can also specify a git tag or branch, "
656+ "e.g. git:develop" ,
624657 )
625658 parser .add_argument (
626659 '--dir' , '-d' , help = "install directory, defaults to '$HOME/.cmdstan"
0 commit comments