def _check_exe(_opts, macro, value, constraint, silent = False): if len(value) == 0 or constraint == 'none': return True orig_value = value if path.isabspath(value): if path.isfile(value): return True if os.name == 'nt': if path.isfile('%s.exe' % (value)): return True value = path.basename(value) absexe = True else: absexe = False paths = os.environ['PATH'].split(os.pathsep) if _check_paths(value, paths): if absexe: if not silent: log.notice('warning: exe: absolute exe found in path: (%s) %s' % (macro, orig_value)) return True if constraint == 'optional': if not silent: log.trace('warning: exe: optional exe not found: (%s) %s' % (macro, orig_value)) return True if not silent: log.notice('error: exe: not found: (%s) %s' % (macro, orig_value)) return False
def _check_paths(name, paths): for p in paths: exe = path.join(p, name) if path.isfile(exe): return True if os.name == 'nt': if path.isfile('%s.exe' % (exe)): return True return False
def deletefile(self, id): id = utf8_str(id) filepath = self.id_to_filepath.get(id, None) if id is None: raise WrapperException('id does not exist in manifest') add_to_deleted = True # if file was added or modified, delete file from outdir if id in self.added or id in self.modified.keys(): filepath = os.path.join(self.outdir,filepath) if path.exists(filepath) and path.isfile(filepath): os.remove(pathof(filepath)) if id in self.added: self.added.remove(id) add_to_deleted = False if id in self.modified.keys(): del self.modified[id] # remove from manifest href = self.id_to_href[id] del self.id_to_href[id] del self.id_to_mime[id] del self.href_to_id[href] # remove from spine new_spine = [] was_modified = False for sid, linear in self.spine: if sid != id: new_spine.append((sid, linear)) else: was_modified = True if was_modified: setspine(new_spine) if add_to_deleted: self.deleted.append(id) self.modified['OEBPS/content.opf'] = 'file' del self.id_to_filepath[id]
def deletefile(self, id): id = utf8_str(id) filepath = self.id_to_filepath.get(id, None) if id is None: raise WrapperException('id does not exist in manifest') add_to_deleted = True # if file was added or modified, delete file from outdir if id in self.added or id in self.modified.keys(): filepath = os.path.join(self.outdir, filepath) if path.exists(filepath) and path.isfile(filepath): os.remove(pathof(filepath)) if id in self.added: self.added.remove(id) add_to_deleted = False if id in self.modified.keys(): del self.modified[id] # remove from manifest href = self.id_to_href[id] del self.id_to_href[id] del self.id_to_mime[id] del self.href_to_id[href] # remove from spine new_spine = [] was_modified = False for sid, linear in self.spine: if sid != id: new_spine.append((sid, linear)) else: was_modified = True if was_modified: setspine(new_spine) if add_to_deleted: self.deleted.append(id) self.modified['OEBPS/content.opf'] = 'file' del self.id_to_filepath[id]
def __call__(self, env, start_response): path = self.config.output + env.get('PATH_INFO') path_index = path + 'index.html' if not path.exists(): return self._not_found(start_response) if path.isfile(): return self._serve_file(path, start_response) if path_index.exists(): return self._serve_file(path_index, start_response) return self._list_dir(path, start_response)
def zipUpDir(self, myzip, tdir, localname): currentdir = tdir if localname != "": currentdir = os.path.join(currentdir,localname) list = path.listdir(currentdir) for file in list: afilename = file localfilePath = os.path.join(localname, afilename) realfilePath = os.path.join(currentdir,file) if path.isfile(realfilePath): myzip.write(pathof(realfilePath), pathof(localfilePath), zipfile.ZIP_DEFLATED) elif path.isdir(realfilePath): self.zipUpDir(myzip, tdir, localfilePath)
def zipUpDir(self, myzip, tdir, localname): currentdir = tdir if localname != "": currentdir = os.path.join(currentdir, localname) list = path.listdir(currentdir) for file in list: afilename = file localfilePath = os.path.join(localname, afilename) realfilePath = os.path.join(currentdir, file) if path.isfile(realfilePath): myzip.write(pathof(realfilePath), pathof(localfilePath), zipfile.ZIP_DEFLATED) elif path.isdir(realfilePath): self.zipUpDir(myzip, tdir, localfilePath)
def addotherfile(self, book_href, data): id = utf8_str(book_href) if id in self.other: raise WrapperException('book href must be unquie') desired_path = id.replace("/", os.sep) filepath = os.path.join(pathof(self.outdir), desired_path) if path.isfile(filepath): raise WrapperException('desired path already exists') base = os.path.dirname(pathof(filepath)) if not path.exists(base): os.makedirs(pathof(base)) with open(pathof(filepath), 'wb') as fp: fp.write(data) self.other.append(id) self.added.append(id) self.id_to_filepath[id] = desired_path
def addotherfile(self, book_href, data) : id = utf8_str(book_href) if id in self.other: raise WrapperException('book href must be unquie') desired_path = id.replace("/",os.sep) filepath = os.path.join(pathof(self.outdir),desired_path) if path.isfile(filepath): raise WrapperException('desired path already exists') base = os.path.dirname(pathof(filepath)) if not path.exists(base): os.makedirs(pathof(base)) with open(pathof(filepath),'wb')as fp: fp.write(data) self.other.append(id) self.added.append(id) self.id_to_filepath[id] = desired_path
def deleteotherfile(self, book_href): id = utf8_str(book_href) filepath = self.id_to_filepath.get(id, None) if id is None: raise WrapperException('book href does not exist') if id in PROTECTED_FILES: raise WrapperException('attempt to delete protected file') add_to_deleted = True # if file was added or modified delete file from outdir if id in self.added or id in self.modified.keys(): filepath = os.path.join(self.outdir, filepath) if path.exists(filepath) and path.isfile(filepath): os.remove(pathof(filepath)) if id in self.added: self.added.remove(id) add_to_deleted = False if id in self.other: self.other.remove(id) if id in self.modified.keys(): del self.modified[id] if add_to_deleted: self.deleted.append(id) del self.id_to_filepath[id]
def deleteotherfile(self, book_href): id = utf8_str(book_href) filepath = self.id_to_filepath.get(id, None) if id is None: raise WrapperException('book href does not exist') if id in PROTECTED_FILES: raise WrapperException('attempt to delete protected file') add_to_deleted = True # if file was added or modified delete file from outdir if id in self.added or id in self.modified.keys(): filepath = os.path.join(self.outdir,filepath) if path.exists(filepath) and path.isfile(filepath): os.remove(pathof(filepath)) if id in self.added: self.added.remove(id) add_to_deleted = False if id in self.other: self.other.remove(id) if id in self.modified.keys(): del self.modified[id] if add_to_deleted: self.deleted.append(id) del self.id_to_filepath[id]
def _http_downloader(url, local, config, opts): if path.exists(local): return True # # Hack for GitHub. # if url.startswith('https://api.github.com'): url = urllib_parse.urljoin(url, config.expand('tarball/%{version}')) dst = os.path.relpath(path.host(local)) log.output('download: %s -> %s' % (url, dst)) log.notice('download: %s -> %s' % (_sensible_url(url, len(dst)), dst)) failed = False if _do_download(opts): _in = None _out = None _length = None _have = 0 _chunk_size = 256 * 1024 _chunk = None _last_percent = 200.0 _last_msg = '' _have_status_output = False _url = url try: try: _in = None _ssl_context = None # See #2656 _req = urllib_request.Request(_url) _req.add_header('User-Agent', 'Wget/1.16.3 (freebsd10.1)') try: import ssl _ssl_context = ssl._create_unverified_context() _in = urllib_request.urlopen(_req, context = _ssl_context) except: _ssl_context = None if _ssl_context is None: _in = urllib_request.urlopen(_req) if _url != _in.geturl(): _url = _in.geturl() log.output(' redirect: %s' % (_url)) log.notice(' redirect: %s' % (_sensible_url(_url))) _out = open(path.host(local), 'wb') try: _length = int(_in.info()['Content-Length'].strip()) except: pass while True: _msg = '\rdownloading: %s - %s ' % (dst, _humanize_bytes(_have)) if _length: _percent = round((float(_have) / _length) * 100, 2) if _percent != _last_percent: _msg += 'of %s (%0.0f%%) ' % (_humanize_bytes(_length), _percent) if _msg != _last_msg: extras = (len(_last_msg) - len(_msg)) log.stdout_raw('%s%s' % (_msg, ' ' * extras + '\b' * extras)) _last_msg = _msg _have_status_output = True _chunk = _in.read(_chunk_size) if not _chunk: break _out.write(_chunk) _have += len(_chunk) log.stdout_raw('\n\r') except: if _have_status_output: log.stdout_raw('\n\r') raise except IOError as err: log.notice('download: %s: error: %s' % (_sensible_url(_url), str(err))) if path.exists(local): os.remove(path.host(local)) failed = True except ValueError as err: log.notice('download: %s: error: %s' % (_sensible_url(_url), str(err))) if path.exists(local): os.remove(path.host(local)) failed = True except: msg = 'download: %s: error' % (_sensible_url(_url)) log.stderr(msg) log.notice(msg) if _in is not None: _in.close() if _out is not None: _out.close() raise if _out is not None: _out.close() if _in is not None: _in.close() del _in if not failed: if not path.isfile(local): raise error.general('source is not a file: %s' % (path.host(local))) if not _hash_check(path.basename(local), local, config.macros, False): raise error.general('checksum failure file: %s' % (dst)) return not failed
except: msg = 'download: %s: error' % (url) log.stderr(msg) log.notice(msg) if _in is not None: _in.close() if _out is not None: _out.close() raise if _out is not None: _out.close() if _in is not None: _in.close() del _in if not failed: if not path.isfile(local): raise error.general('source is not a file: %s' % (path.host(local))) if not _hash_check(path.basename(local), local, config.macros, False): raise error.general('checksum failure file: %s' % (dst)) return not failed def _git_downloader(url, local, config, opts): repo = git.repo(local, opts, config.macros) rlp = os.path.relpath(path.host(local)) us = url.split('?') # # Handle the various git protocols. #
from googlevoice import Voice, utilfrom os import path, removefrom unittest import TestCase, main class VoiceTest(TestCase): voice = Voice() voice.login() outgoing = util.input('Outgoing number (blank to ignore call tests): ') forwarding = None if outgoing: forwarding = util.input('Forwarding number [optional]: ') or None if outgoing: def test_1call(self): self.voice.call(self.outgoing, self.forwarding) def test_sms(self): self.voice.send_sms(self.outgoing, 'i sms u') def test_2cancel(self): self.voice.cancel(self.outgoing, self.forwarding) def test_special(self): self.assert_(self.voice.special) def test_inbox(self): self.assert_(self.voice.inbox) def test_balance(self): self.assert_(self.voice.settings['credits']) def test_search(self): self.assert_(len(self.voice.search('joe'))) def test_disable_enable(self): self.voice.phones[0].disable() self.voice.phones[0].enable() def test_download(self): msg = list(self.voice.voicemail.messages)[0] fn = '%s.mp3' % msg.id if path.isfile(fn): remove(fn) self.voice.download(msg) self.assert_(path.isfile(fn)) def test_zlogout(self): self.voice.logout() self.assert_(self.voice.special is None) def test_config(self): from conf import config self.assert_(config.forwardingNumber) self.assert_(str(config.phoneType) in '1237') self.assertEqual(config.get('wtf'), None) if __name__ == '__main__': main()
except: msg = 'download: %s: error' % (url) log.stderr(msg) log.notice(msg) if _in is not None: _in.close() if _out is not None: _out.close() raise if _out is not None: _out.close() if _in is not None: _in.close() del _in if not failed: if not path.isfile(local): raise error.general('source is not a file: %s' % (path.host(local))) if not _hash_check(path.basename(local), local, config.macros, False): raise error.general('checksum failure file: %s' % (dst)) return not failed def _git_downloader(url, local, config, opts): repo = git.repo(local, opts, config.macros) rlp = os.path.relpath(path.host(local)) us = url.split('?') # # Handle the various git protocols. # # remove 'git' from 'git://xxxx/xxxx?protocol=...' # url_base = us[0][len('git'):]
from googlevoice import Voice os import path unittest import TestCase from samples import main class VoiceTest(TestCase): voice = Voice() voice.login() outgoing = util.input('Outgoing number (blank to ignore call tests): ') forwarding = None if outgoing: forwarding = util.input('Forwarding number [optional]: ') or None if outgoing: def test_1call(self): self.voice.call(self.outgoing, self.forwarding) def test_sms(self): self.voice.send_sms(self.outgoing, 'i sms u') def test_2cancel(self): self.voice.cancel(self.outgoing, self.forwarding) def test_special(self): self.assert_(self.voice.special) def test_inbox(self): self.assert_(self.voice.inbox) def test_balance(self): self.assert_(self.voice.settings['credits']) def test_search(self): self.assert_(len(self.voice.search('joe'))) def test_disable_enable(self): self.voice.phones[0].disable() self.voice.phones[0].enable() def test_download(self): msg = list(self.voice.voicemail.messages)[0] fn = '%s.mp3' % msg.id if path.isfile(fn): remove(fn) self.voice.download(msg) self.assert_(path.isfile(fn)) def test_zlogout(self): self.voice.logout() self.assert_(self.voice.special is None) def test_config(self): pass self.assert_(config.forwardingNumber) self.assert_(str(config.phoneType) in '1237') self.assertEqual(config.get('wtf'), None) if __name__ == '__main__': main()