Advertisement
Guest User

HCF

a guest
Jan 21st, 2011
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.59 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5.  * hcf.py
  6.  *
  7.  * @autor: Sergio López
  8.  * @email: daemonfreedom93@gmail.com
  9.  * @web:   http://daemonfreedom.blogspot.com
  10.  *
  11. """
  12.  
  13. try:
  14.     import sys
  15.     import os.path
  16.     import re
  17.     print "HCF: import sys"
  18.     print "HCF: import os.path"
  19.     print "HCF: import re"
  20. except ImportError:
  21.     print "HCF Error: Can not import sys, os.path, re"
  22.     exit(1)
  23.  
  24. try:
  25.     import hashlib
  26.     #inithash = hashlib.md5()
  27.     hcffrag = int()
  28.     hcffrag = 1
  29.     print "HCF: import hashlib"
  30. except ImportError:
  31.     import md5
  32.     #inithash = md5.new()
  33.     hcffrag = int()
  34.     hcffrag = 2
  35.     print "HCF: import md5"
  36.  
  37.  
  38. def CreateSum(hcfInfo):
  39.     L = list()
  40.     if hcffrag == 1:
  41.         hcfsum = hashlib.md5()
  42.     elif hcffrag == 2:
  43.         hcfsum = md5.new()
  44.    
  45.     try:   
  46.         f = open(hcfInfo, "r")
  47.         while True:
  48.             line = f.readline()
  49.             if not line: break
  50.             L.append(line)
  51.         f.close()
  52.         hcfstr = str(L)
  53.     except IOError:
  54.         print "HCF Error: No such file!"
  55.         exit(1)
  56.        
  57.     hcfsum.update(hcfstr)
  58.     return hcfsum.hexdigest()
  59.  
  60. def CaseManager()
  61.     if(len(sys.argv) == 3):
  62.         if sys.argv[1] == "new":
  63.             hcf.hcfNew(sys.argv[2])
  64.         elif sys.argv[1] == "del":
  65.             hcf.hcfDel(sys.argv[2])
  66.         elif sys.argv[1] == "show":
  67.             hcf.hcfShow(sys.argv[2])   
  68.         elif sys.argv[1] == "check":
  69.             hcf.hcfCheck(sys.argv[2])  
  70.         else:
  71.             print "HCF Error: Invalid argumet!"
  72.             hcf.hcfHelp()
  73.             exit(1)
  74.        
  75.     elif(len(sys.argv) == 2):
  76.         if sys.argv[1] == "help":
  77.             hcf.hcfHelp()
  78.         elif sys.argv[1] == "about":
  79.             hcf.hcfAbout()
  80.         else:
  81.             print "HCF Error: Invalid argumet!"
  82.             hcf.hcfHelp()
  83.             exit(1)
  84.            
  85. def actFile(hlist):
  86.     try:
  87.         f = open("/etc/hcffile", "w")
  88.         for i in hlist:
  89.             f.write(i)
  90.         f.close()
  91.     except IOError:
  92.         print "HCF Error: You need to be root!"
  93.         exit(1)
  94.        
  95. def openFile():
  96.     L = list()
  97.  
  98.     f = open("/etc/hcffile", "r")
  99.     while True:
  100.         line = f.readline()
  101.         if not line: break
  102.         L.append(line)
  103.     f.close()
  104.     return L
  105.  
  106.            
  107. def retSplit(splitcad, param=0):
  108.     retl = splitcad.split(" ")
  109.     if param==0:
  110.         return retl[0]
  111.     elif param==1:
  112.         return retl[1]
  113.        
  114. class hcfMain():
  115.     """Main class"""
  116.     def __init__(self):
  117.         hcfFrag = os.path.isfile("/etc/hcffile")
  118.         if hcfFrag == True:
  119.             print "HCF: Ok..."
  120.         elif hcfFrag == False:
  121.             print "HCF: Creating file..."
  122.             try:
  123.                 f_hcf = open("/etc/hcffile", "w")
  124.                 f_hcf.close()
  125.             except IOError:
  126.                 print "HCF Error: You need to be root!"
  127.                 exit(1)
  128.    
  129.     def hcfHelp(self):
  130.         print "\t-> HCF Help"
  131.         print "\nHcf To use, you must use arguments.\n"
  132.         print "  new <dir>    for new addition"
  133.         print "  del <dir>    to eliminate"
  134.         print "  check <dir>  to check sum"
  135.         print "  show <dir>   to check file"
  136.         print "\n Example:\n\t new /home/user/document.txt"
  137.         print "\nTo run the program needs to be root"
  138.        
  139.     def hcfAbout(self):
  140.         print "\t-> HCF About"
  141.         print "\nHCF is an application written in python, by Sergio Lopez. Used to keep track of the desired files through its md5sum"
  142.         print "\nE-Mail: daemonfreedom93@gmail.com"
  143.         print "Web: daemonfreedom.blogspot.com\n"
  144.        
  145.     def hcfNew(self, hcfDir):
  146.         self.hcfDir = hcfDir
  147.         L = list()
  148.         hcffrag2 = int()
  149.        
  150.         #Crear línea
  151.        
  152.         hcfline = self.hcfDir+" "+CreateSum(self.hcfDir)+"\n"
  153.         #FIN Línea
  154.        
  155.         #Almacenar archivo en buffer(lista L)
  156.         L = openFile()
  157.         #FIN Buffer Lista
  158.  
  159.         #Buscar coincidencias...
  160.         if len(L)>0:
  161.             for i in L:
  162.                 if i == hcfline:
  163.                     hcffrag2 = 1
  164.                 else:
  165.                     hcffrag2 = 2
  166.         else:
  167.             hcffrag2 = 2
  168.         #FIN búsqueda 
  169.        
  170.         if hcffrag2 == 1:
  171.             print "HCF Error: Already exist!"
  172.         elif hcffrag2 == 2:
  173.             L.append(hcfline)
  174.  
  175.         actFile(L) #Actualizar hcffile
  176.        
  177.     def hcfDel(self, hcfDir):
  178.         self.hcfDir = hcfDir
  179.         hcffrag3 = int()
  180.         L = list()
  181.         Laux = list()
  182.         cont = int()
  183.         posx = int()
  184.        
  185.         L = openFile()
  186.        
  187.         #Step1
  188.         cont =0
  189.         for i in L:
  190.             vaux = retSplit(L[cont])
  191.             Laux.append(vaux)
  192.             cont = cont+1
  193.            
  194.         #Step2
  195.         cont =0
  196.         while True:
  197.             try:
  198.                 if Laux[cont] == self.hcfDir:
  199.                     L.pop(cont)
  200.                     print "HCF: Element",cont,"deleted!"
  201.                     break
  202.                 else:
  203.                     cont = cont+1
  204.                
  205.             except IndexError:
  206.                 print "HCF Error: No elements in this list!"
  207.                 exit(1)
  208.                
  209.         actFile(L)
  210.    
  211.        
  212.     def hcfCheck(self, hcfDir):
  213.         self.hcfDir = hcfDir
  214.         L = list()
  215.         Laux = list()
  216.         bf = list()
  217.         cont = int()
  218.         hcffrag4 = int()
  219.        
  220.         hcfnum = CreateSum(self.hcfDir)+"\n"
  221.  
  222.         L = openFile()
  223.        
  224.         cont =0
  225.         for i in L:
  226.             vaux = retSplit(L[cont])
  227.             Laux.append(vaux)
  228.             cont = cont+1
  229.        
  230.         hcffrag4 =0
  231.         cont =0
  232.         while True:
  233.             try:
  234.                 if Laux[cont] == self.hcfDir:
  235.                     hcffrag4 = cont
  236.                     break
  237.                 else:
  238.                     cont = cont+1
  239.                
  240.             except IndexError:
  241.                 print "HCF Error: No elements in this list!"
  242.                 exit(1)
  243.                
  244.         bf.append (retSplit(L[hcffrag4]))
  245.         bf.append (retSplit(L[hcffrag4], 1))
  246.        
  247.         if hcfnum == bf[1]:
  248.             print "HCF: No changes"
  249.         else:
  250.             print "HCF: If there are changes!!!"
  251.             print "OLD sum:", bf[1]
  252.             print "NEW sum:", hcfnum
  253.  
  254.  
  255.     def hcfShow(self, hcfDir):
  256.         self.hcfDir = hcfDir
  257.         L = list()
  258.        
  259.         try:
  260.             f = open(self.hcfDir, "r")
  261.             while True:
  262.                 line = f.readline()
  263.                 if not line: break
  264.                 L.append(line)
  265.             f.close()
  266.         except IOError:
  267.             print "HCF Error:",self.hcfDir,"no such file!"
  268.             exit(1)
  269.        
  270.         for element in L:
  271.             print element
  272.        
  273.  
  274. def main():
  275.     CaseManager()
  276.     return 0
  277.  
  278. if __name__ == '__main__':
  279.     print "\nWelcome to HCF: Hash Control Files"
  280.     hcf = hcfMain()
  281.     main()
  282.    
  283. # Fuentes:
  284. #
  285. #  -> www.elcodigok.com.ar
  286. #  -> Documento "Python_para_todos" de Raúl González Duque
  287. #  -> http://es.w3support.net
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement