diff --git a/main.py b/main.py index 216f729..671c7f8 100755 --- a/main.py +++ b/main.py @@ -18,33 +18,56 @@ GNU General Public License for more details. A copy of the GNU General Public License version 2 is in license.txt. """ +import apt import sys +import argparse import launchpadlib from launchpadlib.launchpad import Launchpad +parser = argparse.ArgumentParser() +parser.add_argument("-f", "--file", help="The path of the input file", required=True) +parser.add_argument("-u", "--lp-user", help="The Launchpad ID of the team you would like to subscribe the bugs to", required=True) +parser.add_argument("-r", "--remove", action="store_true", help="Instead of subscribing to the packages, unsubscribe") +args = parser.parse_args() + def no_credential(): - print("Can't proceed without Launchpad credentials.") - sys.exit() + raise ValueError("Can't proceed without Launchpad credentials.") -lp = Launchpad.login_with('Mass package bug subscription adder', 'production', credential_save_failed=no_credential) -teamtowrite = (input('What person/team would you like to use for this? (Launchpad ID) ')) +lp = Launchpad.login_with("masspackagelp", "production", credential_save_failed=no_credential) -while True: - try: - filepath = (input('Please specify the path of the input file: ')) - with open(filepath, "r") as ins: - filearray = [] - for line in ins: - filearray.append(line.strip()) - break - except FileNotFoundError: - print("File not found.") - -for i in range(len(filearray)): - print("Assigning:") - print(filearray[i]) +def main(): + while True: + try: + with open(args.file, "r") as ins: + filelines = [] + for line in ins: + filelines.append(line.strip()) + break + except FileNotFoundError: + raise FileNotFoundError("File not found.") + + if not args.remove: + for i in range(len(filelines)): + try: + lp.distributions["ubuntu"].getSourcePackage(name=filelines[i]).addBugSubscription(subscriber=lp.people[args.lp_user]) + print("Successfully added subscription for " + filelines[i]) + except AttributeError: + print("Source package " + filelines[i] + " not found") + else: + for i in range(len(filelines)): + print("Removing subscription for: " + filelines[i]) + try: + lp.distributions["ubuntu"].getSourcePackage(name=filelines[i]).removeBugSubscription(subscriber=lp.people[args.lp_user]) + print("Successfully removed subscription for " + filelines[i]) + except AttributeError: + print("Source package not found.") + except: + print(args.lp_user + " is not subscribed to " + filelines[i] + " so not removing") + + +if __name__ == '__main__': try: - lp.distributions['ubuntu'].getSourcePackage(name=filearray[i]).addBugSubscription(subscriber=lp.people[teamtowrite]) - except AttributeError: - print("Source package not found.") - print("Complete") + main() + except KeyboardInterrupt: + print("Exiting...") + exit()