Skip to content

Commit

Permalink
Remove support for Windows (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Nov 11, 2024
1 parent 6a3a7a6 commit b8f2cd3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 46 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
not provided by Python.

- [transparent Unicode support for text file operations (BOM detection)](https://tendo.readthedocs.io/#module-tendo.singleton)
- enable you to use symlinks under windows
- python tee implementation
- [improved execfile](https://tendo.readthedocs.io/#module-tendo.execfile2)

## Requirements and Compatibility

- POSIX-compatible operating system, including Linux and macOS
- python 3.10 or newer
- tox for running tests

Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ classifiers = [
"Intended Audience :: Information Technology",
"Intended Audience :: System Administrators",
"License :: OSI Approved :: Python Software Foundation License",
"Operating System :: OS Independent",
"Operating System :: MacOS",
"Operating System :: POSIX :: Linux",
"Operating System :: POSIX",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
Expand Down
46 changes: 12 additions & 34 deletions src/tendo/singleton.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#! /usr/bin/env python

import fcntl
import logging
import os
import sys
import tempfile

if sys.platform != "win32":
import fcntl


class SingleInstanceException(BaseException):
pass
Expand Down Expand Up @@ -46,28 +44,13 @@ def __init__(self, flavor_id="", lockfile=""):
logger.debug(f"SingleInstance lockfile: {self.lockfile}")

def __enter__(self):
if sys.platform == "win32":
try:
# file already exists, we try to remove (in case previous
# execution was interrupted)
if os.path.exists(self.lockfile):
os.unlink(self.lockfile)
self.fd = os.open(self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR)
except OSError:
type, e, tb = sys.exc_info()
if e.errno == 13:
logger.error("Another instance is already running, quitting.")
raise SingleInstanceException
print(e.errno)
raise
else: # non Windows
self.fp = open(self.lockfile, "w")
self.fp.flush()
try:
fcntl.lockf(self.fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
except OSError:
logger.warning("Another instance is already running, quitting.")
raise SingleInstanceException
self.fp = open(self.lockfile, "w")
self.fp.flush()
try:
fcntl.lockf(self.fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
except OSError:
logger.warning("Another instance is already running, quitting.")
raise SingleInstanceException
self.initialized = True
return self

Expand All @@ -77,15 +60,10 @@ def __exit__(self, exc_type, exc_value, exc_tb):
if exc_value is not None:
logger.warning("Error: %s" % exc_value, exc_info=True)
try:
if sys.platform == "win32":
if hasattr(self, "fd"):
os.close(self.fd)
os.unlink(self.lockfile)
else:
fcntl.lockf(self.fp, fcntl.LOCK_UN)
# os.close(self.fp)
if os.path.isfile(self.lockfile):
os.unlink(self.lockfile)
fcntl.lockf(self.fp, fcntl.LOCK_UN)
# os.close(self.fp)
if os.path.isfile(self.lockfile):
os.unlink(self.lockfile)
except Exception as e:
if logger:
logger.warning(e)
Expand Down
10 changes: 3 additions & 7 deletions src/tendo/tee.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ def quote_command(cmd):
This is required in order to prevent getting "The input line is too long" error message.
"""
if not (os.name == "nt" or os.name == "dos"):
# the escaping is required only on Windows platforms, in fact it will
# break cmd line on others
return cmd
if '"' in cmd[1:-1]:
cmd = '"' + cmd + '"'
return cmd
Expand Down Expand Up @@ -247,13 +243,13 @@ def test_1(self):
os.name = save

def test_2(self):
self.assertEqual(system(["python", "-V"]), 0)
self.assertEqual(system([sys.executable, "-V"]), 0)

def test_3(self):
self.assertEqual(system2(["python", "-V"])[0], 0)
self.assertEqual(system2([sys.executable, "-V"])[0], 0)

def test_4(self):
self.assertEqual(system(["python", "-c", "print('c c')"]), 0)
self.assertEqual(system([sys.executable, "-c", "print('c c')"]), 0)


if __name__ == "__main__":
Expand Down
7 changes: 4 additions & 3 deletions src/tendo/tests/test_tee.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys

from tendo.tee import quote_command, system, system2

Expand Down Expand Up @@ -36,12 +37,12 @@ def test_1():


def test_2():
assert system(["python", "-V"]) == 0
assert system([sys.executable, "-V"]) == 0


def test_3():
assert system2(["python", "-V"])[0] == 0
assert system2([sys.executable, "-V"])[0] == 0


def test_4():
assert system(["python", "-c", "print('c c')"]) == 0
assert system([sys.executable, "-c", "print()"]) == 0

0 comments on commit b8f2cd3

Please sign in to comment.