def zipdir(directory, no_pyc=False): log = logging.getLogger("zipdir") if not os.path.isdir(directory): raise OSError(20, "Not a directory", directory) msg = "Zipping directory '%s'" if no_pyc: msg += " (without pre-compilation)" log.info(msg, directory) filename = os.path.realpath(directory + ".zip") # Open the file in read an update mode if os.path.exists(filename): zip_file = open(filename, "r+b") else: # If the file does not exist, we need to create it. # "append mode" ensures that, in case of two processes trying to # create the file, they do not truncate each other file zip_file = open(filename, "ab") locker.lock(zip_file) try: if zipfile.is_zipfile(filename): infolist = zipfile.ZipFile(filename).infolist() else: infolist = [] (added, modified, untouched, removed) = _zipChanges(directory, infolist) if added or modified or removed: temp_buf = StringIO() z = zipfile.PyZipFile(temp_buf, "w", zipfile.ZIP_DEFLATED) for f in added + modified + untouched: src = os.path.join(directory, f) checkEncoding(open(src, 'rb')) if no_pyc: log.debug("adding '%s'", f) z.write(src, f) else: # Remove the .pyc file to always force a re-compilation if os.path.exists(src + 'c'): log.debug("removing old .pyc for '%s'", f) os.remove(src + 'c') log.debug("adding '%s'", f) z.writepy(src, os.path.dirname(f)) z.close() zip_file.seek(0) zip_file.write(temp_buf.getvalue()) zip_file.truncate() log.info("File '%s' closed", filename) else: log.info("Nothing to do on '%s'", filename) except UnicodeDecodeError as x: log.error("Wrong encoding in file '%s':", src) log.error(" %s", x) log.error("Probably you forgot the line '# -*- coding: utf-8 -*-'") sys.exit(1) finally: locker.unlock(zip_file) zip_file.close()
def zipdir(directory, no_pyc = False): log = logging.getLogger("zipdir") if not os.path.isdir(directory): raise OSError(20, "Not a directory", directory) msg = "Zipping directory '%s'" if no_pyc: msg += " (without pre-compilation)" log.info(msg, directory) filename = os.path.realpath(directory + ".zip") # Open the file in read an update mode if os.path.exists(filename): zipFile = open(filename, "r+b") else: # If the file does not exist, we need to create it. # "append mode" ensures that, in case of two processes trying to # create the file, they do not truncate each other file zipFile = open(filename, "ab") locker.lock(zipFile) try: if zipfile.is_zipfile(filename): infolist = zipfile.ZipFile(filename).infolist() else: infolist = [] (added, modified, untouched, removed) = _zipChanges(directory, infolist) if added or modified or removed: tempBuf = StringIO() z = zipfile.PyZipFile(tempBuf, "w", zipfile.ZIP_DEFLATED) for f in added + modified + untouched: src = os.path.join(directory, f) checkEncoding(open(src, 'rb')) if no_pyc: log.debug("adding '%s'", f) z.write(src, f) else: # Remove the .pyc file to always force a re-compilation if os.path.exists(src + 'c'): log.debug("removing old .pyc for '%s'", f) os.remove(src + 'c') log.debug("adding '%s'", f) z.writepy(src, os.path.dirname(f)) z.close() zipFile.seek(0) zipFile.write(tempBuf.getvalue()) zipFile.truncate() log.info("File '%s' closed", filename) else: log.info("Nothing to do on '%s'", filename) except UnicodeDecodeError, x: log.error("Wrong encoding in file '%s':", src) log.error(" %s", x) log.error("Probably you forgot the line '# -*- coding: utf-8 -*-'") sys.exit(1)
def login(): global sop_off sop_off = False lock() set_row_1("Login") sleep(3) set_row_1("Place finger") success, userid = getprint() if not success: lock() set_row_1("Access DENIED") else: unlock() set_row_1("USR: "******" UNLOCKED") return success, userid
def mergeFiles( fragFileName, mergedFileName, commentChar, doMerge ): isNewFile = not os.path.exists(mergedFileName) # create an empty file if it does not exist if isNewFile: open(mergedFileName,'w') mergedFile = open( mergedFileName, 'r+' ) # locking file, gaining exclusive access to it lock = locker.lock( mergedFile, locker.LOCK_EX ) startMark = "%s --Beg %s" % ( commentChar, os.path.basename(fragFileName) ) timeMark = "%s --Date inserted: %s" % ( commentChar, str(datetime.now()) ) endMark = "%s --End %s" % ( commentChar, os.path.basename(fragFileName) ) newLines = [ ] skipBlock = False for line in mergedFile.readlines(): if line.startswith(startMark): skipBlock = True # remove all the empty lines occurring before the start mark while (len(newLines) > 0) and (newLines[-1].strip() == ''): newLines.pop() if not skipBlock: newLines.append(line) if line.startswith(endMark): skipBlock = False if skipBlock: print "WARNING: missing end mark ('%s')"%endMark if doMerge: # I do not want to add 2 empty lines at the beginning of a file if not isNewFile: newLines.append('\n\n') newLines.append(startMark+'\n') newLines.append(timeMark+'\n') for line in open( fragFileName, 'r' ).readlines(): newLines.append(line) newLines.append(endMark+'\n') mergedFile.seek(0) mergedFile.truncate(0) mergedFile.writelines(newLines) return 0
def mergeFiles(fragFileName, mergedFileName, commentChar, doMerge): isNewFile = not os.path.exists(mergedFileName) # create an empty file if it does not exist if isNewFile: open(mergedFileName, 'w') mergedFile = open(mergedFileName, 'r+') # locking file, gaining exclusive access to it lock = locker.lock(mergedFile, locker.LOCK_EX) startMark = "%s --Beg %s" % (commentChar, os.path.basename(fragFileName)) timeMark = "%s --Date inserted: %s" % (commentChar, str(datetime.now())) endMark = "%s --End %s" % (commentChar, os.path.basename(fragFileName)) newLines = [] skipBlock = False for line in mergedFile.readlines(): if line.startswith(startMark): skipBlock = True # remove all the empty lines occurring before the start mark while (len(newLines) > 0) and (newLines[-1].strip() == ''): newLines.pop() if not skipBlock: newLines.append(line) if line.startswith(endMark): skipBlock = False if skipBlock: print "WARNING: missing end mark ('%s')" % endMark if doMerge: # I do not want to add 2 empty lines at the beginning of a file if not isNewFile: newLines.append('\n\n') newLines.append(startMark + '\n') newLines.append(timeMark + '\n') for line in open(fragFileName, 'r').readlines(): newLines.append(line) newLines.append(endMark + '\n') mergedFile.seek(0) mergedFile.truncate(0) mergedFile.writelines(newLines) return 0
transformer = caffe.io.Transformer({'data': (1,3,im_ht,im_wd)}) transformer.set_transpose('data', (2,0,1)) transformer.set_raw_scale('data', 255) # the reference model operates on images in [0,255] range instead of [0,1] transformer.set_channel_swap('data', (2,1,0)) # the reference model has channels in BGR order instead of RGB transformer.set_mean('data', np.array([104.00699,116.66877,122.67892])) net = caffe.Net(args['netdesc'], args['netmodel'], caffe.TEST) with open(args['list']) as f: imgslist = f.read().splitlines() if not args['dontScaleImageValues']: print 'Using CAFFE.IO.LOAD_IMAGE' for impath in imgslist: outfpath = os.path.join(args['outdir'], impath + '.h5') if not locker.lock(outfpath): continue tic_toc_print('Working on ' + impath) if not args['dontScaleImageValues']: im = caffe.io.load_image(os.path.join(args['dir'], impath)) else: im = plt.imread(os.path.join(args['dir'], impath)) in_ = transformer.preprocess('data', im) net.blobs['data'].reshape(1, *in_.shape) net.blobs['data'].data[...] = in_ net.forward() out = net.blobs[args['layer']].data[0] save_feat(out, outfpath)
return I if __name__ == '__main__': sys.path.append('./python') from locker import lock, unlock with open(modelslistfpath) as f: modelslist = f.read().splitlines() for i in range(1, len(modelslist)): fname = modelslist[i - 1] flen = len(fname) fname = fname[0: flen - 4] mpath = os.path.join(modelsdir,fname, 'textured_meshes/optimized_tsdf_texture_mapped_mesh2.obj') outfpath = os.path.join(outdir, fname) outfpath2 = os.path.join(outdir2, fname) if not lock(outfpath): continue try: os.makedirs(outfpath) except: pass try: os.makedirs(outfpath2) except: pass for el in range(0, 360, 40): for az in range(0, 360, 40): for ti in range(0, 360, 40): for light_id in range(2): nowoutpath = os.path.join(outfpath, str(az) + '_' + str(el) + '_' + str(ti) + '_' + str(light_id) + '.png') nowoutpath2 = os.path.join(outfpath2, str(az) + '_' + str(el) + '_' + str(ti) + '_' + str(light_id) + '.png')
def mergeFiles(frag_file_names, merged_file_name, comment_char, do_merge, ignore_missing): """ main function of the merge script """ start_mark = "%s --Beg " % comment_char time_mark = "%s --Date inserted: %s" % (comment_char, datetime.now()) end_mark = "%s --End " % comment_char name_offset = len(start_mark) basenames = map(os.path.basename, frag_file_names) is_new_file = not os.path.exists(merged_file_name) # create an empty file if it does not exist # "append mode" ensures that, in case of two processes trying to # create the file, they do not truncate each other file if is_new_file: # check if the destination directory exists path_to_file = os.path.split(merged_file_name)[0] if path_to_file and not os.path.isdir(path_to_file): # if doesn't exist, create it os.makedirs(path_to_file) open(merged_file_name, 'a') merged_file = open(merged_file_name, 'r+') # locking file, gaining exclusive access to it _ = locker.lock(merged_file) try: new_lines = [] skip_block = "" for line in merged_file.readlines(): if line.startswith( start_mark) and line[name_offset:].strip() in basenames: skip_block = end_mark + line[name_offset:].strip() # remove all the empty lines occurring before the start mark while len(new_lines) > 0 and (new_lines[-1].strip() == ''): new_lines.pop() if not skip_block: new_lines.append(line) if line.startswith(skip_block): skip_block = "" if skip_block: print("WARNING: missing end mark ('%s')", skip_block) if do_merge: for f in frag_file_names: if ignore_missing and not os.path.exists(f): print("WARNING: '%s' does not exist, I'm ignoring it", f) continue # I do not want to add 2 empty lines at the beginning of a file if new_lines: new_lines.append(os.linesep + os.linesep) bf = os.path.basename(f) new_lines.append(start_mark + bf + os.linesep) new_lines.append(time_mark + os.linesep) file_data = open(f).read() new_lines.append(file_data) if file_data and file_data[-1] != os.linesep: new_lines.append(os.linesep) new_lines.append(end_mark + bf + os.linesep) merged_file.seek(0) merged_file.truncate(0) merged_file.writelines(new_lines) finally: # unlock file locker.unlock(merged_file) return 0
def logout_offcheck(): global sop_off if not sop_off: lock()
def logout(): global sop_off sop_off = False lock()
def mergeFiles( fragFileNames, mergedFileName, commentChar, doMerge, ignoreMissing ): startMark = "%s --Beg " % commentChar timeMark = "%s --Date inserted: %s" % (commentChar, datetime.now()) endMark = "%s --End " % commentChar nameOffset = len(startMark) basenames = map(os.path.basename, fragFileNames) isNewFile = not os.path.exists(mergedFileName) # create an empty file if it does not exist # "append mode" ensures that, in case of two processes trying to # create the file, they do not truncate each other file if isNewFile: # check if the destination directory exists path_to_file = os.path.split(mergedFileName)[0] if path_to_file and not os.path.isdir(path_to_file): # if doesn't exist, create it os.makedirs(path_to_file) open(mergedFileName,'a') mergedFile = open( mergedFileName, 'r+' ) # locking file, gaining exclusive access to it lock = locker.lock( mergedFile ) try: newLines = [ ] skipBlock = "" for line in mergedFile.readlines(): if line.startswith(startMark) and line[nameOffset:].strip() in basenames: skipBlock = endMark + line[nameOffset:].strip() # remove all the empty lines occurring before the start mark while (len(newLines) > 0) and (newLines[-1].strip() == ''): newLines.pop() if not skipBlock: newLines.append(line) if line.startswith(skipBlock): skipBlock = "" if skipBlock: print "WARNING: missing end mark ('%s')" % skipBlock if doMerge: for f in fragFileNames: if ignoreMissing and not os.path.exists(f): print "WARNING: '%s' does not exist, I'm ignoring it" % f continue # I do not want to add 2 empty lines at the beginning of a file if newLines: newLines.append('\n\n') bf = os.path.basename(f) newLines.append(startMark + bf + '\n') newLines.append(timeMark + '\n') fileData = open(f, 'r').read() newLines.append(fileData) if fileData and fileData[-1] != '\n': newLines.append('\n') newLines.append(endMark + bf + '\n') mergedFile.seek(0) mergedFile.truncate(0) mergedFile.writelines(newLines) finally: # unlock file locker.unlock( mergedFile ) return 0
def mergeFiles(fragFileNames, mergedFileName, commentChar, doMerge, ignoreMissing, excludeREs=None): startMark = "%s --Beg " % commentChar timeMark = "%s --Date inserted: %s" % (commentChar, datetime.now()) endMark = "%s --End " % commentChar nameOffset = len(startMark) basenames = map(os.path.basename, fragFileNames) isNewFile = not os.path.exists(mergedFileName) if not excludeREs: excluded = lambda _: False else: excludeREs = [ re.compile(exp) if type(exp) is str else exp for exp in excludeREs ] def excluded(line): for exp in excludeREs: if exp.match(line): return True return False # create an empty file if it does not exist # "append mode" ensures that, in case of two processes trying to # create the file, they do not truncate each other file if isNewFile: # check if the destination directory exists path_to_file = os.path.split(mergedFileName)[0] if path_to_file and not os.path.isdir(path_to_file): # if doesn't exist, create it os.makedirs(path_to_file) open(mergedFileName, 'a') mergedFile = open(mergedFileName, 'r+') # locking file, gaining exclusive access to it lock = locker.lock(mergedFile) try: newLines = [] skipBlock = "" for line in mergedFile.readlines(): if line.startswith( startMark) and line[nameOffset:].strip() in basenames: skipBlock = endMark + line[nameOffset:].strip() # remove all the empty lines occurring before the start mark while (len(newLines) > 0) and (newLines[-1].strip() == ''): newLines.pop() if not skipBlock: newLines.append(line) if line.startswith(skipBlock): skipBlock = "" if skipBlock: print "WARNING: missing end mark ('%s')" % skipBlock if doMerge: for f in fragFileNames: if ignoreMissing and not os.path.exists(f): print "WARNING: '%s' does not exist, I'm ignoring it" % f continue # I do not want to add 2 empty lines at the beginning of a file if newLines: newLines.append('\n\n') bf = os.path.basename(f) newLines.append(startMark + bf + '\n') newLines.append(timeMark + '\n') newLines.extend(line for line in open(f, 'r') if not excluded(line)) if not newLines[-1].endswith('\n'): newLines.append('\n') newLines.append(endMark + bf + '\n') mergedFile.seek(0) mergedFile.truncate(0) mergedFile.writelines(newLines) finally: # unlock file locker.unlock(mergedFile) return 0