Skip to content

Commit

Permalink
Don't use filelock for unix systems
Browse files Browse the repository at this point in the history
  • Loading branch information
7x11x13 committed Jun 23, 2024
1 parent 196d065 commit 5f4ad55
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
2 changes: 1 addition & 1 deletion scdl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- encoding: utf-8 -*-
"""Python Soundcloud Music Downloader."""
__version__ = "v2.8.2"
__version__ = "v2.9.0"
54 changes: 32 additions & 22 deletions scdl/scdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import base64
import cgi
import configparser
import contextlib
import itertools
import logging
import math
Expand All @@ -91,7 +92,16 @@
import warnings
from dataclasses import asdict

import filelock
if os.name == "nt":
import filelock

FileLockTimeout = filelock.Timeout
else:

class FileLockTimeout:
pass


import mutagen
import mutagen.flac
import mutagen.id3
Expand Down Expand Up @@ -139,6 +149,13 @@ class PlaylistInfo(TypedDict):
title: str


def get_filelock(path: pathlib.Path, timeout: int = 10):
if os.name == "nt":
return filelock.FileLock(str(path) + ".scdl.lock", timeout=timeout)
else:
return contextlib.nullcontext()


def main():
"""
Main function, parses the URL from command line arguments
Expand Down Expand Up @@ -187,8 +204,7 @@ def main():
config["scdl"]["client_id"] = client.client_id
# save client_id
config_file.parent.mkdir(parents=True, exist_ok=True)
config_lock = filelock.FileLock(str(config_file) + ".scdl.lock", timeout=10)
with config_lock:
with get_filelock(config_file):
with open(config_file, "w", encoding="UTF-8") as f:
config.write(f)

Expand Down Expand Up @@ -306,9 +322,7 @@ def get_config(config_file: pathlib.Path) -> configparser.ConfigParser:

default_config_file = pathlib.Path(__file__).with_name("scdl.cfg")

config_lock = filelock.FileLock(str(config_file) + ".scdl.lock", timeout=10)

with config_lock:
with get_filelock(config_file):
# load default config first
config.read_file(open(default_config_file, encoding="UTF-8"))

Expand Down Expand Up @@ -469,8 +483,7 @@ def sync(
"""
logger.info("Comparing tracks...")
archive = kwargs.get("sync")
archive_lock = filelock.FileLock(archive + "scdl.lock", timeout=10)
with archive_lock:
with get_filelock(archive):
with open(archive) as f:
try:
old = [int(i) for i in "".join(f.readlines()).strip().split("\n")]
Expand Down Expand Up @@ -820,8 +833,7 @@ def download_track(
# Get user_id from the client
client_user_id = client.get_me().id if client.auth_token else None

lock_file = f"{track.id}.scdl.lock"
lock = filelock.FileLock(lock_file, 0)
lock = get_filelock(f"{track.id}", 0)

# Downloadable track
downloaded_original = False
Expand All @@ -839,8 +851,8 @@ def download_track(
client, track, title, playlist_info, **kwargs
)
downloaded_original = True
except filelock.Timeout:
logger.debug(f"Could not acquire lock: {lock_file}. Skipping")
except FileLockTimeout:
logger.debug(f"Could not acquire lock: {lock}. Skipping")
return

if filename is None:
Expand All @@ -851,8 +863,8 @@ def download_track(
filename, is_already_downloaded = download_hls(
client, track, title, playlist_info, **kwargs
)
except filelock.Timeout:
logger.debug(f"Could not acquire lock: {lock_file}. Skipping")
except FileLockTimeout:
logger.debug(f"Could not acquire lock: {lock}. Skipping")
return

if kwargs.get("remove"):
Expand Down Expand Up @@ -938,13 +950,12 @@ def in_download_archive(track: BasicTrack, **kwargs):
"""
Returns True if a track_id exists in the download archive
"""
if not kwargs.get("download_archive"):
archive_filename = kwargs.get("download_archive")
if not archive_filename:
return

archive_filename = kwargs.get("download_archive")
archive_lock = filelock.FileLock(archive_filename + ".scdl.lock", timeout=10)
try:
with archive_lock:
with get_filelock(archive_filename):
with open(archive_filename, "a+", encoding="utf-8") as file:
file.seek(0)
track_id = str(track.id)
Expand All @@ -962,13 +973,12 @@ def record_download_archive(track: BasicTrack, **kwargs):
"""
Write the track_id in the download archive
"""
if not kwargs.get("download_archive"):
archive_filename = kwargs.get("download_archive")
if not archive_filename:
return

archive_filename = kwargs.get("download_archive")
archive_lock = filelock.FileLock(archive_filename + "scdl.lock", timeout=10)
try:
with archive_lock:
with get_filelock(archive_filename):
with open(archive_filename, "a", encoding="utf-8") as file:
file.write(f"{track.id}\n")
except IOError as ioe:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"clint",
"pathvalidate",
"soundcloud-v2>=1.3.10",
"filelock>=3.0.0",
"filelock>=3.0.0; platform_system=='Windows'",
],
extras_require={"test": ["coveralls", "pytest", "pytest-dotenv", "music-tag"]},
url="https://github.com/flyingrub/scdl",
Expand Down

0 comments on commit 5f4ad55

Please sign in to comment.