Skip to content

Commit 00defb4

Browse files
committed
Refactor.
1 parent cd694a1 commit 00defb4

File tree

2 files changed

+54
-44
lines changed

2 files changed

+54
-44
lines changed

build_docs_to_publish

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ from doc_builder.build_docs import (
1818
main as build_docs,
1919
)
2020
from doc_builder.build_docs_shared_args import main as build_docs_shared_args
21+
from doc_builder.sys_utils import get_git_head_or_branch, check_permanent_file
2122

2223
# Change to the parent director of doc-builder and add to Python path
2324
os.chdir(os.path.join(os.path.dirname(__file__), os.pardir))
@@ -40,36 +41,6 @@ STATIC_DIR = os.path.join(SOURCE, "_static")
4041
TEMPLATES_DIR = os.path.join(SOURCE, "_templates")
4142

4243

43-
def get_git_head_or_branch():
44-
"""
45-
Get the name of the current branch. If detached HEAD, get current commit SHA.
46-
"""
47-
try:
48-
result = subprocess.run(
49-
["git", "symbolic-ref", "--short", "-q", "HEAD"],
50-
stdout=subprocess.PIPE,
51-
stderr=subprocess.DEVNULL,
52-
text=True,
53-
check=True,
54-
)
55-
output = result.stdout.strip()
56-
except subprocess.CalledProcessError:
57-
output = ""
58-
59-
if not output:
60-
# Fallback to commit SHA
61-
result = subprocess.run(
62-
["git", "rev-parse", "HEAD"],
63-
stdout=subprocess.PIPE,
64-
stderr=subprocess.DEVNULL,
65-
text=True,
66-
check=True,
67-
)
68-
output = result.stdout.strip()
69-
70-
return output
71-
72-
7344
def checkout_and_build(version, args):
7445
"""
7546
Check out docs for a version and build
@@ -82,21 +53,9 @@ def checkout_and_build(version, args):
8253
# the permanent_files list.
8354
permanent_files = [CONF_PY, VERSIONS_PY, STATIC_DIR, TEMPLATES_DIR, "doc-builder"]
8455

85-
# Check "permanent" files: Each should (a) exist and (b) not have any uncommitted changes.
56+
# Check some things about "permanent" files before checkout
8657
for filename in permanent_files:
87-
# Error if doesn't exist
88-
if not os.path.exists(filename):
89-
raise FileNotFoundError(filename)
90-
# Error if it contains uncommitted changes
91-
cmd = f"git add . && git diff --quiet {filename} && git diff --cached --quiet {filename}"
92-
try:
93-
subprocess.check_output(cmd, shell=True)
94-
except subprocess.CalledProcessError as e:
95-
subprocess.check_output(
96-
"git reset", shell=True
97-
) # Unstage files staged by `git add`
98-
msg = f"Important file/submodule may contain uncommitted changes: '{filename}'"
99-
raise RuntimeError(msg) from e
58+
check_permanent_file(filename)
10059

10160
# Check out the git reference of this version (branch name, tag, or commit SHA)
10261
subprocess.check_output("git checkout " + version.ref, shell=True)

doc_builder/sys_utils.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,57 @@
66
import os
77

88

9+
def check_permanent_file(filename):
10+
"""
11+
Check a "permanent" file (one that we don't want to change between doc version builds)
12+
"""
13+
14+
# Ensure file exists
15+
if not os.path.exists(filename):
16+
raise FileNotFoundError(filename)
17+
18+
# Error if file contains uncommitted changes
19+
cmd = f"git add . && git diff --quiet {filename} && git diff --cached --quiet {filename}"
20+
try:
21+
subprocess.check_output(cmd, shell=True)
22+
except subprocess.CalledProcessError as e:
23+
subprocess.check_output(
24+
"git reset", shell=True
25+
) # Unstage files staged by `git add`
26+
msg = f"Important file/submodule may contain uncommitted changes: '{filename}'"
27+
raise RuntimeError(msg) from e
28+
29+
30+
def get_git_head_or_branch():
31+
"""
32+
Get the name of the current branch. If detached HEAD, get current commit SHA.
33+
"""
34+
try:
35+
result = subprocess.run(
36+
["git", "symbolic-ref", "--short", "-q", "HEAD"],
37+
stdout=subprocess.PIPE,
38+
stderr=subprocess.DEVNULL,
39+
text=True,
40+
check=True,
41+
)
42+
output = result.stdout.strip()
43+
except subprocess.CalledProcessError:
44+
output = ""
45+
46+
if not output:
47+
# Fallback to commit SHA
48+
result = subprocess.run(
49+
["git", "rev-parse", "HEAD"],
50+
stdout=subprocess.PIPE,
51+
stderr=subprocess.DEVNULL,
52+
text=True,
53+
check=True,
54+
)
55+
output = result.stdout.strip()
56+
57+
return output
58+
59+
960
def git_current_branch():
1061
"""Determines the name of the current git branch
1162

0 commit comments

Comments
 (0)