33import subprocess
44import sys
55from functools import partial
6- from os import environ as env
7- from os .path import join
86from shutil import rmtree
97from shutil import which
10- from tempfile import TemporaryDirectory
118from typing import NoReturn
129
13- TEMPLATE = env .get ('TEMPLATE' , 'master' )
14- TEMPLATE_REPOSITORY = 'https://github.com/dipdup-net/dipdup'
15- TEMPLATE_PATH = 'cookiecutter'
16- CACHED_TEMPLATE_PATH = join (env ["HOME" ], '.cookiecutters' , 'dipdup' )
17- CWD = os .getcwd ()
10+ DEFAULT_REPO = 'https://github.com/dipdup-net/dipdup'
11+ DEFAULT_REF = 'master'
1812
1913run = partial (subprocess .run , check = True , shell = True )
2014
@@ -27,38 +21,56 @@ class bcolors:
2721
2822
2923def echo (msg : str , color : str = bcolors .OKBLUE ) -> None :
30- print (color + f'==> { msg } ' + bcolors .ENDC )
31-
32-
33- def err (msg : str , color : str = bcolors .FAIL ) -> None :
34- print (color + msg + bcolors .ENDC )
24+ print (color + f'=> { msg } ' + bcolors .ENDC )
3525
3626
3727def fail (msg : str ) -> NoReturn :
3828 echo (msg , color = bcolors .FAIL )
3929 sys .exit (1 )
4030
4131
42- echo (f'Installing DipDup from template `{ TEMPLATE } `' )
32+ def done (msg : str ) -> NoReturn :
33+ echo (msg , color = bcolors .OKGREEN )
34+ sys .exit (0 )
35+
36+
37+ def main (
38+ quiet : bool = False ,
39+ repo : str = DEFAULT_REPO ,
40+ ref : str = DEFAULT_REF ,
41+ ) -> None :
42+ if sys .version_info < (3 , 10 ):
43+ fail ('DipDup requires Python 3.10' )
44+
45+ echo ('Welcome to DipDup installer' )
4346
44- echo ( 'Checking for dependencies' )
45- for binary in ( 'git' , 'make' , 'poetry' ):
46- if not which ( binary ):
47- fail ( f'` { binary } ` not found, install it and try again ' )
47+ if not which ( 'pipx' ):
48+ echo ( 'Installing pipx' )
49+ run ( 'pip install --user -q pipx' )
50+ run ( 'python -m pipx ensurepath ' )
4851
49- with TemporaryDirectory (prefix = 'dipdup-install-' ) as tmpdir :
50- echo (f'Preparing `{ tmpdir } ` environment' )
52+ if not which ('cookiecutter' ):
53+ echo ('Installing cookiecutter' )
54+ run ('pipx install cookiecutter' )
5155
52- run (f'python -m venv { tmpdir } ' , cwd = tmpdir )
53- run ('bin/python -m pip install -Uq pip cookiecutter' , cwd = tmpdir )
56+ if not which ('poetry' ):
57+ echo ('Installing poetry' )
58+ run ('pipx install poetry' )
5459
55- rmtree (CACHED_TEMPLATE_PATH , ignore_errors = True )
56- if TEMPLATE == 'CI' :
57- run (f'{ tmpdir } /bin/cookiecutter --no-input { TEMPLATE_PATH } ' )
60+ cookiecutter_cmd = 'cookiecutter'
61+ if repo .startswith (('git@' , 'https://' )):
62+ echo ('Using remote template' )
63+ cookiecutter_cmd += f' -f { repo } -c { ref } --directory cookiecutter'
5864 else :
59- run (f'{ tmpdir } /bin/cookiecutter -f { TEMPLATE_REPOSITORY } -c { TEMPLATE } --directory { TEMPLATE_PATH } ' )
65+ echo ('Using local template' )
66+ cookiecutter_cmd += f' { repo } '
67+ if quiet :
68+ cookiecutter_cmd += ' --no-input'
6069
61- for _dir in os .listdir (CWD ):
70+ rmtree (os .path .expanduser ('~/.cookiecutters/dipdup' ), ignore_errors = True )
71+ run (cookiecutter_cmd )
72+
73+ for _dir in os .listdir (os .getcwd ()):
6274 if not os .path .isfile (f'{ _dir } /dipdup.yml' ):
6375 continue
6476 if os .path .isfile (f'{ _dir } /poetry.lock' ):
@@ -70,9 +82,22 @@ def fail(msg: str) -> NoReturn:
7082 echo ('Running initial setup (can take a while)' )
7183 run ('make install' , cwd = _dir )
7284
73- echo (' Verifying project' )
85+ echo ('Verifying project' )
7486 run ('make lint' , cwd = _dir )
7587
76- break
88+ done ('Done! DipDup is ready to use.' )
89+
90+ fail ('No new projects found' )
91+
92+
93+ if __name__ == '__main__' :
94+ args = sys .argv [1 :]
95+ quiet = False
96+ if '-q' in args :
97+ args .remove ('-q' )
98+ quiet = True
99+
100+ if len (args ) not in (0 , 1 , 2 ):
101+ fail ('Usage: install.py [-q] [repo] [ref]' )
77102
78- echo ( 'Done! DipDup is ready to use.' , color = bcolors . OKGREEN )
103+ main ( quiet , * args )
0 commit comments