def copy_license_notice_files(fields, base_dir, license_notice_text_location, afp): lic_name = u'' for key, value in fields: if key == u'license_file' or key == u'notice_file': lic_name = value from_lic_path = posixpath.join(to_posix(license_notice_text_location), lic_name) about_file_dir = dirname(to_posix(afp)).lstrip('/') to_lic_path = posixpath.join(to_posix(base_dir), about_file_dir) if on_windows: from_lic_path = add_unc(from_lic_path) to_lic_path = add_unc(to_lic_path) # Strip the white spaces from_lic_path = from_lic_path.strip() to_lic_path = to_lic_path.strip() # Errors will be captured when doing the validation if not posixpath.exists(from_lic_path): continue if not posixpath.exists(to_lic_path): os.makedirs(to_lic_path) try: shutil.copy2(from_lic_path, to_lic_path) except Exception as e: print(repr(e)) print('Cannot copy file at %(from_lic_path)r.' % locals())
def checksha1fp(infofile,infohash): """ Hunt down a certificate that matches the .info file (which might be a .0 or a .pem). Then read the data and compute the SHA1 fingerprint of it """ # If this is and old python, don't do hashing if not do_hashing: return True ifile="" newfilepem = re.sub('\.info$','.pem',infofile) newfile0 = re.sub('\.info$','.0',infofile) if (posixpath.exists(newfilepem)): ifile=newfilepem elif (posixpath.exists(newfile0)): ifile=newfile0 if (ifile == ""): return False cleanedhash = re.sub(':','',infohash.lower()) try: fileh = open(ifile,'r') except: return False try: data = ssl.PEM_cert_to_DER_cert(fileh.read()) except Exception, xc: log.warn('%s: %s'%(ifile, xc)) return False
def compile_templates(root, output, minify, input_encoding='utf-8', watch=True): """Compile all templates in root or root's subfolders.""" root = posixpath.normpath(root) root_len = len(root) output = posixpath.normpath(output) for dirpath, dirnames, filenames in os.walk(root): dirpath = posixpath.normpath(dirpath) if posixpath.basename(dirpath).startswith('.'): continue filenames = [f for f in filenames if not f.startswith('.') and not f.endswith('~') and not f.endswith('.py') and not f.endswith('.pyc')] outdir = posixpath.join(output , dirpath[root_len:]) if not posixpath.exists(outdir): os.makedirs(outdir) if not posixpath.exists(posixpath.join(outdir, '__init__.py')): out = open(posixpath.join(outdir, '__init__.py'), 'w') out.close() for f in filenames: path = posixpath.join(dirpath, f).replace('\\','/') outfile = posixpath.join(outdir, f.replace('.','_')+'.py') filemtime = os.stat(path)[stat.ST_MTIME] if not exists(outfile) or os.stat(outfile)[stat.ST_MTIME] < filemtime: uri = path[root_len+1:] print 'compiling', uri text = file(path).read() if minify: text = minify_js_in_html(uri, text) t = mako.template.Template(text=text, filename=path, uri=uri, input_encoding=input_encoding) out = open(outfile, 'w') out.write( t.code) out.close() if watch: watch_folder_for_changes(dirpath, minify)
def send_head(self): path = self.translate_path(self.path) f = None if path is None: self.send_error(404, "File not found") return None if path is '/': return self.list_directory(path) if os.path.isdir(path): # not endswidth / if not self.path.endswith('/'): # redirect browser - doing basically what apache does self.send_response(301) self.send_header("Location", self.path + "/") self.end_headers() return None # looking for index.html or index.htm for index in "index.html", "index.htm": index = os.path.join(path, index) if os.path.exists(index): path = index break else: return self.list_directory(path) # If there's no extension and the file doesn't exist, # see if the file plus the default extension exists. if (SublimeServerHandler.defaultExtension and not posixpath.splitext(path)[1] and not posixpath.exists(path) and posixpath.exists(path + SublimeServerHandler.defaultExtension)): path += SublimeServerHandler.defaultExtension ctype = self.guess_type(path) try: # Always read in binary mode. Opening files in text mode may cause # newline translations, making the actual size of the content # transmitted *less* than the content-length! f = open(path, 'rb') except IOError: self.send_error(404, "File not found") return None try: self.send_response(200) self.send_header("Content-type", ctype) fs = os.fstat(f.fileno()) self.send_header("Content-Length", str(fs[6])) self.send_header("Access-Control-Allow-Origin", "*") self.send_header("Cache-Control", "no-cache, no-store, must-revalidate") self.send_header("Pragma", "no-cache") self.send_header("Expires", "0") self.send_header( "Last-Modified", self.date_time_string(fs.st_mtime)) self.end_headers() return f except: f.close() raise
def safe_rename(old, new): if old != new: if not posixpath.exists(old): raise Exception, "Old path does not exist: %s" % old if posixpath.exists(new): raise Exception, "New path exists already: %s" % new os.rename(old, new)
def test_exists(self): self.assertIs(posixpath.exists(test_support.TESTFN), False) f = open(test_support.TESTFN, "wb") try: f.write("foo") f.close() self.assertIs(posixpath.exists(test_support.TESTFN), True) self.assertIs(posixpath.lexists(test_support.TESTFN), True) finally: if not f.close(): f.close() self.assertRaises(TypeError, posixpath.exists)
def printindex(outfilename,headfilename,levels,titles,tables): # Read in the header file headbuf = '' if posixpath.exists(headfilename) : try: fd = open(headfilename,'r') except: print 'Error reading file',headfilename exit() headbuf = fd.read() fd.close() else: print 'Header file \'' + headfilename + '\' does not exist' # Now open the output file. try: fd = open(outfilename,'w') except: print 'Error writing to file',outfilename exit() # Add the HTML Header info here. fd.write(headbuf) # Add some HTML separators fd.write('\n<P>\n') fd.write('<TABLE>\n') for i in range(len(levels)): level = levels[i] title = titles[i] if len(tables[i]) == 0: # If no functions in 'None' category, then don't print # this category. if level == 'none': continue else: # If no functions in any other category, then print # the header saying no functions in this cagetory. fd.write('</TR><TD WIDTH=250 COLSPAN="3">') fd.write('<B>' + 'No ' + level +' routines' + '</B>') fd.write('</TD></TR>\n') continue fd.write('</TR><TD WIDTH=250 COLSPAN="3">') #fd.write('<B>' + upper(title[0])+title[1:] + '</B>') fd.write('<B>' + title + '</B>') fd.write('</TD></TR>\n') # Now make the entries in the table column oriented tables[i] = maketranspose(tables[i],3) for filename in tables[i]: path,name = posixpath.split(filename) func_name,ext = posixpath.splitext(name) mesg = ' <TD WIDTH=250><A HREF="'+ './' + name + '">' + \ func_name + '</A></TD>\n' fd.write(mesg) if tables[i].index(filename) % 3 == 2 : fd.write('<TR>\n') fd.write('</TABLE>\n') # Add HTML tail info here fd.write('<BR><A HREF="../index.html"><IMG SRC="../up.gif">Table of Contents</A>\n') fd.close()
def makedirs(name, mode=0o777, exist_ok=False): """makedirs(name [, mode=0o777][, exist_ok=False]) Super-mkdir; create a leaf directory and all intermediate ones. Works like mkdir, except that any intermediate path segment (not just the rightmost) will be created if it does not exist. If the target directory already exists, raise an OSError if exist_ok is False. Otherwise no exception is raised. This is recursive. """ head, tail = path.split(name) if not tail: head, tail = path.split(head) if head and tail and not path.exists(head): try: makedirs(head, exist_ok=exist_ok) except FileExistsError: # Defeats race condition when another thread created the path pass cdir = curdir if isinstance(tail, bytes): cdir = bytes(curdir, 'ASCII') if tail == cdir: # xxx/newdir/. exists if xxx/newdir exists return try: mkdir(name, mode) except OSError: # Cannot rely on checking for EEXIST, since the operating system # could give priority to other errors like EACCES or EROFS if not exist_ok or not path.isdir(name): raise
def __setitem__(self, key, value): if key == "includes": if isinstance(value, list): value = value[0] for path in split(value): path = self._parser._interpolate("DEFAULT", None, path, self) path = posixpath.expanduser(path) if not posixpath.exists(path): raise Exception, "No such configuration file: %s" % path if posixpath.isdir(path): logging.info("Parsing config filenames from directory: %s", path) def walk_func(arg, directory, names): for name in names: path = posixpath.join(directory, name) if not posixpath.isdir(path): arg._parser.read(path) posixpath.walk(path, walk_func, self) else: logging.info("Parsing config filename: %s", path) self._parser.read(path) # Environment has precedence over configuration elif not key.startswith("CHECKBOX") or key.upper() not in os.environ: super(IncludeDict, self).__setitem__(key, value)
def test(): #---------------------------------------------------------------------------- ##TODO: a more serious test with distinct processes ! print('Testing glock.py...') # unfortunately can't test inter-process lock here! lockName = 'myFirstLock' l = GlobalLock(lockName) if not _windows: assert posixpath.exists(lockName) l.acquire() l.acquire() # reentrant lock, must not block l.release() l.release() try: l.release() except NotOwner: pass else: raise Exception('should have raised a NotOwner exception') # Check that <> threads of same process do block: import threading, time thread = threading.Thread(target=threadMain, args=(l,)) print('main: locking...', end=' ') l.acquire() print(' done.') thread.start() time.sleep(3) print('\nmain: unlocking...', end=' ') l.release() print(' done.') time.sleep(0.1) print('=> Test of glock.py passed.') return l
def makedirs(name, mode=0o777, exist_ok=False): """makedirs(name [, mode=0o777][, exist_ok=False]) Super-mkdir; create a leaf directory and all intermediate ones. Works like mkdir, except that any intermediate path segment (not just the rightmost) will be created if it does not exist. If the target directory already exists, raise an OSError if exist_ok is False. Otherwise no exception is raised. This is recursive. """ head, tail = path.split(name) if not tail: head, tail = path.split(head) if head and tail and not path.exists(head): try: makedirs(head, mode, exist_ok) except FileExistsError: # be happy if someone already created the path pass cdir = curdir if isinstance(tail, bytes): cdir = bytes(curdir, 'ASCII') if tail == cdir: # xxx/newdir/. exists if xxx/newdir exists return try: mkdir(name, mode) except OSError as e: if not exist_ok or e.errno != errno.EEXIST or not path.isdir(name): raise
def write(self): import posixpath if os.environ.has_key('CDDB_WRITE_DIR'): dir = os.environ['CDDB_WRITE_DIR'] else: dir = os.environ['HOME'] + '/' + _cddbrc file = dir + '/' + self.id + '.rdb' if posixpath.exists(file): posix.rename(file, file + '~') f = open(file, 'w') f.write('album.title:\t' + self.title + '\n') f.write('album.artist:\t' + self.artist + '\n') f.write('album.toc:\t' + self.toc + '\n') for note in self.notes: f.write('album.notes:\t' + note + '\n') prevpref = None for i in range(1, len(self.track)): if self.trackartist[i]: f.write('track%r.artist:\t%s\n' % (i, self.trackartist[i])) track = self.track[i] try: off = track.index(',') except ValueError: prevpref = None else: if prevpref and track[:off] == prevpref: track = track[off:] else: prevpref = track[:off] f.write('track%r.title:\t%s\n' % (i, track)) f.close() return
def add_project(self, args): project = args.project.lower() config_path = get_config_path(project, check_exists=False) if exists(config_path): output('[red]配置 {} 已存在'.format(project)) sys.exit(1) self._new_project(config_path)
def makeDimensionLBNList(dimname): command = "sam translate constraints -a --dim='%s' | grep -v ':' " %(dimname) result = os.popen(command).readlines() result.sort() n = 0 l = 0 s = 0 for file in result: filename = string.strip(file) if(len(filename)<2): continue if(string.find(filename,':') >= 0): continue if(exists(filename+".parentage")): s = s+1 continue result=makeFileParentageList(filename) if result[0] == "OK": n = n + 1 if(len(result[1]) < 1): print " strange problem, no lbns" continue f = open(filename+".parentage","w") i = 0 while i < len(result[1]): f.write("%d %d %s %s %s %s\n"%(result[1][i][0],result[1][i][1],result[1][i][2],result[1][i][3],result[1][i][4],result[1][i][5])) i = i + 1 l = l + 1 f.close else: sys.stderr.write( result[0]) return [n,l,s]
def makedirs(name, mode=511, exist_ok=False): """makedirs(name [, mode=0o777][, exist_ok=False]) Super-mkdir; create a leaf directory and all intermediate ones. Works like mkdir, except that any intermediate path segment (not just the rightmost) will be created if it does not exist. If the target directory already exists, raise an OSError if exist_ok is False. Otherwise no exception is raised. This is recursive. """ head, tail = path.split(name) if not tail: head, tail = path.split(head) if head: if tail: pass if not path.exists(head): try: makedirs(head, exist_ok=exist_ok) except FileExistsError: pass cdir = curdir if isinstance(tail, bytes): cdir = bytes(curdir, 'ASCII') if tail == cdir: return try: mkdir(name, mode) except OSError: if not (exist_ok and path.isdir(name)): raise
def main(args): sections = SplitArgsIntoSections(args[1:]) assert len(sections) == 3 command = sections[0] outputs = sections[1] inputs = sections[2] assert len(inputs) == len(outputs) # print 'command: %s' % command # print 'inputs: %s' % inputs # print 'outputs: %s' % outputs for i in range(0, len(inputs)): # make sure output directory exists out_dir = posixpath.dirname(outputs[i]) if not posixpath.exists(out_dir): posixpath.makedirs(out_dir) # redirect stdout to the output file out_file = open(outputs[i], 'w') # build up the command subproc_cmd = [command[0]] subproc_cmd.append(inputs[i]) subproc_cmd += command[1:] # fork and exec returnCode = subprocess.call(subproc_cmd, stdout=out_file) # clean up output file out_file.close() # make sure it worked assert returnCode == 0 return 0
def makedirs(name, mode=0o777, exist_ok=False): """makedirs(path [, mode=0o777][, exist_ok=False]) Super-mkdir; create a leaf directory and all intermediate ones. Works like mkdir, except that any intermediate path segment (not just the rightmost) will be created if it does not exist. If the target directory with the same mode as we specified already exists, raises an OSError if exist_ok is False, otherwise no exception is raised. This is recursive. """ head, tail = path.split(name) if not tail: head, tail = path.split(head) if head and tail and not path.exists(head): try: makedirs(head, mode, exist_ok) except OSError as e: # be happy if someone already created the path if e.errno != errno.EEXIST: raise if tail == curdir: # xxx/newdir/. exists if xxx/newdir exists return try: mkdir(name, mode) except OSError as e: import stat as st if not (e.errno == errno.EEXIST and exist_ok and path.isdir(name) and st.S_IMODE(lstat(name).st_mode) == _get_masked_mode(mode)): raise
def __init__(self, fpath, lockInitially=False, logger=None): ''' Creates (or opens) a global lock. @param fpath: Path of the file used as lock target. This is also the global id of the lock. The file will be created if non existent. @param lockInitially: if True locks initially. @param logger: an optional logger object. ''' self.logger = logger self.fpath = fpath if posixpath.exists(fpath): self.previous_lockfile_present = True else: self.previous_lockfile_present = False if _windows: self.name = string.replace(fpath, '\\', '_') self.mutex = win32event.CreateMutex(None, lockInitially, self.name) else: # Unix self.name = fpath self.flock = open(fpath, 'w') self.fdlock = self.flock.fileno() self.threadLock = threading.RLock() if lockInitially: self.acquire()
def renames(old, new): """renames(old, new) Super-rename; create directories as necessary and delete any left empty. Works like rename, except creation of any intermediate directories needed to make the new pathname good is attempted first. After the rename, directories corresponding to rightmost path segments of the old name will be pruned way until either the whole path is consumed or a nonempty directory is found. Note: this function can fail with the new directory structure made if you lack permissions needed to unlink the leaf directory or file. """ head, tail = path.split(new) if head and tail and not path.exists(head): makedirs(head) rename(old, new) head, tail = path.split(old) if head and tail: try: removedirs(head) except error: pass
def test_islink(self): self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False) f = open(test_support.TESTFN + "1", "wb") try: f.write("foo") f.close() self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False) if hasattr(os, "symlink"): os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2") self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True) os.remove(test_support.TESTFN + "1") self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True) self.assertIs(posixpath.exists(test_support.TESTFN + "2"), False) self.assertIs(posixpath.lexists(test_support.TESTFN + "2"), True) finally: if not f.close(): f.close() try: os.remove(test_support.TESTFN + "1") except os.error: pass try: os.remove(test_support.TESTFN + "2") except os.error: pass self.assertRaises(TypeError, posixpath.islink)
def __init__(self, filename=None, name=None, robot=None, brain=None, echo=1, mode="w"): """ Pass in robot and brain so that we can query them (and maybe make copies and query them on occation). If echo is True, then it will echo the log file to the terminal """ self.open = 1 timestamp = self.timestamp() if filename == None: filename = timestamp + ".log" while posixpath.exists(filename): timestamp = self.timestamp() filename = timestamp + ".log" self.filename = filename self.file = open(filename, mode) self.echo = echo if mode == "a": self.writeln("... Continuing log at " + timestamp) else: self.writeln("Log opened: " + timestamp) if name != None: self.writeln("Experiment name: " + name) if robot != None: self.writeln("Robot: " + robot.type) if brain != None: self.writeln("Brain: " + brain.name) if os.environ.has_key("HOSTNAME"): self.writeln("Hostname: " + os.environ["HOSTNAME"]) if os.environ.has_key("USER"): self.writeln("User: "******"USER"])
def getDatasetLBNList(defname): command = "sam translate constraints -a --dim='__set__ %s' | grep raw " %(defname) result = os.popen(command).readlines() result.sort() n = 0 l = 0 s = 0 for file in result: filename = string.strip(file) if(len(filename)<2): continue if(string.find(filename,':') >= 0): continue if(exists(filename+".lbn")): s = s+1 continue result=getFileLBNList(filename) if result[0] == "OK": n = n + 1 if(len(result[1]) < 1): print " strange problem, no lbns" continue f = open(filename+".lbn","w") i = 0 while i < len(result[1]): f.write("%d\n"%result[1][i]) i = i + 1 l = l + 1 f.close else: sys.stderr.write( result[0]) return [n,l,s]
def renderArea(self, width, height, srs, xmin, ymin, xmax, ymax, zoom): """ """ if self.mapnik is None: self.mapnik = mapnik.Map(0, 0) if exists(self.mapfile): mapnik.load_map(self.mapnik, str(self.mapfile)) else: handle, filename = mkstemp() os.write(handle, urlopen(self.mapfile).read()) os.close(handle) mapnik.load_map(self.mapnik, filename) os.unlink(filename) self.mapnik.width = width self.mapnik.height = height self.mapnik.zoom_to_box(mapnik.Envelope(xmin, ymin, xmax, ymax)) img = mapnik.Image(width, height) mapnik.render(self.mapnik, img) img = Image.fromstring("RGBA", (width, height), img.tostring()) return img
def file_exists(file_name): from posixpath import exists import string if len(file_name) == 0: return 0 else: return exists(file_name)
def write(self): import posixpath if os.environ.has_key("CDDB_WRITE_DIR"): dir = os.environ["CDDB_WRITE_DIR"] else: dir = os.environ["HOME"] + "/" + _cddbrc file = dir + "/" + self.id + ".rdb" if posixpath.exists(file): # make backup copy posix.rename(file, file + "~") f = open(file, "w") f.write("album.title:\t" + self.title + "\n") f.write("album.artist:\t" + self.artist + "\n") f.write("album.toc:\t" + self.toc + "\n") for note in self.notes: f.write("album.notes:\t" + note + "\n") prevpref = None for i in range(1, len(self.track)): if self.trackartist[i]: f.write("track%r.artist:\t%s\n" % (i, self.trackartist[i])) track = self.track[i] try: off = track.index(",") except ValuError: prevpref = None else: if prevpref and track[:off] == prevpref: track = track[off:] else: prevpref = track[:off] f.write("track%r.title:\t%s\n" % (i, track)) f.close()
def get_icon_url(self): base = join(self.ICON_PATH, self.get_ext()) for ext in self.ICON_EXTENSIONS: relative = "%s.%s" % (base, ext) if exists(join(settings.MEDIA_ROOT, relative)): return join(settings.MEDIA_URL, relative) return None
def __init__(self,filename = None, name = None, robot = None, brain = None, echo = 1, mode = 'w'): """ Pass in robot and brain so that we can query them (and maybe make copies and query them on occation). If echo is True, then it will echo the log file to the terminal """ self.open = 1 timestamp = self.timestamp() if filename == None: filename= timestamp + '.log' while posixpath.exists(filename): timestamp = self.timestamp() filename = timestamp + '.log' self.filename = filename self.file = open(filename, mode) self.echo = echo if mode == 'a': self.writeln('... Continuing log at ' + timestamp) else: self.writeln("Log opened: " + timestamp) if name != None: self.writeln('Experiment name: ' + name) if robot != None: self.writeln('Robot: ' + robot.type) if brain != None: self.writeln('Brain: ' + brain.name) if os.environ.has_key('HOSTNAME'): self.writeln('Hostname: ' + os.environ['HOSTNAME']) if os.environ.has_key('USER'): self.writeln('User: '******'USER'])
def safe_change_mode(path, mode): if not posixpath.exists(path): raise Exception, "Path does not exist: %s" % path old_mode = os.stat(path)[ST_MODE] if mode != S_IMODE(old_mode): os.chmod(path, mode)
def safe_make_fifo(path, mode=0666): if posixpath.exists(path): mode = os.stat(path)[ST_MODE] if not S_ISFIFO(mode): raise Exception, "Path is not a FIFO: %s" % path else: os.mkfifo(path, mode)
def renderArea(self, width, height, srs, xmin, ymin, xmax, ymax, zoom): """ """ if self.mapnik is None: self.mapnik = mapnik.Map(0, 0) if exists(self.mapfile): mapnik.load_map(self.mapnik, str(self.mapfile)) else: handle, filename = mkstemp() os.write(handle, urlopen(self.mapfile).read()) os.close(handle) mapnik.load_map(self.mapnik, filename) os.unlink(filename) self.mapnik.width = width self.mapnik.height = height self.mapnik.zoom_to_box(mapnik.Envelope(xmin, ymin, xmax, ymax)) img = mapnik.Image(width, height) mapnik.render(self.mapnik, img) img = Image.fromstring('RGBA', (width, height), img.tostring()) return img
def write(self): import posixpath if os.environ.has_key('CDDB_WRITE_DIR'): dir = os.environ['CDDB_WRITE_DIR'] else: dir = os.environ['HOME'] + '/' + _cddbrc file = dir + '/' + self.id + '.rdb' if posixpath.exists(file): # make backup copy posix.rename(file, file + '~') f = open(file, 'w') f.write('album.title:\t' + self.title + '\n') f.write('album.artist:\t' + self.artist + '\n') f.write('album.toc:\t' + self.toc + '\n') for note in self.notes: f.write('album.notes:\t' + note + '\n') prevpref = None for i in range(1, len(self.track)): if self.trackartist[i]: f.write('track'+`i`+'.artist:\t'+self.trackartist[i]+'\n') track = self.track[i] try: off = string.index(track, ',') except string.index_error: prevpref = None else: if prevpref and track[:off] == prevpref: track = track[off:] else: prevpref = track[:off] f.write('track' + `i` + '.title:\t' + track + '\n') f.close()
def configure(self, env): env.Append(CPPDEFINES=["WIN32"]) env.Append(CCFLAGS=["/EHsc", "/GR", "/FIwombat\\targetsxs.h"]) env.Append(LINKFLAGS=["/MANIFEST"]) env.Append(CCPDBFLAGS="/Fd${TARGET}.pdb") env.Append(PDB="${TARGET.base}.pdb") # Architecture specific setup if env["target_arch"] == "x86_64": env["bit_suffix"] = "_x64" env["win_arch_path"] = "x64" else: env.Append(CPPDEFINES=["_USE_32BIT_TIME_T"]) env["bit_suffix"] = "" env["win_arch_path"] = "" if env["vsver"] > 7: env.Append(CCFLAGS=["/Z7", "/MP"]) else: env.Append(CCFLAGS=["/Zi"]) # Entitlement setup if "oea" in env["entitlements"]: (oea_major, oea_minor, oea_release) = env["oea_version"].split(".") env.Append( CPPDEFINES=["OEA_MAJVERSION=%s" % (oea_major), "OEA_MINVERSION=%s" % (oea_minor)], CPPPATH=[posixpath.join(env["oea_home"], "include/oea")], ) # Expat Setup # env['expathome'] = posixpath.join( env['shared_directory'], 'Expat/Expat-%s/Source' % ( env['expat_version'] ) ) # env['expatobj'] = posixpath.join( env['expat_home'],'lib/vc%s%s/release_static/*.obj' % ( env['vsver'], env['bit_suffix'] ) ) if env["product"] == "mamdaall": if not env.has_key("dotnet_framework"): print "Calculating dotnet_framework" if env["target_arch"] == "x86_64": framework = "Framework64" else: framework = "Framework" framework_dir = posixpath.join("c:/windows/Microsoft.NET/", framework) if env["dotnet_version"] == "1.0": dotnetver = "v1.1.4322" elif env["dotnet_version"] == "2.0": dotnetver = "v2.0.50727" elif env["dotnet_version"] == "4.0": dotnetver = "v4.0.30319" env["dotnet_framework"] = posixpath.join(framework_dir, dotnetver) if not posixpath.exists(env["dotnet_framework"]): print "ERROR: Dotnet framework dir: %s not found" % (env["dotnet_framework"]) Exit(1)
def readJsonFromDir(path: str) -> str: path = posixpath.join(path, "configure.json") print(f"Reading {path}...") assert posixpath.exists(path) parser = json_parser.QMakeSpecificJSONParser() return parser.parse(path)
def clean_symlinks(dirpath): """Remove all broken symlinks under the given directory.""" # Broken symlinks appear as files, so we skip directories. for dirpath, _, filenames in os.walk(dirpath): for filename in filenames: path = posixpath.join(dirpath, filename) if posixpath.islink(path) and not posixpath.exists(path): os.unlink(path)
def dirs(): """Returns all application etcdirs.""" dirs = filter(lambda d: pp.basename(d) == 'etc', pick(0, os.walk(env.directory))) # Skip ones that are python packages! dirs = filter(lambda d: not pp.exists(pp.join(d, '__init__.py')), dirs) return dirs
def test_exists(self): self.assertIs(posixpath.exists(test_support.TESTFN), False) f = open(test_support.TESTFN, "wb") try: f.write("foo") f.close() self.assertIs(posixpath.exists(test_support.TESTFN), True) self.assertIs(posixpath.lexists(test_support.TESTFN), True) finally: if not f.close(): f.close() try: os.remove(test_support.TESTFN) except os.error: pass self.assertRaises(TypeError, posixpath.exists)
def serialize_picture(self, data, mime): name = hashlib.md5(data).hexdigest() name += get_extension_by_mime(mime) path = posixpath.join(self.picture_dir, name) if not posixpath.exists(path): with open(path, 'wb') as f: f.write(data) return name
def get_config_path(project, check_exists=True): config_dir = get_config_dir() config_path = join(config_dir, '{}.ini'.format(project)) if not exists(config_path) and check_exists: output( '{} 不存在,请运行 bge config 或 bge config add 命令初始化项目配置'.format(project)) sys.exit(1) return config_path
def open_spider(self, spider): base_dir = self.base_dir data_dir = join(base_dir, 'data') if not exists(data_dir): os.mkdir(data_dir) filepath = join(data_dir, 'otc.csv') spider._f = fobj = open(filepath, 'w') spider._w = csv.DictWriter(fobj, fieldnames=YaozhotcItem.fields.keys()) spider._w.writeheader()
def file_exists(file_name): from posixpath import exists if type(file_name) == type(""): if len(file_name) == 0: return 0 else: return exists(file_name) else: raise AttributeError, "filename nust be a string"
def __init__(self, root): self.root = root if not posixpath.exists(posixpath.join(self.root, self.ukbench_dir)): download(self.root, self.filename, self.url) unzip(self.root, self.filename, self.ukbench_dir) self.uris = sorted( list_files(root=posixpath.join(self.root, self.ukbench_dir, 'full'), suffix=('png', 'jpg', 'jpeg', 'gif')))
def get_path(command): for path in get_paths(): absolute = posixpath.join(path, command) if posixpath.exists(absolute): mode = os.stat(absolute)[ST_MODE] if mode & S_IEXEC: return absolute return None
def __init__(self, root): self.root = root if not posixpath.exists(posixpath.join(self.root, self.ukbench_dir)): download(self.url, self.root, self.filename, untar=True) self.uris = list_files(posixpath.join(self.root, self.ukbench_dir, 'full'), ('png', 'jpg', 'jpeg', 'gif')) self.uris.sort()
def changelog_version(changelog="debian/changelog"): version = "dev" if posixpath.exists(changelog): head = open(changelog).readline() match = re.compile(".*\((.*)\).*").match(head) if match: version = match.group(1) return version
def test_makedirs(self, tmpdir): """Tests the `mkdir` method with mode parameter.""" dir_path = posixpath.join(str(tmpdir), "some", "nested", "dir") with LocalHook() as hook: hook.makedirs(dir_path, mode=0o750) assert posixpath.exists(dir_path) assert oct(os.stat(dir_path).st_mode)[-3:] == "750"
def logfile_exists(file=None, filename='logfile.log'): dirname = posixpath.dirname(__file__) logfile = posixpath.join(dirname, filename) if posixpath.exists(logfile): return logfile else: log_file = open(logfile, 'w') log_file.write('') log_file.close() return logfile
def in_project_directory(): if exists("dodo.py") and len(listdir()) > 0: pass else: print( """Please run this command from an existing project directory (project directories contain a dodo.py file)""" ) raise NotInProjectException
def init_docsify(self, args): name = args.name home = args.home if home is None: home = get_home() docs_dir = join(home, name) if exists(docs_dir): output('[red]错误!{} 已存在[/red] '.format(docs_dir)) sys.exit(1) if not exists(home): output('[red]错误!无法找到 home 目录 {}。[/red]'.format(home)) sys.exit(1) with os.popen('docsify init {}'.format(docs_dir)) as f: content = f.read() if content: output( '[green]docsify 项目已初始化,路径为:[/green]{}'.format(docs_dir) ) output('[green]请跳转至项目目录下。[/green]')
def get_docs_dir(self, home=None): if home is None: home = get_home() doc_path = join(home, 'index.html') if not exists(doc_path): output( '[red]请确认当前目录或者输入的项目路径是否为 docsify 项目根目录。[/red]' ) sys.exit(1) return home
def compile_templates(root, output, minify, input_encoding='utf-8', watch=True): """Compile all templates in root or root's subfolders.""" root = posixpath.normpath(root) root_len = len(root) output = posixpath.normpath(output) for dirpath, dirnames, filenames in os.walk(root): dirpath = posixpath.normpath(dirpath) if posixpath.basename(dirpath).startswith('.'): continue filenames = [ f for f in filenames if not f.startswith('.') and not f.endswith('~') and not f.endswith('.py') and not f.endswith('.pyc') ] outdir = posixpath.join(output, dirpath[root_len:]) if not posixpath.exists(outdir): os.makedirs(outdir) if not posixpath.exists(posixpath.join(outdir, '__init__.py')): out = open(posixpath.join(outdir, '__init__.py'), 'w') out.close() for f in filenames: path = posixpath.join(dirpath, f).replace('\\', '/') outfile = posixpath.join(outdir, f.replace('.', '_') + '.py') filemtime = os.stat(path)[stat.ST_MTIME] if not exists( outfile) or os.stat(outfile)[stat.ST_MTIME] < filemtime: uri = path[root_len + 1:] print 'compiling', uri text = file(path).read() if minify: text = minify_js_in_html(uri, text) t = mako.template.Template(text=text, filename=path, uri=uri, input_encoding=input_encoding) out = open(outfile, 'w') out.write(t.code) out.close() if watch: watch_folder_for_changes(dirpath, minify)
def get_globals(script): path = posixpath.expanduser(script) if not posixpath.exists(path): path = get_path(script) if not path: raise Exception("Script not found in PATH: %s" % script) globals = {} exec(compile(open(path).read(), path, 'exec'), globals) return globals
def get_active_project(): active_path = get_active_path() project = constants.DEFAULT_PROJECT if exists(active_path): with open(active_path, 'r') as fp: project = fp.read() else: with open(active_path, 'w') as fp: fp.write(project) return project