From 1d96c6eb2a4ba46a2dadf8a62ad6cc23c93f7fff Mon Sep 17 00:00:00 2001 From: Bogdano Arendartchuk Date: Wed, 8 Feb 2023 11:46:16 +0100 Subject: [PATCH] checkout_package: Use pathlib.Path Modify it to use pathlib.Path along with its callers. Some str(path) was necessary to avoid having a large changeset. It addresses #1258 and continues the work started in bac162a7123524fb3521e52c90d05f6577b7c25e. --- osc/commandline.py | 27 +++++++++++++-------------- osc/core.py | 41 ++++++++++++++++++----------------------- 2 files changed, 31 insertions(+), 37 deletions(-) diff --git a/osc/commandline.py b/osc/commandline.py index c2a7cbf9..304d5394 100644 --- a/osc/commandline.py +++ b/osc/commandline.py @@ -1024,7 +1024,7 @@ class Osc(cmdln.Cmdln): filename = "_patchinfo" else: checkout_package(apiurl, project, patchinfo, prj_dir=project_dir) - filename = project_dir + "/" + patchinfo + "/_patchinfo" + filename = project_dir / patchinfo / "/_patchinfo" run_editor(filename) @@ -2917,8 +2917,7 @@ Please submit there instead, or use --nodevelproject to force direct submission. raise oscerr.WrongArgs('\'checkout\' not possible (request has no \'submit\' actions)') for action in sr_actions: checkout_package(apiurl, action.src_project, action.src_package, - action.src_rev, expand_link=True, prj_dir=action.src_project) - + action.src_rev, expand_link=True, prj_dir=Path(action.src_project)) else: state_map = {'reopen': 'new', 'accept': 'accepted', 'decline': 'declined', 'wipe': 'deleted', 'revoke': 'revoked', 'supersede': 'superseded'} # Change review state only @@ -3759,7 +3758,7 @@ Please submit there instead, or use --nodevelproject to force direct submission. # all packages for package in meta_get_packagelist(apiurl, result): try: - checkout_package(apiurl, result, package, expand_link=True, prj_dir=result) + checkout_package(apiurl, result, package, expand_link=True, prj_dir=Path(result)) except: print('Error while checkout package:\n', package, file=sys.stderr) @@ -3914,7 +3913,7 @@ Please submit there instead, or use --nodevelproject to force direct submission. package = targetpkg or args[1] if opts.checkout: checkout_package(apiurl, targetprj, package, server_service_files=False, - expand_link=True, prj_dir=targetprj) + expand_link=True, prj_dir=Path(targetprj)) if conf.config['verbose']: print('Note: You can use "osc delete" or "osc submitpac" when done.\n') else: @@ -4687,7 +4686,8 @@ Please submit there instead, or use --nodevelproject to force direct submission. m = re.match(r"obs://([^/]+)/(\S+)/([^/]+)/([A-Fa-f\d]+)\-([^:]*)(:\S+)?", args[0]) if m and len(args) == 1: apiurl = "https://" + m.group(1) - project = project_dir = m.group(2) + project = m.group(2) + project_dir = Path(project) # platform = m.group(3) opts.revision = m.group(4) package = m.group(5) @@ -4698,7 +4698,8 @@ Please submit there instead, or use --nodevelproject to force direct submission. project = package = filename = None apiurl = self.get_api_url() try: - project = project_dir = self._process_project_name(args[0]) + project = self._process_project_name(args[0]) + project_dir = Path(project) package = args[1] filename = args[2] except: @@ -4706,7 +4707,7 @@ Please submit there instead, or use --nodevelproject to force direct submission. if len(args) == 1 and is_project_dir(Path.cwd()): project = store_read_project(Path.cwd()) - project_dir = str(Path.cwd()) + project_dir = Path.cwd() package = args[0] if opts.deleted and package: @@ -4743,11 +4744,9 @@ Please submit there instead, or use --nodevelproject to force direct submission. print_request_list(apiurl, project, package) elif project: - prj_dir = opts.output_dir if opts.output_dir else project - if not opts.output_dir and conf.config['checkout_no_colon']: - prj_dir = prj_dir.replace(':', '/') - else: - prj_dir = prj_dir.replace(':', conf.config['project_separator']) + sep = '/' if not opts.output_dir and conf.config['checkout_no_colon'] else conf.config['project_separator'] + chosen_output = opts.output_dir if opts.output_dir else project + prj_dir = Path(chosen_output.replace(':', sep)) if os.path.exists(prj_dir): sys.exit('osc: project directory \'%s\' already exists' % prj_dir) @@ -4759,7 +4758,7 @@ Please submit there instead, or use --nodevelproject to force direct submission. if not os.path.isfile('/usr/lib/obs/service/obs_scm_bridge'): raise oscerr.OscIOError(None, 'Install the obs-scm-bridge package to work on packages managed in scm (git)!') os.putenv("OSC_VERSION", get_osc_version()) - run_external(['/usr/lib/obs/service/obs_scm_bridge', '--outdir', prj_dir, '--url', scm_url]) + run_external(['/usr/lib/obs/service/obs_scm_bridge', '--outdir', str(prj_dir), '--url', scm_url]) Project.init_project(apiurl, prj_dir, project, conf.config['do_package_tracking'], scm_url=scm_url) print(statfrmt('A', prj_dir)) diff --git a/osc/core.py b/osc/core.py index d223caa7..d83363cf 100644 --- a/osc/core.py +++ b/osc/core.py @@ -31,6 +31,7 @@ import time from functools import cmp_to_key, total_ordering from http.client import IncompleteRead from io import StringIO +from pathlib import Path from typing import Optional, Dict, Union, List, Iterable from urllib.parse import urlsplit, urlunsplit, urlparse, quote_plus, urlencode, unquote from urllib.error import HTTPError @@ -712,7 +713,7 @@ class Project: `wc_check` : bool """ - self.dir = dir + self.dir = Path(dir) self.absdir = os.path.abspath(dir) self.store = Store(dir) self.progress_obj = progress_obj @@ -1181,7 +1182,7 @@ class Project: @staticmethod def init_project( apiurl: str, - dir, + dir: Path, project, package_tracking=True, getPackageList=True, @@ -5371,7 +5372,7 @@ def checkout_package( pathname=None, prj_obj=None, expand_link=False, - prj_dir=None, + prj_dir: Path=None, server_service_files=None, service_files=None, progress_obj=None, @@ -5382,20 +5383,19 @@ def checkout_package( try: # the project we're in might be deleted. # that'll throw an error then. - olddir = os.getcwd() - except: - olddir = os.environ.get("PWD") + olddir = Path.cwd() + except FileNotFoundError: + olddir = Path(os.environ.get("PWD")) if not prj_dir: prj_dir = olddir - elif conf.config['checkout_no_colon']: - prj_dir = prj_dir.replace(':', '/') else: - prj_dir = prj_dir.replace(':', conf.config['project_separator']) + sep = "/" if conf.config['checkout_no_colon'] else conf.config['project_separator'] + prj_dir = Path(str(prj_dir).replace(':', sep)) - root_dots = '.' + root_dots = Path('.') if conf.config['checkout_rooted']: - if prj_dir[:1] == '/': + if prj_dir.stem == '/': if conf.config['verbose']: print("checkout_rooted ignored for %s" % prj_dir) # ?? should we complain if not is_project_dir(prj_dir) ?? @@ -5409,27 +5409,25 @@ def checkout_package( # do not easily reveal the fact, that they are part of a project path. # At least this test should find that the parent of 'home/username/branches' # is a project (hack alert). Also goto parent in this case. - root_dots = "../" + root_dots = Path("../") elif is_project_dir("../.."): # testing two levels is better than one. # May happen in case of checkout_no_colon, or # if project roots were previously inconsistent - root_dots = "../../" + root_dots = Path("../../") if is_project_dir(root_dots): oldproj = store_read_project(root_dots) if conf.config['checkout_no_colon']: n = len(oldproj.split(':')) else: n = 1 - if root_dots == '.': - root_dots = '' - root_dots = root_dots + "../" * n + root_dots = root_dots / ("../" * n) - if root_dots != '.': + if str(root_dots) != '.': if conf.config['verbose']: print("%s is project dir of %s. Root found at %s" % (prj_dir, oldproj, os.path.abspath(root_dots))) - prj_dir = root_dots + prj_dir + prj_dir = root_dots / prj_dir if not pathname: pathname = getTransActPath(os.path.join(prj_dir, package)) @@ -7951,11 +7949,8 @@ def getTransActPath(pac_dir): Normally the "dir" attribute of a Package() object will be passed to this method. """ - if pac_dir != '.': - pathn = os.path.normpath(pac_dir) - else: - pathn = '' - return pathn + path = str(Path(pac_dir)) # accept str and Path as pac_dir + return '' if path == '.' else path def get_commit_message_template(pac):