Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.86 KB | None | 0 0
  1. import random
  2. import sys, os
  3. import tarfile
  4. import shutil
  5. import subprocess
  6. import requests
  7. from time import sleep
  8. from git import Repo
  9. from openpyxl import *
  10.  
  11. #Class for the fancy colors in the terminal YAY
  12. class bcolors():
  13.     HEADER = '\033[95m'
  14.     OKBLUE = '\033[94m'
  15.     OKGREEN = '\033[92m'
  16.     WARNING = '\033[93m'
  17.     FAIL = '\033[91m'
  18.     ENDC = '\033[0m'
  19.     CLEAR = '\033[2J'
  20.  
  21. #POST: returns the size of the folder in the path
  22. def getSize(path):
  23.     #start_time = time.time()
  24.     totalSize = 0
  25.     if os.path.isdir(path):
  26.  
  27.         for dirpath, dirnames, filenames in os.walk(path):
  28.             for fName in filenames:
  29.                 fp = os.path.join(dirpath, fName)
  30.                 #Error
  31.                 try:
  32.                     totalSize += os.path.getsize(fp)
  33.                 except ErrorGetSize:
  34.                     print "Error in GetSize"
  35.         #print time.time() - start_time, "seconds"
  36.         return totalSize
  37.  
  38.     else:
  39.         return os.path.getsize(path)
  40.  
  41. def doTheMagic(excelFileDir, columnNumber):
  42.  
  43.     repositoriesId = random.randint(1, 10000213)
  44.     repos = requests.get('https://api.github.com/repositories', params={'since':repositoriesId})
  45.  
  46.     if repos.status_code != 200:
  47.         print("Error in GET")
  48.         sys.exit(1)
  49.  
  50.     reposJSON = repos.json()
  51.     repoHtml = reposJSON[0]["html_url"]
  52.     repoName = str(reposJSON[0]["name"] + '-' + str(repositoriesId))
  53.     cloneDir = os.path.join(os.getcwd(), os.path.join("clones", repoName))
  54.  
  55.     if not os.path.exists(cloneDir):
  56.         os.makedirs(cloneDir)
  57.  
  58.     Repo.clone_from(repoHtml, cloneDir)
  59.  
  60.     shutil.make_archive(cloneDir, format="bztar", root_dir=cloneDir)
  61.     shutil.make_archive(cloneDir, format="tar", root_dir=cloneDir)
  62.     shutil.make_archive(cloneDir, format="gztar", root_dir=cloneDir)
  63.     shutil.make_archive(cloneDir, format="zip", root_dir=cloneDir)
  64.  
  65.     #7zip
  66.     subprocess.call(["./7za", "a", cloneDir, cloneDir])
  67.  
  68.     #DATA
  69.     folderSize = getSize(cloneDir)
  70.     tarSize = os.path.getsize(cloneDir+".tar")
  71.     zipSize = os.path.getsize(cloneDir+".zip")
  72.     gzSize = os.path.getsize(cloneDir+".tar.gz")
  73.     bz2Size = os.path.getsize(cloneDir+".tar.bz2")
  74.     sevenzSize = os.path.getsize(cloneDir+".7z")
  75.  
  76.     #Write results to database
  77.     wb = load_workbook(excelFileDir)
  78.     ws = wb.active
  79.     #Headers
  80.     ws['A2'] = "FolderSize"
  81.     ws['A3'] = "TarSize"
  82.     ws['A4'] = "ZipSize"
  83.     ws['A5'] = "GzSize"
  84.     ws['A6'] = "Bz2Size"
  85.     ws['A7'] = "7sSize"
  86.     ws.cell(row=1, column=columnNumber+2, value=repoName)
  87.     ws.cell(row=2, column=columnNumber+2, value=folderSize)
  88.     ws.cell(row=3, column=columnNumber+2, value=tarSize)
  89.     ws.cell(row=4, column=columnNumber+2, value=zipSize)
  90.     ws.cell(row=5, column=columnNumber+2, value=gzSize)
  91.     ws.cell(row=6, column=columnNumber+2, value=bz2Size)
  92.     ws.cell(row=7, column=columnNumber+2, value=sevenzSize)
  93.     wb.save(excelFileDir)
  94.  
  95.     #OUTPUT
  96.     print bcolors.HEADER + repoName + bcolors.ENDC
  97.     print "Size of the directory:", bcolors.OKBLUE + str(folderSize) + bcolors.ENDC
  98.     print "Size of the tar file", bcolors.OKBLUE + str(tarSize) + bcolors.ENDC
  99.     print "Size of the zip file", bcolors.OKBLUE + str(zipSize) + bcolors.ENDC
  100.     print "Size of the gzip file", bcolors.OKBLUE + str(gzSize) + bcolors.ENDC
  101.     print "Size of the bzip file", bcolors.OKBLUE + str(bz2Size) + bcolors.ENDC
  102.     print "Size of the 7z file", bcolors.OKBLUE + str(sevenzSize) + bcolors.ENDC
  103.  
  104. if __name__ == '__main__':
  105.     excelFolderDir = os.getcwd() + "/excels"
  106.     if not os.path.exists(excelFolderDir):
  107.         os.makedirs(excelFolderDir)
  108.     excelFileDir = excelFolderDir + "/" + str(sys.argv[2]) + ".xlsx"
  109.     #Create excel file
  110.     wb = Workbook()
  111.     wb.save(excelFileDir)
  112.     print "EXCEL CREATED"
  113.  
  114.     for i in range(0, int(sys.argv[1])):
  115.         doTheMagic(excelFileDir, i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement