-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpython.nu
More file actions
130 lines (112 loc) · 4.92 KB
/
Copy pathpython.nu
File metadata and controls
130 lines (112 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Create a python virtual environment and configure cloud sync exclusions.
#
# Parameters:
# dir_name: The name or path of the directory to create the virtual environment in.
#
# Example:
# create-virtualenv my_project/venv
export def create-virtualenv [dir_name:string = "venv"] {
let venv_path = $dir_name | path expand
# Exclude from Dropbox via .mignore BEFORE creating the folder (prevents selective sync conflicts)
_ensure-mignore [
"**/.venv" "**/venv" "**/.vnv"
"**/__pycache__" "**/.pytest_cache"
"**/node_modules" "**/.next"
"**/dist" "**/build"
"**/target"
]
_backup-mignore
# 1. First create the folder
mkdir $venv_path
# Resolve Dropbox and Yandex.Disk roots (support mocking for unit tests)
let dropbox_root = $env.MOCK_DROPBOX_ROOT? | default ("~/Dropbox" | path expand)
let yandex_root = $env.MOCK_YANDEX_ROOT? | default ("~/Yandex.Disk" | path expand)
# 2. Detect if the path is inside Dropbox or Yandex.Disk
if ($venv_path | str starts-with $dropbox_root) {
# Dropbox is handled by .mignore patterns above — no maestral excluded add needed
} else if ($venv_path | str starts-with $yandex_root) {
# Yandex.Disk logic
print $"Stopping Yandex.Disk daemon..."
do -i { nu --config ~/.config/nushell/config.nu --env-config ~/.config/nushell/env.nu -c 'ydx stop' }
let primary_cfg = $env.MOCK_YANDEX_CONFIG_FILE? | default "/home/kira/.config/yandex-disk/config.cfg"
print $"Updating Yandex.Disk config: ($primary_cfg)..."
_update-yandex-config $primary_cfg $venv_path
let backup_dir = $env.MY_ENV_VARS.linux_backup? | default ""
if not ($backup_dir | is-empty) {
let backup_cfg = [$backup_dir "ydx_config.cfg"] | path join
print $"Updating backup Yandex.Disk config: ($backup_cfg)..."
_update-yandex-config $backup_cfg $venv_path
} else {
print -e "Warning: $env.MY_ENV_VARS.linux_backup is not configured. Skipping backup config update."
}
print $"Starting Yandex.Disk daemon..."
do -i { nu --config ~/.config/nushell/config.nu --env-config ~/.config/nushell/env.nu -c 'ydx start' }
}
# 3. Actually create the virtualenv
python3 -m virtualenv $dir_name
}
# Helper to update yandex config file
def _update-yandex-config [config_file: path, new_exclude: string] {
if not ($config_file | path exists) {
print -e $"Warning: Yandex config file ($config_file) not found."
return
}
let content = open --raw $config_file | lines
let has_exclude = $content | any {|line| $line | str starts-with "exclude-dirs=" }
let new_content = $content | each {|line|
if ($line | str starts-with "exclude-dirs=") {
let parts = $line | split row '='
let val_with_quotes = $parts | get 1
let val = $val_with_quotes | str replace --regex '^"' '' | str replace --regex '"$' ''
let dirs = if ($val | is-empty) { [] } else { $val | split row ',' | str trim }
let updated_dirs = if ($new_exclude in $dirs) {
$dirs
} else {
$dirs | append $new_exclude
}
let new_val = $updated_dirs | str join ","
$"exclude-dirs=\"($new_val)\""
} else {
$line
}
}
let final_content = if not $has_exclude {
$new_content | append $"exclude-dirs=\"($new_exclude)\""
} else {
$new_content
}
$final_content | str join "\n" | save -f $config_file
}
# Ensure .mignore patterns exist in Dropbox root (Maestral ignore file)
# Patterns are added idempotently — existing content is preserved.
def _ensure-mignore [patterns: list<string>] {
let dropbox_root = $env.MOCK_DROPBOX_ROOT? | default ("~/Dropbox" | path expand)
if not ($dropbox_root | path exists) { return }
let mignore_path = [$dropbox_root ".mignore"] | path join
let existing = if ($mignore_path | path exists) { open --raw $mignore_path | lines } else { [] }
let to_add = $patterns | where { |p| not ($existing | any { |e| ($e | str trim | str replace --all "*" '\*') == ($p | str trim | str replace --all "*" '\*') }) }
if ($to_add | is-empty) { return }
let new_content = $existing | append $to_add
$new_content | str join "\n" | save -f $mignore_path
}
# Backup .mignore to Yandex.Disk for cross-machine portability
def _backup-mignore [] {
let dropbox_root = $env.MOCK_DROPBOX_ROOT? | default ("~/Dropbox" | path expand)
let mignore_path = [$dropbox_root ".mignore"] | path join
if not ($mignore_path | path exists) { return }
let backup_dir = $env.MOCK_BACKUP_DIR? | default ("/home/kira/Yandex.Disk/Backups/linux")
mkdir $backup_dir
let backup_path = [$backup_dir "mignore"] | path join
cp --force $mignore_path $backup_path
}
export def activate [] {
print ("'overlay use venv/bin/activate.nu' copied to clipboard!")
"overlay use venv/bin/activate.nu" | copy
}
#jdown.py wrapper
export def jdown [
--ubb(-b):string = "0"
] {
overlay use ("~/Yandex.Disk/my_scripts/python/venv/bin/activate.nu" | path expand)
python3 ([$env.MY_ENV_VARS.python_scripts jdown.py] | path join) -b $ubb
}