Compare commits

...

4 Commits

Author SHA1 Message Date
Dirk Müller
f5ffc83a69
Remove double quoting of url parameters
makeurl quotes by itself, so this was messing it up
2024-05-16 11:49:14 +02:00
Dirk Müller
d0ccf83684
Revert "Try to fetch the element as deleted if initial access failed"
The OBS api has been fixed to provide an automatic fallback via
https://github.com/openSUSE/open-build-service/pull/15655

This reverts commit c9e07e536f19820c4bba1f11e2edcb23069874d7.
2024-05-16 11:49:14 +02:00
Dirk Müller
b0ffb01c59
cleanups 2024-05-16 11:49:14 +02:00
Dirk Müller
28d5c6e606
Switch to psycopg rather than psycopg2
It's a bit more modern and uses dedicated c bindings
2024-05-16 11:49:14 +02:00
7 changed files with 18 additions and 36 deletions

View File

@ -1,4 +1,4 @@
sudo zypper in python3-psycopg2 sudo zypper in python3-psycopg
sudo su - postgres sudo su - postgres
# `createdb -O <LOCAL_USER> imported_git` # `createdb -O <LOCAL_USER> imported_git`

View File

@ -15,7 +15,7 @@ def config(filename="database.ini", section="production"):
db[param[0]] = param[1] db[param[0]] = param[1]
else: else:
raise Exception( raise Exception(
"Section {0} not found in the {1} file".format(section, filename) f"Section {section} not found in the {filename} file"
) )
return db return db

View File

@ -1,7 +1,6 @@
import logging import logging
import psycopg2 import psycopg
from psycopg2.extras import LoggingConnection
from lib.config import config from lib.config import config
@ -17,11 +16,10 @@ class DB:
# read the connection parameters # read the connection parameters
params = config(section=self.config_section) params = config(section=self.config_section)
# connect to the PostgreSQL server # connect to the PostgreSQL server
self.conn = psycopg2.connect(connection_factory=LoggingConnection, **params) self.conn = psycopg.connect(conninfo=f"dbname={params['database']}")
logger = logging.getLogger(__name__) logging.getLogger("psycopg.pool").setLevel(logging.INFO)
self.conn.initialize(logger)
except (Exception, psycopg2.DatabaseError) as error: except (Exception, psycopg.DatabaseError) as error:
print(error) print(error)
raise error raise error
@ -32,7 +30,7 @@ class DB:
# execute a statement # execute a statement
try: try:
cur.execute("SELECT MAX(version) from scheme") cur.execute("SELECT MAX(version) from scheme")
except psycopg2.errors.UndefinedTable as error: except psycopg.errors.UndefinedTable as error:
cur.close() cur.close()
self.close() self.close()
self.connect() self.connect()
@ -146,9 +144,9 @@ class DB:
) )
schemes[10] = ( schemes[10] = (
"ALTER TABLE revisions ADD COLUMN request_id INTEGER", "ALTER TABLE revisions ADD COLUMN request_id INTEGER",
"""ALTER TABLE revisions """ALTER TABLE revisions
ADD CONSTRAINT request_id_foreign_key ADD CONSTRAINT request_id_foreign_key
FOREIGN KEY (request_id) FOREIGN KEY (request_id)
REFERENCES requests (id)""", REFERENCES requests (id)""",
"UPDATE scheme SET version=10", "UPDATE scheme SET version=10",
) )
@ -273,7 +271,7 @@ class DB:
cur.execute(command) cur.execute(command)
# commit the changes # commit the changes
self.conn.commit() self.conn.commit()
except (Exception, psycopg2.DatabaseError) as error: except (Exception, psycopg.DatabaseError) as error:
print(error) print(error)
self.close() self.close()
raise error raise error

View File

@ -255,7 +255,7 @@ class DBRevision:
self._files.sort(key=lambda x: x["name"]) self._files.sort(key=lambda x: x["name"])
return self._files return self._files
def calc_delta(self, current_rev: Optional[DBRevision]): def calc_delta(self, current_rev: DBRevision | None):
"""Calculate the list of files to download and to delete. """Calculate the list of files to download and to delete.
Param current_rev is the revision that's currently checked out. Param current_rev is the revision that's currently checked out.
If it's None, the repository is empty. If it's None, the repository is empty.

View File

@ -40,7 +40,7 @@ class GitExporter:
def check_repo_state(self, flats, branch_state): def check_repo_state(self, flats, branch_state):
state_data = dict() state_data = dict()
if os.path.exists(self.state_file): if os.path.exists(self.state_file):
with open(self.state_file, "r") as f: with open(self.state_file) as f:
state_data = yaml.safe_load(f) state_data = yaml.safe_load(f)
if type(state_data) != dict: if type(state_data) != dict:
state_data = {} state_data = {}

View File

@ -148,28 +148,12 @@ class OBS:
] ]
def _download(self, project, package, name, revision): def _download(self, project, package, name, revision):
# the object might be deleted but we can only pass deleted=1
# if it is actually deleted
deleted = 0
while deleted < 2:
url = osc.core.makeurl(
self.url,
["source", project, package, urllib.parse.quote(name)],
{"rev": revision, "expand": 1, "deleted": deleted if deleted else ()},
)
try:
osc.core.http_request("HEAD", url)
break
except Exception:
pass
deleted += 1
url = osc.core.makeurl( url = osc.core.makeurl(
self.url, self.url,
["source", project, package, urllib.parse.quote(name)], ["source", project, package, name],
{"rev": revision, "expand": 1, "deleted": 1 if deleted else ()}, {"rev": revision, "expand": 1},
) )
return osc.core.http_request("GET", url) return osc.core.http_GET(url)
def download( def download(
self, self,

View File

@ -138,7 +138,7 @@ class TreeBuilder:
self.requests.add(node.revision.request_id) self.requests.add(node.revision.request_id)
class FindMergeWalker(AbstractWalker): class FindMergeWalker(AbstractWalker):
def __init__(self, builder: TreeBuilder, requests: Dict) -> None: def __init__(self, builder: TreeBuilder, requests: dict) -> None:
super().__init__() super().__init__()
self.source_revisions = dict() self.source_revisions = dict()
self.builder = builder self.builder = builder