Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- * hcf.py
- *
- * @autor: Sergio López
- * @email: daemonfreedom93@gmail.com
- * @web: http://daemonfreedom.blogspot.com
- *
- """
- try:
- import sys
- import os.path
- import re
- print "HCF: import sys"
- print "HCF: import os.path"
- print "HCF: import re"
- except ImportError:
- print "HCF Error: Can not import sys, os.path, re"
- exit(1)
- try:
- import hashlib
- #inithash = hashlib.md5()
- hcffrag = int()
- hcffrag = 1
- print "HCF: import hashlib"
- except ImportError:
- import md5
- #inithash = md5.new()
- hcffrag = int()
- hcffrag = 2
- print "HCF: import md5"
- def CreateSum(hcfInfo):
- L = list()
- if hcffrag == 1:
- hcfsum = hashlib.md5()
- elif hcffrag == 2:
- hcfsum = md5.new()
- try:
- f = open(hcfInfo, "r")
- while True:
- line = f.readline()
- if not line: break
- L.append(line)
- f.close()
- hcfstr = str(L)
- except IOError:
- print "HCF Error: No such file!"
- exit(1)
- hcfsum.update(hcfstr)
- return hcfsum.hexdigest()
- def CaseManager():
- if(len(sys.argv) == 3):
- if sys.argv[1] == "new":
- hcf.hcfNew(sys.argv[2])
- elif sys.argv[1] == "del":
- hcf.hcfDel(sys.argv[2])
- elif sys.argv[1] == "show":
- hcf.hcfShow(sys.argv[2])
- elif sys.argv[1] == "check":
- hcf.hcfCheck(sys.argv[2])
- else:
- print "HCF Error: Invalid argumet!"
- hcf.hcfHelp()
- exit(1)
- elif(len(sys.argv) == 2):
- if sys.argv[1] == "help":
- hcf.hcfHelp()
- elif sys.argv[1] == "about":
- hcf.hcfAbout()
- else:
- print "HCF Error: Invalid argumet!"
- hcf.hcfHelp()
- exit(1)
- def actFile(hlist):
- try:
- f = open("/etc/hcffile", "w")
- for i in hlist:
- f.write(i)
- f.close()
- except IOError:
- print "HCF Error: You need to be root!"
- exit(1)
- def openFile():
- L = list()
- f = open("/etc/hcffile", "r")
- while True:
- line = f.readline()
- if not line: break
- L.append(line)
- f.close()
- return L
- def retSplit(splitcad, param=0):
- retl = splitcad.split(" ")
- if param==0:
- return retl[0]
- elif param==1:
- return retl[1]
- class hcfMain():
- """Main class"""
- def __init__(self):
- hcfFrag = os.path.isfile("/etc/hcffile")
- if hcfFrag == True:
- print "HCF: Ok..."
- elif hcfFrag == False:
- print "HCF: Creating file..."
- try:
- f_hcf = open("/etc/hcffile", "w")
- f_hcf.close()
- except IOError:
- print "HCF Error: You need to be root!"
- exit(1)
- def hcfHelp(self):
- print "\t-> HCF Help"
- print "\nHcf To use, you must use arguments.\n"
- print " new <dir> for new addition"
- print " del <dir> to eliminate"
- print " check <dir> to check sum"
- print " show <dir> to check file"
- print "\n Example:\n\t new /home/user/document.txt"
- print "\nTo run the program needs to be root"
- def hcfAbout(self):
- print "\t-> HCF About"
- print "\nHCF is an application written in python, by Sergio Lopez. Used to keep track of the desired files through its md5sum"
- print "\nE-Mail: daemonfreedom93@gmail.com"
- print "Web: daemonfreedom.blogspot.com\n"
- def hcfNew(self, hcfDir):
- self.hcfDir = hcfDir
- L = list()
- hcffrag2 = int()
- #Crear línea
- hcfline = self.hcfDir+" "+CreateSum(self.hcfDir)+"\n"
- #FIN Línea
- #Almacenar archivo en buffer(lista L)
- L = openFile()
- #FIN Buffer Lista
- #Buscar coincidencias...
- if len(L)>0:
- for i in L:
- if i == hcfline:
- hcffrag2 = 1
- else:
- hcffrag2 = 2
- else:
- hcffrag2 = 2
- #FIN búsqueda
- if hcffrag2 == 1:
- print "HCF Error: Already exist!"
- elif hcffrag2 == 2:
- L.append(hcfline)
- actFile(L) #Actualizar hcffile
- def hcfDel(self, hcfDir):
- self.hcfDir = hcfDir
- hcffrag3 = int()
- L = list()
- Laux = list()
- cont = int()
- posx = int()
- L = openFile()
- #Step1
- cont =0
- for i in L:
- vaux = retSplit(L[cont])
- Laux.append(vaux)
- cont = cont+1
- #Step2
- cont =0
- while True:
- try:
- if Laux[cont] == self.hcfDir:
- L.pop(cont)
- print "HCF: Element",cont,"deleted!"
- break
- else:
- cont = cont+1
- except IndexError:
- print "HCF Error: No elements in this list!"
- exit(1)
- actFile(L)
- def hcfCheck(self, hcfDir):
- self.hcfDir = hcfDir
- L = list()
- Laux = list()
- bf = list()
- cont = int()
- hcffrag4 = int()
- hcfnum = CreateSum(self.hcfDir)+"\n"
- L = openFile()
- cont =0
- for i in L:
- vaux = retSplit(L[cont])
- Laux.append(vaux)
- cont = cont+1
- hcffrag4 =0
- cont =0
- while True:
- try:
- if Laux[cont] == self.hcfDir:
- hcffrag4 = cont
- break
- else:
- cont = cont+1
- except IndexError:
- print "HCF Error: No elements in this list!"
- exit(1)
- bf.append (retSplit(L[hcffrag4]))
- bf.append (retSplit(L[hcffrag4], 1))
- if hcfnum == bf[1]:
- print "HCF: No changes"
- else:
- print "HCF: If there are changes!!!"
- print "OLD sum:", bf[1]
- print "NEW sum:", hcfnum
- def hcfShow(self, hcfDir):
- self.hcfDir = hcfDir
- L = list()
- try:
- f = open(self.hcfDir, "r")
- while True:
- line = f.readline()
- if not line: break
- L.append(line)
- f.close()
- except IOError:
- print "HCF Error:",self.hcfDir,"no such file!"
- exit(1)
- for element in L:
- print element
- def main():
- CaseManager()
- return 0
- if __name__ == '__main__':
- print "\nWelcome to HCF: Hash Control Files"
- hcf = hcfMain()
- main()
- # Fuentes:
- #
- # -> www.elcodigok.com.ar
- # -> Documento "Python_para_todos" de Raúl González Duque
- # -> http://es.w3support.net
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement