[Bf-blender-cvs] [b35e5240f60] soc-2017-package_manager: fix utils
gandalf3
noreply at git.blender.org
Tue Aug 29 15:23:27 CEST 2017
Commit: b35e5240f60f4e328d006e7d1ed453e201aa30a2
Author: gandalf3
Date: Tue Aug 29 06:11:47 2017 -0700
Branches: soc-2017-package_manager
https://developer.blender.org/rBb35e5240f60f4e328d006e7d1ed453e201aa30a2
fix utils
===================================================================
M release/scripts/modules/bpkg/utils.py
===================================================================
diff --git a/release/scripts/modules/bpkg/utils.py b/release/scripts/modules/bpkg/utils.py
index eabd9f14c51..63c7826e7f4 100644
--- a/release/scripts/modules/bpkg/utils.py
+++ b/release/scripts/modules/bpkg/utils.py
@@ -2,6 +2,7 @@ from pathlib import Path
import shutil
import logging
+
def fmt_version(version_number: tuple) -> str:
"""Take version number as a tuple and format it as a string"""
vstr = str(version_number[0])
@@ -9,6 +10,7 @@ def fmt_version(version_number: tuple) -> str:
vstr += "." + str(component)
return vstr
+
def format_filename(s: str, ext=None) -> str:
"""Take a string and turn it into a reasonable filename"""
import string
@@ -16,26 +18,29 @@ def format_filename(s: str, ext=None) -> str:
ext = ""
valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
filename = ''.join(char for char in s if char in valid_chars)
- filename = filename.replace(' ','_')
+ filename = filename.replace(' ', '_')
filename.lower()
filename += ext
return filename
+
def sanitize_repository_url(url: str) -> str:
"""Sanitize repository url"""
from urllib.parse import urlsplit, urlunsplit
parsed_url = urlsplit(url)
- # new_path = parsed_url.path.rstrip("repo.json")
- new_path = parsed_url.path
+ new_path = parsed_url.path.rstrip("repo.json")
return urlunsplit((parsed_url.scheme, parsed_url.netloc, new_path, parsed_url.query, parsed_url.fragment))
+
def add_repojson_to_url(url: str) -> str:
"""Add `repo.json` to the path component of a url"""
from urllib.parse import urlsplit, urlunsplit
parsed_url = urlsplit(url)
- new_path = str(Path(parsed_url.path) / "repo.json")
+ new_path = parsed_url.path.rstrip('/')
+ new_path += "/repo.json"
return urlunsplit((parsed_url.scheme, parsed_url.netloc, new_path, parsed_url.query, parsed_url.fragment))
+
def load_repositories(repo_storage_path: Path) -> list:
"""Load all json files in repo storage path"""
repositories = []
@@ -45,6 +50,7 @@ def load_repositories(repo_storage_path: Path) -> list:
repositories.append(repo)
return repositories
+
def rm(path: Path):
"""Delete whatever is specified by `path`"""
if path.is_dir():
@@ -52,6 +58,7 @@ def rm(path: Path):
else:
path.unlink()
+
class InplaceBackup:
"""Utility class for moving a file out of the way by appending a '~'"""
@@ -59,6 +66,8 @@ class InplaceBackup:
def __init__(self, path: Path):
self.path = path
+ # contains Path() when not None
+ self.backup_path = None
self.backup()
def backup(self):
@@ -66,25 +75,26 @@ class InplaceBackup:
if not self.path.exists():
raise FileNotFoundError("Can't backup path which doesn't exist")
- self.backup_path = Path(str(self.path) + '~')
+ self.backup_path = self.path.with_name(self.path.name + '~')
if self.backup_path.exists():
- self.log.warning("Overwriting existing backup '{}'".format(self.backup_path))
+ self.log.warning(
+ "Overwriting existing backup '{}'".format(self.backup_path))
rm(self.backup_path)
shutil.move(str(self.path), str(self.backup_path))
def restore(self):
"""Move 'path~' to 'path'"""
- try:
- getattr(self, 'backup_path')
- except AttributeError as err:
- raise RuntimeError("Can't restore file before backing it up") from err
+ if not self.backup_path:
+ raise RuntimeError(
+ "Can't restore file before backing it up") from err
if not self.backup_path.exists():
raise FileNotFoundError("Can't restore backup which doesn't exist")
if self.path.exists():
- self.log.warning("Overwriting '{0}' with backup file".format(self.path))
+ self.log.warning(
+ "Overwriting '{0}' with backup file".format(self.path))
rm(self.path)
shutil.move(str(self.backup_path), str(self.path))
@@ -92,11 +102,3 @@ class InplaceBackup:
def remove(self):
"""Remove 'path~'"""
rm(self.backup_path)
-
-
-def add_repojson_to_url(url: str) -> str:
- """Add `repo.json` to the path component of a url"""
- from urllib.parse import urlsplit, urlunsplit
- parsed_url = urlsplit(url)
- new_path = parsed_url.path + "/repo.json"
- return urlunsplit((parsed_url.scheme, parsed_url.netloc, new_path, parsed_url.query, parsed_url.fragment))
More information about the Bf-blender-cvs
mailing list