Skip to content
Snippets Groups Projects
Verified Commit df895484 authored by Daniel Hornung's avatar Daniel Hornung
Browse files

MAINT, DOC: More documentation, type hints and linting.

parent 0a902228
No related branches found
No related tags found
2 merge requests!128MNT: Added a warning when column metadata is not configured, and a better...,!113F doc loadfiles
...@@ -23,6 +23,16 @@ ...@@ -23,6 +23,16 @@
# ** end header # ** end header
# #
"""Utilities to make the LinkAhead server aware of files.
Installation of `caosadvancedtools` also creates an executable script ``linkahead-loadfiles`` which
calls the `loadpath` function. Get the full help with ``linkahead-loadfiles --help``. In short,
that script tells the LinkAhead server to create `FILE` entities for existing files in one branch of
the directory tree. It is necessary that this directory is already visible for the server (for
example because it is defined as ``extroot`` in the LinkAhead profile).
"""
import argparse import argparse
import logging import logging
import os import os
...@@ -31,30 +41,30 @@ import sys ...@@ -31,30 +41,30 @@ import sys
import re import re
from argparse import ArgumentParser from argparse import ArgumentParser
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
from typing import Union
import shutil
import caosdb as db import caosdb as db
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
timeout_fallback = 20 timeout_fallback = 20
def convert_size(size): def convert_size(size: int):
"""Convert `size` from B to a human-readable file size in KB, """Convert `size` from bytes to a human-readable file size in KB,
MB, ... MB, ...
""" """
if (size == 0): if (size == 0):
return '0B' return '0B'
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size, 1000))) index = int(math.floor(math.log(size, 1000)))
p = math.pow(1000, i) p = math.pow(1000, index)
s = round(size / p, 2) s = round(size / p, 2)
return '%s %s' % (s, size_name[i]) return f"{s} {size_name[index]}"
def combine_ignore_files(caosdbignore, localignore, dirname=None): def combine_ignore_files(caosdbignore: str, localignore: str, dirname=None) -> str:
"""Append the contents of localignore to caosdbignore, save the result, """Append the contents of localignore to caosdbignore, save the result,
and return the name. and return the name.
...@@ -86,7 +96,7 @@ def combine_ignore_files(caosdbignore, localignore, dirname=None): ...@@ -86,7 +96,7 @@ def combine_ignore_files(caosdbignore, localignore, dirname=None):
return tmp.name return tmp.name
def compile_file_list(caosdbignore, localpath): def compile_file_list(caosdbignore: str, localpath: str) -> list[str]:
"""Create a list of files that contain all files under localpath except """Create a list of files that contain all files under localpath except
those excluded by caosdbignore. those excluded by caosdbignore.
...@@ -109,11 +119,11 @@ def compile_file_list(caosdbignore, localpath): ...@@ -109,11 +119,11 @@ def compile_file_list(caosdbignore, localpath):
matches = parse_gitignore(caosdbignore) matches = parse_gitignore(caosdbignore)
current_ignore = caosdbignore current_ignore = caosdbignore
non_ignored_files = [] non_ignored_files = []
ignore_files = [] ignore_files: list[tuple[str, str]] = []
for root, dirs, files in os.walk(localpath): for root, dirs, files in os.walk(localpath):
# remove local ignore files that do no longer apply to the current subtree (branch switch) # remove local ignore files that do no longer apply to the current subtree (branch switch)
while len(ignore_files) > 0 and not root.startswith(ignore_files[-1][0]): while len(ignore_files) > 0 and not root.startswith(ignore_files[-1][0]):
shutil.os.remove(ignore_files[-1][1]) os.remove(ignore_files[-1][1])
ignore_files.pop() ignore_files.pop()
# use the global one if there are no more local ones # use the global one if there are no more local ones
...@@ -143,10 +153,10 @@ def compile_file_list(caosdbignore, localpath): ...@@ -143,10 +153,10 @@ def compile_file_list(caosdbignore, localpath):
return non_ignored_files return non_ignored_files
def create_re_for_file_list(files, localroot, remoteroot): def create_re_for_file_list(files: list[str], localroot: str, remoteroot: str) -> str:
"""Create a regular expression that matches file paths contained """Create a regular expression that matches file paths contained
in the files argument and all parent directories. The prefix in the `files` argument and all parent directories. The prefix
localroot is replaced by the prefix remoteroot. `localroot is replaced by the prefix `remoteroot`.
Parameters Parameters
---------- ----------
...@@ -179,10 +189,10 @@ def create_re_for_file_list(files, localroot, remoteroot): ...@@ -179,10 +189,10 @@ def create_re_for_file_list(files, localroot, remoteroot):
return "^("+regexp[1:]+")$" return "^("+regexp[1:]+")$"
def loadpath(path, include, exclude, prefix, dryrun, forceAllowSymlinks, caosdbignore=None, def loadpath(path: str, include: Union[str, None], exclude: Union[str, None], prefix: str,
localpath=None): dryrun: bool, forceAllowSymlinks: bool, caosdbignore: Union[str, None] = None,
"""Make all files in `path` available to the LinkAhead server as localpath: Union[str, None] = None):
FILE entities. """Make all files in `path` available to the LinkAhead server as FILE entities.
Notes Notes
----- -----
...@@ -216,10 +226,8 @@ def loadpath(path, include, exclude, prefix, dryrun, forceAllowSymlinks, caosdbi ...@@ -216,10 +226,8 @@ def loadpath(path, include, exclude, prefix, dryrun, forceAllowSymlinks, caosdbi
since the check is done locally. If this is given, any since the check is done locally. If this is given, any
`include` is ignored. `include` is ignored.
localpath : str, optional localpath : str, optional
Path of `path` on the local machine. Only needed in the Path of `path` on the local machine. Only needed in combination with a
combination with a `caosdbignore` file since it is processed ``caosdbignore`` file since that is processed locally.
locally.
""" """
if caosdbignore: if caosdbignore:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment