remove requirements file when pip install fails

This commit is contained in:
2021-12-03 18:37:54 +01:00
parent c6a1a659c2
commit bd0d697adc
2 changed files with 45 additions and 25 deletions

View File

@ -98,30 +98,40 @@ def router(**opts):
def pip():
for user, requirements in pip_requirements.items():
if user == 'root':
filename = join('/root', 'pip_requirements.txt')
pipcmd = 'pip3 install -r %s' % filename
tmpname = join('/root', 'pip_requirements.tmp')
pipcmd = 'pip3 install -r %s' % tmpname
else:
filename = join('/home', user, 'pip_requirements.txt')
pipcmd = 'sudo --user %s pip3 install --user -r %s' % (user, filename)
tmpname = join('/home', user, 'pip_requirements.tmp')
pipcmd = 'sudo --user %s pip3 install --user -r %s' % (user, tmpname)
filename = tmpname.replace('.tmp', '.txt')
content = ''.join('# --- for %s ---\n%s\n' % kv for kv in requirements.items())
if write_when_new(filename, content):
unix_cmd(pipcmd)
if write_when_new(filename, content, True):
if doit:
os.rename(filename, tmpname)
if os.system(pipcmd) == 0:
os.rename(tmpname, filename)
else:
os.remove(tmpname)
else:
print(pipcmd)
# unix_cmd(pipcmd, stdout=None)
show.dirty = True
SERVICES = dict(router=router, frappy=frappy)
def unix_cmd(command, always=False):
def unix_cmd(command, always=False, stdout=PIPE):
if doit or always:
if not always:
print('$ %s' % command)
return Popen(command.split(), stdout=PIPE).communicate()[0].decode()
result = Popen(command.split(), stdout=stdout).communicate()[0]
return (result or b'').decode()
else:
print('> %s' % command)
def write_when_new(filename, content):
def write_when_new(filename, content, ignore_reduction=False):
if content is None:
lines = []
else:
@ -135,6 +145,14 @@ def write_when_new(filename, content):
old = None
if old == content:
return False
if ignore_reduction:
content_set = set(v for v in lines if not v.startswith('#'))
old_set = set(v for v in (old or '').split('\n') if not v.startswith('#'))
content_set -= old_set
if content_set:
print('missing packages: %s' % (', '.join(content_set - old_set)))
else:
return False
if doit:
if lines:
with open(filename, 'w') as fil:
@ -371,7 +389,7 @@ def handle_config():
raise
return False
actions_dict = {}
to_start = {}
reload_systemd = False
for service, template_func in SERVICES.items():
section = service.upper()
@ -387,32 +405,32 @@ def handle_config():
enabled = True
elif line.strip() == 'ActiveState=active':
active = True
actions = set()
if template is None:
if active:
actions.add('stop')
elif enabled:
actions.add('disable')
unix_cmd('systemctl stop %s' % service)
show.dirty = True
if enabled:
unix_cmd('systemctl disable %s' % service)
show.dirty = True
else:
if not active:
actions.add('restart')
if not enabled:
actions.add('enable')
to_start[service] = 'enable'
elif not active:
to_start[service] = 'restart'
if write_when_new('/etc/systemd/system/%s.service' % service, template):
show.dirty = True
reload_systemd = True
if 'enable' in actions:
actions.add('restart')
actions_dict[service] = actions
if template and to_start.get('service') is None:
to_start[service] = 'restart'
pip()
if reload_systemd:
unix_cmd('systemctl daemon-reload')
for service in SERVICES:
for action in 'stop', 'restart', 'disable', 'enable':
if action in actions_dict[service]:
show.dirty = True
unix_cmd('systemctl %s %s' % (action, service))
for service, action in to_start.items():
show.dirty = True
if action == 'restart':
unix_cmd('systemctl restart %s' % service)
unix_cmd('systemctl enable %s' % service)
return True

View File

@ -1,10 +1,12 @@
[NETWORK]
; please refer to README.md for help
enp1s0=192.168.127.1/24
enp2s0=192.168.2.1/24
enp3s0=192.168.3.1/24
enp4s0=dhcp
[ROUTER]
; please refer to README.md for help
3000=/dev/ttyUSB0
5900=192.168.2.33
8080=192.168.127.254:80