@@ -72,9 +72,12 @@ def _file_unchanged(old, new):
72
72
def shell_out (cmd ):
73
73
logging .debug ("Running command %r" , cmd )
74
74
try :
75
- subprocess .check_output (cmd , shell = True , stdin = subprocess .PIPE , stderr = subprocess .STDOUT )
75
+ return subprocess .check_output (cmd , shell = True ,
76
+ stdin = subprocess .PIPE ,
77
+ stderr = subprocess .STDOUT ,
78
+ universal_newlines = True )
76
79
except subprocess .CalledProcessError as e :
77
- logging .error ( "command failed with output %r" , e .output . decode ( "utf-8" ) )
80
+ logging .debug ( "Command failed with output %r" , e .output )
78
81
raise
79
82
80
83
@@ -95,13 +98,14 @@ def changed_files(directory, other):
95
98
return changed
96
99
97
100
98
- def git_clone (repository , directory , branch = 'master' ):
99
- """Clone or update the given branch of the given repository in the
100
- given directory .
101
+ def git_clone (repository , directory , branch = None ):
102
+ """Clone or update the given repository in the given directory.
103
+ Optionally checking out a branch .
101
104
"""
102
105
logging .info ("Updating repository %s in %s" , repository , directory )
103
106
try :
104
- shell_out ("git -C {} checkout {}" .format (directory , branch ))
107
+ if branch :
108
+ shell_out ("git -C {} checkout {}" .format (directory , branch ))
105
109
shell_out ("git -C {} pull --ff-only" .format (directory ))
106
110
except subprocess .CalledProcessError :
107
111
if os .path .exists (directory ):
@@ -110,7 +114,8 @@ def git_clone(repository, directory, branch='master'):
110
114
os .makedirs (directory , mode = 0o775 )
111
115
shell_out ("git clone --depth 1 --no-single-branch {} {}" .format (
112
116
repository , directory ))
113
- shell_out ("git -C {} checkout {}" .format (directory , branch ))
117
+ if branch :
118
+ shell_out ("git -C {} checkout {}" .format (directory , branch ))
114
119
115
120
116
121
def pep_545_tag_to_gettext_tag (tag ):
@@ -124,6 +129,27 @@ def pep_545_tag_to_gettext_tag(tag):
124
129
return language + '_' + region .upper ()
125
130
126
131
132
+ def translation_branch (locale_repo , locale_clone_dir , needed_version ):
133
+ """Some cpython versions may be untranslated, being either too old or
134
+ too new.
135
+
136
+ This function looks for remote branches on the given repo, and
137
+ returns the name of the nearest existing branch.
138
+ """
139
+ git_clone (locale_repo , locale_clone_dir )
140
+ remote_branches = shell_out (
141
+ "git -C {} branch -r" .format (locale_clone_dir ))
142
+ translated_branches = []
143
+ for translated_branch in remote_branches .split ('\n ' ):
144
+ if not translated_branch :
145
+ continue
146
+ try :
147
+ translated_branches .append (float (translated_branch .split ('/' )[1 ]))
148
+ except ValueError :
149
+ pass # Skip non-version branches like 'master' if they exists.
150
+ return sorted (translated_branches , key = lambda x : abs (needed_version - x ))[0 ]
151
+
152
+
127
153
def build_one (version , isdev , quick , sphinxbuild , build_root , www_root ,
128
154
skip_cache_invalidation = False , group = 'docs' , git = False ,
129
155
log_directory = '/var/log/docsbuild/' , language = 'en' ):
@@ -139,7 +165,9 @@ def build_one(version, isdev, quick, sphinxbuild, build_root, www_root,
139
165
locale_dirs , gettext_language_tag , 'LC_MESSAGES' )
140
166
locale_repo = 'https://github.com/python/python-docs-{}.git' .format (
141
167
language )
142
- git_clone (locale_repo , locale_clone_dir , version )
168
+ git_clone (locale_repo , locale_clone_dir ,
169
+ translation_branch (locale_repo , locale_clone_dir ,
170
+ version ))
143
171
sphinxopts += ('-D locale_dirs={} '
144
172
'-D language={} '
145
173
'-D gettext_compact=0' ).format (locale_dirs ,
0 commit comments