@@ -21,17 +21,16 @@ def emit(self, record):
21
21
self .flush ()
22
22
23
23
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
-
31
24
class 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 :
33
31
self .language = language
34
32
self .branch = branch
33
+ self .logger = logger
35
34
self .translation_repo = f"python-docs-{ self .language } "
36
35
self .cpython_repo = f"{ self .translation_repo } /venv/cpython"
37
36
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:
43
42
return response .read ().decode ()
44
43
45
44
def create_dirs (self ) -> None :
46
- logger .info ("Creating directories..." )
45
+ self . logger .info ("Creating directories..." )
47
46
os .makedirs (self .translation_repo , exist_ok = True )
48
47
os .makedirs (self .cpython_repo , exist_ok = True )
49
- print ("✅" )
48
+ self . logger . info ("✅\n " )
50
49
51
50
def setup_cpython_repo (self ) -> None :
52
51
if not os .path .exists (f"{ self .cpython_repo } /.git" ) and not os .path .isdir (
53
52
f"{ self .cpython_repo } /.git"
54
53
):
55
- logger .info ("Cloning CPython repo..." )
54
+ self . logger .info ("Cloning CPython repo..." )
56
55
subprocess .run (
57
56
[
58
57
"git" ,
59
58
"clone" ,
60
59
"https://github.com/python/cpython.git" ,
61
60
self .cpython_repo ,
61
+ f"--branch={ self .branch } " ,
62
62
"-q" ,
63
63
],
64
64
check = True ,
65
65
)
66
- print ("✅" )
66
+ self . logger . info ("✅\n " )
67
67
68
- subprocess .run (
69
- ["git" , "-C" , self .cpython_repo , "checkout" , self .branch , "-q" ], check = True
70
- )
68
+ self .logger .info ("Updating CPython repo..." )
71
69
subprocess .run (
72
70
["git" , "-C" , self .cpython_repo , "pull" , "--ff-only" , "-q" ], check = True
73
71
)
72
+ self .logger .info ("✅\n " )
74
73
75
- logger .info ("Building gettext files..." )
74
+ self . logger .info ("Building gettext files..." )
76
75
subprocess .run (
77
76
[
78
77
"sphinx-build" ,
@@ -85,21 +84,18 @@ def setup_cpython_repo(self) -> None:
85
84
cwd = self .cpython_repo ,
86
85
check = True ,
87
86
)
88
- print ("✅" )
87
+ self . logger . info ("✅\n " )
89
88
90
89
def setup_translation_repo (self ) -> None :
91
- logger .info ("Initializing translation repo..." )
90
+ self . logger .info ("Initializing translation repo..." )
92
91
subprocess .run (["git" , "init" , "-q" ], cwd = self .translation_repo , check = True )
93
92
subprocess .run (
94
93
["git" , "branch" , "-m" , self .branch ], cwd = self .translation_repo , check = True
95
94
)
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 " )
102
96
97
+ self .logger .info ("Copying gettext files..." )
98
+ files = glob .glob (f"{ self .cpython_repo } /pot/**/*.pot" , recursive = True )
103
99
files = [path .replace ("\\ " , "/" ) for path in files ]
104
100
105
101
for file in files :
@@ -108,69 +104,62 @@ def setup_translation_repo(self) -> None:
108
104
".pot" , ".po"
109
105
)
110
106
)
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 )
115
108
shutil .copyfile (file , dest_path )
116
109
files [files .index (file )] = dest_path
117
- print ("✅" )
110
+ self . logger . info ("✅\n " )
118
111
119
- logger .info ("Cleaning up gettext files..." )
112
+ self . logger .info ("Cleaning up gettext files..." )
120
113
for file in files :
121
114
with open (file , "r" , encoding = "utf-8" ) as f :
122
115
contents = f .read ()
123
116
contents = re .sub ("^#: .*Doc/" , "#: " , contents , flags = re .M )
124
117
with open (file , "w" , encoding = "utf-8" ) as f :
125
118
f .write (contents )
126
- print ("✅" )
119
+ self . logger . info ("✅\n " )
127
120
128
121
def create_readme (self ) -> None :
129
- logger .info ("Creating README.md..." )
122
+ self . logger .info ("Creating README.md..." )
130
123
try :
131
124
readme = self ._request (self .readme_url )
132
125
except (urllib .error .HTTPError , urllib .error .URLError ):
133
- logger .warning (
126
+ self . logger .warning (
134
127
"\n ⚠️ Failed to fetch README.md from GitHub, using local copy..."
135
128
)
136
129
readme = Path (f"{ os .path .dirname (__file__ )} /data/README.md" ).read_text (
137
130
encoding = "utf-8"
138
131
)
139
-
140
132
readme = readme .replace ("{{translation.language}}" , self .language )
141
-
142
133
with open (f"{ self .translation_repo } /README.md" , "w" , encoding = "utf-8" ) as f :
143
134
f .write (readme )
144
- print ("✅" )
135
+ self . logger . info ("✅\n " )
145
136
146
137
def create_gitignore (self ) -> None :
147
- logger .info ("Creating .gitignore..." )
138
+ self . logger .info ("Creating .gitignore..." )
148
139
try :
149
140
gitignore = self ._request (self .gitignore_url )
150
141
except (urllib .error .HTTPError , urllib .error .URLError ):
151
- logger .warning (
142
+ self . logger .warning (
152
143
"\n ⚠️ Failed to fetch .gitignore from GitHub, using local copy..."
153
144
)
154
145
gitignore = Path (f"{ os .path .dirname (__file__ )} /data/.gitignore" ).read_text (
155
146
encoding = "utf-8"
156
147
)
157
-
158
148
with open (f"{ self .translation_repo } /.gitignore" , "w" , encoding = "utf-8" ) as f :
159
149
f .write (gitignore )
160
- print ("✅" )
150
+ self . logger . info ("✅\n " )
161
151
162
152
def create_makefile (self ) -> None :
163
- logging .info ("Creating .makefile ..." )
153
+ logging .info ("Creating Makefile ..." )
164
154
try :
165
155
makefile = self ._request (self .makefile_url )
166
156
except (urllib .error .HTTPError , urllib .error .URLError ):
167
- logger .warning (
157
+ self . logger .warning (
168
158
"\n ⚠️ Failed to fetch Makefile from GitHub, using local copy..."
169
159
)
170
160
makefile = Path (f"{ os .path .dirname (__file__ )} /data/Makefile" ).read_text (
171
161
encoding = "utf-8"
172
162
)
173
-
174
163
head = (
175
164
subprocess .run (
176
165
["git" , "-C" , self .cpython_repo , "rev-parse" , "HEAD" ],
@@ -180,14 +169,12 @@ def create_makefile(self) -> None:
180
169
.stdout .strip ()
181
170
.decode ()
182
171
)
183
-
184
172
makefile = makefile .replace ("{{translation.language}}" , self .language )
185
173
makefile = makefile .replace ("{{translation.branch}}" , self .branch )
186
174
makefile = makefile .replace ("{{translation.head}}" , head )
187
-
188
175
with open (f"{ self .translation_repo } /Makefile" , "w" , encoding = "utf-8" ) as f :
189
176
f .write (makefile )
190
- print ("✅" )
177
+ self . logger . info ("✅\n " )
191
178
192
179
def run (self ) -> None :
193
180
try :
@@ -197,9 +184,11 @@ def run(self) -> None:
197
184
self .create_readme ()
198
185
self .create_gitignore ()
199
186
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
+ )
201
190
except Exception as e :
202
- logger .critical (
191
+ self . logger .critical (
203
192
f"❌ Bootstrapping of the { self .language } translation failed: { e } \n "
204
193
)
205
194
sys .exit (1 )
@@ -218,6 +207,11 @@ def main() -> None:
218
207
"-b" , "--branch" , type = str , default = "3.12" , help = "CPython branch (e.g. 3.12)"
219
208
)
220
209
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 )
221
215
Bootstrapper (args .language .lower ().replace ("_" , "-" ), args .branch ).run ()
222
216
223
217
0 commit comments