#!/usr/bin/python
# yum -- Yum rpm-updating Client (Yellowdog Updater Modified)
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Copyright 2002 Duke University 


import sys
import os
sys.path.insert(1,'/usr/share/yum')
import yummain
import yumlock

uid = os.geteuid()
lockfile = '/var/run/yum.pid'
mypid = str(os.getpid())

if uid == 0:
    #check out/get the lockfile
    if yumlock.lock(lockfile, mypid, 0644):
        pass
    else:
        fd = open(lockfile, 'r')
        oldpid = fd.readline()
        if not os.path.exists('/proc/' + oldpid):
            os.unlink(lockfile)
            yumlock.lock(lockfile, mypid, 0644)
        else:
            print 'Existing lock %s. Aborting.' % lockfile
            sys.exit(1)

try:
    yummain.main(sys.argv[1:])
except SystemExit, e:
    if uid == 0:
        yumlock.unlock(lockfile)
    sys.exit(e)
except KeyboardInterrupt, e:
    print 'Exiting on User Cancel'
    if uid == 0:
        yumlock.unlock(lockfile)
    sys.exit(1)
except IOError, (errno, strerror):
    print 'IOError - # %s - %s' % (errno, strerror)
    if uid == 0:
        yumlock.unlock(lockfile)
    sys.exit(1)


