def test_base_methods(): path_1 = path("/tmp/file with spaces").replace(" ", "-") path_2 = path("smallfile").upper() assert isinstance(path_1, path) assert path_1 == '/tmp/file-with-spaces' assert isinstance(path_2, path) assert path_2 == 'SMALLFILE'
def test_unlink(): target = os.path.join('xxx', 'symlink') os.symlink( os.path.realpath(file_1_path), target ) assert path(target).exists path(target).unlink() assert not path(target).exists
def create(self, args): if len(args.all) > 2: template = args.all[2] else: template = 'basic' if len(args.all) > 1: name = args.all[1] (path(__file__).dir() / 'templates' / template).cp( path(os.getcwd()) / name, r=True) else: print "name required"
def create(self, args): if len(args.all) > 2: template = args.all[2] else: template = 'basic' if len(args.all) > 1: name = args.all[1] (path(__file__).dir() / 'templates' / template).cp( path(os.getcwd()) / name, r=True ) else: print "name required"
def test_ls_dirs(): dir_content = path(root).ls_dirs() assert len(dir_content) == len(dir_list) assert set(dir_list).issubset(dir_content) assert not set(file_list).issubset(dir_content)
def test__div__(): joined_path = path(root) / path(file_1) assert joined_path.exists joined_path = path(root) / file_1 assert joined_path.exists joined_path = path(root) / path(file_1) / path("xxx") assert not joined_path.exists joined_path = path(root) / path(file_1) / "xxx" assert not joined_path.exists
def test_rm(): path(file_1_path).rm() assert not os.path.exists(file_1_path) path(dir_1_path).rm() assert not os.path.exists(dir_1_path) file_location = os.path.join(dir_2_path, 'xxx') open(file_location, "w") with pytest.raises(OSError): path(dir_2_path).rm() assert os.path.exists(dir_2_path) path(dir_2_path).rm(r=True) assert not os.path.exists(dir_2_path)
def _watch(self): old_hash = "" while True: hash_elements = [] for f in path(self.BASE_DIR).walk(r=True): if not any([fnmatch.fnmatch(f.relative(self.BASE_DIR), pattern) for pattern in self.EXCLUDE]): hash_elements.append(f.m_datetime.isoformat()) current_hash = hashlib.md5("".join(hash_elements)).hexdigest() if current_hash != old_hash: old_hash = current_hash self._build() time.sleep(0.2)
def _watch(self): old_hash = "" while True: hash_elements = [] for f in path(self.BASE_DIR).walk(r=True): if not any([ fnmatch.fnmatch(f.relative(self.BASE_DIR), pattern) for pattern in self.EXCLUDE ]): hash_elements.append(f.m_datetime.isoformat()) current_hash = hashlib.md5("".join(hash_elements)).hexdigest() if current_hash != old_hash: old_hash = current_hash self._build() time.sleep(0.2)
def test_is_file(): assert path(file_1_path).is_file() assert path(file_2_path).is_file() assert not path(dir_1_path).is_file() assert not path(dir_2_path).is_file()
def test_absolute(): assert os.path.abspath(file_1_path) == path(file_1_path).absolute
def template(self, template_name): env = Environment(loader=FileSystemLoader(self.TEMPLATES_DIR)) return env.get_template(path(template_name))
def render(self, value): for v in value: path(v).cp(path(self.klass.BUILD_DIR) / v, r=True)
def render(self, value): for name, content in value: self.ensure_path(name) self.write(path(self.klass.BUILD_DIR) / name, content)
def _clean_build_dir(self): self.BUILD_DIR = path(self.BUILD_DIR) if self.BUILD_DIR.exists: [element.rm(r=True) for element in self.BUILD_DIR] else: self.BUILD_DIR.mkdir()
def test_touch(): path_string = os.path.join(root, "test") path(path_string).touch() assert os.path.exists(path_string) assert os.path.isfile(path_string)
def test_cp(): file_copy = path(file_1_path).cp(os.path.join(root, 'file_copy')) assert os.path.exists(file_copy) dir_copy = path(dir_1_path).cp(os.path.join(root, 'dir_copy')) assert os.path.exists(dir_copy)
def test_mkdir_p(): path_string = os.path.join(root, "level1", "level2") path(path_string).mkdir(p=True) assert os.path.exists(path_string) assert os.path.isdir(path_string)
def test_ln_s(): symlink = path(file_1_path).ln(os.path.join('xxx', 'symlink')) assert symlink.exists assert symlink.is_link()
def test_ln_hard(): symlink = path(file_1_path).ln(os.path.join('xxx', 'symlink'), s=False) assert symlink.exists assert not symlink.is_link()
def test_iter(): dir_content = path(root) assert len([e for e in dir_content]) == len(dir_list + file_list)
def test_mkdir(): path_string = os.path.join(root, "test") path(path_string).mkdir() assert os.path.exists(path_string) assert os.path.isdir(path_string)
def ensure_path(self, name): directory = path(name).dir() directory_build = path(self.klass.BUILD_DIR) / directory if directory and not directory_build.exists: directory_build.mkdir(p=True)
def test_path(): assert path(root, file_1).exists assert not path(root, file_1, 'xxxss').exists
def render(self, value): for name, template, context in value: self.ensure_path(name) content = self.klass.template(template).render(**context) self.write(path(self.klass.BUILD_DIR) / name, content)
def test_exists(): assert path(dir_1_path).exists assert path(file_1_path).exists
class Base(object): BASE_DIR = path(os.getcwd()) BUILD_DIR = '_build' TEMPLATES_DIR = 'templates' SERVER_PORT = 8000 EXCLUDE = ['.git*', '*.py', '*.pyc', "%s/*" % BUILD_DIR, BUILD_DIR] def __init__(self): render.render.klass = self arg_0 = args.all[0] if arg_0 not in command.register: print "No such command '%s'" % arg_0 else: getattr(self, arg_0)(args) def template(self, template_name): env = Environment(loader=FileSystemLoader(self.TEMPLATES_DIR)) return env.get_template(path(template_name)) def render_template(self, template_name, **context): return self.template(template_name).render(**context) def _clean_memoized(self): for m in memoized.register: m.cache = {} def _clean_build_dir(self): self.BUILD_DIR = path(self.BUILD_DIR) if self.BUILD_DIR.exists: [element.rm(r=True) for element in self.BUILD_DIR] else: self.BUILD_DIR.mkdir() def _build(self): self._clean_memoized() self._clean_build_dir() for name in render.register: getattr(self, name)() print "Generated %s" % datetime.now() def _watch(self): old_hash = "" while True: hash_elements = [] for f in path(self.BASE_DIR).walk(r=True): if not any([ fnmatch.fnmatch(f.relative(self.BASE_DIR), pattern) for pattern in self.EXCLUDE ]): hash_elements.append(f.m_datetime.isoformat()) current_hash = hashlib.md5("".join(hash_elements)).hexdigest() if current_hash != old_hash: old_hash = current_hash self._build() time.sleep(0.2) def _server(self): self._build() os.chdir(self.BUILD_DIR) Handler = SimpleHTTPServer.SimpleHTTPRequestHandler SocketServer.ThreadingTCPServer.allow_reuse_address = True httpd = SocketServer.ThreadingTCPServer(("", self.SERVER_PORT), Handler) print "serving at port %s" % self.SERVER_PORT httpd.serve_forever() @command def build(self, args): self._build() @command def watch(self, args): self._watch() @command def server(self, args): server = Process(target=self._server) server.start() try: if '--no-watch' not in args.flags: self._watch() except KeyboardInterrupt: server.terminate() @command def create(self, args): if len(args.all) > 2: template = args.all[2] else: template = 'basic' if len(args.all) > 1: name = args.all[1] (path(__file__).dir() / 'templates' / template).cp( path(os.getcwd()) / name, r=True) else: print "name required"
# -*- coding: utf-8 -*- from setuptools import setup, find_packages from osome import path description = "The bucket of python shell helpers, no dependencies, simple API." project = path(__file__).dir() long_description = (project / 'README.rst').open("r").read() version = (project / 'VERSION').open("r").read() license = (project / 'LICENSE').open("r").read() setup(name='osome', version=version, packages=find_packages(), author='Sebastian Pawluś', author_email='*****@*****.**', url='https://github.com/xando/osome', description=description, keywords="shell tools shell path ", license=license, long_description=long_description, include_package_data=True, platforms=['any'], classifiers=[ 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'Natural Language :: English', 'License :: OSI Approved :: BSD License', 'Operating System :: OS Independent',