-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_env.py
More file actions
229 lines (206 loc) · 7.23 KB
/
setup_env.py
File metadata and controls
229 lines (206 loc) · 7.23 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import os
from pathlib import Path
def setup_git():
__git_config = r"""# Autogenerated do not change
[user]
name = {user}
useconfigonly = true
[core]
filemode = false
# editor = vim
[fetch]
prune = true
[alias]
co = checkout
st = status
ls = log --pretty=format:"%C(yellow)%h\\ %cr%Cgreen%d\\ %Creset%s%C(cyan)\\ [%an]" --decorate --date=relative
lsr = log --pretty=format:"%C(yellow)%h\\ %Creset%s%C(cyan)\\ [%an]"
ll = log --pretty=format:"%C(yellow)%h\\ %cr%Cgreen%d\\ %Creset%s%C(cyan)\\ [%an]" --decorate --numstat --date=relative
lg = log --graph --oneline --decorate
ld = log --pretty=oneline --left-right
dt = difftool
mt = mergetool
hc = clean -xffd
brAll = branch --list --remote
br = branch --list
showBr = show --pretty=format:"%C(yellow)%h\\ %ad%Cgreen%d\\ %Creset%s%C(cyan)\\ [%an]" --decorate --date=relative
delBr = branch -D
pf = push --force-with-lease
su = branch --set-upstream-to
ca = commit --amend --no-edit
tagList = for-each-ref --format '%(refname) %09 %(taggerdate) %(subject) %(taggeremail)' refs/tags --sort=taggerdate
sbu = submodule update
sbs = submodule sync
sbf = submodule foreach
pmrm = push -o merge_request.create -o merge_request.remove_source_branch -o merge_request.target=master
pmrs = push -o merge_request.create -o merge_request.remove_source_branch -o merge_request.target=sweet500_sdv_master
pmra = push -o merge_request.create -o merge_request.remove_source_branch -o merge_request.target=ad2gen2_master
pmrd = push -o merge_request.create -o merge_request.remove_source_branch -o merge_request.target=sweet400_d2c_frcamadas_master
[push]
default = upstream
autoSetupRemote = true
[difftool]
prompt = false
[diff]
tool = nvim
[difftool "nvim"]
cmd = "nvim -d \"$LOCAL\" \"$REMOTE\""
[merge]
tool = nvim
[mergetool]
prompt = false
[mergetool "nvim"]
cmd = nvim -d $LOCAL $REMOTE $MERGED -c '$wincmd w' -c 'wincmd J'
[credential]
helper = store --file ~/.git.store
[http]
postBuffer = 1048576000
[lfs]
fetchrecentrefsdays = 0
fetchrecentcommitsdays = 0
fetchrecentremoterefs = 0
pruneoffsetdays = 0
concurrenttransfers = 8
forceprogress = 1
[url "https://"]
insteadOf = git://
[pull]
rebase = false
[color]
ui = auto
[filter "lfs"]
smudge = git-lfs smudge --skip -- %f
process = git-lfs filter-process --skip
required = true
clean = git-lfs clean -- %f
"""
default_user = "Nadhmi JAZI"
user = input(f"User Name [{default_user}]:")
if not user:
user = default_user
home = os.getenv("HOME", "")
with open(os.path.join(home, ".gitconfig"), "w") as f:
f.write(__git_config.format(user=user))
def setup_bash(home: Path):
__bash = r"""#!/bin/bash
########################################
# /!\ Autogenerated file do not change #
########################################
# History
export HISTCONTROL=erasedups # when adding an item to history, delete itentical commands upstream
export HISTSIZE=10000 # save 10000 items in history
shopt -s histappend # append history to ~\.bash_history when exiting shell
export PROMPT_COMMAND="history -a;$PROMPT_COMMAND"
# proxy
{proxy}
# aliases
alias ..='cd ..'
alias ll='ls -alFh'
alias la='ls -A'
alias lla='ls -Al'
alias l='ls -CF'
alias envgrep='env|grep'
alias hgrep='history|grep'
alias cgrep='grep --include="*.[ch]"'
alias sudo='sudo -E'
alias amn='export SOONG_GEN_RUST_PROJECT=1 && export SOONG_GEN_COMPDB=1 && m nothing'
alias aln='ln -s $ANDROID_BUILD_TOP/out/soong/rust-project.json $ANDROID_BUILD_TOP'
alias callgrind='valgrind --tool=callgrind --dump-instr=yes --cache-sim=yes --branch-sim=yes --cacheuse=yes'
alias gmco='br=$(git rev-parse --abbrev-ref HEAD) && git submodule foreach "if [ ! -z \"\$(git branch -r | grep $br)\" ]; then git checkout $br ; fi" && unset br'
alias gmcor='br=$(git rev-parse --abbrev-ref HEAD) && git submodule foreach "if [ ! -z \"\$(git branch -r | grep $br)\" ]; then git fetch; git checkout $br ; git reset --hard origin/$br; fi" && unset br'
alias termreset="tput cnorm"
# vim / nvim / nvr
if [[ -x "$(command -v nvim)" ]]; then
alias vim=nvim
export VISUAL=nvim
if [[ -n "$NVIM_LISTEN_ADDRESS" ]]; then
alias vim="nvr -cc split --remote-wait +'set bufhidden=wipe'"
alias nvim="nvr -cc split --remote-wait +'set bufhidden=wipe'"
export VISUAL="nvr -cc split --remote-wait +'set bufhidden=wipe'"
fi
# export MANPAGER="nvim -c 'set ft=man' -"
fi
# auto into tmux session
# if [[ -n "\$PS1" ]] && [[ -z "\$TMUX" ]] && [[ -n "\$SSH_CONNECTION" ]]; then
# tmux attach-session -t remote || tmux new-session -s remote
# fi
# Starship
if [[ -x "$(command -v starship)" ]]; then
export STARSHIP_CONFIG={starship}
eval "$(starship init bash)"
fi
# bumpversion for python
if [[ -x "$(command -v bumpversion)" ]]; then
alias bumpbuild="bumpversion build --allow-dirty"
alias bumppatch="bumpversion patch --allow-dirty"
alias bumpminor="bumpversion minor --allow-dirty"
alias bumpmajor="bumpversion major --allow-dirty"
alias bumprelease="bumpversion --commit --tag release"
fi
# cargo
if [ -d "$HOME/.cargo/bin" ]; then
export PATH=$PATH:$HOME/.cargo/bin
if [[ -x "$(command -v eza)" ]]; then
alias ll="eza -1 --icons -l --git-ignore"
alias lla="eza -1 --icons -l --git-ignore -a"
alias tree="eza --icons -T"
fi
if [[ -x "$(command -v bat)" ]]; then
alias cat="bat"
fi
fi
"""
proxy = []
http_proxy = os.getenv("http_proxy", None)
https_proxy = os.getenv("https_proxy", None)
if http_proxy is not None:
proxy.append(f"export http_proxy={http_proxy}")
if https_proxy is not None:
proxy.append(f"export https_proxy={https_proxy}")
starship = os.path.abspath(os.path.join(os.path.dirname(__file__), "starship.toml"))
# for MSYS make sure to have the correct path
if os.getenv("MSYSTEM", None) is not None:
starship = os.popen(f"cygpath {starship}").read().strip()
with open(home / ".bash_aliases", "w") as f:
f.write(__bash.format(proxy="\n".join(proxy), starship=starship))
def setup_profile(home: Path):
profile = home / ".profile"
if profile.exists():
print(f"{profile} exist skipping generation ...")
return
with open(profile, "w") as f:
f.write(
"""# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$PATH"
fi
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# AAOSP
export SOONG_GEN_RUST_PROJECT=1
export SOONG_GEN_COMPDB=1
"""
)
def setup(home: Path):
setup_git()
setup_bash(home)
setup_profile(home)
if __name__ == "__main__":
home = os.getenv("HOME", None)
if home is None:
raise ValueError("HOME env not found")
home = Path(home)
if not home.exists():
raise FileExistsError(f"{home} does not exist")
if not home.is_dir():
raise TypeError(f"{home} is not a directory")
setup(home)