initial commit
This commit is contained in:
37
functions/helpers.py
Executable file
37
functions/helpers.py
Executable file
@@ -0,0 +1,37 @@
|
||||
"""Misc helper functions for seafile mirror"""
|
||||
|
||||
import logging
|
||||
import socket
|
||||
import sys
|
||||
|
||||
|
||||
def get_lock(process_name):
|
||||
"""Get the lock for this program to avoid double-execution"""
|
||||
# Without holding a reference to our socket somewhere it gets garbage
|
||||
# collected when the function exits
|
||||
# pylint: disable=protected-access
|
||||
get_lock._lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
|
||||
|
||||
try:
|
||||
# The null byte (\0) means the socket is created
|
||||
# in the abstract namespace instead of being created
|
||||
# on the file system itself.
|
||||
# Works only in Linux
|
||||
# pylint: disable=protected-access
|
||||
get_lock._lock_socket.bind("\0" + process_name)
|
||||
except socket.error:
|
||||
logging.critical("This script is already executed in another instance. Abort.")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def findstring(text, string):
|
||||
"""Check if a certain string exists in an output"""
|
||||
if text.find(string) >= 0:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def countlines(string: str) -> int:
|
||||
"""Count number of lines in a variable"""
|
||||
return len(string.splitlines())
|
||||
Reference in New Issue
Block a user