Advertisement
Guest User

Source Control in progress

a guest
Mar 4th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.63 KB | None | 0 0
  1. #Used to create the GUI
  2. import tkinter as tk
  3. from tkinter import *
  4. import sys
  5.  
  6. #Used to handle files
  7. import glob
  8. import os
  9. from pathlib import Path
  10. import shutil
  11. from shutil import *
  12.  
  13. #Used for the log to insert date/time
  14. import time
  15.  
  16. nameArray = []
  17.  
  18. #Generates folders + standard documents
  19.  
  20. class sourceControl():
  21.  
  22.     def __init__(self, root):
  23.  
  24.         tk.Frame.__init__(self, root)
  25.        
  26.         #Labels
  27.         sourceLabel = Label(root, text='Source Folder')
  28.         sourceLabel.grid(row=0, column=0, sticky=W)
  29.  
  30.         workingLabel = Label(root, text='Working Folder')
  31.         workingLabel.grid(row=0, column=1, sticky=W, padx=(50,0))
  32.  
  33.         logLabel = Label(root, text='Activity Log')
  34.         logLabel.grid(row=3, column=0, sticky=W, pady=(10,0))
  35.  
  36.         #List boxes
  37.         workingList = Listbox(root, height=10, width=50, exportselection=FALSE)
  38.         workingList.grid(row=1, column=1, padx=(50,0))
  39.  
  40.         sourceList = Listbox(root, height=10, width=50, exportselection=FALSE)
  41.         sourceList.grid(row=1, column=0)
  42.  
  43.         #Log text box
  44.         textLog = Text(root, height= 10, width= 80, wrap="word")
  45.         textLog.grid(row=4, column=0, columnspan=2)
  46.  
  47.         logScroll = Scrollbar(root, command=textLog.yview)
  48.         textLog.config(yscrollcommand=logScroll.set)
  49.         logScroll.grid(row=4, column=1, sticky='nse', padx=(0))
  50.  
  51.         #Options for buttons
  52.         button_opt = {'fill': constants.BOTH, 'padx': 5, 'pady': 5}
  53.  
  54.         #Define buttons
  55.         genFileBut = Button(root, text= 'Generate \nfile structure', width=15, height=2, fg = 'black', command= lambda: genFolders())
  56.         genFileBut.grid(row=1, column=5, padx=20, sticky=N)
  57.  
  58.         copyFileBut = Button(root, text= 'copy \n files', width=15, height=2, fg= 'black', command= lambda: copySelectedFolder())
  59.         copyFileBut.grid(row=1, column=6, padx=10, sticky=N)
  60.  
  61.         revertBut = Button(root, text= 'Move folders\n to Source location', width= 15, height= 2, fg= 'black')
  62.         revertBut.grid(row= 1, column= 5, padx= 10, sticky=S)
  63.  
  64.         showLogBut = Button(root, text= 'Show \n changelog', width = 15, height = 2, fg= 'black')
  65.         showLogBut.grid(row= 1, column= 6, padx= 10, sticky=S)
  66.  
  67.         createReleaseBut = Button(root, text= 'Create\n release pack', width = 15, height = 2, fg= 'black')
  68.         createReleaseBut.grid(row= 4, column= 5, padx= 10)
  69.  
  70.         helpBut = Button(root, text='Help', width= 15, height= 2, fg= 'black')
  71.         helpBut.grid(row= 4, column= 6, padx= 10)
  72.  
  73.  
  74.         #End of button creation
  75.         def getFileName(self):
  76.             return self.w.value
  77.  
  78.         def nameFolderStructure():
  79.             self.w = inputPopup(self)
  80.             self.wait_window(self)
  81.            
  82.  
  83.             print ('FN: ' + fileName)
  84.  
  85.             with open("D:\\Users\\chdick\\Documents\\Level8Prog\\FolderLocations.txt", "r") as ins:
  86.                 nameArray = [line.rstrip('\n') for line in open("D:\\Users\\chdick\\Documents\\Level8Prog\\FolderLocations.txt", "r")]
  87.  
  88.             for i in nameArray:
  89.                 writeFolderLists(i)
  90.  
  91.         def genFolders():
  92.             #Generates pre-determined folder structure
  93.             path=nameArray[1] #Dislike hard-coded value, needs changed.
  94.             foldersGenerated = False #Bool used to check if creation was successful
  95.             if (os.path.isdir(path+'/test')):
  96.                 tk.messagebox.showwarning('Error','File: %s' % path + '\\test already exists')
  97.             else:
  98.                 tk.messagebox.showwarning('Folder created: ' + path + '\test')
  99.                 os.mkdir(path+'/test')
  100.                 foldersGenerated = True
  101.  
  102.             if (os.path.isdir(path+'/code')):
  103.                 tk.messagebox.showwarning('Error','File: %s' % path + '\code already exists')
  104.             else:
  105.                 tk.messagebox.showwarning('Folder created: ' + path + '\code')
  106.                 os.mkdir(path+'/code')
  107.                 foldersGenerated = True
  108.                
  109.             if (os.path.isdir(path+'/docs')):
  110.                 tk.messagebox.showwarning('Error','File: %s' % path + '\docs already exists')
  111.             else:
  112.                 tk.messagebox.showwarning('Folder created: ' + path + '\docs')
  113.                 os.mkdir(path+'/docs')
  114.                 foldersGenerated = True
  115.  
  116.             #Output to log text box
  117.             if foldersGenerated == True:
  118.                 textLog.insert(END, 'Folder structure generated at: ' + time.strftime("%H:%M") + ' - ' + time.strftime("%d/%m/%Y") + '\n')
  119.             else:
  120.                 textLog.insert(END, 'Attempt at generating folder structure at: ' + time.strftime("%H:%M") + ' - ' + time.strftime("%d/%m/%Y") + '. Failed as structure already exists.\n')
  121.            
  122.             for i in nameArray:
  123.                 writeFolderLists('D:\working')
  124.                
  125.         #Writes folders to lsitboxes
  126.         def writeFolderLists(parentFolder):
  127.             fileList = []
  128.             for dirname, dirnames, filenames in os.walk(parentFolder):
  129.                 #Stores files in array
  130.                     for i in glob.glob(dirname):
  131.                         identLevel = i.count('\\')-1
  132.                         ident = '   ' * identLevel
  133.                         fileList.append(ident+i)
  134.                     for i in filenames:
  135.                         p = os.path.join(dirname, i)
  136.                         ident = '   ' * (identLevel+1)
  137.                         fileList.append(ident+p)
  138.  
  139.             #Writes array of files to list.
  140.             if 'source' in parentFolder:
  141.                 sourceList.delete(0, END)
  142.                 for item in fileList:
  143.                     sourceList.insert(END, item)
  144.                     fileList = []
  145.  
  146.             elif 'working' in parentFolder:
  147.                 workingList.delete(0, END)
  148.                 for item in fileList:
  149.                     workingList.insert(END, item)
  150.                     fileList = []
  151.  
  152.             else:
  153.                 print("Error! Unexpected file found!")
  154.  
  155.         #Copy selected item from Source to selected Working location
  156.         def copySelectedFolder():
  157.             try:
  158.                 selectedSource = str(sourceList.get(sourceList.curselection()[0])).lstrip() #checks a folder or file is selected
  159.                 selectedWork = str(workingList.get(workingList.curselection()[0])).lstrip() #before trying to copy it
  160.             except IndexError:
  161.                 tk.messagebox.showwarning('Oops', 'Please select a folder or file')
  162.                     #TODO: add in logic to tell user if working or source file needs selected(IF statement?)
  163.             else:
  164.                 shutil.copy2(selectedSource, selectedWork)
  165.                 textLog.insert(END, selectedSource + ' copied to ' + selectedWork + ' at: '+ time.strftime("%H:%M") + ' - ' + time.strftime("%d/%m/%Y"))
  166.                 for i in nameArray:
  167.                     writeFolderLists(i)
  168.                     #rewrites updated file list display
  169.  
  170.         nameFolderStructure()
  171.  
  172. class inputPopup(object):
  173.     frame= Tk()
  174.     frame.title('Enter folder name')
  175.     frame.geometry('{}x{}'.format(200,100))
  176.  
  177.     def __init__(self, root):
  178.         self.root = root
  179.         self.l = Label(text= 'Enter name for folder structure')
  180.         self.l.grid(row=1, column=1, sticky='E')
  181.         self.e = Entry()
  182.         self.e.grid(row=2, column=1, pady=10)
  183.         self.b = Button(text= 'Submit', command= self.cleanup)
  184.         self.b.grid(row=3, column=1)
  185.        
  186.        
  187.     def cleanup(self):
  188.         self.value = self.e.get()
  189.         self.frame.destroy()
  190.  
  191. root = Tk()
  192. root.title('Source Control!')
  193. root.geometry('{}x{}'.format(1000,400))
  194. sourceControl(root)
  195. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement