-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
The current script seems only works on Windows as it assumes Windows paths with C: drives and uses direct path object comparison. I've updated the path matching to work on any platform by focusing on the folder name instead of exact path matching.
import argparse
import datetime
import json
import pathlib
import os
parser = argparse.ArgumentParser(description="Easily bulk restore files in vscode.")
parser.add_argument(
"restore_from",
type=str,
help=r"The folder that gets restored.",
)
parser.add_argument(
"history",
type=str,
help=r"The vscode folder where local history is stored. Changes depending on OS but for windows usually C:\Users\{USER_NAME}\AppData\Roaming\Code\User\History",
)
parser.add_argument(
"restore_to",
type=str,
help=r"Where folder gets restored.",
)
parser.add_argument(
"--no-entries",
action="store_true",
help=r"Doesn't show the amount of entries in files.",
)
parser.add_argument(
"--no-date",
action="store_true",
help=r"Doesn't show the date of the last save.",
)
args = parser.parse_args()
history = pathlib.Path(args.history)
restore_from = pathlib.Path(args.restore_from)
restore_to = pathlib.Path(args.restore_to)
def convert_timestamp(timestamp):
return datetime.datetime.fromtimestamp(int(timestamp) / 1000)
max_entries = 0
all_history_data = []
for file_history_folder in history.glob("*"):
json_file = file_history_folder / "entries.json"
if not json_file.exists():
continue
data = json.load(open(json_file))
entries = len(data["entries"])
if entries > max_entries:
max_entries = entries
all_history_data.append((file_history_folder, data))
# Store the restore_from folder name for cross-platform matching
restore_from_name = restore_from.name
for file_history_folder, data in all_history_data:
# Handle path cross-platform by removing drive letter if present
if os.name == 'nt': # Windows
path = pathlib.Path("C:\\") / pathlib.Path(
*pathlib.Path(data["resource"]).parts[2:]
)
else: # Linux/Mac
path = pathlib.Path("/") / pathlib.Path(
pathlib.Path(data["resource"]).parts[2:]
)
put_in = restore_to.joinpath(path.parts[1:])
entries = len(data["entries"])
if entries / max_entries == 1:
status = "Modified A Lot"
elif entries / max_entries >= 0.4:
status = "Modified Often"
elif entries / max_entries < 0.4:
status = "Not Modified Often"
elif entries / max_entries == 1 / max_entries:
status = "Copied"
# Cross-platform path matching
path_parts = [p.name for p in path.parents]
if restore_from_name in path_parts:
# File belongs to our target folder
pass
else:
# Not apart of restore_from
continue
# Create Parent Folders
put_in.parent.mkdir(exist_ok=True, parents=True)
date = datetime.datetime.strftime(
convert_timestamp(data["entries"][-1]["timestamp"]),
"%Y-%m-%d %I:%M %p %B, %d, %A",
)
code_path = file_history_folder / data["entries"][-1]["id"]
with open(code_path, "r", encoding="utf-8") as f:
code = f.read()
with put_in.open("w", encoding="utf-8") as f:
text = code
if put_in.suffix not in [".json", ".toml"]:
if not args.no_date:
text = f"'{date}'\n" + code
if not args.no_entries:
text = f"'Entries: {entries}/{max_entries} ({status})'\n\n" + code
f.write(text)
print("RESTORED:", path)Py-mon
Metadata
Metadata
Assignees
Labels
No labels