Skip to content

Commit 61ea30a

Browse files
committed
Implement pyinstaller spec for generating
1 parent 1aa4bda commit 61ea30a

File tree

5 files changed

+89
-4
lines changed

5 files changed

+89
-4
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,6 @@ deb_dist/
361361
# Usually these files are written by a python script from a template
362362
# before PyInstaller builds the exe, so as to inject date/other infos into it.
363363
*.manifest
364-
*.spec
365364

366365
# Installer logs
367366
pip-log.txt

CONTRIBUTORS.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
* almost everything!
55

66
**Nicholas Car**
7-
* a few small admin bits and pieces
7+
* a few small admin bits and pieces
8+
9+
**Jonathan Yu**
10+
* pyinstaller spec for compiling pySHACL cli as a Windows binary
11+

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,29 @@ optional arguments:
9393
Send output to a file (defaults to stdout).
9494
```
9595
96+
## Windows CLI
97+
98+
[Pyinstaller](https://www.pyinstaller.org/) can be
99+
[used](https://pyinstaller.readthedocs.io/en/stable/usage.html) to create an
100+
executable for Windows that has the same characteristics as the Linux/Mac
101+
CLI program.
102+
The necessary ``.spec`` file is already included in ``pyshacl/pyshacl-cli.spec``.
103+
The ``pyshacl-cli.spec`` PyInstaller spec file creates a ``.exe`` for the
104+
pySHACL Command Line utility. See above for the pySHACL command line util usage instructions.
105+
106+
See [the PyInstaller installation guide](https://pyinstaller.readthedocs.io/en/stable/installation.html#installing-in-windows) for info on how to install PyInstaller for Windows.
107+
108+
Once you have pyinstaller, use pyinstaller to generate the ``pyshacl.exe`` CLI file like so:
109+
```
110+
$ cd src/pyshacl
111+
$ pyinstaller pyshacl-cli.spec
112+
```
113+
This will output ``pyshacl.exe`` in the ``dist`` directory in ``src/pyshacl``.
114+
115+
You can now run the pySHACL Command Line utility via ``pyshacl.exe``.
116+
See above for the pySHACL command line util usage instructions.
117+
118+
96119
## Python Module Use
97120
For basic use of this module, you can just call the `validate` function of the `pyshacl` module like this:
98121

pyshacl/pyshacl-cli.spec

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# -*- mode: python ; coding: utf-8 -*-
2+
## run `pyinstaller pyshacl-cli.spec` to create `dist/pyshacl.exe` dist
3+
## note it requires pywin32
4+
5+
block_cipher = None
6+
7+
a = Analysis(
8+
['cli.py'],
9+
pathex=['.'],
10+
binaries=[
11+
('shacl-shacl.pickle','.')
12+
],
13+
datas=[
14+
],
15+
hiddenimports=[
16+
'rdflib.plugins',
17+
'rdflib',
18+
'urllib3',
19+
'rdflib_jsonld',
20+
'win32com.gen_py',
21+
'pkg_resources.py2_warn'
22+
],
23+
hookspath=[],
24+
runtime_hooks=[],
25+
excludes=[],
26+
win_no_prefer_redirects=False,
27+
win_private_assemblies=False,
28+
cipher=block_cipher,
29+
noarchive=False
30+
)
31+
32+
pyz = PYZ(
33+
a.pure,
34+
a.zipped_data,
35+
cipher=block_cipher
36+
)
37+
38+
exe = EXE(
39+
pyz,
40+
a.scripts,
41+
a.binaries,
42+
a.zipfiles,
43+
a.datas,
44+
[],
45+
name='pyshacl',
46+
debug=False,
47+
bootloader_ignore_signals=False,
48+
strip=False,
49+
upx=True,
50+
upx_exclude=[],
51+
runtime_tmpdir=None,
52+
console=True
53+
)

pyshacl/validate.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,14 @@ def meta_validate(shacl_graph, inference='rdfs', **kwargs):
199199
if shacl_shacl_graph is None:
200200
from os import path
201201
import pickle
202-
here_dir = path.dirname(__file__)
203-
pickle_file = path.join(here_dir, "shacl-shacl.pickle")
202+
import sys
203+
if getattr( sys, 'frozen', False ) :
204+
# runs in a pyinstaller bundle
205+
here_dir = sys._MEIPASS
206+
pickle_file = path.join(here_dir, "shacl-shacl.pickle")
207+
else :
208+
here_dir = path.dirname(__file__)
209+
pickle_file = path.join(here_dir, "shacl-shacl.pickle")
204210
with open(pickle_file, 'rb') as shacl_pickle:
205211
u = pickle.Unpickler(shacl_pickle, fix_imports=False)
206212
shacl_shacl_store = u.load()

0 commit comments

Comments
 (0)