def cmdFinished(self, data): print('[Screengrab.py] cmdFinished') if self.stillAlive: self.request.setResponseCode(http.OK) if int(data) == 0 and self.target != None: try: self.request.setHeader('Content-Length', '%i' % os_path_getsize(self.target)) with open(self.target) as fp: self.request.write(fp.read()) if self.save == False: os_remove(self.target) print('[Screengrab.py] %s removed' % self.target) except Exception as e: self.request.write( 'Internal error while reading target file') self.request.setResponseCode(http.INTERNAL_SERVER_ERROR) elif int(data) == 0 and self.target == None: self.request.write(self.output) elif int(data) == 1: self.request.write(self.output) else: self.request.setResponseCode(http.INTERNAL_SERVER_ERROR) self.request.finish() else: print('[Screengrab.py] already disconnected!')
def writeLOG(msg): print(msg) with open(LOG_FILE_PATH, 'a') as f: f.write('[ %s ] %s\n' % (datetime.now().strftime("%Y-%m-%d %H:%M:%S"), msg)) if os_path_getsize(LOG_FILE_PATH) > 255000: with open(LOG_FILE_PATH, 'r') as f: cache = f.readlines() with open(LOG_FILE_PATH, 'w') as f: f.writelines(cache[len(cache) / 2:])
def calcular_tamaño(archivo): escalas = ["B", "KB", "MB", "GB", "TB"] escala_actual = 0 peso = os_path_getsize(archivo) while peso > 1000: if escala_actual < len(escalas): peso = int(peso / 1000) escala_actual += 1 else: break return str(peso), escalas[escala_actual]
def cmdFinished(self, data): print '[Screengrab.py] cmdFinished' if int(data) is 0 and self.target is not None: try: self.request.setHeader('Content-Length', '%i' %os_path_getsize(self.target)) fp = open(self.target) self.request.write(fp.read()) fp.close() if self.save is False: os_remove(self.target) print '[Screengrab.py] %s removed' %self.target except Exception,e: self.request.write('Internal error while reading target file')
def cmdFinished(self, data): print '[Screengrab.py] cmdFinished' if self.stillAlive: self.request.setResponseCode(http.OK) if int(data) is 0 and self.target is not None: try: self.request.setHeader('Content-Length', '%i' %os_path_getsize(self.target)) with open(self.target) as fp: self.request.write(fp.read()) if self.save is False: os_remove(self.target) print '[Screengrab.py] %s removed' %self.target except Exception,e: self.request.write('Internal error while reading target file') self.request.setResponseCode(http.INTERNAL_SERVER_ERROR) elif int(data) is 0 and self.target is None: self.request.write(self.output) elif int(data) is 1: self.request.write(self.output) else: self.request.setResponseCode(http.INTERNAL_SERVER_ERROR) self.request.finish()
def header(infolder, line=comment_LINE, block=comment_BLOCK, include=INCLUDE, exclude=EXCLUDE, overwrite=False): # Compile regular expression pattern to match in scanned files pattern = re_compile(_COMMENT.format(r'|'.join(map(comment_escape, line)), *comment_block_comments(block)), flags=re_DOTALL | re_VERBOSE | re_MULTILINE) # Define default values align = _FORMAT['CENTER'] width = 80 # Update values based on INFO file values = {} with open(os_path_join(infolder, 'INFO'), 'r', encoding='utf-8') as file: header = file.read() match = re_match(r'\s*#\s*format\s+' r'((?P<align>CENTER|LEFT|RIGHT)\s+)?' r'(?P<width>\d+)?', header) if match: align, width = match.group('align', 'width') align = _FORMAT.get(align, _FORMAT['CENTER']) try: width = int(width) except TypeError: pass # Add leading and trailing empty line header = '\n{}\n'.format(header[match.end():].strip()) # Get file contents of special files for filename in _FILES: try: with open(os_path_join(infolder, filename), 'r', encoding='utf-8') as file: values[filename] = file.read().strip() except FileNotFoundError: values[filename] = '' # Get special values values['DATE'] = datetime.now().strftime('%Y.%m.%d') # Exclude containers except_dirs = [] # relative path to dir from root except_files = [] # relative path to file from root except_names = [] # filename (with extension) anywhere except_exts = [] # extension anywhere # If 'exclude' is dictionary like object try: _empty = () # Excludes relative to root for key, container in zip(('folders', 'files'), (except_dirs, except_files)): container.extend(os_path_join(infolder, p) for p in exclude.get(key, _empty)) # Excludes anywhere for key, container in zip(('names', 'extensions'), (except_names, except_exts)): container.extend(exclude.get(key, _empty)) # If 'exclude' is an iterable object except AttributeError: except_names = exclude # Include containers permit_names = [] # filename (with extension) anywhere permit_exts = [] # extension anywhere # If 'include' is dictionary like object try: _empty = () # Includes anywhere for key, container in zip(('names', 'extensions'), (permit_names, permit_exts)): container.extend(include.get(key, _empty)) # If 'include' is an iterable object except AttributeError: permit_names = include # Walk through all files and folders in the passed folder # FIXME: what if none of the files changed only INFO has been updated? # Scan through all files and folders with check_Checker(infolder, file='.clic_cache') as checker: for root, dirs, filenames in os_walk(infolder): # If skip this folder and all subfolders if root in except_dirs: dirs.clear() continue # Check all files in folder for filename in filenames: filepath = os_path_join(root, filename)[2:] # If skip this exact file if filepath in except_files: continue name, extension = os_path_splitext(filename) # If file or extension is not banned and it is on the # white-list and it changed since last time checked and # this is not and overwrite-call if (filename not in except_names and extension not in except_exts and (extension in permit_exts or filename in permit_names) and checker.ischanged(filepath) and not overwrite): values['SIZE'] = _size(os_path_getsize(filepath)) # FIXME: make it more generic than ./ -- what if ../../? values['FILE'] = filepath[2:] if filepath.startswith('./') else filepath values['FILE_NAME'] = file values['FILE_BASE'] = name if _comment(header.format(**values), filepath, pattern, align, width): # Update checker after the file has been modified checker.update() # Report print('CLIC: processed {!r}'.format(filepath))
def header(infolder, line=comment_LINE, block=comment_BLOCK, include=INCLUDE, exclude=EXCLUDE, overwrite=False): # Compile regular expression pattern to match in scanned files pattern = re_compile(_COMMENT.format(r'|'.join(map(comment_escape, line)), *comment_block_comments(block)), flags=re_DOTALL | re_VERBOSE | re_MULTILINE) # Define default values align = _FORMAT['CENTER'] width = 80 # Update values based on INFO file values = {} with open(os_path_join(infolder, 'INFO'), 'r', encoding='utf-8') as file: header = file.read() match = re_match( r'\s*#\s*format\s+' r'((?P<align>CENTER|LEFT|RIGHT)\s+)?' r'(?P<width>\d+)?', header) if match: align, width = match.group('align', 'width') align = _FORMAT.get(align, _FORMAT['CENTER']) try: width = int(width) except TypeError: pass # Add leading and trailing empty line header = '\n{}\n'.format(header[match.end():].strip()) # Get file contents of special files for filename in _FILES: try: with open(os_path_join(infolder, filename), 'r', encoding='utf-8') as file: values[filename] = file.read().strip() except FileNotFoundError: values[filename] = '' # Get special values values['DATE'] = datetime.now().strftime('%Y.%m.%d') # Exclude containers except_dirs = [] # relative path to dir from root except_files = [] # relative path to file from root except_names = [] # filename (with extension) anywhere except_exts = [] # extension anywhere # If 'exclude' is dictionary like object try: _empty = () # Excludes relative to root for key, container in zip(('folders', 'files'), (except_dirs, except_files)): container.extend( os_path_join(infolder, p) for p in exclude.get(key, _empty)) # Excludes anywhere for key, container in zip(('names', 'extensions'), (except_names, except_exts)): container.extend(exclude.get(key, _empty)) # If 'exclude' is an iterable object except AttributeError: except_names = exclude # Include containers permit_names = [] # filename (with extension) anywhere permit_exts = [] # extension anywhere # If 'include' is dictionary like object try: _empty = () # Includes anywhere for key, container in zip(('names', 'extensions'), (permit_names, permit_exts)): container.extend(include.get(key, _empty)) # If 'include' is an iterable object except AttributeError: permit_names = include # Walk through all files and folders in the passed folder # FIXME: what if none of the files changed only INFO has been updated? # Scan through all files and folders with check_Checker(infolder, file='.clic_cache') as checker: for root, dirs, filenames in os_walk(infolder): # If skip this folder and all subfolders if root in except_dirs: dirs.clear() continue # Check all files in folder for filename in filenames: filepath = os_path_join(root, filename)[2:] # If skip this exact file if filepath in except_files: continue name, extension = os_path_splitext(filename) # If file or extension is not banned and it is on the # white-list and it changed since last time checked and # this is not and overwrite-call if (filename not in except_names and extension not in except_exts and (extension in permit_exts or filename in permit_names) and checker.ischanged(filepath) and not overwrite): values['SIZE'] = _size(os_path_getsize(filepath)) # FIXME: make it more generic than ./ -- what if ../../? values['FILE'] = filepath[2:] if filepath.startswith( './') else filepath values['FILE_NAME'] = file values['FILE_BASE'] = name if _comment(header.format(**values), filepath, pattern, align, width): # Update checker after the file has been modified checker.update() # Report print('CLIC: processed {!r}'.format(filepath))