Use pathlib.Path instead of str to represent paths
This commit is contained in:
parent
a3e9994fcc
commit
d9f3c00355
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from typing import Dict, List
|
from typing import Dict, List
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
import argparse
|
import argparse
|
||||||
import contextlib
|
import contextlib
|
||||||
import datetime
|
import datetime
|
||||||
@ -26,25 +27,25 @@ class CB:
|
|||||||
def __init__(self, config: str, system_datadir: str) -> None:
|
def __init__(self, config: str, system_datadir: str) -> None:
|
||||||
self.parse_config(config)
|
self.parse_config(config)
|
||||||
|
|
||||||
data_dir = (os.getenv('XDG_DATA_HOME',
|
data_dir = (Path(os.getenv('XDG_DATA_HOME',
|
||||||
os.path.expanduser('~/.local/share'))
|
'~/.local/share')).expanduser()
|
||||||
+ f'/{PROG}/')
|
/ f'{PROG}')
|
||||||
self.data_dir = data_dir
|
self.data_dir = data_dir
|
||||||
|
|
||||||
self.ensure_run_once()
|
self.ensure_run_once()
|
||||||
|
|
||||||
self.images_dir = data_dir + 'images/'
|
self.images_dir = data_dir / 'images'
|
||||||
self.work_dir = data_dir + 'work/'
|
self.work_dir = data_dir / 'work'
|
||||||
self.out_dir = data_dir + 'out/'
|
self.out_dir = data_dir / 'out'
|
||||||
self.system_datadir = system_datadir
|
self.system_datadir = system_datadir
|
||||||
|
|
||||||
self.date = datetime.date.today().strftime('%Y%m%d')
|
self.date = datetime.date.today().strftime('%Y%m%d')
|
||||||
self.service_default_state = 'enabled'
|
self.service_default_state = 'enabled'
|
||||||
self.created_scripts: List[str] = []
|
self.created_scripts: List[Path] = []
|
||||||
|
|
||||||
self.ensure_dirs()
|
self.ensure_dirs()
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
filename=f'{data_dir}{PROG}.log',
|
filename=f'{data_dir}/{PROG}.log',
|
||||||
format='%(levelname)s:%(asctime)s - %(message)s',
|
format='%(levelname)s:%(asctime)s - %(message)s',
|
||||||
)
|
)
|
||||||
self.log = logging.getLogger(PROG)
|
self.log = logging.getLogger(PROG)
|
||||||
@ -60,12 +61,12 @@ class CB:
|
|||||||
|
|
||||||
for name in self.created_scripts:
|
for name in self.created_scripts:
|
||||||
unlink(name)
|
unlink(name)
|
||||||
unlink(f'{self.work_dir}mkimage-profiles/conf.d/{PROG}.mk')
|
unlink(self.work_dir / f'mkimage-profiles/conf.d/{PROG}.mk')
|
||||||
|
|
||||||
self.info(f'Finish {PROG}')
|
self.info(f'Finish {PROG}')
|
||||||
|
|
||||||
def ensure_run_once(self):
|
def ensure_run_once(self):
|
||||||
self.lock_file = open(self.data_dir + f'{PROG}.lock', 'w')
|
self.lock_file = open(self.data_dir / f'{PROG}.lock', 'w')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
fcntl.flock(self.lock_file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
|
fcntl.flock(self.lock_file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||||
@ -176,10 +177,10 @@ class CB:
|
|||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
os.makedirs(value, exist_ok=True)
|
os.makedirs(value, exist_ok=True)
|
||||||
for branch in self.branches:
|
for branch in self.branches:
|
||||||
os.makedirs(self.images_dir + branch, exist_ok=True)
|
os.makedirs(self.images_dir / branch, exist_ok=True)
|
||||||
|
|
||||||
def generate_apt_files(self) -> None:
|
def generate_apt_files(self) -> None:
|
||||||
apt_dir = self.work_dir + 'apt'
|
apt_dir = self.work_dir / 'apt'
|
||||||
os.makedirs(apt_dir, exist_ok=True)
|
os.makedirs(apt_dir, exist_ok=True)
|
||||||
for branch in self.branches:
|
for branch in self.branches:
|
||||||
for arch in self.arches_by_branch(branch):
|
for arch in self.arches_by_branch(branch):
|
||||||
@ -387,23 +388,23 @@ Dir::Etc::preferencesparts "/var/empty";
|
|||||||
branch: str,
|
branch: str,
|
||||||
arch: str,
|
arch: str,
|
||||||
kind: str
|
kind: str
|
||||||
) -> str:
|
) -> Path:
|
||||||
self.ensure_mkimage_profiles()
|
self.ensure_mkimage_profiles()
|
||||||
|
|
||||||
target = f'{target}_{self.escape_branch(branch)}'
|
target = f'{target}_{self.escape_branch(branch)}'
|
||||||
image = re.sub(r'.*/', '', target)
|
image = re.sub(r'.*/', '', target)
|
||||||
full_target = f'{target}.{kind}'
|
full_target = f'{target}.{kind}'
|
||||||
tarball = f'{self.out_dir}{image}-{self.date}-{arch}.{kind}'
|
tarball = self.out_dir / f'{image}-{self.date}-{arch}.{kind}'
|
||||||
apt_dir = self.work_dir + 'apt'
|
apt_dir = self.work_dir / 'apt'
|
||||||
with self.pushd(self.work_dir + 'mkimage-profiles'):
|
with self.pushd(self.work_dir / 'mkimage-profiles'):
|
||||||
if os.path.exists(tarball):
|
if tarball.exists():
|
||||||
self.info(f'Skip building of {full_target} {arch}')
|
self.info(f'Skip building of {full_target} {arch}')
|
||||||
else:
|
else:
|
||||||
cmd = [
|
cmd = [
|
||||||
'make',
|
'make',
|
||||||
f'APTCONF={apt_dir}/apt.conf.{branch}.{arch}',
|
f'APTCONF={apt_dir}/apt.conf.{branch}.{arch}',
|
||||||
f'ARCH={arch}',
|
f'ARCH={arch}',
|
||||||
f'IMAGE_OUTDIR={self.out_dir.rstrip("/")}',
|
f'IMAGE_OUTDIR={self.out_dir}',
|
||||||
full_target,
|
full_target,
|
||||||
]
|
]
|
||||||
self.info(f'Begin building of {full_target} {arch}')
|
self.info(f'Begin building of {full_target} {arch}')
|
||||||
@ -415,25 +416,28 @@ Dir::Etc::preferencesparts "/var/empty";
|
|||||||
|
|
||||||
return tarball
|
return tarball
|
||||||
|
|
||||||
def image_path(self, image: str, branch: str, arch: str, kind: str) -> str:
|
def image_path(
|
||||||
path = '{}{}/alt-{}-{}-{}.{}'.format(
|
self,
|
||||||
self.images_dir,
|
image: str,
|
||||||
branch,
|
branch: str,
|
||||||
branch.lower(),
|
arch: str,
|
||||||
image,
|
kind: str
|
||||||
arch,
|
) -> Path:
|
||||||
kind,
|
path = (
|
||||||
|
self.images_dir
|
||||||
|
/ branch
|
||||||
|
/ f'alt-{branch.lower()}-{image}-{arch}.{kind}'
|
||||||
)
|
)
|
||||||
return path
|
return path
|
||||||
|
|
||||||
def copy_image(self, src: str, dst: str) -> None:
|
def copy_image(self, src: Path, dst: Path) -> None:
|
||||||
os.link(src, dst)
|
os.link(src, dst)
|
||||||
|
|
||||||
def clear_imager_dir(self):
|
def clear_imager_dir(self):
|
||||||
for branch in self.branches:
|
for branch in self.branches:
|
||||||
directory = f'{self.images_dir}{branch}'
|
directory = self.images_dir / branch
|
||||||
for path in os.listdir(directory):
|
for path in directory.glob('*'):
|
||||||
os.unlink(f'{directory}/{path}')
|
os.unlink(path)
|
||||||
|
|
||||||
def remove_old_tarballs(self):
|
def remove_old_tarballs(self):
|
||||||
with self.pushd(self.out_dir):
|
with self.pushd(self.out_dir):
|
||||||
@ -451,15 +455,18 @@ Dir::Etc::preferencesparts "/var/empty";
|
|||||||
self.target_by_image(image))
|
self.target_by_image(image))
|
||||||
if not target_type:
|
if not target_type:
|
||||||
target_type = 'distro'
|
target_type = 'distro'
|
||||||
scripts_path = '''
|
scripts_path = (
|
||||||
{}mkimage-profiles/features.in/build-{}/image-scripts.d/
|
self.work_dir
|
||||||
'''.format(self.work_dir, target_type).strip()
|
/ 'mkimage-profiles'
|
||||||
|
/ 'features.in'
|
||||||
|
/ f'build-{target_type}'
|
||||||
|
/ 'image-scripts.d'
|
||||||
|
)
|
||||||
for name, content in self.scripts_by_image(image).items():
|
for name, content in self.scripts_by_image(image).items():
|
||||||
path = scripts_path + name
|
script = scripts_path / name
|
||||||
self.created_scripts.append(path)
|
self.created_scripts.append(script)
|
||||||
with open(path, 'w') as f:
|
script.write_text(content)
|
||||||
print(content, file=f)
|
os.chmod(script, 0o755)
|
||||||
os.chmod(path, 0o755)
|
|
||||||
|
|
||||||
def create_images(self) -> None:
|
def create_images(self) -> None:
|
||||||
self.clear_imager_dir()
|
self.clear_imager_dir()
|
||||||
@ -519,7 +526,7 @@ Dir::Etc::preferencesparts "/var/empty";
|
|||||||
self.create_images()
|
self.create_images()
|
||||||
for branch in self.branches:
|
for branch in self.branches:
|
||||||
remote = self.remote(branch)
|
remote = self.remote(branch)
|
||||||
files = glob.glob(f'{self.images_dir}{branch}/*')
|
files = glob.glob(f'{self.images_dir}/{branch}/*')
|
||||||
cmd = ['rsync', '-v'] + files + [remote]
|
cmd = ['rsync', '-v'] + files + [remote]
|
||||||
self.call(cmd)
|
self.call(cmd)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user