Skip to content

Instantly share code, notes, and snippets.

@acristoffers
Last active March 20, 2020 21:17
Show Gist options
  • Save acristoffers/278d6c418f2ecacba6df762d7c6f85c6 to your computer and use it in GitHub Desktop.
Save acristoffers/278d6c418f2ecacba6df762d7c6f85c6 to your computer and use it in GitHub Desktop.
Linux/macOS Profile Generator
#!/usr/bin/env python3
import sys
import os
def ident(txt: str):
return txt.replace(' ', '').replace('\t', ' ').strip()
def generate_profile(os: str):
xs = ['#!/bin/bash', 'export HOME=~', 'umask 077',
'alias ccat="pygmentize -g"', 'alias pip="pip2"']
if os == 'macos':
xs += ['alias ls="ls -G"',
'alias mysql_start="brew services start mysql"',
'alias mysql_stop="brew services stop mysql"'
'alias postgres_start="brew services start postgres"',
'alias postgres_stop="brew services stop postgres"',
'alias ct="rm -r ${HOME}/.Trash/*"'
r'alias rep="resize -s 24 80 &>/dev/null;printf \'\e[3;0;0t\';clear"',
r'alias rep2="resize -s 24 155 &>/dev/null;printf \'\e[3;500;0t\';clear"',
r'alias rep3="resize -s 34 80 &>/dev/null;printf \'\e[3;0;370t\';clear"',
r'alias rep4="resize -s 34 155 &>/dev/null;printf \'\e[3;500;370t\';clear"',
'alias fv="resize -s 60 155 &>/dev/null;printf \'\e[3;500;0t\';clear"']
else:
xs += ['alias ls="ls --color=auto"']
xs += [
ident('''
function add_to_path_if_exists()
{
\tif [[ -d "$1" ]]
\tthen
\t\texport PATH=$PATH:$1
\tfi
}
''')]
if os == 'macos':
xs += ['old_ifs=$IFS',
'IFS=:',
'qt=(${HOME}/Qt/*/clang_64/bin)',
'export PATH=$PATH:"${qt[*]}"',
'IFS=$old_ifs']
xs += [
'add_to_path_if_exists ${HOME}/bin',
'add_to_path_if_exists ${HOME}/.config/yarn/global/node_modules/.bin',
'export PYTHON=$(which python3)',
'export LC_ALL=en_US.UTF-8',
'export LANG=$LC_ALL',
'export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/opt/openblas/lib/pkgconfig"',
ident('''
function tbz()
{
\ttar -c "$(basename "$1")" | bzip2 -9 > "$(basename "$1").tbz"
}
function tgz()
{
\ttar -c "$(basename "$1")" | gzip -9 > "$(basename "$1").tgz"
}
function txz()
{
\ttar -c "$(basename "$1")" | xz -9 -T 0 > "$(basename "$1").txz"
}
function zipup()
{
\tzip -9 -r "$(basename "$1")" "$(basename "$1")"
}
if [ -f /etc/bash_completion ]; then
\tsource /etc/bash_completion
fi
''')]
xs += [ident('''
if [[ $- == *i* ]]; then
\texport PS1="\\[\\033[38;5;14m\\]\\W\\[$(tput sgr0)\\]\\[\\033[38;5;15m\\] \\[$(tput sgr0)\\]\\[\\033[38;5;2m\\]\\\\$\\[$(tput sgr0)\\]\\[\\033[38;5;15m\\] \\[$(tput sgr0)\\]"
fi
''')]
if os == 'macos':
xs += [ident(
'''
function pip-up()
{
\tpip2 install --upgrade pip
\tpip2 list --outdated --no-cache-dir | awk 'FNR>2 {print $1}' | xargs pip2 install --no-cache-dir --upgrade
}
function pip3-up()
{
\tpip3 install --upgrade pip
\tpip3 list --outdated --no-cache-dir | awk 'FNR>2 {print $1}' | xargs pip3 install --no-cache-dir --upgrade
}
function grid()
{
\tif [ "$1" == "v" ]
\tthen
\t\tosascript ~/bin/grid.scpt vertical
\t\trep
\telif [ "$1" == "h" ]
\tthen
\t\tosascript ~/bin/grid.scpt horizontal
\t\trep
\telif [ "$1" == "v2" ]
\tthen
\t\tosascript ~/bin/grid.scpt vertical2
\t\trep2
\telif [ "$1" == "h2" ]
\tthen
\t\tosascript ~/bin/grid.scpt horizontal2
\t\trep3
\telse
\t\tosascript ~/bin/grid.scpt
\t\trep
\tfi
}
function pastecb()
{
\tsleep 5
\tosascript -e 'tell application "System Events" to keystroke the clipboard as text'
}
''')]
else:
xs += [ident(
'''
function pip-up()
{
\tpip2 install --upgrade --user pip
\tpip2 list --outdated --no-cache-dir --user | awk 'FNR>2 {print $1}' | xargs pip2 install --no-cache-dir --upgrade --user
}
function pip3-up()
{
\tpip3 install --upgrade pip --user
\tpip3 list --outdated --no-cache-dir --user | awk 'FNR>2 {print $1}' | xargs pip3 install --no-cache-dir --upgrade --user
}
''')]
xs += [ident('''
if [[ $- == *i* ]]
then
\tbind '"\\e[A": history-search-backward'
\tbind '"\\e[B": history-search-forward'
fi
''')]
if os == 'debian':
xs += [ident('''
function update()
{
\tsudo apt update
\tsudo apt-get dist-upgrade -y
\tsudo apt-get autoremove -y
\tpip-up
\tpip3-up
}
''')]
elif os == 'fedora':
xs += [ident('''
function update()
{
\tsudo dnf distro-sync -y
\tsudo dnf autoremove -y
\tsudo dnf remove --duplicates --obsoletes -y
\tpip3-up
}
''')]
elif os == 'suse':
xs += [ident('''
function update()
{
\tsudo zypper dup -y
\tpip3-up
}
''')]
elif os == 'arch':
xs += [ident('''
function update()
{
\taur sync -u
\tsudo pacman -Syu
\tpip3-up
}
''')]
return '\n'.join(xs)
if __name__ == '__main__':
args = sys.argv[1:]
ds = ['debian', 'fedora', 'suse', 'arch', 'macos']
ds = [d for d in ds if f'--{d}' in args]
if len(ds) != 1:
print('Specify one of:')
print('\t--debian: For Debian based distros (Debian, Ubuntu, Mint, etc)')
print('\t--fedora: For Fedora based distros')
print('\t--suse: For SUSE based distros (SUSE, OpenSUSE)')
print('\t--arch: For Arch based distros')
print('\t--macos: For macOS')
print('### THIS COMMAND WILL OVERWRITE ~/.bashrc AND ~/.profile ###')
exit(0)
distro = ds[0]
print(f'Generating .profile and .bashrc for {distro}')
with open(os.path.expanduser('~/.profile'), 'tw') as profile:
profile.write(generate_profile(distro))
with open(os.path.expanduser('~/.bashrc'), 'tw') as bashrc:
bashrc.write('source ~/.profile')

Simply call like

generate_profile.py --debian

or

generate_profile.py --fedora

It will overwrite your ~/.profile and ~/.bashrc, so backup it if you need things in it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment