| 1 |  |  | #!/usr/bin/env python | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import os | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import pathlib | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import subprocess | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import sys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | base_path: pathlib.Path = pathlib.Path(__file__).resolve().parent.parent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | templates_path = base_path / "ci" / "templates" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | def check_call(args): | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     print("+", *args) | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     subprocess.check_call(args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | def exec_in_env(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     env_path = base_path / ".tox" / "bootstrap" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     if sys.platform == "win32": | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         bin_path = env_path / "Scripts" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         bin_path = env_path / "bin" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     if not env_path.exists(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         import subprocess | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         print(f"Making bootstrap env in: {env_path} ...") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             check_call([sys.executable, "-m", "venv", env_path]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         except subprocess.CalledProcessError: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                 check_call([sys.executable, "-m", "virtualenv", env_path]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             except subprocess.CalledProcessError: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 check_call(["virtualenv", env_path]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         print("Installing `jinja2` into bootstrap environment...") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         check_call([bin_path / "pip", "install", "jinja2", "tox"]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     python_executable = bin_path / "python" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     if not python_executable.exists(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         python_executable = python_executable.with_suffix(".exe") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     print(f"Re-executing with: {python_executable}") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     print("+ exec", python_executable, __file__, "--no-env") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     os.execv(python_executable, [python_executable, __file__, "--no-env"]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | def main(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     import jinja2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     print(f"Project path: {base_path}") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     jinja = jinja2.Environment( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         loader=jinja2.FileSystemLoader(str(templates_path)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         trim_blocks=True, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         lstrip_blocks=True, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         keep_trailing_newline=True, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     tox_environments = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         line.strip() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         # 'tox' need not be installed globally, but must be importable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         # by the Python that is running this script. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         # This uses sys.executable the same way that the call in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         # cookiecutter-pylibrary/hooks/post_gen_project.py | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         # invokes this bootstrap.py itself. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         for line in subprocess.check_output( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             [sys.executable, "-m", "tox", "--listenvs"], universal_newlines=True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         ).splitlines() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     tox_environments = [line for line in tox_environments if line.startswith("py")] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     for template in templates_path.rglob("*"): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if template.is_file(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             template_path = str(template.relative_to(templates_path)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             destination = base_path / template_path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             destination.parent.mkdir(parents=True, exist_ok=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             destination.write_text( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 jinja.get_template(template_path).render( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     tox_environments=tox_environments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             print(f"Wrote {template_path}") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     print("DONE.") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | if __name__ == "__main__": | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     args = sys.argv[1:] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     if args == ["--no-env"]: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         main() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     elif not args: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         exec_in_env() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         print(f"Unexpected arguments: {args}", file=sys.stderr) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  |         sys.exit(1) | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  |