1515from jinja2 import Environment , FileSystemLoader
1616from packaging import version as packaging_version
1717
18- OUTPUT_FORMATS = [' pdf' , ' epub' , ' html' ]
18+ OUTPUT_FORMATS = [" pdf" , " epub" , " html" ]
1919
2020
2121def get_stable_version (versions ):
@@ -51,18 +51,18 @@ def get_version_object(version):
5151 """
5252 Returns a packaging version object for the given version dictionary.
5353 """
54- version_name = version [' name' ]
54+ version_name = version [" name" ]
5555 # Special case for 'dev' version: we treat it as having a version
5656 # of '0' so that it will come as last when we perform max operation.
57- if version_name == ' dev' :
58- version_name = '0'
57+ if version_name == " dev" :
58+ version_name = "0"
5959 return packaging_version .parse (version_name )
6060
6161 if len (versions ) == 1 :
62- return versions [0 ][' name' ]
62+ return versions [0 ][" name" ]
6363
6464 stable_version = max (versions , key = get_version_object )
65- return stable_version [' name' ]
65+ return stable_version [" name" ]
6666
6767
6868def merge_module_versions (modules1 , modules2 ):
@@ -90,7 +90,7 @@ def merge_module_versions(modules1, modules2):
9090 modules1 = deepcopy (modules1 )
9191 for module2 in modules2 :
9292 for module1 in modules1 :
93- if module1 [' name' ] == module2 [' name' ]:
93+ if module1 [" name" ] == module2 [" name" ]:
9494 module1 .update (module2 )
9595 break
9696 else :
@@ -103,7 +103,7 @@ def get_build_versions(all_versions, output_version):
103103 # Build all versions
104104 return all_versions
105105 for version in all_versions :
106- if version [' name' ] == output_version :
106+ if version [" name" ] == output_version :
107107 return [version ]
108108
109109
@@ -147,18 +147,18 @@ def parse_modules_arg(value):
147147 The `repository` should be in the format `repo-owner/repo-name`, e.g. `openwisp/openwisp2-docs`.
148148 The `dir_name` is optional and defaults to the module name if not specified.
149149 """
150- entries = value .split (',' )
150+ entries = value .split ("," )
151151 result = {}
152152 for entry in entries :
153153 module = {}
154- version = ' dev'
155- for item in entry .split (':' ):
156- key , value = item .split ('=' )
157- if key == ' version' :
154+ version = " dev"
155+ for item in entry .split (":" ):
156+ key , value = item .split ("=" )
157+ if key == " version" :
158158 version = value
159- elif key == ' repository' :
160- owner , name = value .split ('/' )
161- module .update ({' name' : name , ' owner' : owner })
159+ elif key == " repository" :
160+ owner , name = value .split ("/" )
161+ module .update ({" name" : name , " owner" : owner })
162162 else :
163163 module [key ] = value
164164 try :
@@ -186,11 +186,11 @@ def parse_formats_arg(value):
186186 If any format is not supported, it prints an error message and exits the program.
187187 If all formats are supported, it returns the list of output formats.
188188 """
189- output_formats = value .split (',' )
189+ output_formats = value .split ("," )
190190 # Validate all output formats are supported
191191 for format in output_formats :
192- if format not in OUTPUT_FORMATS and format != ' version_map' :
193- print (f' ERROR: { format } is not a valid output format' )
192+ if format not in OUTPUT_FORMATS and format != " version_map" :
193+ print (f" ERROR: { format } is not a valid output format" )
194194 exit (2 )
195195 return output_formats
196196
@@ -209,12 +209,12 @@ def parse_version_arg(value):
209209 SystemExit: If the version is not found in the config.yml file.
210210 """
211211 # Validate passed version exists in config.yml
212- with open (' config.yml' , 'r' ) as f :
212+ with open (" config.yml" , "r" ) as f :
213213 config = yaml .safe_load (f )
214- for version in config [' versions' ]:
215- if version [' name' ] == value :
214+ for version in config [" versions" ]:
215+ if version [" name" ] == value :
216216 return value
217- print (f' ERROR: { value } is not a valid version' )
217+ print (f" ERROR: { value } is not a valid version" )
218218 exit (3 )
219219
220220
@@ -231,10 +231,10 @@ def create_symlink(src, dest):
231231 src (str): The source path to be linked from
232232 dest (str): The destination path to link to
233233 """
234- if dest == ' openwisp-docs' :
235- dest = ' staging-dir'
234+ if dest == " openwisp-docs" :
235+ dest = " staging-dir"
236236 else :
237- dest = os .path .join (' staging-dir' , dest )
237+ dest = os .path .join (" staging-dir" , dest )
238238 if os .path .islink (dest ):
239239 os .unlink (dest )
240240 os .symlink (src , dest )
@@ -247,48 +247,48 @@ def remove_symlink(dest):
247247 Args:
248248 dest (str): The destination path of the symbolic link to be removed.
249249 """
250- if dest == ' openwisp-docs' :
251- dest = ' staging-dir'
250+ if dest == " openwisp-docs" :
251+ dest = " staging-dir"
252252 else :
253- dest = os .path .join (' staging-dir' , dest )
253+ dest = os .path .join (" staging-dir" , dest )
254254 if os .path .islink (dest ):
255255 os .unlink (dest )
256256
257257
258258def git_is_on_branch (repo_path ):
259259 result = subprocess .run (
260- [' git' , ' rev-parse' , ' --abbrev-ref' , ' HEAD' ],
260+ [" git" , " rev-parse" , " --abbrev-ref" , " HEAD" ],
261261 cwd = repo_path ,
262262 capture_output = True ,
263263 text = True ,
264264 )
265- return result .stdout .strip () != ' HEAD'
265+ return result .stdout .strip () != " HEAD"
266266
267267
268- def clone_or_update_repo (name , branch , dir_name , owner = ' openwisp' , dest = None ):
268+ def clone_or_update_repo (name , branch , dir_name , owner = " openwisp" , dest = None ):
269269 """
270270 Clone or update a repository based on the module name and branch provided.
271271 If the repository already exists, update it. Otherwise, clone the repository.
272272 """
273- repository = f' { owner } /{ name } '
274- if os .environ .get (' SSH' ):
273+ repository = f" { owner } /{ name } "
274+ if os .environ .get (" SSH" ):
275275 # SSH cloning is a convenient option for local development, as it
276276 # allows you to commit changes directly to the repository, but it
277277 # requires that you have added your public SSH key to your GitHub
278278 # account and have access to the repository. This means that it won't
279279 # work on GitHub Actions, and it won't work for contributors who don't
280280 # use SSH to access GitHub.
281- repo_url = f' git@github.com:{ repository } .git'
281+ repo_url = f" git@github.com:{ repository } .git"
282282 else :
283- repo_url = f' https://github.com/{ repository } .git'
284- clone_path = os .path .abspath (os .path .join (' modules' , dir_name ))
283+ repo_url = f" https://github.com/{ repository } .git"
284+ clone_path = os .path .abspath (os .path .join (" modules" , dir_name ))
285285
286286 if os .path .exists (clone_path ):
287287 print (f"Repository '{ name } ' already exists. Updating..." )
288288 # "-c advice.detachedHead=false" is used to suppress the warning
289289 # about being in a detached HEAD state when checking out tags.
290290 subprocess .run (
291- [' git' , '-c' , ' advice.detachedHead=false' , ' checkout' , branch ],
291+ [" git" , "-c" , " advice.detachedHead=false" , " checkout" , branch ],
292292 cwd = clone_path ,
293293 check = True ,
294294 )
@@ -297,18 +297,18 @@ def clone_or_update_repo(name, branch, dir_name, owner='openwisp', dest=None):
297297 # network calls.
298298 # During local development, we attempt to pull updates, but only if the
299299 # current HEAD is on a branch (i.e., not detached, such as when on a tag).
300- if not os .environ .get (' PRODUCTION' , False ) and git_is_on_branch (clone_path ):
301- subprocess .run ([' git' , ' pull' ], cwd = clone_path , check = True )
300+ if not os .environ .get (" PRODUCTION" , False ) and git_is_on_branch (clone_path ):
301+ subprocess .run ([" git" , " pull" ], cwd = clone_path , check = True )
302302 else :
303303 print (f"Cloning repository '{ name } '..." )
304304 subprocess .run (
305305 [
306- ' git' ,
307- ' clone' ,
308- ' --depth' ,
309- '1' ,
310- ' --no-single-branch' ,
311- ' --branch' ,
306+ " git" ,
307+ " clone" ,
308+ " --depth" ,
309+ "1" ,
310+ " --no-single-branch" ,
311+ " --branch" ,
312312 branch ,
313313 repo_url ,
314314 clone_path ,
@@ -318,7 +318,7 @@ def clone_or_update_repo(name, branch, dir_name, owner='openwisp', dest=None):
318318 # Create a symlink to either the 'docs' directory inside the cloned repository
319319 # or to the entire repository if no 'docs' directory exists.
320320 # This makes the documentation sources available to the Sphinx build process.
321- src = os .path .join (clone_path , ' docs' )
321+ src = os .path .join (clone_path , " docs" )
322322 if not os .path .exists (src ):
323323 # If no 'docs' directory exists, use the entire repository
324324 src = clone_path
@@ -329,49 +329,49 @@ def clone_or_update_repo(name, branch, dir_name, owner='openwisp', dest=None):
329329def main ():
330330 parser = argparse .ArgumentParser ()
331331 parser .add_argument (
332- ' --formats' ,
332+ " --formats" ,
333333 type = parse_formats_arg ,
334334 default = OUTPUT_FORMATS ,
335- help = ' comma separated output formats (pdf, epub, or html)' ,
335+ help = " comma separated output formats (pdf, epub, or html)" ,
336336 )
337337 parser .add_argument (
338- ' --version' ,
338+ " --version" ,
339339 type = parse_version_arg ,
340340 default = None ,
341- help = ' document version to build' ,
341+ help = " document version to build" ,
342342 )
343343 parser .add_argument (
344- ' --modules' ,
344+ " --modules" ,
345345 default = {},
346346 type = parse_modules_arg ,
347- help = ' comma separated modules to build'
348- ' in the format of <version:module-name>:<branch>:<dir-name>:<repository-owner>' ,
347+ help = " comma separated modules to build"
348+ " in the format of <version:module-name>:<branch>:<dir-name>:<repository-owner>" ,
349349 )
350350 args = parser .parse_args ()
351351
352- with open (' config.yml' ) as f :
352+ with open (" config.yml" ) as f :
353353 config = yaml .safe_load (f )
354354
355- build_versions = get_build_versions (config [' versions' ], args .version )
355+ build_versions = get_build_versions (config [" versions" ], args .version )
356356 stable_version = get_stable_version (build_versions )
357- docs_root = ''
358- html_base_url = ''
359- build_dir = ' _build'
360- if os .environ .get (' PRODUCTION' , False ):
361- docs_root = ' /docs'
362- html_base_url = ' https://openwisp.io'
363- build_dir = f' { build_dir } { docs_root } '
357+ docs_root = ""
358+ html_base_url = ""
359+ build_dir = " _build"
360+ if os .environ .get (" PRODUCTION" , False ):
361+ docs_root = " /docs"
362+ html_base_url = " https://openwisp.io"
363+ build_dir = f" { build_dir } { docs_root } "
364364
365365 for version in build_versions :
366- version_name = version [' name' ]
366+ version_name = version [" name" ]
367367 module_dirs = []
368368
369369 # Modules which are configured to be included in all the builds
370370 default_modules = (
371- config [' modules' ] if not version .get (' overwrite_modules' ) else []
371+ config [" modules" ] if not version .get (" overwrite_modules" ) else []
372372 )
373373 # Modules which are defined for specific version
374- version_modules = version .get (' modules' , [])
374+ version_modules = version .get (" modules" , [])
375375 # Modules which are defined from the command line
376376 overridden_modules = args .modules .get (version_name , [])
377377 modules = get_modules (
@@ -382,27 +382,27 @@ def main():
382382
383383 # If a module does not define a branch,
384384 # it will fallback to the version_branch.
385- version_branch = version .get (' module_branch' , version [' name' ])
386- docs_branch = version .get (' docs_branch' , ' master' )
385+ version_branch = version .get (" module_branch" , version [" name" ])
386+ docs_branch = version .get (" docs_branch" , " master" )
387387 clone_or_update_repo (
388- name = ' openwisp-docs' ,
388+ name = " openwisp-docs" ,
389389 branch = docs_branch ,
390- dir_name = ' openwisp-docs' ,
390+ dir_name = " openwisp-docs" ,
391391 )
392392 for module in modules :
393393 clone_or_update_repo (
394- branch = module .pop (' branch' , version_branch ),
394+ branch = module .pop (" branch" , version_branch ),
395395 ** module ,
396396 )
397- module_dirs .append (module [' dir_name' ])
398- sphinx_src_dir = version .get (' sphinx_src_dir' , ' staging-dir' )
399- for format in [' spellcheck' ] + args .formats :
397+ module_dirs .append (module [" dir_name" ])
398+ sphinx_src_dir = version .get (" sphinx_src_dir" , " staging-dir" )
399+ for format in [" spellcheck" ] + args .formats :
400400 subprocess .run (
401401 [
402- ' make' ,
402+ " make" ,
403403 format ,
404- f' SRCDIR={ sphinx_src_dir } ' ,
405- f' BUILDDIR={ build_dir } /{ version_name } ' ,
404+ f" SRCDIR={ sphinx_src_dir } " ,
405+ f" BUILDDIR={ build_dir } /{ version_name } " ,
406406 ],
407407 env = dict (
408408 os .environ ,
@@ -418,18 +418,18 @@ def main():
418418 remove_symlink (dir )
419419
420420 # Generate the index.html file which redirects to the stable version.
421- env = Environment (loader = FileSystemLoader (' _static' ))
422- template = env .get_template (' index.jinja2' )
423- with open (f' { build_dir } /index.html' , 'w' ) as f :
421+ env = Environment (loader = FileSystemLoader (" _static" ))
422+ template = env .get_template (" index.jinja2" )
423+ with open (f" { build_dir } /index.html" , "w" ) as f :
424424 f .write (template .render (stable_version = stable_version , docs_root = docs_root ))
425425
426426 # Create a symbolic link for the stable version
427427 subprocess .run (
428428 [
429- 'ln' ,
430- ' -rsf' ,
431- f' { build_dir } /{ stable_version } ' ,
432- f' { build_dir } /stable' ,
429+ "ln" ,
430+ " -rsf" ,
431+ f" { build_dir } /{ stable_version } " ,
432+ f" { build_dir } /stable" ,
433433 ],
434434 check = True ,
435435 )
0 commit comments