From 4f177d9d93b48ab523538265bdf0b25b7db08db7 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Oct 26 2015 10:04:47 +0000 Subject: Port the server and the get_repo_md script to use the file_lock module --- diff --git a/get_repo_md.py b/get_repo_md.py index 7eddfbe..3735f6d 100644 --- a/get_repo_md.py +++ b/get_repo_md.py @@ -44,6 +44,8 @@ import tempfile import requests +import file_lock + KOJI_REPO = 'https://kojipkgs.fedoraproject.org/repos/' PKGDB2_URL = 'https://admin.fedoraproject.org/pkgdb/' @@ -64,22 +66,26 @@ def decompress_primary_db(archive, location): import lzma with contextlib.closing(lzma.LZMAFile(archive)) as stream_xz: data = stream_xz.read() - with open(location, 'wb') as stream: - stream.write(data) + with file_lock.FileFlock(location + '.lock'): + with open(location, 'wb') as stream: + stream.write(data) elif archive.endswith('.gz'): import tarfile - with tarfile.open(archive) as tar: - tar.extractall(path=location) + with file_lock.FileFlock(location + '.lock'): + with tarfile.open(archive) as tar: + tar.extractall(path=location) elif archive.endswith('.bz2'): import bz2 - with open(location, 'wb') as out: - bzar = bz2.BZ2File(archive) - out.write(bzar.read()) - bzar.close() + with file_lock.FileFlock(location + '.lock'): + with open(location, 'wb') as out: + bzar = bz2.BZ2File(archive) + out.write(bzar.read()) + bzar.close() elif archive.endswith('.sqlite'): - with open(location, 'wb') as out: - with open(archive) as inp: - out.write(inp.read()) + with file_lock.FileFlock(location + '.lock'): + with open(location, 'wb') as out: + with open(archive) as inp: + out.write(inp.read()) def process_repo(tupl): diff --git a/mdapi/__init__.py b/mdapi/__init__.py index 4250773..538b589 100644 --- a/mdapi/__init__.py +++ b/mdapi/__init__.py @@ -33,6 +33,7 @@ import asyncio from aiohttp import web import lib as mdapilib +import file_lock CONFIG = dict() @@ -67,15 +68,17 @@ def _get_pkg(branch, name): CONFIG['DB_FOLDER'], branch, repotype) else: dbfile = '%s/mdapi-%s-primary.sqlite' % (CONFIG['DB_FOLDER'], branch) + if not os.path.exists(dbfile): wrongdb = True continue wrongdb = False - session = mdapilib.create_session('sqlite:///%s' % dbfile) - pkg = mdapilib.get_package(session, name) - session.close() + with file_lock.FileFlock(dbfile + '.lock'): + session = mdapilib.create_session('sqlite:///%s' % dbfile) + pkg = mdapilib.get_package(session, name) + session.close() if pkg: break @@ -98,16 +101,18 @@ def get_pkg(request): dbfile = '%s/mdapi-%s%s-primary.sqlite' % ( CONFIG['DB_FOLDER'], branch, '-%s' % repotype if repotype else '') - session = mdapilib.create_session('sqlite:///%s' % dbfile) - if pkg.rpm_sourcerpm: - output['co-packages'] = list(set([ - cpkg.name - for cpkg in mdapilib.get_co_packages(session, pkg.rpm_sourcerpm) - ])) - else: - output['co-packages'] = [] - output['repo'] = repotype if repotype else 'release' - session.close() + + with file_lock.FileFlock(dbfile + '.lock'): + session = mdapilib.create_session('sqlite:///%s' % dbfile) + if pkg.rpm_sourcerpm: + output['co-packages'] = list(set([ + cpkg.name + for cpkg in mdapilib.get_co_packages(session, pkg.rpm_sourcerpm) + ])) + else: + output['co-packages'] = [] + output['repo'] = repotype if repotype else 'release' + session.close() return web.Response(body=json.dumps(output).encode('utf-8')) @@ -122,9 +127,10 @@ def get_pkg_files(request): if not os.path.exists(dbfile): raise web.HTTPBadRequest() - session2 = mdapilib.create_session('sqlite:///%s' % dbfile) - filelist = mdapilib.get_files(session2, pkg.pkgId) - session2.close() + with file_lock.FileFlock(dbfile + '.lock'): + session2 = mdapilib.create_session('sqlite:///%s' % dbfile) + filelist = mdapilib.get_files(session2, pkg.pkgId) + session2.close() return web.Response(body=json.dumps({ 'files': [fileinfo.to_json() for fileinfo in filelist], 'repo': repotype if repotype else 'release', @@ -142,9 +148,10 @@ def get_pkg_changelog(request): if not os.path.exists(dbfile): raise web.HTTPBadRequest() - session2 = mdapilib.create_session('sqlite:///%s' % dbfile) - changelogs = mdapilib.get_changelog(session2, pkg.pkgId) - session2.close() + with file_lock.FileFlock(dbfile + '.lock'): + session2 = mdapilib.create_session('sqlite:///%s' % dbfile) + changelogs = mdapilib.get_changelog(session2, pkg.pkgId) + session2.close() return web.Response(body=json.dumps({ 'files': [changelog.to_json() for changelog in changelogs], 'repo': repotype if repotype else 'release',