Skip to content

Commit 29491e6

Browse files
kevin1kevin1kCarreau
authored andcommitted
Refactor with Pathlib for setupbase.py
1 parent e63a316 commit 29491e6

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

setupbase.py

Lines changed: 25 additions & 26 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,9 +31,7 @@
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

3736
def execfile(fname, globs, locs=None):
3837
locs = locs or globs
@@ -44,7 +43,8 @@ def execfile(fname, globs, locs=None):
4443
#---------------------------------------------------------------------------
4544

4645
# release.py contains version, authors, license, url, keywords, etc.
47-
execfile(pjoin(repo_root, 'IPython','core','release.py'), globals())
46+
with open(repo_root / "IPython" / "core" / "release.py", encoding="utf-8") as f:
47+
exec(f.read(), globals())
4848

4949
# Create a dict with the basic information
5050
# This dict is eventually passed to setup after additional keys are added.
@@ -62,13 +62,13 @@ def check_package_data(package_data):
6262
"""verify that package_data globs make sense"""
6363
print("checking package data")
6464
for pkg, data in package_data.items():
65-
pkg_root = pjoin(*pkg.split('.'))
65+
pkg_root = Path(*pkg.split("."))
6666
for d in data:
67-
path = pjoin(pkg_root, d)
67+
path = pkg_root / d
6868
if '*' in path:
6969
assert len(glob(path)) > 0, "No files match pattern %s" % path
7070
else:
71-
assert os.path.exists(path), "Missing package data: %s" % path
71+
assert path.exists(), f"Missing package data: {path}"
7272

7373

7474
def check_package_data_first(command):
@@ -95,26 +95,26 @@ def find_data_files():
9595
"""
9696

9797
if "freebsd" in sys.platform:
98-
manpagebase = pjoin('man', 'man1')
98+
manpagebase = Path("man") / "man1"
9999
else:
100-
manpagebase = pjoin('share', 'man', 'man1')
100+
manpagebase = Path("share") / "man" / "man1"
101101

102102
# Simple file lists can be made by hand
103-
manpages = [f for f in glob(pjoin('docs','man','*.1.gz')) if isfile(f)]
103+
manpages = [f for f in Path("docs/man").glob("*.1.gz") if f.is_file()]
104104
if not manpages:
105105
# 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)]
106+
manpages = [f for f in Path("docs/man").glob("*.1") if f.is_file()]
107107

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

111111
return data_files
112112

113113

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

117-
def target_outdated(target,deps):
117+
def target_outdated(target, deps):
118118
"""Determine whether a target is out of date.
119119
120120
target_outdated(target,deps) -> 1/0
@@ -126,27 +126,27 @@ def target_outdated(target,deps):
126126
true, otherwise return false.
127127
"""
128128
try:
129-
target_time = os.path.getmtime(target)
130-
except os.error:
129+
target_time = Path(target).stat().st_mtime
130+
except FileNotFoundError:
131131
return 1
132132
for dep in deps:
133-
dep_time = os.path.getmtime(dep)
133+
dep_time = Path(dep).stat().st_mtime
134134
if dep_time > target_time:
135135
# print("For target",target,"Dep failed:",dep) # dbg
136136
# print("times (dep,tar):",dep_time,target_time) # dbg
137137
return 1
138138
return 0
139139

140140

141-
def target_update(target,deps,cmd):
141+
def target_update(target, deps, cmd):
142142
"""Update a target with a given command given a list of dependencies.
143143
144144
target_update(target,deps,cmd) -> runs cmd if target is outdated.
145145
146146
This is just a wrapper around target_outdated() which calls the given
147147
command if target is outdated."""
148148

149-
if target_outdated(target,deps):
149+
if target_outdated(target, deps):
150150
os.system(cmd)
151151

152152
#---------------------------------------------------------------------------
@@ -190,25 +190,24 @@ def _record_commit(self, base_dir):
190190
repo_commit, _ = proc.communicate()
191191
repo_commit = repo_commit.strip().decode("ascii")
192192

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

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

200200
# remove to avoid overwriting original via hard link
201201
try:
202-
os.remove(out_pth)
203-
except (IOError, OSError):
202+
out_pth.unlink()
203+
except FileNotFoundError:
204204
pass
205-
with open(out_pth, "w", encoding="utf-8") as out_file:
205+
with out_pth.open("w", encoding="utf-8") as out_file:
206206
out_file.writelines(
207207
[
208208
"# GENERATED BY setup.py\n",
209-
'commit = "%s"\n' % repo_commit,
209+
f'commit = "{repo_commit}"\n',
210210
]
211211
)
212212

213213
return MyBuildPy
214-

0 commit comments

Comments
 (0)