def backup(path, suffix='.bak'): """ Rename a file or directory safely without overwriting an existing backup of the same name. :param path: The path object to the file or directory to make a backup of. :param suffix: The suffix to rename files with. :returns: The new path of backed up file/directory :rtype: str """ count = -1 new_path = None while True: if path.exists() : if count == -1: new_path = Path("{0}{1}".format(path, suffix)) else: new_path = Path("{0}{1}.{2}".format(path, suffix, count)) if new_path.exists(): count += 1 continue else: path.copy(new_path) else: break return new_path
def uploadFile(self, srcPath, destPath): db = self.getClient() srcPath = Path(srcPath) destPath = Path(destPath) destPath.makedirs() if srcPath.exists(): result = db.put_file(destPath, open(srcPath,'rb')) return result return None
def download_file(url, local_dir=None): url_file_name = Path(urlsplit(url)[2]).name if local_dir is None: local_dir = apiUtils.getTempDir() url_local_path = Path(local_dir).join(url_file_name) fullurl = urllib.parse.quote(url, safe="%/:=&?~#+!$,;'@()*[]") url_file_handle = urllib.request.urlopen(fullurl) print(url_local_path) with open(url_local_path, 'wb') as file_handle: file_handle.write(url_file_handle.read()) return url_local_path
def __init__(self, db=None): logging.Handler.__init__(self) if db is None: self.db = ':memory:Temp.db' else: self.db = Path(db) self.db.makedirs() # Create table if needed: conn = sqlite3.connect(self.db) conn.execute(SQLiteHandler.initial_sql) conn.commit()
def loadUi(self, parent=None): if issubclass(self.__class__, QtGui.QWizard) : return try: path = Path(sys.modules[self.__module__].__file__) except: path = Path(apiUtils.getMainDir()) if path : ui_file = path.dir().join('views','{0}.ui'.format(self.__class__.__name__)) if ui_file.exists() : self = loadUi(ui_file, parent, self, self.customWidgets()) else: print 'Unable to load ui file | {0}'.format(ui_file)
def loadUi(self, parent=None): if issubclass(self.__class__, QtGui.QWizard): return try: path = Path(sys.modules[self.__module__].__file__) except: path = Path(apiUtils.getMainDir()) if path: ui_file = path.dir().join('views', '{0}.ui'.format(self.__class__.__name__)) if ui_file.exists(): self = loadUi(ui_file, parent, self, self.customWidgets()) else: print('Unable to load ui file | {0}'.format(ui_file))
def updateWorkspace(self, mappings, root=[]): if guiUtils.getConfirmDialog( 'Would you like to use your default workspace?\n{0}'.format( self.p4Client())): workspace_name = self.p4Client else: sample = str( os.getenv('USERNAME') + '_' + os.getenv('COMPUTERNAME') + '_dev') workspace_success, workspace_name = guiUtils.getUserInput( 'Please specify a name for the P4 workspace to update.\n EXAMPLE: {0}' .format(os.getenv('P4CLIENT', sample))) if not workspace_success: return workspace_name = workspace_name.replace(" ", "_") client_spec = self.p4Conn.fetch_client(workspace_name) if 'Update' not in client_spec or 'Access' not in client_spec: #NEW WORKSPACE drives = apiUtils.getDrives() success, choice = ChoiceWidget.getChoice( msg='Please specify a drive location for this P4 workspace.', choices=drives) if not success: return False client_spec['Root'] = str(Path(drives[choice] + ':\\').join(*root)) client_spec['View'] = [x.format(workspace_name) for x in mappings] self.p4Conn.save_client(client_spec) return True
def getStyleSheet(): ss_file = Path( settings.PKG_RESOURCE_PATH).join('darkorange.stylesheet') data = '' with open(ss_file, 'r') as file_handle: data = file_handle.read() return '%s' % data
def _readSettings(self): settings = QtCore.QSettings(Path.getTempPath().join('ui_settings', apiUtils.getClassName(self) + '.ini'), QtCore.QSettings.IniFormat) settings.beginGroup(apiUtils.getClassName(self)) self.readSettings(settings) settings.endGroup()
def __init__(self, func, sort='cumulative', strip_dirs=False): super(Profile, self).__init__(func) self.sort = sort self.strip_dirs = strip_dirs base_path = Path.getTempPath().join('profile') base_path.makedirs() self.profile_path = base_path.join('{0}.{1}.profile'.format(func.__module__, func.__name__)) self.stats_path = base_path.join('{0}.{1}.log'.format(func.__module__, func.__name__)) self.profile = cProfile.Profile()
def _readSettings(self): settings = QtCore.QSettings( Path.getTempPath().join('ui_settings', apiUtils.getClassName(self) + '.ini'), QtCore.QSettings.IniFormat) settings.beginGroup(apiUtils.getClassName(self)) self.readSettings(settings) settings.endGroup()
def uploadFile(self, srcPath, destPath): db = self.getClient() srcPath = Path(srcPath) destPath = Path(destPath) destPath.makedirs() if srcPath.exists(): result = db.put_file(destPath, open(srcPath, 'rb')) return result return None
def __init__(self, func, sort='cumulative', strip_dirs=False): super(Profile, self).__init__(func) self.sort = sort self.strip_dirs = strip_dirs base_path = Path.getTempPath().join('profile') base_path.makedirs() self.profile_path = base_path.join('{0}.{1}.profile'.format( func.__module__, func.__name__)) self.stats_path = base_path.join('{0}.{1}.log'.format( func.__module__, func.__name__)) self.profile = cProfile.Profile()
def runFile( filepath, basePath=None, cmd=None, debug=False ): """ Runs the filepath in a shell with proper commands given, or passes the command to the shell. (CMD in windows) the current platform :param filepath: path to the file to execute :param basePath: working directory where the command should be called from. If omitted, the current working directory is used. """ status = False filepath = Path(filepath) # make sure the filepath we're running is a file if not filepath.isfile(): return status # determine the base path for the system if basePath is None: basePath = filepath.dir() options = { 'filepath': filepath, 'basepath': basePath } if cmd == None : if filepath.ext in ['.py','.pyw']: if debug: cmd = 'python.exe "{0}"'.format(filepath) else: cmd = 'pythonw.exe "{0}"'.format(filepath) status = subprocess.Popen( cmd, stdout=sys.stdout, stderr=sys.stderr, shell=debug, cwd=basePath) if not status : try: status = os.startfile(filepath) except: print 'runFile cannot run type (*{0})'.format(filepath.ext) return status
def __init__(self, db=None): logging.Handler.__init__(self) if db is None: self.db = ":memory:Temp.db" else: self.db = Path(db) self.db.makedirs() # Create table if needed: conn = sqlite3.connect(self.db) conn.execute(SQLiteHandler.initial_sql) conn.commit()
def runFile( filepath, basePath=None, cmd=None, debug=False ): """ Runs the filepath in a shell with proper commands given, or passes the command to the shell. (CMD in windows) the current platform :param filepath: path to the file to execute :param basePath: working directory where the command should be called from. If omitted, the current working directory is used. """ status = False filepath = Path(filepath) # make sure the filepath we're running is a file if not filepath.isfile(): return status # determine the base path for the system if basePath is None: basePath = filepath.dir() options = { 'filepath': filepath, 'basepath': basePath } if cmd == None : if filepath.ext in ['.py','.pyw']: if debug: cmd = 'python.exe "{0}"'.format(filepath) else: cmd = 'pythonw.exe "{0}"'.format(filepath) status = subprocess.Popen( cmd, stdout=sys.stdout, stderr=sys.stderr, shell=debug, cwd=basePath) if not status : try: status = os.startfile(filepath) except: print('runFile cannot run type (*{0})'.format(filepath.ext)) return status
def onFinalize(self, loginMsg='Login', credentialsFile=''): apiUtils.synthesize(self, 'loginMsg', loginMsg) apiUtils.synthesize(self, 'credentialsFile', Path(credentialsFile)) apiUtils.synthesize(self, 'submitted', False) self.setModal(True) self.ui_loginMessage.setText(self.loginMsg) self.ui_submit.clicked.connect(self.emitLoginSubmitted) self.ui_username.returnPressed.connect(self.emitLoginSubmitted) self.ui_password.returnPressed.connect(self.emitLoginSubmitted) self.ui_password.setEchoMode(QtGui.QLineEdit.Password) self.center() if self.credentialsFile: self._readCredentials()
def _tryCredentials(self, msg='P4 Login', force=False): #Validate Credentials if self.p4User is None or self.p4Password is None: success, user, password = LoginWidget.getCredentials(loginMsg=msg, credentialsFile=Path.getTempPath().join('p4_login.dat'), force=force) if not success: raise ValueError('Invalid Login Infomation!') self.setP4User(user) self.setP4Password(password) self.p4Conn.user = self.p4User self.p4Conn.password = self.p4Password try: self.p4Conn.run_login() except P4Exception, e: self.setP4User(None) self.setP4Password(None) self._tryCredentials(msg='P4 Login Invalid!', force=True)
def _tryCredentials(self, msg='P4 Login', force=False): #Validate Credentials if self.p4User is None or self.p4Password is None: success, user, password = LoginWidget.getCredentials( loginMsg=msg, credentialsFile=Path.getTempPath().join('p4_login.dat'), force=force) if not success: raise ValueError('Invalid Login Infomation!') self.setP4User(user) self.setP4Password(password) self.p4Conn.user = self.p4User self.p4Conn.password = self.p4Password try: self.p4Conn.run_login() except P4Exception as e: self.setP4User(None) self.setP4Password(None) self._tryCredentials(msg='P4 Login Invalid!', force=True) except: raise
import unittest from DTL.api import Path, Profile, loggingUtils LOCALPATH = Path(__file__).parent @Profile def main(): suite = unittest.TestLoader().discover(LOCALPATH.join('unittests')) unittest.TextTestRunner(verbosity=2).run(suite) if __name__ == "__main__": main()
def _return_file(self, file_dialog): if file_dialog.exec_(): returned_file = str(file_dialog.selectedFiles()[0]) return Path(returned_file).expand() return Path('')
import sys import P4 import subprocess import webbrowser import difflib from DTL.api import CheckVersion, Path from DTL.gui import guiUtils from DTL.perforce import P4Client from DTL.crucible import CrucibleClient LOCALPATH = Path.getMainDir() def main(p4clientName, p4changeId): delete = [] add = [] notDiffable = [] unchanged = [] shelved = [] diff = [] p4_client = P4Client(p4clientName) #Compile opened and shelved files files = [] for item in [f for f in p4_client.run('describe', p4changeId)[0].get('depotFile',[])]: files.append(p4_client.run('fstat', item)[0]) for item in [f for f in p4_client.run('describe', '-S', p4changeId)[0].get('depotFile',[])]: files.append(p4_client.run('fstat', '-Rs', '-e', p4changeId, item)[0])
def openPath(self): userInputPath = Path(str(self.ui_Field.text())) if userInputPath: subprocess.call('explorer ' + userInputPath.dir().caseSensative())
def getWorkspaceRoot(self): return Path(self.p4Info['clientRoot'])
def getDirFromUser(self, parent=None): output = cmds.fileDialog2(fm=3, caption='Choose...') if output is None : return Path('') else : return Path(output[0])
def deserialize(self, datadict={}, filepath=''): apiUtils.synthesize(self, 'filepath', Path(filepath)) self.read() self._set_data(datadict=datadict)
def getWorkspacePath(self, path): return Path( path.replace("//" + self.p4Info['clientName'], self.p4Info['clientRoot']))
def openPath(self): userInputPath = Path(str(self.ui_Field.text())) if userInputPath : subprocess.call('explorer ' + userInputPath.dir().caseSensative())
class SQLiteHandler(logging.Handler): """ Logging handler for SQLite. Based on Vinay Sajip's DBHandler class (http://www.red-dove.com/python_logging.html) This version sacrifices performance for thread-safety: Instead of using a persistent cursor, we open/close connections for each entry. AFAIK this is necessary in multi-threaded applications, because SQLite doesn't allow access to objects across threads. """ initial_sql = """CREATE TABLE IF NOT EXISTS log( Created text, Name text, LogLevel int, LogLevelName text, Message text, Args text, Module text, FuncName text, LineNo int, Exception text, Process int, Thread text, ThreadName text )""" insertion_sql = """INSERT INTO log( Created, Name, LogLevel, LogLevelName, Message, Args, Module, FuncName, LineNo, Exception, Process, Thread, ThreadName ) VALUES ( '%(dbtime)s', '%(name)s', %(levelno)d, '%(levelname)s', '%(msg)s', '%(args)s', '%(module)s', '%(funcName)s', %(lineno)d, '%(exc_text)s', %(process)d, '%(thread)s', '%(threadName)s' ); """ #------------------------------------------------------------ def __init__(self, db=None): logging.Handler.__init__(self) if db is None: self.db = ':memory:Temp.db' else: self.db = Path(db) self.db.makedirs() # Create table if needed: conn = sqlite3.connect(self.db) conn.execute(SQLiteHandler.initial_sql) conn.commit() #------------------------------------------------------------ def formatDBTime(self, record): record.dbtime = time.strftime(CODETIMEFORMAT, time.localtime(record.created)) #------------------------------------------------------------ def emit(self, record): # Use default formatting: self.format(record) # Set the database time up: self.formatDBTime(record) if record.exc_info: record.exc_text = logging._defaultFormatter.formatException( record.exc_info) else: record.exc_text = "" # Insert log record: sql = SQLiteHandler.insertion_sql % record.__dict__ conn = sqlite3.connect(self.db) conn.execute(sql) conn.commit()
def setFilepath(self, filepath): self._filepath = Path(filepath)
def getSaveFileFromUser(self, parent=None, ext=[]): output = cmds.fileDialog2(fm=0, okc='Save', caption='Save...') if output is None : return Path('') else : return Path(output[0])
class SQLiteHandler(logging.Handler): """ Logging handler for SQLite. Based on Vinay Sajip's DBHandler class (http://www.red-dove.com/python_logging.html) This version sacrifices performance for thread-safety: Instead of using a persistent cursor, we open/close connections for each entry. AFAIK this is necessary in multi-threaded applications, because SQLite doesn't allow access to objects across threads. """ initial_sql = """CREATE TABLE IF NOT EXISTS log( Created text, Name text, LogLevel int, LogLevelName text, Message text, Args text, Module text, FuncName text, LineNo int, Exception text, Process int, Thread text, ThreadName text )""" insertion_sql = """INSERT INTO log( Created, Name, LogLevel, LogLevelName, Message, Args, Module, FuncName, LineNo, Exception, Process, Thread, ThreadName ) VALUES ( '%(dbtime)s', '%(name)s', %(levelno)d, '%(levelname)s', '%(msg)s', '%(args)s', '%(module)s', '%(funcName)s', %(lineno)d, '%(exc_text)s', %(process)d, '%(thread)s', '%(threadName)s' ); """ # ------------------------------------------------------------ def __init__(self, db=None): logging.Handler.__init__(self) if db is None: self.db = ":memory:Temp.db" else: self.db = Path(db) self.db.makedirs() # Create table if needed: conn = sqlite3.connect(self.db) conn.execute(SQLiteHandler.initial_sql) conn.commit() # ------------------------------------------------------------ def formatDBTime(self, record): record.dbtime = time.strftime(CODETIMEFORMAT, time.localtime(record.created)) # ------------------------------------------------------------ def emit(self, record): # Use default formatting: self.format(record) # Set the database time up: self.formatDBTime(record) if record.exc_info: record.exc_text = logging._defaultFormatter.formatException(record.exc_info) else: record.exc_text = "" # Insert log record: sql = SQLiteHandler.insertion_sql % record.__dict__ conn = sqlite3.connect(self.db) conn.execute(sql) conn.commit()