From: Michael Tremer Date: Wed, 12 Oct 2022 15:19:43 +0000 (+0000) Subject: backend: Take a basepath X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4924e54ed7f7f55aaa2c71f8d8958195341d6e27;p=pbs.git backend: Take a basepath The entire filesystem structure will be based on this path, so that we will be able to create test environments which we can easily destroy again. Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/__init__.py b/src/buildservice/__init__.py index 0ad5d82a..5b122e63 100644 --- a/src/buildservice/__init__.py +++ b/src/buildservice/__init__.py @@ -52,6 +52,9 @@ class Backend(object): # Read configuration file. self.config = self.read_config(config_file) + # Fetch the base path + self.basepath = self.config.get("DEFAULT", "basepath") or "/pub/pakfire" + # Global pakfire settings (from database). self.settings = settings.Settings(self) @@ -132,6 +135,12 @@ class Backend(object): return database.Connection(hostname, name, user=user, password=password) + def path(self, *args): + """ + Takes a relative path and makes it absolute + """ + return os.path.join(self.basepath, *args) + def path_to_url(self, path): """ Takes a path to a file on the file system and converts it into a URL @@ -141,7 +150,7 @@ class Backend(object): # Path to package path = os.path.join( - "files", os.path.relpath(path, PAKFIRE_DIR), + "files", os.path.relpath(path, self.basepath), ) # Join it all together @@ -359,13 +368,13 @@ class Backend(object): "--delay-updates", # Add source & target - "%s/" % PAKFIRE_DIR, + "%s/" % self.basepath, target, ] # Add all mirrored repositories for repo in self.repos.mirrored: - path = os.path.relpath(repo.local_path(), PAKFIRE_DIR) + path = os.path.relpath(repo.local_path(), self.basepath) commandline.append("--include=%s***" % path) diff --git a/src/buildservice/constants.py.in b/src/buildservice/constants.py.in index 96bc2a93..8014aa6f 100644 --- a/src/buildservice/constants.py.in +++ b/src/buildservice/constants.py.in @@ -13,13 +13,6 @@ LOCALEDIR = "@localedir@" TEMPLATESDIR = "@templatesdir@" STATICDIR = "@staticdir@" -PAKFIRE_DIR = "/pub/pakfire" -LOGS_DIR = os.path.join(PAKFIRE_DIR, "logs") -PACKAGES_DIR = os.path.join(PAKFIRE_DIR, "packages") -REPOS_DIR = os.path.join(PAKFIRE_DIR, "repos") -SOURCES_DIR = os.path.join(PAKFIRE_DIR, "sources") -UPLOADS_DIR = os.path.join(PAKFIRE_DIR, "tmp/uploads") - BUFFER_SIZE = 1024 * 100 # 100kb FILE_EXTENSIONS_VIEWABLE = ( diff --git a/src/buildservice/jobs.py b/src/buildservice/jobs.py index dc5b41a3..9b74f4b9 100644 --- a/src/buildservice/jobs.py +++ b/src/buildservice/jobs.py @@ -300,7 +300,8 @@ class Job(base.DataObject): # Remove all logfiles for logfile in self.logfiles: - self.backend.delete_file(os.path.join(PACKAGES_DIR, logfile.path)) + path = self.backend.path("packages", logfile.path) + self.backend.delete_file(path) self.db.execute("DELETE FROM logfiles WHERE job_id = %s", self.id) @@ -348,8 +349,8 @@ class Job(base.DataObject): async def _import_log(self, upload): # Create some destination path - path = os.path.join( - LOGS_DIR, + path = self.backend.path( + "logs", "jobs", self.uuid[0:2], self.uuid[2:4], diff --git a/src/buildservice/packages.py b/src/buildservice/packages.py index 50677884..f5985e1e 100644 --- a/src/buildservice/packages.py +++ b/src/buildservice/packages.py @@ -355,8 +355,8 @@ class Package(base.DataObject): Imports the package into the filesystem """ # Determine the new path - path = os.path.join( - PACKAGES_DIR, + path = self.backend.path( + "packages", self.uuid[0:2], self.uuid[2:4], self.uuid[4:], diff --git a/src/buildservice/repository.py b/src/buildservice/repository.py index baa17c53..506eda87 100644 --- a/src/buildservice/repository.py +++ b/src/buildservice/repository.py @@ -260,10 +260,7 @@ class Repository(base.DataObject): """ Path to the repository """ - path = os.path.join( - REPOS_DIR, - self.path, - ) + path = self.backend.path("repos", self.path) if arch: path = os.path.join(path, arch) diff --git a/src/buildservice/uploads.py b/src/buildservice/uploads.py index 64c1eef7..52ad35d5 100644 --- a/src/buildservice/uploads.py +++ b/src/buildservice/uploads.py @@ -38,7 +38,9 @@ class Uploads(base.Object): """ Returns a file handle which can be used to write temporary data to. """ - return tempfile.NamedTemporaryFile(dir=UPLOADS_DIR, delete=False) + path = self.backend.path("tmp", "uploads") + + return tempfile.NamedTemporaryFile(dir=path, delete=False) def create(self, filename, path, size=None, builder=None, user=None): # Check if either builder or user are set diff --git a/tests/test.py b/tests/test.py index c7359215..22ebf5e0 100644 --- a/tests/test.py +++ b/tests/test.py @@ -62,9 +62,17 @@ class TestCase(unittest.IsolatedAsyncioTestCase): } async def asyncSetUp(self): + # Create a new temporary directory + self.testdir = tempfile.TemporaryDirectory() + # Create a configuration file conf = configparser.ConfigParser() + # Set the base path + conf["DEFAULT"] = { + "basepath" : self.testdir.name, + } + # Setup the database conf["database"] = self._setup_database() @@ -84,6 +92,15 @@ class TestCase(unittest.IsolatedAsyncioTestCase): # Create some default objects await self._create_default_objects() + async def asyncTearDown(self): + # Dump a listing of all temporary files + with os.scandir(self.testdir.name) as listing: + for entry in sorted(listing): + print(" %s" % entry.path) + + # Removing any temporary files + self.testdir.cleanup() + async def _create_default_objects(self): """ Creates some random objects that are created by default so