Skip to content

Commit

Permalink
fixed pre commit errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Shofiya2003 committed Dec 22, 2024
1 parent d9cda06 commit aeea8c2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
1 change: 0 additions & 1 deletion probe_src/python/probe_py/manual/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import typer
import shutil
import rich
from probe_py.manual import util
from probe_py.manual.scp import scp_with_provenance
import rich.console
import rich.pretty
Expand Down
26 changes: 13 additions & 13 deletions probe_src/python/probe_py/manual/persistent_provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@


def get_local_node_name() -> str:
node_name = PROBE_HOME / "node_name"
if node_name.exists():
return node_name.read_text()
node_name_path = PROBE_HOME / "node_name"
if node_name_path.exists():
return node_name_path.read_text()
else:
hostname = socket.gethostname()
rng = random.Random(int(datetime.datetime.now().timestamp()) ^ hash(hostname))
bits_per_hex_digit = 4
hex_digits = 8
random_number = rng.getrandbits(bits_per_hex_digit * hex_digits)
node_name = f"{random_number:0{hex_digits}x}.{hostname}"
file_path = pathlib.Path(node_name)
file_path = pathlib.Path(node_name_path)
file_path.write_text(node_name)
return node_name

Expand All @@ -52,7 +52,7 @@ class Inode:
device_minor: int
inode: int

def to_dict(self):
def to_dict(self) -> dict:
return {
'host': self.host,
'device_major': self.device_major,
Expand All @@ -69,7 +69,7 @@ def str_id(self) -> str:
return f"{number:012x}-{self.device_major:04x}-{self.device_minor:04x}-{self.inode:016x}"

@staticmethod
def from_local_path(path: pathlib.Path, stat_info: os.StatResult | None = None) -> Inode:
def from_local_path(path: pathlib.Path, stat_info: os.stat_result | None = None) -> Inode:
if stat_info is None:
stat_info = os.stat(path)
device_major = os.major(stat_info.st_dev)
Expand Down Expand Up @@ -101,15 +101,15 @@ class InodeVersion:
# Our xattr would be "true mtime" that we would maintain and can't be changed by normal tools.
# - Cry.

def to_dict(self):
def to_dict(self) -> dict:
data = {"mtime": self.mtime, 'inode': self.inode.to_dict(), "size": self.size}
return data

def str_id(self) -> str:
return f"{self.inode.str_id()}-{self.mtime:016x}-{self.size:016x}"

@staticmethod
def from_local_path(path: pathlib.Path, stat_info: os.StatInfo) -> InodeVersion:
def from_local_path(path: pathlib.Path, stat_info: os.stat_result) -> InodeVersion:
if stat_info is None:
stat_info = os.stat(path)
mtime = int(stat_info.st_mtime * 1_000_000_000)
Expand All @@ -125,7 +125,7 @@ class InodeMetadata:
uid: int
gid: int

def to_dict(self):
def to_dict(self) -> dict:
return {
"inode": self.inode.to_dict(),
"mode": self.mode,
Expand All @@ -135,7 +135,7 @@ def to_dict(self):
}

@staticmethod
def from_local_path(path: pathlib.Path, stat_info: os.StatInfo) -> InodeMetadata:
def from_local_path(path: pathlib.Path, stat_info: os.stat_result) -> InodeMetadata:
if stat_info is None:
stat_info = os.stat(path)
return InodeMetadata(
Expand All @@ -159,7 +159,7 @@ class Process:
env: tuple[tuple[str, str], ...]
wd: pathlib.Path

def to_dict(self):
def to_dict(self) -> dict:
return {
'input_inodes': [inode_version.to_dict() for inode_version in self.input_inodes],
'input_inode_metadatas': [metadata.to_dict() for metadata in self.input_inode_metadatas],
Expand Down Expand Up @@ -188,8 +188,8 @@ def get_prov_upstream(
inode_version_queue.extend(root_inode_version)

# Stuff we need to transfer
inode_version_writes = dict[InodeVersion, int | None]()
process_closure = dict[int, Process]()
inode_version_writes = typing.Mapping[InodeVersion, int | None]()
process_closure = typing.Mapping[int, Process]()

while inode_version_queue:
inode_version = inode_version_queue.pop()
Expand Down
28 changes: 13 additions & 15 deletions probe_src/python/probe_py/manual/remote_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import json
import os
import shlex
import random
import subprocess
import pathlib
import yaml
Expand Down Expand Up @@ -115,10 +114,10 @@ def augment_provenance(
while scp_process_id in process_closure:
scp_process_id = generate_random_pid()
scp_process = Process(
source_inode_versions,
source_inode_metadatas,
destination_inode_versions,
destination_inode_metadatas,
frozenset(source_inode_versions),
frozenset(source_inode_metadatas),
frozenset(destination_inode_versions),
frozenset(destination_inode_metadatas),
time,
tuple(cmd),
scp_process_id,
Expand Down Expand Up @@ -205,11 +204,11 @@ def get_descendants(root: pathlib.Path, include_directories: bool) -> list[pathl
def lookup_provenance_local(path: pathlib.Path, get_persistent_provenance: bool) -> ProvenanceInfo:
if path.is_dir():
inode_versions = [
InodeVersion.from_local_path(descendant)
InodeVersion.from_local_path(descendant, None)
for descendant in get_descendants(path, False)
]
inode_metadatas = [
InodeMetadata.from_local_path(descendant)
InodeMetadata.from_local_path(descendant, None)
for descendant in get_descendants(path, True)
]
else:
Expand Down Expand Up @@ -261,9 +260,9 @@ def lookup_provenance_remote(host: Host, path: pathlib.Path, get_persistent_prov
inode_metadatas = []
inode_versions = []
for _child_path, device, inode, mtime, size, mode, nlink, uid, gid in itertools.batched(fields[2:11], 10):
inode = Inode(node_name, os.major(int(device)), os.minor(int(device)), int(inode))
inode_versions.append(InodeVersion(inode, int(float(mtime)), int(size)))
inode_metadatas.append(InodeMetadata(inode, int(mode), int(nlink), int(uid), int(gid)))
inode_object = Inode(node_name, os.major(int(device)), os.minor(int(device)), int(inode))
inode_versions.append(InodeVersion(inode_object, int(float(mtime)), int(size)))
inode_metadatas.append(InodeMetadata(inode_object, int(mode), int(nlink), int(uid), int(gid)))

if not get_persistent_provenance:
return inode_versions, inode_metadatas, {}, {}
Expand Down Expand Up @@ -323,15 +322,14 @@ def upload_provenance_remote(dest: Host, provenance_info: ProvenanceInfo) -> Non
assert address is not None
echo_commands = []
for inode_version, process_id in augmented_inode_writes.items():
inode_version = inode_version.str_id()
inode_version_str_id = inode_version.str_id()
echo_commands.append(
f"echo {shlex.quote(json.dumps(process_id))} > \"${{process_that_wrote}}/{inode_version}.json\""
f"echo {shlex.quote(json.dumps(process_id))} > \"${{process_that_wrote}}/{inode_version_str_id}.json\""
)

for process_id, process in augmented_process_closure.items():
process_id = str(process_id)
echo_commands.append(
f"echo {(json.dumps(process.to_dict()))} > \"${{processes_by_id}}/{process_id}.json\""
f"echo {(json.dumps(process.to_dict()))} > \"${{processes_by_id}}/{str(process_id)}.json\""
)

commands = [
Expand All @@ -349,7 +347,7 @@ def upload_provenance_remote(dest: Host, provenance_info: ProvenanceInfo) -> Non
commands.extend(echo_commands)
print(echo_commands)
full_command = "sh -c '" + "; ".join(commands) + "'"
proc = subprocess.run(
subprocess.run(
[
"ssh",
*dest.ssh_options,
Expand Down
6 changes: 3 additions & 3 deletions probe_src/python/probe_py/manual/scp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def scp_with_provenance(scp_args: list[str]) -> int:
if proc.returncode == 0:
sources, destination = parse_scp_args(scp_args)
for source in sources:
copy_provenance(source, destination, ["scp", *scp_args])
copy_provenance(source, destination, ("scp", *scp_args))
return 0
else:
return proc.returncode
Expand Down Expand Up @@ -99,7 +99,7 @@ def parse_scp_args(scp_args: list[str]) -> tuple[list[HostPath], HostPath]:
this_ssh_options.append("-P")
this_ssh_options.append(match.group("port"))
sources.append(HostPath(
Host(match.group("host"), match.group("user"), None, this_ssh_options, this_scp_options),
Host(match.group("host"), match.group("user"), this_ssh_options, this_scp_options),
Path(match.group("path") if match.group("path") else "")
))
elif match := re.match(scp_path_regex, arg):
Expand Down Expand Up @@ -130,7 +130,7 @@ def optional(arg: str) -> str:
def named_group(name: str, arg: str) -> str:
return f"(?P<{name}>{arg})?"
def whole_string(arg: str) -> str:
return re.compile("^" + arg + "$")
return "^" + arg + "$"

# TODO: do options only apply to the host following it?
# E.g., does scp -J host-A host-B host-C only apply a jump-host to the host-B?
Expand Down

0 comments on commit aeea8c2

Please sign in to comment.