def unzip_preset(src, dest): tempdir = os.path.dirname(src) if self.verbose: lib.echo("Unpacking %s -> %s" % (src, tempdir)) try: tar = tarfile.open(src) tar.extractall(tempdir) finally: tar.close() # GitHub tars always come with a single directory try: repo = [os.path.join(tempdir, d) for d in os.listdir(tempdir) if os.path.isdir(os.path.join(tempdir, d))][0] except: raise ValueError("%s is not a preset (this is a bug)" % src) if self.verbose: lib.echo("Moving %s -> %s" % (repo, dest)) presets_dir() # Create if it doesn't exist shutil.move(repo, dest) return dest
def pull_preset(repo, preset_dir): repo, tag = repo.split(":", 1) + [None] if not repo.count("/") == 1 or len(repo.split("/")) != 2: raise ValueError("Repository syntax is: " "username/repo or gist/id (not %s)" % repo) is_preset, source = repo_is_preset(repo) if not is_preset: lib.echo("ERROR: %s does not appear to be a preset, " "try --verbose for more information." % repo) sys.exit(lib.USER_ERROR) if source == "gist": url = "https://gist.github.com/%s/download" else: url = "https://api.github.com/repos/%s/tarball" r = get(url % repo, stream=True) tempdir = tempfile.mkdtemp() temppath = "/".join([tempdir, repo.rsplit("/", 1)[-1] + ".tar.gz"]) with open(temppath, "wb") as f: for chunk in r.iter_content(): if not chunk: continue f.write(chunk) f.flush() try: return unzip_preset(temppath, preset_dir) finally: shutil.rmtree(tempdir)
def repl(match): lib.echo("Deprecation warning: The {@ref} syntax is being removed") key = pattern[match.start():match.end()].strip("@{}") if key not in templates: sys.stderr.write("Unresolvable reference: \"%s\"" % key) sys.exit(lib.USER_ERROR) return templates[key]
def what(): """Print current context""" if not self.isactive(): lib.echo("No topic") sys.exit(lib.USER_ERROR) lib.echo(os.environ.get("BE_TOPICS", "This is a bug"))
def github_presets(): """Return remote presets hosted on GitHub""" addr = ("https://raw.githubusercontent.com" "/mottosso/be-presets/master/presets.json") response = get(addr) if response.status_code == 404: lib.echo("Could not connect with preset database") sys.exit(lib.PROGRAM_ERROR) return dict((package["name"], package["repository"]) for package in response.json().get("presets"))
def lambda_handler(event, context): command(['ls','-la', '/opt']) command(['ls','-la', '/opt/extensions']) command(['ls','-la', '/opt/python']) echo('this is layer lib.') return { "statusCode": 200, "body": json.dumps({ "message": "hello world" }), }
def remove_preset(preset): """Physically delete local preset Arguments: preset (str): Name of preset """ preset_dir = os.path.join(presets_dir(), preset) try: shutil.rmtree(preset_dir) except IOError: lib.echo("\"%s\" did not exist" % preset)
def ls(topics): """List contents of current context \b Usage: $ be ls - spiderman - hulk $ be ls spiderman - peter - mjay $ be ls spiderman seq01 - 1000 - 2000 - 2500 Return codes: 0 Normal 2 When insufficient arguments are supplied, or a template is unsupported. """ if self.isactive(): lib.echo("ERROR: Exit current project first") sys.exit(lib.USER_ERROR) # List projects if len(topics) == 0: for project in lib.list_projects(): lib.echo("- %s (project)" % project) sys.exit(lib.NORMAL) # List inventory of project elif len(topics) == 1: for item, binding in lib.list_inventory(project=topics[0]): lib.echo("- %s (%s)" % (item, binding)) sys.exit(lib.NORMAL) # List specific portion of template else: try: for item in lib.list_pattern(topics): lib.echo("- %s" % item) except IndexError as exc: lib.echo(exc) sys.exit(lib.USER_ERROR) sys.exit(lib.NORMAL)
def preset_ls(remote): """List presets \b Usage: $ be preset ls - ad - game - film """ if self.isactive(): lib.echo("ERROR: Exit current project first") sys.exit(lib.USER_ERROR) if remote: presets = _extern.github_presets() else: presets = _extern.local_presets() if not presets: lib.echo("No presets found") sys.exit(lib.NORMAL) for preset in sorted(presets): lib.echo("- %s" % preset) sys.exit(lib.NORMAL)
def preset_find(query): """Find preset from hub \b $ be find mypreset https://github.com/mottosso/be-mypreset.git """ if self.isactive(): lib.echo("ERROR: Exit current project first") sys.exit(lib.USER_ERROR) found = _extern.github_presets().get(query) if found: lib.echo(found) else: lib.echo("Unable to locate preset \"%s\"" % query)
def get(path, **kwargs): """requests.get wrapper""" token = os.environ.get(BE_GITHUB_API_TOKEN) if token: kwargs["headers"] = { "Authorization": "token %s" % token } try: response = requests.get(path, verify=False, **kwargs) if response.status_code == 403: lib.echo("Patience: You can't pull more than 60 " "presets per hour without an API token.\n" "See https://github.com/mottosso/be/wiki" "/advanced#extended-preset-access") sys.exit(lib.USER_ERROR) return response except Exception as e: if self.verbose: lib.echo("ERROR: %s" % e) else: lib.echo("ERROR: Something went wrong. " "See --verbose for more information")
def in_(ctx, topics, yes, as_, enter): """Set the current topics to `topics` Environment: BE_PROJECT: First topic BE_CWD: Current `be` working directory BE_TOPICS: Arguments to `in` BE_DEVELOPMENTDIR: Absolute path to current development directory BE_PROJECTROOT: Absolute path to current project BE_PROJECTSROOT: Absolute path to where projects are located BE_ACTIVE: 0 or 1, indicates an active be environment BE_USER: Current user, overridden with `--as` BE_SCRIPT: User-supplied shell script BE_PYTHON: User-supplied python script BE_ENTER: 0 or 1 depending on whether the topic was entered BE_GITHUB_API_TOKEN: Optional GitHub API token BE_ENVIRONMENT: Space-separated list of user-added environment variables BE_TEMPDIR: Directory in which temporary files are stored BE_PRESETSDIR: Directory in which presets are searched BE_ALIASDIR: Directory in which aliases are written BE_BINDING: Binding between template and item in inventory \b Usage: $ be in project topics """ topics = map(str, topics) # They enter as unicode if self.isactive(): lib.echo("ERROR: Exit current project first") sys.exit(lib.USER_ERROR) # Determine topic syntax if len(topics[0].split("/")) == 3: topic_syntax = lib.FIXED project = topics[0].split("/")[0] else: topic_syntax = lib.POSITIONAL project = topics[0] project_dir = _format.project_dir(_extern.cwd(), project) if not os.path.exists(project_dir): lib.echo("Project \"%s\" not found. " % project) lib.echo("\nAvailable:") ctx.invoke(ls) sys.exit(lib.USER_ERROR) # Boot up context = lib.context(project) be = _extern.load_be(project) templates = _extern.load_templates(project) inventory = _extern.load_inventory(project) context.update({ "BE_PROJECT": project, "BE_USER": str(as_), "BE_ENTER": "1" if enter else "", "BE_TOPICS": " ".join(topics) }) # Remap topic syntax, for backwards compatibility # In cases where the topic is entered in a way that # differs from the template, remap topic to template. if any(re.findall("{\d+}", pattern) for pattern in templates.values()): template_syntax = lib.POSITIONAL else: template_syntax = lib.FIXED if topic_syntax & lib.POSITIONAL and not template_syntax & lib.POSITIONAL: topics = ["/".join(topics)] if topic_syntax & lib.FIXED and not template_syntax & lib.FIXED: topics[:] = topics[0].split("/") try: key = be.get("templates", {}).get("key") or "{1}" item = _format.item_from_topics(key, topics) binding = _format.binding_from_item(inventory, item) context["BE_BINDING"] = binding except IndexError as exc: lib.echo("At least %s topics are required" % str(exc)) sys.exit(lib.USER_ERROR) except KeyError as exc: lib.echo("\"%s\" not found" % item) if exc.bindings: lib.echo("\nAvailable:") for item_ in sorted(exc.bindings, key=lambda a: (exc.bindings[a], a)): lib.echo("- %s (%s)" % (item_, exc.bindings[item_])) sys.exit(lib.USER_ERROR) # Finally, determine a development directory # based on the template-, not topic-syntax. if template_syntax & lib.POSITIONAL: try: development_dir = _format.pos_development_directory( templates=templates, inventory=inventory, context=context, topics=topics, user=as_, item=item) except KeyError as exc: lib.echo("\"%s\" not found" % item) if exc.bindings: lib.echo("\nAvailable:") for item_ in sorted(exc.bindings, key=lambda a: (exc.bindings[a], a)): lib.echo("- %s (%s)" % (item_, exc.bindings[item_])) sys.exit(lib.USER_ERROR) else: # FIXED topic_syntax development_dir = _format.fixed_development_directory( templates, inventory, topics, as_) context["BE_DEVELOPMENTDIR"] = development_dir tempdir = (tempfile.mkdtemp() if not os.environ.get("BE_TEMPDIR") else os.environ["BE_TEMPDIR"]) context["BE_TEMPDIR"] = tempdir # Should it be entered? if enter and not os.path.exists(development_dir): create = False if yes: create = True else: sys.stdout.write("No development directory found. Create? [Y/n]: ") if raw_input().lower() in ("", "y", "yes"): create = True if create: ctx.invoke(mkdir, dir=development_dir) else: sys.stdout.write("Cancelled") sys.exit(lib.NORMAL) # Parse be.yaml if "script" in be: context["BE_SCRIPT"] = _extern.write_script( be["script"], tempdir) if "python" in be: script = "\n".join(be["python"]) context["BE_PYTHON"] = script try: exec script in {"__name__": __name__} except Exception as e: lib.echo("ERROR: %s" % e) invalids = [v for v in context.values() if not isinstance(v, str)] assert all(isinstance(v, str) for v in context.values()), invalids # Create aliases aliases_dir = _extern.write_aliases( be.get("alias", {}), tempdir) context["PATH"] = (aliases_dir + os.pathsep + context.get("PATH", "")) context["BE_ALIASDIR"] = aliases_dir # Parse redirects _format.parse_redirect( be.get("redirect", {}), topics, context) # Override inherited context # with that coming from be.yaml. if "environment" in be: parsed = _format.parse_environment( fields=be["environment"], context=context, topics=topics) context["BE_ENVIRONMENT"] = " ".join(parsed.keys()) context.update(parsed) if "BE_TESTING" in context: os.chdir(development_dir) os.environ.update(context) else: parent = lib.parent() cmd = lib.cmd(parent) # Store reference to calling shell context["BE_SHELL"] = parent try: sys.exit(subprocess.call(cmd, env=context)) finally: import shutil shutil.rmtree(tempdir)
import os try: dbDir = os.environ['LXR_DATA_DIR'] except KeyError: print(argv[0] + ': LXR_DATA_DIR needs to be set') exit(1) db = data.DB(dbDir, readonly=True) cmd = argv[1] if cmd == 'versions': for p in scriptLines('list-tags', '-r'): if db.vers.exists(p): echo(p + b'\n') elif cmd == 'type': version = argv[2] path = argv[3] p = script('get-type', version, path) echo(p) elif cmd == 'dir': version = argv[2] path = argv[3] p = script('get-dir', version, path) echo(p) elif cmd == 'file': version = argv[2]
def dump(): """Print current environment Environment is outputted in a YAML-friendly format \b Usage: $ be dump Prefixed: - BE_PROJECT=spiderman - BE_ITEM=peter - BE_TYPE=model - ... """ if not self.isactive(): lib.echo("ERROR: Enter a project first") sys.exit(lib.USER_ERROR) # Print custom environment variables first custom = sorted(os.environ.get("BE_ENVIRONMENT", "").split()) if custom: lib.echo("Custom:") for key in custom: lib.echo("- %s=%s" % (key, os.environ.get(key))) # Then print redirected variables project = os.environ["BE_PROJECT"] root = os.environ["BE_PROJECTSROOT"] be = _extern.load(project, "be", optional=True, root=root) redirect = be.get("redirect", {}).items() if redirect: lib.echo("\nRedirect:") for map_source, map_dest in sorted(redirect): lib.echo("- %s=%s" % (map_dest, os.environ.get(map_dest))) # And then everything else prefixed = dict((k, v) for k, v in os.environ.iteritems() if k.startswith("BE_")) if prefixed: lib.echo("\nPrefixed:") for key in sorted(prefixed): if not key.startswith("BE_"): continue lib.echo("- %s=%s" % (key, os.environ.get(key))) sys.exit(lib.NORMAL)
def update(preset, clean): """Update a local preset This command will cause `be` to pull a preset already available locally. \b Usage: $ be update ad Updating "ad".. """ if self.isactive(): lib.echo("ERROR: Exit current project first") sys.exit(lib.USER_ERROR) presets = _extern.github_presets() if preset not in presets: sys.stdout.write("\"%s\" not found" % preset) sys.exit(lib.USER_ERROR) lib.echo("Are you sure you want to update \"%s\", " "any changes will be lost?: [y/N]: ", newline=False) if raw_input().lower() in ("y", "yes"): presets_dir = _extern.presets_dir() preset_dir = os.path.join(presets_dir, preset) repository = presets[preset] if clean: try: _extern.remove_preset(preset) except: lib.echo("ERROR: Could not clean existing preset") sys.exit(lib.USER_ERROR) lib.echo("Updating %s.. " % repository) try: _extern.pull_preset(repository, preset_dir) except IOError as e: lib.echo(e) sys.exit(lib.USER_ERROR) else: lib.echo("Cancelled")
def new(preset, name, silent, update): """Create new default preset \b Usage: $ be new ad "blue_unicorn" created $ be new film --name spiderman "spiderman" created """ if self.isactive(): lib.echo("Please exit current preset before starting a new") sys.exit(lib.USER_ERROR) if not name: count = 0 name = lib.random_name() while name in _extern.projects(): if count > 10: lib.echo("ERROR: Couldn't come up with a unique name :(") sys.exit(lib.USER_ERROR) name = lib.random_name() count += 1 project_dir = _format.project_dir(_extern.cwd(), name) if os.path.exists(project_dir): lib.echo("\"%s\" already exists" % name) sys.exit(lib.USER_ERROR) username, preset = ([None] + preset.split("/", 1))[-2:] presets_dir = _extern.presets_dir() preset_dir = os.path.join(presets_dir, preset) # Is the preset given referring to a repository directly? relative = False if username else True try: if not update and preset in _extern.local_presets(): _extern.copy_preset(preset_dir, project_dir) else: lib.echo("Finding preset for \"%s\".. " % preset, silent) time.sleep(1 if silent else 0) if relative: # Preset is relative, look it up from the Hub presets = _extern.github_presets() if preset not in presets: sys.stdout.write("\"%s\" not found" % preset) sys.exit(lib.USER_ERROR) time.sleep(1 if silent else 0) repository = presets[preset] else: # Absolute paths are pulled directly repository = username + "/" + preset lib.echo("Pulling %s.. " % repository, silent) repository = _extern.fetch_release(repository) # Remove existing preset if preset in _extern.local_presets(): _extern.remove_preset(preset) try: _extern.pull_preset(repository, preset_dir) except IOError as e: lib.echo("ERROR: Sorry, something went wrong.\n" "Use be --verbose for more") lib.echo(e) sys.exit(lib.USER_ERROR) try: _extern.copy_preset(preset_dir, project_dir) finally: # Absolute paths are not stored locally if not relative: _extern.remove_preset(preset) except IOError as exc: if self.verbose: lib.echo("ERROR: %s" % exc) else: lib.echo("ERROR: Could not write, do you have permission?") sys.exit(lib.PROGRAM_ERROR) lib.echo("\"%s\" created" % name, silent)
# coding:utf-8 # Python Override Existed Module Method, monkey patch... # This action must in main.py import lib_lib def echo2(): print 'after be override by lambda' from lib import echo echo() # 就可以 Override Global 的 Module Method, 路徑要完整 # 不能使用: # from lib_lib import do_in_echo # # do_in_echo = lambda: echo2() # # 這樣他不會改到那個 Module 的物件。只會改道 Local Function Ref, 只有在這個 Main 有效。 lib_lib.do_in_echo = lambda: echo2() from lib import echo, echo3, echoX echo() echo3()
# coding:utf-8 # Python Override Existed Module Method, monkey patch... # This action must in main.py import lib_lib def echo2(): print 'after be override by lambda' from lib import echo echo() # 就可以 Override Global 的 Module Method, 路徑要完整 # 不能使用: # from lib_lib import do_in_echo # # do_in_echo = lambda: echo2() # # 這樣他不會改到那個 Module 的物件。只會改道 Local Function Ref, 只有在這個 Main 有效。 lib_lib.do_in_echo = lambda: echo2() from lib import echo, echo3, echoX echo() echo3() def _echo3(self, data): print self, data, 'after override class method'