Skip to content

Commit 06847de

Browse files
authored
migrate from setup.py to pyproject.toml (slgobinath#639)
* migrate from setup.py to pyproject.toml * read version from pyproject.toml * update release action to use python -m build * fix typos * setup.py: refactor mo building to use pathlib * fix deprecation notice from setuptools * remove empty line
1 parent 7154b50 commit 06847de

File tree

6 files changed

+134
-112
lines changed

6 files changed

+134
-112
lines changed

.github/workflows/release.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ jobs:
2424
pip install build wheel
2525
2626
- name: Get Current Version
27-
run: |
28-
project_version=$(python3 setup.py --version)
29-
echo "project_version=$project_version" >> $GITHUB_OUTPUT
27+
uses: SebRollen/[email protected]
28+
with:
29+
file: "pyproject.toml"
30+
field: project.version
3031
id: get_current_version
3132

3233
- name: Create Tag
3334
uses: mathieudutour/[email protected]
3435
with:
35-
custom_tag: "${{steps.get_current_version.outputs.project_version}}"
36+
custom_tag: "${{steps.get_current_version.outputs.value}}"
3637
github_token: ${{ secrets.GH_API_SECRET }}
3738

3839
- name: Build Changelog
@@ -44,7 +45,7 @@ jobs:
4445
- name: Create Release
4546
uses: softprops/action-gh-release@v1
4647
with:
47-
tag_name: 'v${{steps.get_current_version.outputs.project_version}}'
48+
tag_name: 'v${{steps.get_current_version.outputs.value}}'
4849
body: ${{steps.build_changelog.outputs.changelog}}
4950
token: ${{ secrets.GH_API_SECRET }}
5051

MANIFEST.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
include LICENSES/GPL-3.0-or-later.txt
22
include README.md
3+
4+
graft safeeyes
5+
6+
global-exclude *.py[cod]

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,8 @@ To ensure the new strings are well-formed, you can use `python validate_po.py --
200200
1. Checkout the latest commits from the `master` branch
201201
2. Run `python3 -m safeeyes` to make sure nothing is broken
202202
3. Update the Safe Eyes version in the following places (Open the project in VSCode and search for the current version):
203-
- [setup.py](https://github.com/slgobinath/SafeEyes/blob/master/setup.py#L82)
204-
- [setup.py](https://github.com/slgobinath/SafeEyes/blob/master/setup.py#L89)
205-
- [safeeyes.py](https://github.com/slgobinath/SafeEyes/blob/master/safeeyes/safeeyes.py#L42)
203+
- [pyproject.toml](https://github.com/slgobinath/SafeEyes/blob/master/pyproject.toml#L4)
204+
- [pyproject.toml](https://github.com/slgobinath/SafeEyes/blob/master/pyproject.toml#L35)
206205
- [io.github.slgobinath.SafeEyes.metainfo.xml](https://github.com/slgobinath/SafeEyes/blob/master/safeeyes/platform/io.github.slgobinath.SafeEyes.metainfo.xml#L56)
207206
- [about_dialog.glade](https://github.com/slgobinath/SafeEyes/blob/master/safeeyes/glade/about_dialog.glade#L74)
208207
4. Update the [changelog](https://github.com/slgobinath/SafeEyes/blob/master/debian/changelog) (for Ubuntu PPA release)

pyproject.toml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
[project]
2+
name = "safeeyes"
3+
version = "2.2.3"
4+
description = "Protect your eyes from eye strain using this continuous breaks reminder."
5+
keywords = ["linux utility health eye-strain safe-eyes"]
6+
readme = "README.md"
7+
authors = [
8+
{name = "Gobinath Loganathan", email = "[email protected]"},
9+
]
10+
classifiers = [
11+
"Development Status :: 5 - Production/Stable",
12+
"Environment :: X11 Applications :: GTK",
13+
"Environment :: Other Environment",
14+
"Intended Audience :: End Users/Desktop",
15+
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
16+
"Operating System :: POSIX :: Linux",
17+
"Programming Language :: Python :: 3",
18+
"Programming Language :: Python :: 3.10",
19+
"Programming Language :: Python :: 3.11",
20+
"Programming Language :: Python :: 3.12",
21+
"Topic :: Utilities",
22+
]
23+
dependencies = [
24+
"pywayland",
25+
"PyGObject",
26+
"babel",
27+
"psutil",
28+
"packaging",
29+
"python-xlib",
30+
]
31+
requires-python = ">=3.10"
32+
33+
[project.urls]
34+
Homepage = "https://github.com/slgobinath/SafeEyes"
35+
Downloads = "https://github.com/slgobinath/SafeEyes/archive/v2.2.3.tar.gz"
36+
37+
[project.scripts]
38+
safeeyes = "safeeyes.__main__:main"
39+
40+
[project.optional-dependencies]
41+
healthstats = ["croniter"]
42+
43+
[build-system]
44+
requires = ["setuptools"]
45+
build-backend = "setuptools.build_meta"
46+
47+
[tool.setuptools.packages.find]
48+
include=["safeeyes*"]

safeeyes/safeeyes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import logging
2525
import os
2626
from threading import Timer
27+
from importlib import metadata
2728

2829
import gi
2930
from safeeyes import utility
@@ -39,7 +40,7 @@
3940
gi.require_version('Gtk', '4.0')
4041
from gi.repository import Gtk, Gio, GLib
4142

42-
SAFE_EYES_VERSION = "2.2.3"
43+
SAFE_EYES_VERSION = metadata.version("safeeyes")
4344

4445

4546
class SafeEyes(Gtk.Application):

setup.py

Lines changed: 72 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,73 @@
1-
import os, sys, site
2-
import subprocess
3-
import setuptools
4-
5-
6-
requires = [
7-
'pywayland',
8-
'babel',
9-
'psutil',
10-
'croniter',
11-
'PyGObject',
12-
'packaging',
13-
'python-xlib'
14-
]
15-
16-
_ROOT = os.path.abspath(os.path.dirname(__file__))
17-
18-
with open(os.path.join(_ROOT, 'README.md')) as f:
19-
long_description = f.read()
20-
21-
22-
def __compile_po_files():
23-
"""
24-
Compile the *.po trainslation files.
25-
"""
26-
localedir = 'safeeyes/config/locale'
27-
po_dirs = [localedir + '/' + l + '/LC_MESSAGES/'
28-
for l in next(os.walk(localedir))[1]]
29-
for po_dir in po_dirs:
30-
po_files = [f
31-
for f in next(os.walk(po_dir))[2]
32-
if os.path.splitext(f)[1] == '.po']
33-
for po_file in po_files:
34-
filename, _ = os.path.splitext(po_file)
35-
mo_file = filename + '.mo'
36-
msgfmt_cmd = 'msgfmt {} -o {}'.format(
37-
po_dir + po_file, po_dir + mo_file)
38-
subprocess.call(msgfmt_cmd, shell=True)
39-
40-
41-
def __data_files():
42-
"""
43-
Collect the data files.
44-
"""
45-
root_dir = sys.prefix
46-
return [(os.path.join(root_dir, "share/applications"), ["safeeyes/platform/io.github.slgobinath.SafeEyes.desktop"]),
47-
(os.path.join(root_dir, "share/icons/hicolor/24x24/status"), ["safeeyes/platform/icons/hicolor/24x24/status/io.github.slgobinath.SafeEyes-disabled.png", "safeeyes/platform/icons/hicolor/24x24/status/io.github.slgobinath.SafeEyes-enabled.png", "safeeyes/platform/icons/hicolor/24x24/status/io.github.slgobinath.SafeEyes-timer.png"]),
48-
(os.path.join(root_dir, "share/icons/hicolor/24x24/apps"), ["safeeyes/platform/icons/hicolor/24x24/apps/io.github.slgobinath.SafeEyes.png"]),
49-
(os.path.join(root_dir, "share/icons/hicolor/16x16/status"), ["safeeyes/platform/icons/hicolor/16x16/status/io.github.slgobinath.SafeEyes-disabled.png", "safeeyes/platform/icons/hicolor/16x16/status/io.github.slgobinath.SafeEyes-enabled.png", "safeeyes/platform/icons/hicolor/16x16/status/io.github.slgobinath.SafeEyes-timer.png"]),
50-
(os.path.join(root_dir, "share/icons/hicolor/16x16/apps"), ["safeeyes/platform/icons/hicolor/16x16/apps/io.github.slgobinath.SafeEyes.png"]),
51-
(os.path.join(root_dir, "share/icons/hicolor/32x32/status"), ["safeeyes/platform/icons/hicolor/32x32/status/io.github.slgobinath.SafeEyes-disabled.png", "safeeyes/platform/icons/hicolor/32x32/status/io.github.slgobinath.SafeEyes-enabled.png"]),
52-
(os.path.join(root_dir, "share/icons/hicolor/32x32/apps"), ["safeeyes/platform/icons/hicolor/32x32/apps/io.github.slgobinath.SafeEyes.png"]),
53-
(os.path.join(root_dir, "share/icons/hicolor/64x64/apps"), ["safeeyes/platform/icons/hicolor/64x64/apps/io.github.slgobinath.SafeEyes.png"]),
54-
(os.path.join(root_dir, "share/icons/hicolor/128x128/apps"), ["safeeyes/platform/icons/hicolor/128x128/apps/io.github.slgobinath.SafeEyes.png"]),
55-
(os.path.join(root_dir, "share/icons/hicolor/48x48/status"), ["safeeyes/platform/icons/hicolor/48x48/status/io.github.slgobinath.SafeEyes-disabled.png", "safeeyes/platform/icons/hicolor/48x48/status/io.github.slgobinath.SafeEyes-enabled.png"]),
56-
(os.path.join(root_dir, "share/icons/hicolor/48x48/apps"), ["safeeyes/platform/icons/hicolor/48x48/apps/io.github.slgobinath.SafeEyes.png"]),]
57-
58-
59-
def __package_files(directory):
60-
"""
61-
Collect the package files.
62-
"""
63-
paths = []
64-
for (path, _, filenames) in os.walk(directory):
65-
for filename in filenames:
66-
paths.append(os.path.join('..', path, filename))
67-
return paths
68-
69-
70-
def __package_data():
71-
"""
72-
Return a list of package data.
73-
"""
74-
__compile_po_files()
75-
data = ['glade/*.glade', 'resource/*']
76-
data.extend(__package_files('safeeyes/config'))
77-
data.extend(__package_files('safeeyes/plugins'))
78-
data.extend(__package_files('safeeyes/platform'))
79-
return data
80-
81-
setuptools.setup(
82-
name="safeeyes",
83-
version="2.2.3",
84-
description="Protect your eyes from eye strain using this continuous breaks reminder.",
85-
long_description=long_description,
86-
long_description_content_type="text/markdown",
87-
author="Gobinath Loganathan",
88-
author_email="[email protected]",
89-
url="https://github.com/slgobinath/SafeEyes",
90-
download_url="https://github.com/slgobinath/SafeEyes/archive/v2.2.3.tar.gz",
91-
packages=setuptools.find_packages(),
92-
package_data={'safeeyes': __package_data()},
93-
data_files=__data_files(),
94-
install_requires=requires,
95-
entry_points={'console_scripts': ['safeeyes = safeeyes.__main__:main']},
96-
keywords='linux utility health eye-strain safe-eyes',
97-
classifiers=[
98-
"Operating System :: POSIX :: Linux",
99-
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
100-
"Development Status :: 5 - Production/Stable",
101-
"Environment :: X11 Applications :: GTK",
102-
"Intended Audience :: End Users/Desktop",
103-
"Topic :: Utilities"] + [('Programming Language :: Python :: %s' % x) for x in '3 3.5 3.6 3.7 3.8 3.9'.split()]
1+
#!/usr/bin/python3
2+
3+
import os
4+
5+
from pathlib import Path
6+
from setuptools import Command, setup
7+
from setuptools.command.build import build as OriginalBuildCommand
8+
9+
class BuildCommand(OriginalBuildCommand):
10+
sub_commands = [('build_mo', None), *OriginalBuildCommand.sub_commands]
11+
12+
13+
class BuildMoSubCommand(Command):
14+
description = 'Compile .po files into .mo files'
15+
16+
files = None
17+
18+
def initialize_options(self):
19+
self.files = None
20+
self.editable_mode = False
21+
self.build_lib = None
22+
23+
def finalize_options(self):
24+
self.set_undefined_options("build_py", ("build_lib", "build_lib"))
25+
26+
def run(self):
27+
files = self._get_files()
28+
29+
for build_file, source_file in files.items():
30+
if not self.editable_mode:
31+
# Parent directory required for msgfmt to work correctly
32+
Path(build_file).parent.mkdir(parents=True, exist_ok=True)
33+
self.spawn(['msgfmt', source_file, '-o', build_file])
34+
35+
def _get_files(self):
36+
if self.files is not None:
37+
return self.files
38+
39+
files = {}
40+
41+
localedir = Path('safeeyes/config/locale')
42+
po_dirs = [l.joinpath('LC_MESSAGES') for l in localedir.iterdir() if l.is_dir()]
43+
for po_dir in po_dirs:
44+
po_files = [f
45+
for f in po_dir.iterdir()
46+
if f.is_file() and f.suffix == '.po']
47+
for po_file in po_files:
48+
mo_file = po_file.with_suffix(".mo")
49+
50+
source_file = po_file
51+
build_file = mo_file
52+
53+
if not self.editable_mode:
54+
build_file = Path(self.build_lib).joinpath(build_file)
55+
56+
files[str(build_file)] = str(source_file)
57+
58+
self.files = files
59+
return files
60+
61+
def get_output_mapping(self):
62+
return self._get_files()
63+
64+
def get_outputs(self):
65+
return self._get_files().keys()
66+
67+
def get_source_files(self):
68+
return self._get_files().values()
69+
70+
71+
setup(
72+
cmdclass={'build': BuildCommand, 'build_mo': BuildMoSubCommand}
10473
)

0 commit comments

Comments
 (0)