Skip to content

Commit 7ef55d0

Browse files
authored
Merge pull request #4 from boltronics/ab-windows_fixes
Fix problems running on a Windows host
2 parents 7c7ab40 + 382f436 commit 7ef55d0

File tree

7 files changed

+257
-46
lines changed

7 files changed

+257
-46
lines changed

README.rst

Lines changed: 119 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
###########################
12
Cookiecutter Python Project
23
###########################
34

@@ -64,28 +65,121 @@ It is assumed that the new Python package will eventually be:
6465
The generated docs have some references and links to those sites.
6566

6667

68+
===============
6769
Getting Started
6870
===============
6971

72+
--------------------
7073
One Time Setup Steps
7174
--------------------
7275

76+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
77+
Install cookiecutter via pip
78+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
79+
7380
The process for using Cookiecutter to create a new Python package project
7481
starts with installing Cookiecutter. This is best done by creating a new
7582
virtual environment specifically for cookiecutter and then installing
7683
cookiecutter using ``pip``. The example below shows how to do this.
7784

78-
.. code-block:: console
85+
.. code-block:: shell-session
7986
8087
$ python -m venv --prompt cc ccvenv
88+
$
8189
$ source ccvenv/bin/activate
90+
$ # or for cmd.exe:
91+
$ # ccvenv\Scripts\activate.bat
92+
$ # or for PowerShell:
93+
$ # ccvenv\Scripts\Activate.ps1
94+
$
8295
(cc) $ pip install -U pip # update pip to avoid any warnings
8396
(cc) $ pip install cookiecutter
8497
85-
You are now ready to create a new Python project from the Cookiecutter
86-
template provided by this project.
8798
99+
^^^^^^^^^^^^^
100+
Install hatch
101+
^^^^^^^^^^^^^
102+
103+
If you do not yet have Hatch installed, now would be a good time to do
104+
so. Refer to the installation instructions for your operating system
105+
`here <https://hatch.pypa.io/latest/install/>`_.
106+
107+
108+
^^^^^^^^^^^^^^^^^^^^^^
109+
Install git (optional)
110+
^^^^^^^^^^^^^^^^^^^^^^
111+
112+
It may also be a good idea to ensure you have ``git`` installed (and
113+
it may be required for cookiecutter to function if using it to clone
114+
this template). Under Windows, you can use `winget
115+
<https://learn.microsoft.com/en-us/windows/package-manager/winget/>`_.
116+
117+
.. code-block:: shell-session
118+
119+
(cc) $ winget install --id Git.Git --exact --source winget
120+
121+
Under macOS you can use `brew <https://brew.sh/>`_.
122+
123+
.. code-block:: shell-session
124+
125+
(cc) $ brew install git
126+
127+
Users of other operating systems likely already have it installed or
128+
will be able to install it via their operating system's package
129+
manager.
130+
131+
132+
^^^^^^^^^^^^^^^^^^^^^^^
133+
Install make (optional)
134+
^^^^^^^^^^^^^^^^^^^^^^^
135+
136+
If you wish to use the fancy Makefile included in this project, you
137+
may wish to install the ``make`` command. Under Windows, again using
138+
winget:
139+
140+
.. code-block:: shell-session
141+
142+
(cc) $ winget install --id GnuWin32.Make --exact --source winget
143+
144+
Unlike with git, you will need to `manually add
145+
<https://stackoverflow.com/a/44272417/8243194>`_ the directory
146+
containing ``make.exe`` to your PATH, which is typically something like:
147+
``C:\Program Files(x86)\GnuWin32\bin\``.
148+
149+
Under macOS you can again use brew.
150+
151+
.. code-block:: shell-session
152+
153+
(cc) $ brew install make
154+
155+
Users of other operating systems should again have no trouble finding
156+
it in their operating system's package manager.
88157

158+
159+
.. code-block:: console
160+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
161+
An important note for Windows users running make
162+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
163+
164+
If you are using the Makefile system, be aware that two of the targets
165+
(``help`` and ``dist-test``) make use of PowerShell scripts to achieve
166+
Windows compatibility. These may not run unless you adjust an
167+
execution policy to permit them. This can be done by opening a Windows
168+
PowerShell as an administrator (just right-click the launcher and
169+
select ``Run as Administrator``) and issuing the following command:
170+
171+
.. code-block:: shell-session
172+
173+
PS > Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
174+
175+
You can read more about this `here
176+
<https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies>`_.
177+
178+
Finally, you are ready to create a new Python project from the
179+
Cookiecutter template provided by this project.
180+
181+
182+
--------------------
89183
Create a new project
90184
--------------------
91185

@@ -94,17 +188,18 @@ simply navigate to a directory where you want to create the new project, then
94188
run the ``cookiecutter`` command with a command line argument referencing this
95189
template.
96190

97-
The easiest method is to reference this template via its GitHub URL (where 'gh'
98-
is a shortened form for GitHub):
191+
The easiest method (which will fail if ``git`` is not installed) is to
192+
reference this template via its GitHub URL (where 'gh' is a shortened
193+
form for GitHub):
99194

100-
.. code-block:: console
195+
.. code-block:: shell-session
101196
102197
(cc) $ cookiecutter gh:boltronics/cookiecutter-python-project
103198
104-
Alternatively, if you have cloned a local copy of this template you can
105-
reference it directly:
199+
Alternatively, if you have cloned or downloaded a local copy of this
200+
template, you can reference it directly:
106201

107-
.. code-block:: console
202+
.. code-block:: shell-session
108203
109204
(cc) $ cookiecutter path/to/cookiecutter-python-project
110205
@@ -115,12 +210,13 @@ shown in order.
115210
Once you have generated your new Python package project you can exit the
116211
cookiecutter virtual environment as it is no longer required.
117212

118-
.. code-block:: console
213+
.. code-block:: shell-session
119214
120215
(cc) $ deactivate
121216
$
122217
123218
219+
--------------------
124220
Manual Modifications
125221
--------------------
126222

@@ -141,6 +237,7 @@ using the new project.
141237
<https://pypi.python.org/pypi?:action=list_classifiers>`_.
142238

143239

240+
=======
144241
Example
145242
=======
146243

@@ -161,7 +258,7 @@ and hyphens in it. The package display name is first converted to lowercase
161258
text and then any spaces or hyphens are converted to underscores to produce a
162259
Python package name.
163260

164-
.. code-block:: console
261+
.. code-block:: shell-session
165262
166263
(cc) $ cookiecutter gh:boltronics/cookiecutter-python-project
167264
[1/10] package_display_name (Package-Name): abc 123
@@ -187,18 +284,18 @@ Python package name.
187284
188285
The project has been created in the ``abc_123`` directory.
189286

190-
.. code-block:: console
287+
.. code-block:: shell-session
191288
192289
$ cd abc_123
193290
194291
If you are planning to use git, it might be a good idea to create a
195292
new repository at this point.
196293

197-
.. code-block:: console
294+
.. code-block:: shell-session
198295
199296
$ git init
200297
$ git add .
201-
$ git commit -m 'Initial cookiecutter-python-project setup'
298+
$ git commit -m "Initial cookiecutter-python-project setup"
202299
203300
With that out of the way, it will be easy to use git to undo any
204301
potential mistakes made while experimenting.
@@ -210,7 +307,7 @@ First, let's enter a project-specific virtual environment. Hatch
210307
will install any of the project's dependencies (if added to pyproject.toml) as well as
211308
the project itself as an editable package.
212309

213-
.. code-block:: console
310+
.. code-block:: shell-session
214311
215312
$ hatch shell
216313
(abc_123) $
@@ -224,7 +321,7 @@ There are a number of other virtual environments available to you, and
224321
most of these have their own packages and scripts to ease
225322
development. You can bring up a summary like so:
226323

227-
.. code-block:: console
324+
.. code-block:: shell-session
228325
229326
$ hatch env show
230327
Standalone
@@ -261,7 +358,7 @@ development. You can bring up a summary like so:
261358
262359
You can enter use these virtual environments like so:
263360

264-
.. code-block:: console
361+
.. code-block:: shell-session
265362
266363
$ hatch shell types
267364
(types) $ pip freeze
@@ -294,7 +391,7 @@ If you have make installed, the included Makefile provides handy
294391
shortcuts for various Hatch commands and the configured scripts. You
295392
can print a summary of options via the `make help` command, like so:
296393

297-
.. code-block:: console
394+
.. code-block:: shell-session
298395
299396
$ make help
300397
@@ -323,10 +420,12 @@ can print a summary of options via the `make help` command, like so:
323420
dist-test - test a wheel distribution package
324421
dist-upload - upload a wheel distribution package
325422
423+
$
424+
326425
327426
Here is an example of one in action:
328427

329-
.. code-block:: console
428+
.. code-block:: shell-session
330429
331430
$ make test-verbose
332431
────────────────────────────── hatch-test.py3.13 ───────────────────────────────
@@ -384,6 +483,7 @@ Here is an example of one in action:
384483
$
385484
386485
486+
=====================================
387487
Suggestions? Contributions? Problems?
388488
=====================================
389489

{{cookiecutter.package_name}}/Makefile

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
# This makefile has been created to help developers perform common actions.
2-
# Most actions assume it is operating in a virtual environment where the
3-
# python command links to the appropriate virtual environment Python.
42

53
MAKEFLAGS += --no-print-directory
6-
GIT := $(shell command -v git)
4+
ifeq ($(OS),Windows_NT)
5+
UNAME_S := Windows
6+
else
7+
UNAME_S := $(shell uname -s)
8+
endif
9+
10+
# Set GIT variable based on the operating system
11+
ifeq ($(UNAME_S), Linux)
12+
GIT := $(shell command -v git)
13+
endif
14+
ifeq ($(UNAME_S), Darwin)
15+
GIT := $(shell command -v git)
16+
endif
17+
ifeq ($(UNAME_S), Windows)
18+
GIT := $(shell where git)
19+
endif
720

821

922
define CHECK_GIT
@@ -21,11 +34,16 @@ endef
2134

2235
# help: help - display makefile help information
2336
.PHONY: help
37+
ifeq ($(UNAME_S), Windows)
38+
help:
39+
@powershell -File scripts/generate_help.ps1 -MakefilePath Makefile
40+
else
2441
help:
2542
@grep "^#\shelp:" Makefile | \
2643
grep -v grep | \
2744
sed 's/\# help\: //' | \
2845
sed 's/\# help\://'
46+
endif
2947

3048

3149
# help: venv - enter a dev virtual environment
@@ -143,10 +161,18 @@ dist:
143161

144162
# help: dist-test - test a wheel distribution package
145163
.PHONY: dist-test
164+
ifeq ($(UNAME_S), Windows)
165+
dist-test: dist
166+
@cd dist && \
167+
@powershell \
168+
-File ../tests/test-dist.ps1 \
169+
./{{cookiecutter.package_name}}-*-py3-none-any.whl
170+
else
146171
dist-test: dist
147172
@cd dist && \
148173
../tests/test-dist.bash \
149174
./{{cookiecutter.package_name}}-*-py3-none-any.whl
175+
endif
150176

151177

152178
# help: dist-upload - upload a wheel distribution package

{{cookiecutter.package_name}}/pyproject.toml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,7 @@ extra-dependencies = [
111111

112112
[tool.hatch.envs.docs.scripts]
113113
build = [
114-
"""
115-
rm -rf \
116-
docs/source/api/sitemap_generator*.rst \
117-
docs/source/api/modules.rst \
118-
docs/build/*
119-
""",
114+
"python scripts/cleanup_docs.py",
120115
"sphinx-build -M html docs/source docs/build",
121116
]
122117
build-dummy = [
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Remove docs build files and generated API docs"""
2+
3+
import shutil
4+
from pathlib import Path
5+
6+
paths = [
7+
"docs/build",
8+
"docs/source/api/modules.rst",
9+
"docs/source/api/{{cookiecutter.package_name}}*.rst",
10+
]
11+
12+
for path in paths:
13+
p = Path(path)
14+
if p.exists():
15+
if p.is_dir():
16+
shutil.rmtree(p)
17+
else:
18+
p.unlink()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
param (
2+
[string]$MakefilePath = "Makefile"
3+
)
4+
5+
if (Test-Path $MakefilePath) {
6+
Get-Content $MakefilePath | Select-String '^#\shelp:' | ForEach-Object {
7+
$_.Line -replace '# help: ', '' -replace '# help:', ''
8+
}
9+
} else {
10+
Write-Error "Makefile not found at path: $MakefilePath"
11+
}

0 commit comments

Comments
 (0)