diff --git a/withlock b/withlock deleted file mode 100644 index a8fca3a..0000000 --- a/withlock +++ /dev/null @@ -1,201 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2009,2010 Peter Poeml -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# -# -# withlock -- http://code.google.com/p/withlock/ -# -# -# A locking wrapper that make sure that a program isn't run more than once. -# It creates locks that are valid only while the wrapper is running, and thus -# will never require a cleanup, e.g. after a reboot. - -# Parts of the locking strategy, and parts of the usage semantics, of this -# script were inspired from with-lock-ex.c, which was written by Ian Jackson and -# placed in the public domain by him. -# -# -# Usage is simple. Instead of your command -# CMD ARGS... -# you simply use -# withlock LOCKFILE CMD ARGS... -# -# See --help output for more options. - - -__version__ = '0.2' - -import os -import os.path -import errno -import sys -import time -import stat -import fcntl -import signal -import atexit -from optparse import OptionParser - - -class SignalInterrupt(Exception): - """Exception raised on SIGTERM and SIGHUP.""" - - -def catchterm(*args): - raise SignalInterrupt - - -for name in 'SIGBREAK', 'SIGHUP', 'SIGTERM': - num = getattr(signal, name, None) - if num: signal.signal(num, catchterm) - - -def cleanup(lockfile): - try: - os.unlink(lockfile) - except: - pass - - -def main(): - - usage = 'usage: %prog [options] LOCKFILE CMD ARGS...' - version = '%prog ' + __version__ - - parser = OptionParser(usage=usage, version=version) - parser.disable_interspersed_args() - - parser.add_option('-w', '--wait', - dest='wait', - help="wait for maximum SECONDS until the lock is acquired", - metavar="SECONDS") - - parser.add_option("-q", "--quiet", - action="store_true", dest="quiet", default=False, - help="if lock can't be acquired immediately, silently " - "quit without error") - - parser.add_option("-v", "--verbose", - action="store_true", dest="verbose", default=False, - help="print debug messages to stderr") - - - - (options, args) = parser.parse_args() - - usage = usage.replace('%prog', os.path.basename(sys.argv[0])) - - - if len(args) < 2: - sys.exit(usage) - - lockfile = args[0] - cmd = args[1:] - waited = 0 - if options.wait: - options.wait = int(options.wait) - - if options.verbose: - sys.stderr.write('lockfile: %r\n' % lockfile) - sys.stderr.write('cmd: %r\n' % cmd) - - # check that symlink attacks in the lockfile directory are not possible - lockdir = os.path.dirname(os.path.realpath(lockfile)) or os.curdir - lockdir_mode = stat.S_IMODE(os.stat(lockdir).st_mode) - if options.verbose: - sys.stderr.write('lockdir: %r\n' % lockdir) - sys.stderr.write('lockdir mode: %s\n' % oct(lockdir_mode)) - if (lockdir_mode & stat.S_IWGRP) or (lockdir_mode & stat.S_IWOTH): - sys.stderr.write('withlock: the destination directory for %r is %r, \n' - ' which is writable by other users. That allows for symlink attacks. \n' - ' Choose another directory.\n' \ - % (lockfile, lockdir)) - sys.exit(3) - - - prev_umask = os.umask(0066) - - lock = open(lockfile, 'w') - - while 1 + 1 == 2: - - try: - fcntl.lockf(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) - break - except IOError, e: - if e.errno in [ errno.EAGAIN, - errno.EACCES, - errno.EWOULDBLOCK, - errno.EBUSY ]: - if options.wait: - if waited >= options.wait: - if options.quiet: - if options.verbose: - sys.stderr.write('quitting silently\n') - sys.exit(0) - else: - sys.stderr.write('could not acquire lock\n') - sys.exit(1) - if options.verbose and waited == 0: - sys.stderr.write('waiting up to %ss for lock\n' \ - % options.wait) - time.sleep(1) - waited += 1 - continue - elif options.quiet: - if options.verbose: - sys.stderr.write('quitting silently\n') - sys.exit(0) - else: - sys.exit('could not acquire lock') - - try: - os.stat(lockfile) - break - except OSError, e: - if e.errno == errno.ENOENT: - sys.exit('====== could not stat %s, which we have just locked' \ - % lockfile) - - - atexit.register(cleanup, lockfile) - os.umask(prev_umask) - - import subprocess - rc = subprocess.call(' '.join(cmd), shell=True) - sys.stdout.flush() - sys.stderr.flush() - - if options.verbose: - sys.stderr.write('command terminated with exit code %s\n' % rc) - - if options.verbose: - sys.stderr.write('removing lockfile\n') - - sys.exit(rc) - - -if __name__ == '__main__': - - try: - main() - - except SignalInterrupt: - print >>sys.stderr, 'killed!' - - except KeyboardInterrupt: - print >>sys.stderr, 'interrupted!' - diff --git a/withlock-0.2.tar.gz b/withlock-0.2.tar.gz new file mode 100644 index 0000000..466f47c --- /dev/null +++ b/withlock-0.2.tar.gz @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9395ecbed705b17db4472b884aa31c28dd14d9727733c9cabe4678483a758cde +size 2336 diff --git a/withlock.changes b/withlock.changes index ba7562e..0543320 100644 --- a/withlock.changes +++ b/withlock.changes @@ -1,3 +1,10 @@ +------------------------------------------------------------------- +Mon Feb 10 21:49:18 UTC 2014 - poeml@cmdline.net + +- withlock is now distributed as a tarball, which is generally + easier for packaging on some platforms (at least Solaris). The + only other change: updated copyright year. + ------------------------------------------------------------------- Mon Aug 27 09:07:26 UTC 2012 - cfarrell@suse.com diff --git a/withlock.spec b/withlock.spec index b61d7a5..72da357 100644 --- a/withlock.spec +++ b/withlock.spec @@ -1,7 +1,7 @@ # # spec file for package withlock # -# Copyright (c) 2009-2012 SUSE LINUX Products GmbH, Nuernberg, Germany. +# Copyright (c) 2014 SUSE LINUX Products GmbH, Nuernberg, Germany. # # All modifications and additions to the file contributed by third parties # remain the property of their copyright owners, unless otherwise agreed @@ -15,15 +15,16 @@ # Please submit bugfixes or comments via http://bugs.opensuse.org/ # + Name: withlock Version: 0.2 -Release: 1 -License: Apache-2.0 +Release: 0 Summary: A locking wrapper script -Url: http://code.google.com/p/withlock/ +License: Apache-2.0 Group: System/Management +Url: http://code.google.com/p/withlock/ Requires: python -Source0: %{name} +Source0: http://mirrorbrain.org/files/releases/%{name}-%{version}.tar.gz Source1: %name-README.SuSE BuildRoot: %{_tmppath}/%{name}-%{version}-build BuildArch: noarch @@ -38,11 +39,12 @@ never require a cleanup, as after a reboot. Thus, the wrapper is safe and easy to use, and much better than implementing half-hearted locking within scripts. %prep +%setup # %build # %install -install -Dm755 %{SOURCE0} %{buildroot}%{_bindir}/%{name} +install -Dm755 withlock %{buildroot}%{_bindir}/%{name} install -Dm644 %{SOURCE1} %{buildroot}%{_defaultdocdir}/%{name}/README.SuSE sed -i "s|/usr/bin/env python|%{_bindir}/python|g" %{buildroot}%{_bindir}/%{name}