PrinzPiuz

Software Engineer | Ziff Davis | Cochin


Python Saves My Time

Published June 2, 2020 πŸ• 3 Mins

“Repetitive task should be automated” - somebody, somewhere 😎

Its common philosophy in the field of it to automate repetitive tasks, even if it takes some time now to set up it will save a lot of time in the future. In this post, I like to share the kinda experience I had today while updating my machine and how python solved it easily.

In my work machine, I am running Manjaro Linux, which is a Rolling Release based on Archlinux. So while updating today I had an β€œerror__: failed to commit transaction (conflicting files)" error, which is a common error in archlinux due to the same file already existing in the machine(file conflict) you can read more about it here. The solution for this error is to delete each file manually, so easy na ?. Just remove that file with the rm command. But what if we have this many conflicted files?

expand
btrfs-progs: /usr/bin/fsck.btrfs exists in filesystem
btrfs-progs: /usr/bin/mkfs.btrfs exists in filesystem
btrfs-progs: /usr/include/btrfsutil.h exists in filesystem
btrfs-progs: /usr/lib/initcpio/hooks/btrfs exists in filesystem
btrfs-progs: /usr/lib/initcpio/install/btrfs exists in filesystem
btrfs-progs: /usr/lib/libbtrfs.so exists in filesystem?
btrfs-progs: /usr/lib/libbtrfs.so.0 exists in filesystem
btrfs-progs: /usr/lib/libbtrfs.so.0.1 exists in filesystem
btrfs-progs: /usr/lib/libbtrfsutil.so exists in filesystem
btrfs-progs: /usr/lib/libbtrfsutil.so.1 exists in filesystem
btrfs-progs: /usr/lib/python3.8/site-packages/btrfsutil.cpython-38-x86_64-linux-gnu.so exists in filesystem
btrfs-progs: /usr/lib/systemd/system/btrfs-scrub@.service exists in filesystem
btrfs-progs: /usr/lib/systemd/system/btrfs-scrub@.timer exists in filesystem
btrfs-progs: /usr/lib/udev/rules.d/64-btrfs-dm.rules exists in filesystem
btrfs-progs: /usr/share/bash-completion/completions/btrfs exists in filesystem
btrfs-progs: /usr/share/man/man5/btrfs.5.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-balance.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-check.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-convert.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-device.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-filesystem.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-find-root.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-image.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-inspect-internal.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-map-logical.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-property.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-qgroup.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-quota.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-receive.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-replace.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-rescue.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-restore.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-scrub.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-select-super.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-send.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs-subvolume.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfs.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfsck.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/btrfstune.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/fsck.btrfs.8.gz exists in filesystem
btrfs-progs: /usr/share/man/man8/mkfs.btrfs.8.gz exists in filesystem
so it will be tough to delete all this conflict files one by one. I use python for help in this kind of context. So my idea is to save this shell output as a text file and parse strings one by one from it into a python list and split strings based on space and get the file path from it and run shell commands on that path from python

import os
file=open("conflict.txt", "r")
for string in file:
  os.system("sudo rm "+ string.split(" ")[1])

This python snippet does the thing for me and saves a lot of time. Read more about os.system

Note: This was solution I tried when I was not aware of the actual solution, and I was in a hurry to complete the update too. Later I found a Solution from archlinux wiki itself. So for the same error above correct and easy solution is, you may explicitly run pacman -S --overwrite glob package to force pacman to overwrite files that match glob.