def test_redirect_binary(self): with sh.pipes(sh.cat(__file__)) as cmd: cmd > 'tmp' ls = str(sh.ls('-l tmp')) with sh.pipes(sh.cat(__file__)) as cmd: cmd >> 'tmp' self.assertFalse(ls == str(sh.ls('-l tmp'))) sh.rm('tmp')
def __call__(self, environ, start_response): req = Request(environ) resp = Response() filename = req.path_info.strip('/') lfilename = filename.lower() if not req.path_info.strip('/') or os.path.isdir(filename): if filename: filename = path(filename, 'index.html') else: filename = 'index.html' body = open(filename, 'rb').read() resp.body = body elif os.path.isfile(filename): if req.method.lower() == 'delete': sh.rm(filename + '*', shell=True) resp = exc.HTTPNoContent() return resp(environ, start_response) if req.path_info.endswith('.metadata'): cfg = ConfigObject(filename=filename) if req.method.lower() == 'get': resp.content_type = 'application/json' elif req.method.lower() == 'put': data = json.loads(req.body) cfg.metadata.update(data) cfg.write() metadata = dict(cfg.metadata.items()) metadata.update(tags=cfg.metadata.tags.as_list()) resp.body = json.dumps(metadata) elif req.path_info.endswith('.js'): resp.content_type = 'text/javascript' elif req.path_info.endswith('.json'): resp.content_type = 'application/json' elif req.path_info.endswith('.css'): resp.content_type = 'text/css' elif lfilename.endswith('.jpg'): resp.charset = None resp.content_type = 'image/jpeg' print(filename) if not resp.content_length: resp.app_iter = fd(filename) elif req.path_info.startswith('/delete/'): filename = req.path_info[8:-1] self.delete(filename) resp.status_int = 302 resp.location = '/' + path.dirname(filename) else: resp.body = str(req.path_info) resp.last_modified = datetime.now() return resp(environ, start_response)
def delete(self, filename): if path.isdir(filename): sh.rm('-Rf', filename) > 1 for ext in ('.jpg', '.JPG'): f = path.join(env.photos, filename + ext) if path.isfile(f): sh.rm('-Rf', f) > 1 f += '.metadata' if path.isfile(f): sh.rm('-Rf', f) > 1 os.chdir(env.photos) sh[sys.argv[0]]('update') > 1 os.chdir(env.build_path)
def tearDown(self): sh.rm('-f tmp') sh.rm('-f sudo')
def test_output(self): self.assertEqual(sh.rm('/chut').succeeded, False) self.assertEqual(sh.rm('/chut').failed, True) self.assertTrue(len(sh.rm('/chut').stderr) >= 0)