8
8
## Sphinx related options
9
9
10
10
# Sphinx output and source directories
11
- OUTPUT_DIR = pathlib .Path ("_build" , "html" )
11
+ BUILD_DIR = '_build'
12
+ OUTPUT_DIR = pathlib .Path (BUILD_DIR , "html" )
12
13
SOURCE_DIR = pathlib .Path ("." )
13
14
15
+ # Location of the translation templates
16
+ TRANSLATION_TEMPLATE_DIR = pathlib .Path (BUILD_DIR , "gettext" )
17
+
14
18
# Sphinx build commands
15
19
SPHINX_BUILD = "sphinx-build"
16
20
SPHINX_AUTO_BUILD = "sphinx-autobuild"
21
25
# Sphinx parameters used to test the build of the guide
22
26
TEST_PARAMETERS = ['-W' , '--keep-going' , '-E' , '-a' ]
23
27
28
+ # Sphinx parameters to generate translation templates
29
+ TRANSLATION_TEMPLATE_PARAMETERS = ["-b" , "gettext" ]
30
+
24
31
# Sphinx-autobuild ignore and include parameters
25
32
AUTOBUILD_IGNORE = [
26
33
"_build" ,
34
+ ".nox" ,
27
35
"build_assets" ,
28
36
"tmp" ,
29
37
]
30
38
AUTOBUILD_INCLUDE = [
31
39
pathlib .Path ("_static" , "pyos.css" )
32
40
]
33
41
42
+ ## Localization options (translations)
43
+
44
+ # List of languages for which locales will be generated in (/locales/<lang>)
45
+ LANGUAGES = ["es" ]
46
+
47
+ # List of languages that should be built when releasing the guide
48
+ RELEASE_LANGUAGES = ['es' ]
49
+
50
+
34
51
@nox .session
35
52
def docs (session ):
36
53
"""Build the packaging guide."""
37
54
session .install ("-e" , "." )
38
- cmd = [SPHINX_BUILD , * BUILD_PARAMETERS , SOURCE_DIR , OUTPUT_DIR , * session .posargs ]
39
- session .run (* cmd )
55
+ session .run (SPHINX_BUILD , * BUILD_PARAMETERS , SOURCE_DIR , OUTPUT_DIR , * session .posargs )
56
+ # When building the guide, also build the translations in RELEASE_LANGUAGES
57
+ session .notify ("build-translations" , ['release-build' ])
58
+
40
59
41
60
@nox .session (name = "docs-test" )
42
61
def docs_test (session ):
43
- """Build the packaging guide with additional parameters."""
62
+ """
63
+ Build the packaging guide with more restricted parameters.
64
+
65
+ Note: this is the session used in CI/CD to release the guide.
66
+ """
44
67
session .install ("-e" , "." )
45
- cmd = [SPHINX_BUILD , * BUILD_PARAMETERS , * TEST_PARAMETERS , SOURCE_DIR , OUTPUT_DIR , * session .posargs ]
46
- session .run (* cmd )
68
+ session .run (SPHINX_BUILD , * BUILD_PARAMETERS , * TEST_PARAMETERS , SOURCE_DIR , OUTPUT_DIR , * session .posargs )
69
+ # When building the guide with additional parameters, also build the translations in RELEASE_LANGUAGES
70
+ # with those same parameters.
71
+ session .notify ("build-translations" , ['release-build' , * TEST_PARAMETERS ])
72
+
47
73
48
74
@nox .session (name = "docs-live" )
49
75
def docs_live (session ):
50
- """Build and launch a local copy of the guide."""
76
+ """
77
+ Build and launch a local copy of the guide.
78
+
79
+ This session will use sphinx-autobuild to build the guide and launch a local server so you can
80
+ browse it locally. It will automatically rebuild the guide when changes are detected in the source.
81
+
82
+ It can be used with the language parameter to build a specific translation, for example:
83
+
84
+ nox -s docs-live -- -D language=es
85
+
86
+ Note: The docs-live-lang session below is provided as a convenience session for beginner contributors
87
+ so they don't need to remember the specific sphinx-build parameters to build a different language.
88
+ """
51
89
session .install ("-e" , "." )
52
90
cmd = [SPHINX_AUTO_BUILD , * BUILD_PARAMETERS , SOURCE_DIR , OUTPUT_DIR , * session .posargs ]
53
91
for folder in AUTOBUILD_IGNORE :
54
92
cmd .extend (["--ignore" , f"*/{ folder } /*" ])
93
+ # This part was commented in the previous version of the nox file, keeping the same here
94
+ # for folder in AUTOBUILD_INCLUDE:
95
+ # cmd.extend(["--watch", folder])
55
96
session .run (* cmd )
56
97
98
+
99
+ @nox .session (name = "docs-live-lang" )
100
+ def docs_live_lang (session ):
101
+ """
102
+ A convenience session for beginner contributors to use the docs-live session with
103
+ a specific language.
104
+
105
+ It expects the language code to be passed as the first positional argument, so it needs
106
+ to be called with from the command line with the following syntax:
107
+
108
+ nox -s docs-live-lang -- LANG
109
+
110
+ where LANG is one of the available languages defined in LANGUAGES.
111
+ For example, for Spanish: nox -s docs-live-lang -- es
112
+ """
113
+ if not session .posargs :
114
+ session .error (
115
+ "Please provide a language using:\n \n "
116
+ "nox -s docs-live-lang -- LANG\n \n "
117
+ f" where LANG is one of: { LANGUAGES } "
118
+ )
119
+ lang = session .posargs [0 ]
120
+ if lang in LANGUAGES :
121
+ session .posargs .pop (0 )
122
+ session .notify ("docs-live" , ('-D' , f"language={ lang } " , * session .posargs ))
123
+ else :
124
+ session .error (
125
+ f"[{ lang } ] locale is not available. Try using:\n \n "
126
+ "nox -s docs-live-lang -- LANG\n \n "
127
+ f"where LANG is one of: { LANGUAGES } "
128
+ )
129
+
130
+
57
131
@nox .session (name = "docs-clean" )
58
132
def clean_dir (session ):
59
133
"""Clean out the docs directory used in the live build."""
@@ -65,3 +139,76 @@ def clean_dir(session):
65
139
shutil .rmtree (content )
66
140
else :
67
141
os .remove (content )
142
+
143
+
144
+ @nox .session (name = "update-translations" )
145
+ def update_translations (session ):
146
+ """
147
+ Update the translation files (./locales/*/.po) for all languages translations.
148
+
149
+ Note: this step is important because it makes sure that the translation files are
150
+ up to date with the latest changes in the guide.
151
+ """
152
+ session .install ("-e" , "." )
153
+ session .install ("sphinx-intl" )
154
+ session .log ("Updating templates (.pot)" )
155
+ session .run (SPHINX_BUILD , * TRANSLATION_TEMPLATE_PARAMETERS , SOURCE_DIR , TRANSLATION_TEMPLATE_DIR , * session .posargs )
156
+ for lang in LANGUAGES :
157
+ session .log (f"Updating .po files for [{ lang } ] translation" )
158
+ session .run ("sphinx-intl" , "update" , "-p" , TRANSLATION_TEMPLATE_DIR , "-l" , lang )
159
+
160
+
161
+ @nox .session (name = "build-languages" )
162
+ def build_languages (session ):
163
+ """
164
+ Build the translations of the guide for the specified language.
165
+
166
+ Note: This sessions expects a list of languages to build in the first position of the session arguments.
167
+ It does not need to be called directly, it is started by build_translations session.
168
+ """
169
+ if not session .posargs :
170
+ session .error ("Please provide the list of languages to build the translation for" )
171
+ languages_to_build = session .posargs .pop (0 )
172
+
173
+ session .install ("-e" , "." )
174
+ for lang in languages_to_build :
175
+ if lang not in LANGUAGES :
176
+ session .warn (f"Language [{ lang } ] is not available for translation" )
177
+ continue
178
+ session .log (f"Building [{ lang } ] guide" )
179
+ session .run (SPHINX_BUILD , * BUILD_PARAMETERS , "-D" , f"language={ lang } " , "." , OUTPUT_DIR / lang , * session .posargs )
180
+
181
+
182
+ @nox .session (name = "build-translations" )
183
+ def build_translations (session ):
184
+ """
185
+ Build translations of the guide.
186
+
187
+ Note: this session can be called directly to build all available translations (defined in LANGUAGES).
188
+ It is also called by the docs and docs-test sessions with 'release-build' as the first positional
189
+ argument, to build only the translations defined in RELEASE_LANGUAGES.
190
+ """
191
+ release_build = False
192
+ if session .posargs and session .posargs [0 ] == 'release-build' :
193
+ session .posargs .pop (0 )
194
+ release_build = True
195
+ # if running from the docs or docs-test sessions, build only release languages
196
+ BUILD_LANGUAGES = RELEASE_LANGUAGES if release_build else LANGUAGES
197
+ session .log (f"Existing translations: { LANGUAGES } " )
198
+ session .log (f"Release Languages: { RELEASE_LANGUAGES } " )
199
+ session .log (f"Building languages{ ' for release' if release_build else '' } : { BUILD_LANGUAGES } " )
200
+ if not BUILD_LANGUAGES :
201
+ session .warn ("No translations to build" )
202
+ else :
203
+ session .notify ("build-languages" , [BUILD_LANGUAGES , * session .posargs ])
204
+
205
+
206
+ @nox .session (name = "build-translations-test" )
207
+ def build_translations_test (session ):
208
+ """
209
+ Build all translations of the guide with testing parameters.
210
+
211
+ This is a convenience session to test the build of all translations with the testing parameters
212
+ in the same way docs-test does for the English version.
213
+ """
214
+ session .notify ("build-translations" , [* TEST_PARAMETERS ])
0 commit comments