@@ -21,17 +21,16 @@ def emit(self, record):
2121 self .flush ()
2222
2323
24- logger = logging .getLogger ()
25- logger .setLevel (logging .INFO )
26- handler = _NoNewLine ()
27- handler .setFormatter (logging .Formatter ("%(message)s" ))
28- logger .addHandler (handler )
29-
30-
3124class Bootstrapper :
32- def __init__ (self , language : str , branch : str = "3.12" ) -> None :
25+ def __init__ (
26+ self ,
27+ language : str ,
28+ branch : str = "3.12" ,
29+ logger : logging .Logger = logging .getLogger (),
30+ ) -> None :
3331 self .language = language
3432 self .branch = branch
33+ self .logger = logger
3534 self .translation_repo = f"python-docs-{ self .language } "
3635 self .cpython_repo = f"{ self .translation_repo } /venv/cpython"
3736 self .readme_url = "https://raw.githubusercontent.com/egeakman/python-docs-bootstrapper/master/bootstrapper/data/README.md"
@@ -43,36 +42,36 @@ def _request(self, url: str) -> str:
4342 return response .read ().decode ()
4443
4544 def create_dirs (self ) -> None :
46- logger .info ("Creating directories..." )
45+ self . logger .info ("Creating directories..." )
4746 os .makedirs (self .translation_repo , exist_ok = True )
4847 os .makedirs (self .cpython_repo , exist_ok = True )
49- print ("✅" )
48+ self . logger . info ("✅\n " )
5049
5150 def setup_cpython_repo (self ) -> None :
5251 if not os .path .exists (f"{ self .cpython_repo } /.git" ) and not os .path .isdir (
5352 f"{ self .cpython_repo } /.git"
5453 ):
55- logger .info ("Cloning CPython repo..." )
54+ self . logger .info ("Cloning CPython repo..." )
5655 subprocess .run (
5756 [
5857 "git" ,
5958 "clone" ,
6059 "https://github.com/python/cpython.git" ,
6160 self .cpython_repo ,
61+ f"--branch={ self .branch } " ,
6262 "-q" ,
6363 ],
6464 check = True ,
6565 )
66- print ("✅" )
66+ self . logger . info ("✅\n " )
6767
68- subprocess .run (
69- ["git" , "-C" , self .cpython_repo , "checkout" , self .branch , "-q" ], check = True
70- )
68+ self .logger .info ("Updating CPython repo..." )
7169 subprocess .run (
7270 ["git" , "-C" , self .cpython_repo , "pull" , "--ff-only" , "-q" ], check = True
7371 )
72+ self .logger .info ("✅\n " )
7473
75- logger .info ("Building gettext files..." )
74+ self . logger .info ("Building gettext files..." )
7675 subprocess .run (
7776 [
7877 "sphinx-build" ,
@@ -85,21 +84,18 @@ def setup_cpython_repo(self) -> None:
8584 cwd = self .cpython_repo ,
8685 check = True ,
8786 )
88- print ("✅" )
87+ self . logger . info ("✅\n " )
8988
9089 def setup_translation_repo (self ) -> None :
91- logger .info ("Initializing translation repo..." )
90+ self . logger .info ("Initializing translation repo..." )
9291 subprocess .run (["git" , "init" , "-q" ], cwd = self .translation_repo , check = True )
9392 subprocess .run (
9493 ["git" , "branch" , "-m" , self .branch ], cwd = self .translation_repo , check = True
9594 )
96- print ("✅" )
97-
98- logger .info ("Copying gettext files..." )
99- files = glob .glob (f"{ self .cpython_repo } /pot/**/*.pot" ) + glob .glob (
100- f"{ self .cpython_repo } /pot/*.pot"
101- )
95+ self .logger .info ("✅\n " )
10296
97+ self .logger .info ("Copying gettext files..." )
98+ files = glob .glob (f"{ self .cpython_repo } /pot/**/*.pot" , recursive = True )
10399 files = [path .replace ("\\ " , "/" ) for path in files ]
104100
105101 for file in files :
@@ -108,69 +104,62 @@ def setup_translation_repo(self) -> None:
108104 ".pot" , ".po"
109105 )
110106 )
111-
112- if len (file .split ("/" )) > 5 :
113- os .makedirs ("/" .join (dest_path .split ("/" )[:2 ]), exist_ok = True )
114-
107+ os .makedirs (os .path .dirname (dest_path ), exist_ok = True )
115108 shutil .copyfile (file , dest_path )
116109 files [files .index (file )] = dest_path
117- print ("✅" )
110+ self . logger . info ("✅\n " )
118111
119- logger .info ("Cleaning up gettext files..." )
112+ self . logger .info ("Cleaning up gettext files..." )
120113 for file in files :
121114 with open (file , "r" , encoding = "utf-8" ) as f :
122115 contents = f .read ()
123116 contents = re .sub ("^#: .*Doc/" , "#: " , contents , flags = re .M )
124117 with open (file , "w" , encoding = "utf-8" ) as f :
125118 f .write (contents )
126- print ("✅" )
119+ self . logger . info ("✅\n " )
127120
128121 def create_readme (self ) -> None :
129- logger .info ("Creating README.md..." )
122+ self . logger .info ("Creating README.md..." )
130123 try :
131124 readme = self ._request (self .readme_url )
132125 except (urllib .error .HTTPError , urllib .error .URLError ):
133- logger .warning (
126+ self . logger .warning (
134127 "\n ⚠️ Failed to fetch README.md from GitHub, using local copy..."
135128 )
136129 readme = Path (f"{ os .path .dirname (__file__ )} /data/README.md" ).read_text (
137130 encoding = "utf-8"
138131 )
139-
140132 readme = readme .replace ("{{translation.language}}" , self .language )
141-
142133 with open (f"{ self .translation_repo } /README.md" , "w" , encoding = "utf-8" ) as f :
143134 f .write (readme )
144- print ("✅" )
135+ self . logger . info ("✅\n " )
145136
146137 def create_gitignore (self ) -> None :
147- logger .info ("Creating .gitignore..." )
138+ self . logger .info ("Creating .gitignore..." )
148139 try :
149140 gitignore = self ._request (self .gitignore_url )
150141 except (urllib .error .HTTPError , urllib .error .URLError ):
151- logger .warning (
142+ self . logger .warning (
152143 "\n ⚠️ Failed to fetch .gitignore from GitHub, using local copy..."
153144 )
154145 gitignore = Path (f"{ os .path .dirname (__file__ )} /data/.gitignore" ).read_text (
155146 encoding = "utf-8"
156147 )
157-
158148 with open (f"{ self .translation_repo } /.gitignore" , "w" , encoding = "utf-8" ) as f :
159149 f .write (gitignore )
160- print ("✅" )
150+ self . logger . info ("✅\n " )
161151
162152 def create_makefile (self ) -> None :
163- logging .info ("Creating .makefile ..." )
153+ logging .info ("Creating Makefile ..." )
164154 try :
165155 makefile = self ._request (self .makefile_url )
166156 except (urllib .error .HTTPError , urllib .error .URLError ):
167- logger .warning (
157+ self . logger .warning (
168158 "\n ⚠️ Failed to fetch Makefile from GitHub, using local copy..."
169159 )
170160 makefile = Path (f"{ os .path .dirname (__file__ )} /data/Makefile" ).read_text (
171161 encoding = "utf-8"
172162 )
173-
174163 head = (
175164 subprocess .run (
176165 ["git" , "-C" , self .cpython_repo , "rev-parse" , "HEAD" ],
@@ -180,14 +169,12 @@ def create_makefile(self) -> None:
180169 .stdout .strip ()
181170 .decode ()
182171 )
183-
184172 makefile = makefile .replace ("{{translation.language}}" , self .language )
185173 makefile = makefile .replace ("{{translation.branch}}" , self .branch )
186174 makefile = makefile .replace ("{{translation.head}}" , head )
187-
188175 with open (f"{ self .translation_repo } /Makefile" , "w" , encoding = "utf-8" ) as f :
189176 f .write (makefile )
190- print ("✅" )
177+ self . logger . info ("✅\n " )
191178
192179 def run (self ) -> None :
193180 try :
@@ -197,9 +184,11 @@ def run(self) -> None:
197184 self .create_readme ()
198185 self .create_gitignore ()
199186 self .create_makefile ()
200- logger .info (f"🎉 Done bootstrapping the { self .language } translation ✅\n " )
187+ self .logger .info (
188+ f"🎉 Done bootstrapping the { self .language } translation ✅\n "
189+ )
201190 except Exception as e :
202- logger .critical (
191+ self . logger .critical (
203192 f"❌ Bootstrapping of the { self .language } translation failed: { e } \n "
204193 )
205194 sys .exit (1 )
@@ -218,6 +207,11 @@ def main() -> None:
218207 "-b" , "--branch" , type = str , default = "3.12" , help = "CPython branch (e.g. 3.12)"
219208 )
220209 args = parser .parse_args ()
210+ logger = logging .getLogger ()
211+ logger .setLevel (logging .INFO )
212+ handler = _NoNewLine ()
213+ handler .setFormatter (logging .Formatter ("%(message)s" ))
214+ logger .addHandler (handler )
221215 Bootstrapper (args .language .lower ().replace ("_" , "-" ), args .branch ).run ()
222216
223217
0 commit comments