Skip to content

Commit 25703b5

Browse files
authored
Refactor with Pathlib for setupbase.py (ipython#14543)
Continued efforts for ipython#12515 though it has been closed. Validated to be okay by running the following. - `pip install -e ".[all]"` - `pytest` Please note that there are also some syntactical modifications, and part of them are from the darker linter.
2 parents de562f8 + 5f39fd8 commit 25703b5

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

setupbase.py

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# Distributed under the terms of the Modified BSD License.
1414

1515
import os
16+
from pathlib import Path
1617
import re
1718
import sys
1819
from glob import glob
@@ -30,21 +31,19 @@
3031
#-------------------------------------------------------------------------------
3132

3233
# A few handy globals
33-
isfile = os.path.isfile
34-
pjoin = os.path.join
35-
repo_root = os.path.dirname(os.path.abspath(__file__))
34+
repo_root = Path(__file__).resolve().parent
3635

37-
def execfile(fname, globs, locs=None):
36+
def execfile(path, globs, locs=None):
3837
locs = locs or globs
39-
with open(fname, encoding="utf-8") as f:
40-
exec(compile(f.read(), fname, "exec"), globs, locs)
38+
with path.open(encoding="utf-8") as f:
39+
exec(compile(f.read(), str(path), "exec"), globs, locs)
4140

4241
#---------------------------------------------------------------------------
4342
# Basic project information
4443
#---------------------------------------------------------------------------
4544

4645
# release.py contains version, authors, license, url, keywords, etc.
47-
execfile(pjoin(repo_root, 'IPython','core','release.py'), globals())
46+
execfile(Path(repo_root, "IPython", "core", "release.py"), globals())
4847

4948
# Create a dict with the basic information
5049
# This dict is eventually passed to setup after additional keys are added.
@@ -62,13 +61,13 @@ def check_package_data(package_data):
6261
"""verify that package_data globs make sense"""
6362
print("checking package data")
6463
for pkg, data in package_data.items():
65-
pkg_root = pjoin(*pkg.split('.'))
64+
pkg_root = Path(*pkg.split("."))
6665
for d in data:
67-
path = pjoin(pkg_root, d)
68-
if '*' in path:
69-
assert len(glob(path)) > 0, "No files match pattern %s" % path
66+
path = pkg_root / d
67+
if "*" in str(path):
68+
assert len(glob(str(path))) > 0, "No files match pattern %s" % path
7069
else:
71-
assert os.path.exists(path), "Missing package data: %s" % path
70+
assert path.exists(), f"Missing package data: {path}"
7271

7372

7473
def check_package_data_first(command):
@@ -95,26 +94,26 @@ def find_data_files():
9594
"""
9695

9796
if "freebsd" in sys.platform:
98-
manpagebase = pjoin('man', 'man1')
97+
manpagebase = Path("man") / "man1"
9998
else:
100-
manpagebase = pjoin('share', 'man', 'man1')
99+
manpagebase = Path("share") / "man" / "man1"
101100

102101
# Simple file lists can be made by hand
103-
manpages = [f for f in glob(pjoin('docs','man','*.1.gz')) if isfile(f)]
102+
manpages = [f for f in Path("docs/man").glob("*.1.gz") if f.is_file()]
104103
if not manpages:
105104
# When running from a source tree, the manpages aren't gzipped
106-
manpages = [f for f in glob(pjoin('docs','man','*.1')) if isfile(f)]
105+
manpages = [f for f in Path("docs/man").glob("*.1") if f.is_file()]
107106

108107
# And assemble the entire output list
109-
data_files = [ (manpagebase, manpages) ]
108+
data_files = [(str(manpagebase), [str(f) for f in manpages])]
110109

111110
return data_files
112111

113112

114113
# The two functions below are copied from IPython.utils.path, so we don't need
115114
# to import IPython during setup, which fails on Python 3.
116115

117-
def target_outdated(target,deps):
116+
def target_outdated(target, deps):
118117
"""Determine whether a target is out of date.
119118
120119
target_outdated(target,deps) -> 1/0
@@ -126,27 +125,27 @@ def target_outdated(target,deps):
126125
true, otherwise return false.
127126
"""
128127
try:
129-
target_time = os.path.getmtime(target)
130-
except os.error:
128+
target_time = Path(target).stat().st_mtime
129+
except FileNotFoundError:
131130
return 1
132131
for dep in deps:
133-
dep_time = os.path.getmtime(dep)
132+
dep_time = Path(dep).stat().st_mtime
134133
if dep_time > target_time:
135134
# print("For target",target,"Dep failed:",dep) # dbg
136135
# print("times (dep,tar):",dep_time,target_time) # dbg
137136
return 1
138137
return 0
139138

140139

141-
def target_update(target,deps,cmd):
140+
def target_update(target, deps, cmd):
142141
"""Update a target with a given command given a list of dependencies.
143142
144143
target_update(target,deps,cmd) -> runs cmd if target is outdated.
145144
146145
This is just a wrapper around target_outdated() which calls the given
147146
command if target is outdated."""
148147

149-
if target_outdated(target,deps):
148+
if target_outdated(target, deps):
150149
os.system(cmd)
151150

152151
#---------------------------------------------------------------------------
@@ -190,25 +189,24 @@ def _record_commit(self, base_dir):
190189
repo_commit, _ = proc.communicate()
191190
repo_commit = repo_commit.strip().decode("ascii")
192191

193-
out_pth = pjoin(base_dir, pkg_dir, 'utils', '_sysinfo.py')
194-
if os.path.isfile(out_pth) and not repo_commit:
192+
out_pth = Path(base_dir) / pkg_dir / "utils" / "_sysinfo.py"
193+
if out_pth.is_file() and not repo_commit:
195194
# nothing to write, don't clobber
196195
return
197196

198-
print("writing git commit '%s' to %s" % (repo_commit, out_pth))
197+
print(f"writing git commit '{repo_commit}' to {out_pth}")
199198

200199
# remove to avoid overwriting original via hard link
201200
try:
202-
os.remove(out_pth)
203-
except (IOError, OSError):
201+
out_pth.unlink()
202+
except FileNotFoundError:
204203
pass
205-
with open(out_pth, "w", encoding="utf-8") as out_file:
204+
with out_pth.open("w", encoding="utf-8") as out_file:
206205
out_file.writelines(
207206
[
208207
"# GENERATED BY setup.py\n",
209-
'commit = "%s"\n' % repo_commit,
208+
f'commit = "{repo_commit}"\n',
210209
]
211210
)
212211

213212
return MyBuildPy
214-

0 commit comments

Comments
 (0)