Commit b67a58b2 authored by diesys's avatar diesys 🎈

some fix to installer and help

parent c13dada7
#!/usr/bin/env python3
import sys, re, math, getpass
from os import chdir
from subprocess import run, PIPE
from pathlib import Path
# import getpass #useful for unit systemd
version = "0.9"
# version = "0.9"
current_bl = open('/sys/class/backlight/intel_backlight/brightness', 'r+')
max_bl = open('/sys/class/backlight/intel_backlight/max_brightness', 'r')
state = {'current': int(current_bl.read()), 'max': int(max_bl.read()), 'changed': False, 'current_info': '', 'invalid_option': True}
state = {'version': "0.9", 'current': int(current_bl.read()), 'max': int(max_bl.read()), 'changed': False, 'current_info': '', 'invalid_option': True,\
'executable': Path('/usr/bin/iblu'), 'src_dir': Path('/usr/share/iblu'),\
'git_repo': 'https://git.eigenlab.org/sbiego/iblu.git'}
state['new'] = state['current']
percentize = 100/state['max']
shift_one_pc = int(state['max'])/100 #unita' percentuale sulla luminosita' massima
......@@ -16,14 +22,81 @@ state['current_pc'] = 0 if (state['current'] < shift_one_pc-1) else math.ceil(in
state['current_info'] = str(state['current_pc']) + '% (' + str(state['current']) + '/' + str(state['max']) + ')'
help = " _ __ __\n (_) / / / _ __\n / / _ \/ / // /\n/_/_.__/_/\_,_/ v" + version + "\
\nIntel Black-Light Util:\n\n\t0-100\t\tsets backlight to the given percentage\n\ti (inc)\t\tincreases backlight by a step, optionally add a number to custom percentage (default is " + str(shift_std_pc) + "%)\n\td (dec)\t\tdecreases backlight, optionally add a number to custom percentage (default is " + str(shift_std_pc) + "%)\n\tc (curr)\t\tshows the current\n\tv\t\tshows verbose output\n\tV\t\tshows very verbose output (for debug)\n\tOFF\t\tturns off backlight (use with a grain of salt)\n\tUNIT\t\tprompts in terminal the Systemd unit raw text (better using with I/O redirecting)\n\nexample: iblu i30\t#increases of 30% the current backlight (30 is optional)\n"
unit = "[Unit]\nDescription=Intel BackLight Util, changes owner of /sys/class/blacklight/intel_blacklight/brightness\n\n[Service]\nExecStart=/usr/bin/chown " + getpass.getuser() + ":wheel /sys/class/backlight/intel_backlight/brightness\n\n[Install]\nWantedBy=multi-user.target\n"
## IMPORTED FROM MOTOGIF
HELP = {"asciiart": " _ __ __\n (_) / / / _ __\n / / _ \/ / // /\n/_/_.__/_/\_,_/ v",
"title": "iblu",
"version": " v",
"descr": "\nIntel Black-Light Util: a simple utility to change backlight via cli.\n",
"usage": "\n\t0-100\t\tsets backlight to the given percentage\n\ti (inc)\t\tincreases backlight by a step, optionally add a number to custom percentage (default is " + str(shift_std_pc) + "%)\n\td (dec)\t\tdecreases backlight, optionally add a number to custom percentage (default is " + str(shift_std_pc) + "%)\n\tc (curr)\t\tshows the current\n\tv\t\tshows verbose output\n\tV\t\tshows very verbose output (for debug)\n\tOFF\t\tturns off backlight (use with a grain of salt)\n\tUNIT\t\tprompts in terminal the Systemd unit raw text (better using with I/O redirecting)\n\n\t--install \tinstall from local directory (a cloned repo!)\n\t--install-git\tclone the online repo and install it into the system\n\t-u, --update\tchek for any new commits from git repo and install them\n\nexample: iblu i30\t#increases of 30% the current backlight (30 is optional)\n",
# "notes": "\n\n\t! WARNING: motogif may overwrite or remove its own temp files or any files passed as argument",
"license": "\nLICENSE: GPLv3 - https://www.gnu.org/licenses/gpl-3.0.html\n",
"repo": "\nGit repo: 'https://git.eigenlab.org/sbiego/iblu.git'",
"sysd_unit" : "[Unit]\nDescription=Intel BackLight Util, changes owner of /sys/class/blacklight/intel_blacklight/brightness\n\n[Service]\nExecStart=/usr/bin/chown " + getpass.getuser() + ":wheel /sys/class/backlight/intel_backlight/brightness\n\n[Install]\nWantedBy=multi-user.target\n"
}
def calcRevision():
if state['src_dir'].exists() and re.search('^[0-9]+.[0-9]+$', state['version']):
cwd = Path.cwd()
chdir(state['src_dir'])
update_version = ''
if Path('.git').exists():
# using git number of commit, adding revision to the major version
git_log = run(['git', 'log', '--oneline'], stdout=PIPE,
stderr=PIPE, universal_newlines=True)
# the new command to be compiled (down)
update_version = "state['version']=str('" + str(
state['version']) + "'+'-'+'" + str(len(re.findall('\n', git_log.stdout))) + "');"
# write it in the HELP too
update_version += "HELP['version']+=state['version']"
chdir(cwd)
# the new command being compiled and evaluated, so the revision is added to the major version (A.BC-XY)
newcode = compile(update_version, "", 'exec')
eval(newcode)
def promptHelp(info, sections=['asciiart', 'version', 'descr', 'usage']):
help = ''
for section in sections:
help += HELP[section]
# print(HELP[section])
if info != '':
help += str('\n\n· ' + info + '\n')
# print('· ' + info)
print(help)
exit
def install(opt='local'):
print("· Cloning into "+ str(state['src_dir']) +" ...")
if not state['src_dir'].exists():
print("· Creating the folder ...")
run(['sudo', 'mkdir', state['src_dir']])
if opt == 'git':
git_clone = run(['git', 'clone', str(state['git_repo']), '/tmp/iblu', '--progress'], stdout=PIPE, stderr=PIPE, universal_newlines=True)
print(git_clone.stdout)
run(['sudo', 'cp', '-r', '/tmp/iblu', str(state['src_dir'])])
elif opt == 'local':
run(['sudo', 'cp', '-r', '.', str(state['src_dir'])])
print("· Linking the executable " + str(state['executable']) + " ...")
run(['sudo', 'ln', '-fs', Path(str(state['src_dir'])+'/iblu.py'), state['executable']], stdout=PIPE, stderr=PIPE, universal_newlines=True)
print('Done')
promptHelp('', ['usage'])
exit
def update():
cwd = Path.cwd()
chdir(state['src_dir'])
git_pull=run(['sudo', 'git', 'pull', state['git_repo'], '--progress'], stdout=PIPE, stderr=PIPE, universal_newlines=True)
chdir(cwd)
print(git_pull.stdout)
### TODO
# - import HELP structure, version 'calcRevision', directory structure and install/update from MoToGIF
# - usare +/- non i e d..........
# - attivare/disattivare/controllare stato direttamente della unit systemd da iblu
# ····························
### FARE UNA REGEX ALL'INIZIO E POI LE SOTTOSELEZIONI CON LE PIU SEMPLICI (utile per parametri giusti e no e semplificare regex)
......@@ -53,7 +126,7 @@ def updateState(new_percent):
def verboseOut(print_state=False):
if state['changed']:
print("old: ", state['current_info'])
# print("old: ", state['current_info'])
print("new: ", state['new_info'])
else:
print("current: ", state['current_info'])
......@@ -68,27 +141,9 @@ def increase(percentage=shift_std_pc):
updateState(int(state['current_pc']) + percentage)
# revision for exact verson number based on git
def calcRevision():
if re.search('^[0-9]+.[0-9]+$', state['version']):
cwd = Path.cwd()
chdir(state['src_dir'])
if Path('.git').exists():
# using git number of commit, adding revision to the major version
git_log = run(['git', 'log', '--oneline'], stdout=PIPE, stderr=PIPE, universal_newlines=True)
# the new command to be compiled (down)
update_version = "state['version']=str('" + str(state['version']) + "'+'-'+'" + str(len(re.findall('\n', git_log.stdout))) + "');"
# write it in the HELP too
update_version += "HELP['version']+=state['version']"
chdir(cwd)
# the new command being compiled and evaluated, so the revision is added to the major version (A.BC-XY)
newcode=compile(update_version, "",'exec')
eval(newcode)
if(len(sys.argv) == 2): ## getting parameters if exist
option = sys.argv[1]
calcRevision()
# percentage set
if(re.search(r"^[v|V]{0,1}[100]{1}$|^[100]{1}[v|V]{0,1}$|^[v|V]{0,1}[0-9]{1,2}$|^[0-9]{1,2}[v|V]{0,1}$", option)): ## percentage
......@@ -102,53 +157,73 @@ if(len(sys.argv) == 2): ## getting
verboseOut(debug)
# decrease 1
if(re.search(r"^[v|V]{0,1}d$|^d[v|V]{0,1}$|^[v|V]{0,1}dec$|^dec[v|V]{0,1}$", option)): ## decrease bl by a step
elif(re.search(r"^[v|V]{0,1}d$|^d[v|V]{0,1}$|^[v|V]{0,1}dec$|^dec[v|V]{0,1}$", option)): ## decrease bl by a step
decrease()
if(re.search(r"[v|V]{1}", option)): ## verbose
debug = True if(re.search(r"V", option)) else False
verboseOut(debug)
# 2 decrease
if(re.search(r"^d[0-9]{1,2}$|^dec[0-9]{1,2}$|^[0-9]{1,2}d$|^[0-9]{1,2}dec$", option)): ## dec bl by percentage
elif(re.search(r"^d[0-9]{1,2}$|^dec[0-9]{1,2}$|^[0-9]{1,2}d$|^[0-9]{1,2}dec$", option)): ## dec bl by percentage
decrease(int(re.findall(r"[0-9]{1,2}", option)[0]))
if(re.search(r"[v|V]{1}", option)): ## verbose
debug = True if(re.search(r"V", option)) else False
verboseOut(debug)
# increase 1
if(re.search(r"^i$|^inc$", option)): ## increase bl by a step
elif(re.search(r"^i$|^inc$", option)): ## increase bl by a step
increase()
if(re.search(r"[v|V]{1}", option)): ## verbose
debug = True if(re.search(r"V", option)) else False
verboseOut(debug)
# 2 increase
if(re.search(r"^i[0-9]{1,2}$|^inc[0-9]{1,2}$|^[0-9]{1,2}i$|^[0-9]{1,2}inc$", option)): ## inc bl by percentage
elif(re.search(r"^i[0-9]{1,2}$|^inc[0-9]{1,2}$|^[0-9]{1,2}i$|^[0-9]{1,2}inc$", option)): ## inc bl by percentage
increase(int(re.findall(r"[0-9]{1,2}", option)[0]))
if(re.search(r"[v|V]{1}", option)): ## verbose
debug = True if(re.search(r"V", option)) else False
verboseOut(debug)
# current info
if(re.search(r"^c$|^curr$", option)): ## see current bl
state['invalid_option'] = False
elif(re.search(r"^c$|^curr$", option)): ## see current bl
# state['invalid_option'] = False
debug = True if(re.search(r"V", option)) else False
verboseOut(debug)
# install
elif(re.search(r"^--install(-git)*$", option)):
# state['invalid_option'] = False
promptHelp('Installing ...', ['asciiart', 'version'])
if len(sys.argv) == 3 and re.search("^git$", sys.argv[2]):
install('git')
exit
else:
install()
exit
debug = True if(re.search(r"V", option)) else False
verboseOut(debug)
# update
elif re.search("^-+(-update|u)$", sys.argv[1]):
promptHelp('Updating ...', ['asciiart', 'version'])
update()
# turns off
if(re.search(r"^OFF$", option)):
state['invalid_option'] = False
elif(re.search(r"^OFF$", option)):
# state['invalid_option'] = False
state['new'] = 0
debug = True if(re.search(r"V", option)) else False
verboseOut(debug)
# prints the systemd unit file
if(re.search(r"^UNIT$", option)):
state['invalid_option'] = False
elif(re.search(r"^UNIT$", option)):
# state['invalid_option'] = False
print(unit)
if(state['invalid_option']):
print(help)
# if(state['invalid_option']):
if re.search("^-(h|-help)$", sys.argv[1]):
# print(help)
promptHelp('')
# writing the file.. actual changing brightness
#verboseOut(True) ## debug
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment