def test_mv(self): "Simple case" one = self.tpath + 'one.txt' one << 'Contentz' target = str(self.tpath) + '/two.txt' self.assertFalse(os.path.exists(target)) nix.mv(str(one), target) two = self.tpath + 'two.txt' self.assertTrue(os.path.exists(target)) self.assertEqual('Contentz', two.contents)
def test_mv_target_exists(self): "Should just overwrite" one = Path(str(self.tpath) + '//one.txt') two = Path(str(self.tpath) + '//two.txt') one << 'Contentz' two << 'Contentz Two' target = str(two) self.assertTrue(os.path.exists(target)) nix.mv(str(one), target) self.assertTrue(os.path.exists(target)) self.assertFalse(os.path.exists(str(one))) self.assertEqual('Contentz', two.contents)
def start_plugin(name, USERLAND): name = name write('Bootstrapping "{0}" - your new Opal plugin...'.format(name)) if 'opal' in name: reponame = name name = name.replace('opal-', '') else: reponame = 'opal-{0}'.format(name) root = USERLAND / reponame # 1. Copy across scaffold nix.cp_r(PLUGIN_SCAFFOLD, root) # 2n. Interpolate scaffold interpolate_dir(root, name=name, version=opal.__version__) # 3. Rename the code dir code_root = root / name nix.mv(root / 'app', code_root) # 4. Create some extra directories. create_lookuplists(code_root) templates = code_root / 'templates' templates.mkdir() static = code_root / 'static' static.mkdir() jsdir = static / 'js/{0}'.format(name) jsdir.mkdir() cssdir = static / 'css' cssdir.mkdir() controllers = jsdir / 'controllers' controllers.mkdir() services = jsdir / 'services' services.mkdir() # 5. Initialize git repo call_if_exists( ('git', 'init'), 'Unable to locate git; Skipping git repository initialization.', cwd=root, stdout=subprocess.PIPE) write('Plugin complete at {0}'.format(reponame)) return
def start_plugin(name, USERLAND): name = name write('Bootstrapping "{0}" - your new Opal plugin...'.format(name)) if 'opal' in name: reponame = name name = name.replace('opal-', '') else: reponame = 'opal-{0}'.format(name) root = USERLAND/reponame # 1. Copy across scaffold nix.cp_r(PLUGIN_SCAFFOLD, root) # 2n. Interpolate scaffold interpolate_dir(root, name=name, version=opal.__version__) # 3. Rename the code dir code_root = root/name nix.mv(root/'app', code_root) # 4. Create some extra directories. create_lookuplists(code_root) templates = code_root/'templates' templates.mkdir() static = code_root/'static' static.mkdir() jsdir = static/'js/{0}'.format(name) jsdir.mkdir() cssdir = static/'css' cssdir.mkdir() controllers = jsdir/'controllers' controllers.mkdir() services = jsdir/'services' services.mkdir() # 5. Initialize git repo call_if_exists( ('git', 'init'), 'Unable to locate git; Skipping git repository initialization.', cwd=root, stdout=subprocess.PIPE ) write('Plugin complete at {0}'.format(reponame)) return
def start_plugin(name, USERLAND): name = name write('Bootstrapping "{0}" - your new Opal plugin...'.format(name)) if 'opal' in name: reponame = name name = name.replace('opal-', '') else: reponame = 'opal-{0}'.format(name) root = USERLAND / reponame # 1. Copy across scaffold shutil.copytree(PLUGIN_SCAFFOLD, root) # 2n. Interpolate scaffold interpolate_dir(root, name=name, version=opal.__version__) # 3. Rename the code dir code_root = root / name nix.mv(root / 'app', code_root) # 4. Create some extra directories. create_lookuplists(code_root) templates = code_root / 'templates' templates.mkdir() static = code_root / 'static' static.mkdir() jsdir = static / 'js/{0}'.format(name) jsdir.mkdir() cssdir = static / 'css' cssdir.mkdir() controllers = jsdir / 'controllers' controllers.mkdir() services = jsdir / 'services' services.mkdir() # 5. Initialize git repo os.system('cd {0}; git init'.format(reponame)) write('Plugin complete at {0}'.format(reponame)) return
def start_plugin(name, USERLAND): name = name write('Bootstrapping "{0}" - your new Opal plugin...'.format(name)) if 'opal' in name: reponame = name name = name.replace('opal-', '') else: reponame = 'opal-{0}'.format(name) root = USERLAND/reponame # 1. Copy across scaffold shutil.copytree(PLUGIN_SCAFFOLD, root) # 2n. Interpolate scaffold interpolate_dir(root, name=name, version=opal.__version__) # 3. Rename the code dir code_root = root/name nix.mv(root/'app', code_root) # 4. Create some extra directories. create_lookuplists(code_root) templates = code_root/'templates' templates.mkdir() static = code_root/'static' static.mkdir() jsdir = static/'js/{0}'.format(name) jsdir.mkdir() cssdir = static/'css' cssdir.mkdir() controllers = jsdir/'controllers' controllers.mkdir() services = jsdir/'services' services.mkdir() # 5. Initialize git repo os.system('cd {0}; git init'.format(reponame)) write('Plugin complete at {0}'.format(reponame)) return
def __lshift__(self, contents): """ we overload the << operator to allow us easy file writing according to the following rules: if contents is not a stringtype, raise TypeError. otherwise, treat self like a file and append contents to it. note:: if components of the path leading to self do not exist, they will be created. it is assumed that the user knows their own mind. arguments: - `contents`: stringtype return: None exceptions: TypeError """ if not isinstance(contents, six.string_types): raise TypeError("you have to write with a stringtype larry... ") temp = path.Path.newdir() FOUND = False with zipfile.ZipFile(self._archive, 'r') as rz: with zipfile.ZipFile(temp/'tmp.zip', 'w') as wz: for info in rz.infolist(): content = rz.open(info).read() if info.filename == self._inner_value: FOUND = True content += "\n"+contents wz.writestr(info, content) if not FOUND: wz.writestr(self._inner_value, contents) nix.mv(temp/'tmp.zip', self._archive) nix.rmdir(temp) return
def __lshift__(self, contents): """ we overload the << operator to allow us easy file writing according to the following rules: if contents is not a stringtype, raise TypeError. otherwise, treat self like a file and append contents to it. note:: if components of the path leading to self do not exist, they will be created. it is assumed that the user knows their own mind. arguments: - `contents`: stringtype return: None exceptions: TypeError """ if not isinstance(contents, six.string_types): raise TypeError("you have to write with a stringtype larry... ") temp = path.Path.newdir() FOUND = False with zipfile.ZipFile(self._archive, 'r') as rz: with zipfile.ZipFile(temp / 'tmp.zip', 'w') as wz: for info in rz.infolist(): content = rz.open(info).read() if info.filename == self._inner_value: FOUND = True content += "\n" + contents wz.writestr(info, content) if not FOUND: wz.writestr(self._inner_value, contents) nix.mv(temp / 'tmp.zip', self._archive) nix.rmdir(temp) return
def startproject(args, USERLAND_HERE): """ In which we perform the steps required to start a new OPAL project. 1. Run Django' Startproject 2. Create a data/lookuplists dir 3. Copy across the scaffolding directory 4. Interpolate our project data into the templates. 5. Swap our scaffold app with the Django created app 6. Interpolate the code templates from our scaffold app 7. Create extra directories we need 8. Run Django's migrations 9. Create a superuser 10. Initialise our git repo """ name = args.name project_dir = USERLAND_HERE / name if project_dir: write("\n\nDirectory {0} already exists !".format(project_dir)) write("Please remove it or choose a new name.\n\n") sys.exit(1) # 1. Run Django Startproject write("Creating project dir at {0}".format(project_dir)) os.system('django-admin.py startproject {0}'.format(name)) write("Bootstrapping your OPAL project...") # 2. Create empty directories lookuplists = project_dir / 'data/lookuplists' lookuplists.mkdir() # 3. Copy across the scaffold with SCAFFOLD: for p in SCAFFOLD.ls(): target = project_dir / p[-1] p.cp(target) # Dotfiles need their dot back gitignore = project_dir / 'gitignore' gitignore.mv(project_dir / '.gitignore') # 3. Interpolate the project data interpolate_dir(project_dir, name=name, secret_key=get_random_secret_key()) app_dir = project_dir / name # 5. Django Startproject creates some things - let's kill them & # replace with our own things. nix.rm(app_dir, recursive=True, force=True) nix.mv(project_dir / 'app', app_dir) # 7. Create extra directories we need js = app_dir / 'static/js/{0}'.format(name) css = app_dir / 'static/css' js.mkdir() css.mkdir() nix.mv(app_dir / 'static/js/app/routes.js', app_dir / 'static/js/{0}/routes.js'.format(name)) nix.mv(app_dir / 'static/js/app/flow.js', app_dir / 'static/js/{0}/flow.js'.format(name)) templates = app_dir / 'templates' / name templates.mkdir() assets = app_dir / 'assets' assets.mkdir() assets_explainer = assets / 'README.md' assets_explainer << """ This placeholder file is here to ensure that there we still have our STATICFILES_DIRS target if we commit generated code to source control. This means that we can run collectstatic OK. """ # We have this here because it uses name from above. def manage(command): args = ['python', '{0}/manage.py'.format(name)] args += command.split() args.append('--traceback') try: subprocess.check_call(args) except subprocess.CalledProcessError: sys.exit(1) return # 8. Run Django's migrations write('Creating Database') manage('makemigrations {0}'.format(name)) manage('migrate') # 9. Create a superuser sys.path.append(os.path.join(os.path.abspath('.'), name)) _set_settings_module(name) from django.contrib.auth.models import User user = User(username='******') user.set_password('super1') user.is_superuser = True user.is_staff = True user.save() from opal.models import UserProfile profile, _ = UserProfile.objects.get_or_create(user=user) profile.force_password_change = False profile.save() # 11. Initialise git repo os.system('cd {0}; git init'.format(name))
def start_project(name, USERLAND_HERE): """ In which we perform the steps required to start a new Opal project. 1. Run Django' Startproject 2. Create a data/lookuplists dir 3. Copy across the scaffolding directory 4. Interpolate our project data into the templates. 5. Swap our scaffold app with the Django created app 6. Interpolate the code templates from our scaffold app 7. Create extra directories we need 8. Run Django's migrations 9. Create a superuser 10. Initialise our git repo 11. Load referencedata shipped with Opal """ project_dir = USERLAND_HERE / name if project_dir: write("\n\nDirectory {0} already exists !".format(project_dir)) write("Please remove it or choose a new name.\n\n") sys.exit(1) write("Bootstrapping your Opal project...") # 1. Run Django Startproject write("Creating project dir at {0}".format(project_dir)) project_dir.mkdir() management.call_command('startproject', name, project_dir) # 3. Copy across the scaffold with SCAFFOLD: for p in SCAFFOLD.ls(): target = project_dir / p[-1] p.cp(target) # Dotfiles need their dot back gitignore = project_dir / 'gitignore' gitignore.mv(project_dir / '.gitignore') # 4. Interpolate the project data interpolate_dir(project_dir, name=name, secret_key=get_random_secret_key(), version=opal.__version__) app_dir = project_dir / name # 5. Django Startproject creates some things - let's kill them & # replace with our own things. nix.rm(app_dir, recursive=True, force=True) nix.mv(project_dir / 'app', app_dir) # 7. Create extra directories we need js = app_dir / 'static/js/{0}'.format(name) css = app_dir / 'static/css' js.mkdir() css.mkdir() nix.mv(app_dir / 'static/js/app/routes.js', app_dir / 'static/js/{0}/routes.js'.format(name)) nix.mv(app_dir / 'static/js/app/flow.js', app_dir / 'static/js/{0}/flow.js'.format(name)) templates = app_dir / 'templates' / name templates.mkdir() assets = app_dir / 'assets' assets.mkdir() assets_explainer = assets / 'README.md' assets_explainer << """ This placeholder file is here to ensure that there we still have our STATICFILES_DIRS target if we commit generated code to source control. This means that we can run collectstatic OK. """ # 2. Create lookup lists create_lookuplists(app_dir) # We have this here because it uses name from above. def manage(command): args = ['python', os.path.join(name, 'manage.py')] args += command.split() args.append('--traceback') call(args) # 8. Run Django's migrations write('Creating Database') manage('makemigrations {0}'.format(name)) manage('migrate') # 9. Create a superuser write('Creating superuser') manage('createopalsuperuser') # 10. Initialise git repo call_if_exists( ('git', 'init'), 'Unable to locate git; Skipping git repository initialization.', cwd=project_dir, stdout=subprocess.PIPE) # 11. Load referencedata shipped with Opal manage('load_lookup_lists')
def mv(self, resource, target): return nix.mv(resource, target)
def start_project(name, USERLAND_HERE): """ In which we perform the steps required to start a new Opal project. 1. Run Django' Startproject 2. Create a data/lookuplists dir 3. Copy across the scaffolding directory 4. Interpolate our project data into the templates. 5. Swap our scaffold app with the Django created app 6. Interpolate the code templates from our scaffold app 7. Create extra directories we need 8. Run Django's migrations 9. Create a superuser 10. Initialise our git repo """ project_dir = USERLAND_HERE/name if project_dir: write("\n\nDirectory {0} already exists !".format(project_dir)) write("Please remove it or choose a new name.\n\n") sys.exit(1) # 1. Run Django Startproject write("Creating project dir at {0}".format(project_dir)) os.system('django-admin.py startproject {0}'.format(name)) write("Bootstrapping your Opal project...") if not project_dir: project_dir.mkdir() # Copy across the scaffold with SCAFFOLD: for p in SCAFFOLD.ls(): target = project_dir/p[-1] p.cp(target) # Dotfiles need their dot back gitignore = project_dir/'gitignore' gitignore.mv(project_dir/'.gitignore') # Interpolate the project data interpolate_dir(project_dir, name=name, secret_key=get_random_secret_key(), version=opal.__version__) app_dir = project_dir/name # Django Startproject creates some things - let's kill them & # replace with our own things. nix.rm(app_dir, recursive=True, force=True) nix.mv(project_dir/'app', app_dir) # Create extra directories we need js = app_dir/'static/js/{0}'.format(name) css = app_dir/'static/css' js.mkdir() css.mkdir() nix.mv(app_dir/'static/js/app/routes.js', app_dir/'static/js/{0}/routes.js'.format(name)) nix.mv(app_dir/'static/js/app/flow.js', app_dir/'static/js/{0}/flow.js'.format(name)) templates = app_dir/'templates'/name templates.mkdir() assets = app_dir/'assets' assets.mkdir() assets_explainer = assets/'README.md' assets_explainer << """ This placeholder file is here to ensure that there we still have our STATICFILES_DIRS target if we commit generated code to source control. This means that we can run collectstatic OK. """ # Create lookup lists create_lookuplists(app_dir) # We have this here because it uses name from above. def manage(command): args = ['python', '{0}/manage.py'.format(name)] args += command.split() args.append('--traceback') try: subprocess.check_call(args) except subprocess.CalledProcessError: sys.exit(1) return # 8. Run Django's migrations write('Creating Database') manage('makemigrations {0}'.format(name)) manage('migrate') # 9. Create a superuser sys.path.append(os.path.join(os.path.abspath('.'), name)) _set_settings_module(name) from django.contrib.auth.models import User user = User(username='******') user.set_password('super1') user.is_superuser = True user.is_staff = True user.save() from opal.models import UserProfile profile, _ = UserProfile.objects.get_or_create(user=user) profile.force_password_change = False profile.save() # 11. Initialise git repo os.system('cd {0}; git init'.format(name))
def start_project(name, USERLAND_HERE): """ In which we perform the steps required to start a new Opal project. 1. Run Django' Startproject 2. Create a data/lookuplists dir 3. Copy across the scaffolding directory 4. Interpolate our project data into the templates. 5. Swap our scaffold app with the Django created app 6. Interpolate the code templates from our scaffold app 7. Create extra directories we need 8. Run Django's migrations 9. Create a superuser 10. Initialise our git repo 11. Load referencedata shipped with Opal """ project_dir = USERLAND_HERE/name if project_dir: write("\n\nDirectory {0} already exists !".format(project_dir)) write("Please remove it or choose a new name.\n\n") sys.exit(1) write("Bootstrapping your Opal project...") # 1. Run Django Startproject write("Creating project dir at {0}".format(project_dir)) project_dir.mkdir() management.call_command('startproject', name, project_dir) # 3. Copy across the scaffold with SCAFFOLD: for p in SCAFFOLD.ls(): target = project_dir/p[-1] p.cp(target) # Dotfiles need their dot back gitignore = project_dir/'gitignore' gitignore.mv(project_dir/'.gitignore') # 4. Interpolate the project data interpolate_dir(project_dir, name=name, secret_key=get_random_secret_key(), version=opal.__version__) app_dir = project_dir/name # 5. Django Startproject creates some things - let's kill them & # replace with our own things. nix.rm(app_dir, recursive=True, force=True) nix.mv(project_dir/'app', app_dir) # 7. Create extra directories we need js = app_dir/'static/js/{0}'.format(name) css = app_dir/'static/css' js.mkdir() css.mkdir() nix.mv(app_dir/'static/js/app/routes.js', app_dir/'static/js/{0}/routes.js'.format(name)) templates = app_dir/'templates'/name templates.mkdir() assets = app_dir/'assets' assets.mkdir() assets_explainer = assets/'README.md' assets_explainer << """ This placeholder file is here to ensure that there we still have our STATICFILES_DIRS target if we commit generated code to source control. This means that we can run collectstatic OK. """ # 2. Create lookup lists create_lookuplists(app_dir) # We have this here because it uses name from above. def manage(command): args = ['python', os.path.join(name, 'manage.py')] args += command.split() args.append('--traceback') call(args) # 8. Run Django's migrations write('Creating Database') manage('makemigrations {0}'.format(name)) manage('migrate') # 9. Create a superuser write('Creating superuser') manage('createopalsuperuser') # 10. Initialise git repo call_if_exists( ('git', 'init'), 'Unable to locate git; Skipping git repository initialization.', cwd=project_dir, stdout=subprocess.PIPE ) # 11. Load referencedata shipped with Opal manage('load_lookup_lists')