Example #1
0
def command(args):
    import sys

    from sr.tools.inventory import assetcode
    from sr.tools.inventory.inventory import get_inventory

    inv = get_inventory()

    parts = []
    for c in args.part_code:
        code = assetcode.normalise(c)

        try:
            assetcode.code_to_num(code)
        except:
            print("Error: %s is an invalid code." % code, file=sys.stderr)
            sys.exit(1)

        try:
            part = inv.root.parts[code]
        except KeyError:
            print("Error: There is no part with code %s." % code,
                  file=sys.stderr)
            sys.exit(1)

        parts.append(part)

    for part in parts:
        print("# item -> parent")

        if hasattr(part.parent, "code"):
            print("%s -> %s" % (part.code, part.parent.code))

        elif hasattr(part.parent, "name"):
            print("%s -> dir(%s)" % (part.code, part.parent.name))
Example #2
0
def command(args):
    import sys

    from sr.tools.inventory import assetcode
    from sr.tools.inventory.inventory import get_inventory
    from sr.tools.environment import open_editor

    inv = get_inventory()

    parts = []
    for c in args.part_code:
        code = assetcode.normalise(c)

        try:
            assetcode.code_to_num(code)
        except:
            print("Error: %s is an invalid code." % code, file=sys.stderr)
            sys.exit(1)

        try:
            part = inv.root.parts[code]
        except KeyError:
            print("Error: There is no part with code %s." %
                  code, file=sys.stderr)
            sys.exit(1)

        parts.append(part)

    for part in parts:
        open_editor(part.path)
Example #3
0
def command(args):
    import sys

    from sr.tools.inventory import assetcode
    from sr.tools.inventory.inventory import get_inventory

    inv = get_inventory()

    parts = []
    for c in args.part_code:
        code = assetcode.normalise(c)

        try:
            assetcode.code_to_num(code)
        except:
            print("Error: %s is an invalid code." % code, file=sys.stderr)
            sys.exit(1)

        try:
            part = inv.root.parts[code]
        except KeyError:
            print("Error: There is no part with code %s." %
                  code, file=sys.stderr)
            sys.exit(1)

        parts.append(part)

    for part in parts:
        print("# item -> parent")

        if hasattr(part.parent, "code"):
            print("%s -> %s" % (part.code, part.parent.code))

        elif hasattr(part.parent, "name"):
            print("%s -> dir(%s)" % (part.code, part.parent.name))
Example #4
0
def command(args):
    import sys

    from sr.tools.inventory import assetcode
    from sr.tools.inventory.inventory import get_inventory
    from sr.tools.environment import open_editor

    inv = get_inventory()

    parts = []
    for c in args.part_code:
        code = assetcode.normalise(c)

        try:
            assetcode.code_to_num(code)
        except:
            print("Error: %s is an invalid code." % code, file=sys.stderr)
            sys.exit(1)

        try:
            part = inv.root.parts[code]
        except KeyError:
            print("Error: There is no part with code %s." % code,
                  file=sys.stderr)
            sys.exit(1)

        parts.append(part)

    for part in parts:
        open_editor(part.path)
Example #5
0
def command(args):
    import argparse
    import os

    import yaml

    from sr.tools.environment import open_editor
    from sr.tools.inventory.inventory import get_inventory
    from sr.tools.cli import inv_new_asset

    dirname = args.dirname

    inventory = get_inventory()
    gitdir = inventory.root_path

    # Find out if there's a template for the 'info' file
    templatefn = os.path.join(gitdir, ".meta", "assemblies", dirname)
    if not os.path.isfile(templatefn):
        templatefn = os.path.join(gitdir, ".meta", "assemblies", "default")

    userno = inventory.current_user_number
    assetcd = inventory.get_next_asset_code(userno)

    groupname = "%s-sr%s" % (dirname, assetcd)

    if os.path.isdir(dirname):
        os.rename(dirname, groupname)
    else:
        os.mkdir(groupname)

    print("Created new assembly with name \"%s\"" % groupname)

    # Copy the group template to the group dir
    # Insert the asset code into the file while we're at it
    templatefile = open(templatefn)
    assetfile = open(os.path.join(groupname, "info"), "w")

    for line in templatefile:
        assetfile.write(line.replace("[ASSET_CODE]", assetcd))

    templatefile.close()
    assetfile.close()

    if args.start_editor:
        open_editor(os.path.join(groupname, "info"))

    if args.create_all:
        assy_data = yaml.load(open(templatefn))
        if "elements" in assy_data:
            os.chdir(groupname)
            for element in assy_data["elements"]:
                other_args = argparse.Namespace()
                other_args.assetname = element
                other_args.start_editor = args.start_editor
                inv_new_asset.command(other_args)
Example #6
0
def command(args):
    import argparse
    import os

    import yaml

    from sr.tools.environment import open_editor
    from sr.tools.inventory.inventory import get_inventory
    from sr.tools.cli import inv_new_asset

    dirname = args.dirname

    inventory = get_inventory()
    gitdir = inventory.root_path

    # Find out if there's a template for the 'info' file
    templatefn = os.path.join(gitdir, ".meta", "assemblies", dirname)
    if not os.path.isfile(templatefn):
        templatefn = os.path.join(gitdir, ".meta", "assemblies", "default")

    userno = inventory.current_user_number
    assetcd = inventory.get_next_asset_code(userno)

    groupname = "%s-sr%s" % (dirname, assetcd)

    if os.path.isdir(dirname):
        os.rename(dirname, groupname)
    else:
        os.mkdir(groupname)

    print("Created new assembly with name \"%s\"" % groupname)

    # Copy the group template to the group dir
    # Insert the asset code into the file while we're at it
    templatefile = open(templatefn)
    assetfile = open(os.path.join(groupname, "info"), "w")

    for line in templatefile:
        assetfile.write(line.replace("[ASSET_CODE]", assetcd))

    templatefile.close()
    assetfile.close()

    if args.start_editor:
        open_editor(os.path.join(groupname, "info"))

    if args.create_all:
        assy_data = yaml.load(open(templatefn))
        if "elements" in assy_data:
            os.chdir(groupname)
            for element in assy_data["elements"]:
                other_args = argparse.Namespace()
                other_args.assetname = element
                other_args.start_editor = args.start_editor
                inv_new_asset.command(other_args)
Example #7
0
def command(args):
    from sr.tools.inventory.inventory import get_inventory

    inventory = get_inventory()

    errors = 0
    for check_fn in CHECKS:
        message = check_fn(inventory)
        if message:
            print(message)
            errors += 1

    if errors == 0:
        print('No problems found. :)')
Example #8
0
def command(args):
    import datetime
    import os
    import textwrap

    import pygit2

    from sr.tools.environment import get_terminal_size
    from sr.tools.inventory.inventory import assetcode, get_inventory

    inventory = get_inventory()
    repo = pygit2.Repository(inventory.root_path)

    asset_code = 'sr{}'.format(assetcode.normalise(args.asset_code))

    for event in get_history(repo, asset_code):
        status = event[0]
        commit = event[1]

        if args.output == 'commits':
            print(commit.id)
        else:
            description = ''
            if status == 'A':
                description = "'{}' created.".format(event[2])
            elif status == 'R':
                old_path = event[2][0]
                new_path = event[2][1]
                description = "Moved from '{}' into '{}'." \
                              .format(os.path.dirname(old_path),
                                      os.path.dirname(new_path))
            elif status == 'M':
                description = 'Contents modifed.'
            else:
                description = 'Something happened.'

            terminal_width, terminal_height = get_terminal_size()

            if args.output == 'full':
                print('Commit {} by {} on {}'.format(
                    commit.id,
                    commit.committer.name,
                    datetime.datetime.fromtimestamp(commit.commit_time)
                ))

                for line in textwrap.wrap(description, width=terminal_width-2):
                    print(' ', line)
                print()
            else:
                print(description)
Example #9
0
def command(args):
    import os

    from sr.tools.inventory.inventory import get_inventory

    inventory = get_inventory()
    templatedir = os.path.join(inventory.root_path, ".meta", "assemblies" if args.assemblies else "parts")
    templates = os.listdir(templatedir)

    for template in templates:
        if template in ["default"]:
            continue

        print(template)
Example #10
0
def command(args):
    import os
    import subprocess
    import sys

    from sr.tools.inventory import assetcode
    from sr.tools.inventory.inventory import get_inventory

    inv = get_inventory()
    cwd = os.getcwd()

    parts = []
    for c in args.assetcodes:
        code = assetcode.normalise(c)

        try:
            assetcode.code_to_num(code)
        except:
            print("Error: {} is an invalid asset code.".format(c),
                  file=sys.stderr)
            sys.exit(1)

        try:
            part = inv.root.parts[code]
        except:
            print("Error: There is no part with code {}.".format(code),
                  file=sys.stderr)
            sys.exit(1)

        if part.parent.path == cwd:
            print("Warning: Part {} is already in {}.".format(code, cwd))
            continue

        if hasattr(part.parent, "code"):
            if args.assy:
                parts.append(part.parent)
            else:
                print("Warning: Part {} is in an assembly.".format(code),
                      "To move the assembly, use the -a switch.",
                      file=sys.stderr)
                parts.append(part)

        else:
            parts.append(part)

    if parts:
        paths = [x.path for x in parts]
        subprocess.check_call(['git', 'mv'] + paths + ['.'])
    else:
        print("Warning: No parts to move", file=sys.stderr)
Example #11
0
def command(args):
    import datetime
    import os
    import textwrap

    import pygit2

    from sr.tools.environment import get_terminal_size
    from sr.tools.inventory.inventory import assetcode, get_inventory

    inventory = get_inventory()
    repo = pygit2.Repository(inventory.root_path)

    asset_code = 'sr{}'.format(assetcode.normalise(args.asset_code))

    for event in get_history(repo, asset_code):
        status = event[0]
        commit = event[1]

        if args.output == 'commits':
            print(commit.id)
        else:
            description = ''
            if status == 'A':
                description = "'{}' created.".format(event[2])
            elif status == 'R':
                old_path = event[2][0]
                new_path = event[2][1]
                description = "Moved from '{}' into '{}'." \
                              .format(os.path.dirname(old_path),
                                      os.path.dirname(new_path))
            elif status == 'M':
                description = 'Contents modifed.'
            else:
                description = 'Something happened.'

            terminal_width, terminal_height = get_terminal_size()

            if args.output == 'full':
                print('Commit {} by {} on {}'.format(
                    commit.id, commit.committer.name,
                    datetime.datetime.fromtimestamp(commit.commit_time)))

                for line in textwrap.wrap(description,
                                          width=terminal_width - 2):
                    print(' ', line)
                print()
            else:
                print(description)
Example #12
0
def command(args):
    import os

    from sr.tools.inventory.inventory import get_inventory

    inventory = get_inventory()
    templatedir = os.path.join(inventory.root_path, ".meta",
                               'assemblies' if args.assemblies else 'parts')
    templates = os.listdir(templatedir)

    for template in templates:
        if template in ["default"]:
            continue

        print(template)
Example #13
0
def command(args):
    import sys

    from sr.tools.inventory import assetcode
    from sr.tools.inventory.inventory import get_inventory

    inv = get_inventory()

    for code in args.asset:
        part = inv.root.parts[assetcode.normalise(code)]
        if part is not None:
            replace_line(part.path, args.attrname, args.attrvalue)
        else:
            print("Could not find asset:", code, file=sys.stderr)
            sys.exit(1)
Example #14
0
def command(args):
    import sys

    from sr.tools.inventory import assetcode
    from sr.tools.inventory.inventory import get_inventory

    inv = get_inventory()

    for code in args.asset:
        part = inv.root.parts[assetcode.normalise(code)]
        if part is not None:
            replace_line(part.info_path, args.attrname, args.attrvalue)
        else:
            print("Could not find asset:", code, file=sys.stderr)
            sys.exit(1)
Example #15
0
def command(args):
    from sr.tools.inventory.inventory import get_inventory

    inventory = get_inventory()

    errors = 0

    # check for duplicate asset codes
    duplicate_asset_codes = get_duplicates(inventory.asset_codes)
    if duplicate_asset_codes:
        print('There are duplicate asset codes:',
              ', '.join(duplicate_asset_codes))
        errors += 1

    if errors == 0:
        print('No problems found. :)')
Example #16
0
def command(args):
    import os

    from sr.tools.environment import open_editor
    from sr.tools.inventory.inventory import get_inventory

    assetname = args.assetname

    inventory = get_inventory()
    gitdir = inventory.root_path

    # Check that a template for the new asset exists
    templatefn = os.path.join(gitdir, ".meta", "parts", assetname)
    if not os.path.isfile(templatefn):
        print('A template for the asset "{}" could not be found. '
              'The default template will be used.'.format(assetname))
        templatefn = os.path.join(gitdir, ".meta", "parts", "default")

    # Get the git name/email of the user
    userno = inventory.current_user_number
    assetcd = inventory.get_next_asset_code(userno)

    assetfn = "%s-sr%s" % (assetname, assetcd)

    print('Created new asset with name "{0}-\033[1msr{1}\033[0m"'
          .format(assetname, assetcd))

    # Copy the template to the actual asset file
    # Insert the asset code into the file while we're at it
    templatefile = open(templatefn)
    assetfile = open(assetfn, "w")

    for line in templatefile:
        assetfile.write(line.replace("[ASSET_CODE]", assetcd))

    templatefile.close()
    assetfile.close()

    if args.start_editor:
        open_editor(assetfn)
Example #17
0
def command(args):
    import sys

    from sr.tools.inventory.inventory import get_inventory
    from pyparsing import ParseException

    inventory = get_inventory()

    query_str = args.query
    style = 'codes' if args.codes else 'paths'
    verbose = args.v

    try:
        count = 0
        for asset in inventory.query(query_str):
            count += 1
            print(asset.code if style == 'codes' else asset.path)
        if verbose:
            print("# {0} results".format(count), file=sys.stderr)
    except ParseException as e:
        print("Query Error:", e, file=sys.stderr)
        sys.exit(1)
Example #18
0
def command(args):
    import pydoc
    import subprocess

    from sr.tools.inventory import assetcode
    from sr.tools.inventory.inventory import get_inventory

    inv = get_inventory()

    partcode = assetcode.normalise(args.partcode)
    part = inv.root.parts[partcode]

    pager_text = "Full path: " + part.path + '\n'
    with open(part.info_path, 'r') as info_file:
        pager_text += info_file.read() + '\n'

    pager_text += "Log\n===\n"
    pager_text += subprocess.check_output(['git', '--no-pager', 'log',
                                           '--color', '--follow', '-C', '-M',
                                           part.path],
                                          universal_newlines=True)

    pydoc.pager(pager_text)
Example #19
0
def command(args):
    import pydoc
    import subprocess

    from sr.tools.inventory import assetcode
    from sr.tools.inventory.inventory import get_inventory

    inv = get_inventory()

    partcode = assetcode.normalise(args.partcode)
    part = inv.root.parts[partcode]

    pager_text = "Full path: " + part.path + '\n'
    with open(part.info_path, 'r') as info_file:
        pager_text += info_file.read() + '\n'

    pager_text += "Log\n===\n"
    pager_text += subprocess.check_output([
        'git', '--no-pager', 'log', '--color', '--follow', '-C', '-M',
        part.path
    ],
                                          universal_newlines=True)

    pydoc.pager(pager_text)
Example #20
0
def command(args):
    import os
    import sys

    from sr.tools.inventory import assetcode
    from sr.tools.inventory.inventory import get_inventory

    ASSET_CODE = 0
    PART_TYPE = 1

    COLOUR_RESET = "\033[0m"
    COLOUR_GREEN = "\033[1;32m"
    COLOUR_RED = "\033[1;31m"
    COLOUR_YELLOW = "\033[1;33m"

    inv = get_inventory()

    parts = []
    spec_type = ASSET_CODE
    for c in args.itemspecs:
        code = assetcode.normalise(c)

        try:
            assetcode.code_to_num(code)
            spec_type = ASSET_CODE
        except:
            if c in inv.root.types:
                spec_type = PART_TYPE
            else:
                print("Error: %s is an invalid asset code or part type." %
                      c, file=sys.stderr)
                sys.exit(1)

        if spec_type == ASSET_CODE:
            try:
                part = inv.root.parts[code]
            except KeyError:
                print("Error: There is no part with code %s." %
                      code, file=sys.stderr)
                sys.exit(1)

            parts.append(part)
        else:
            try:
                parts.extend(inv.root.types[c])
            except:
                print("Error: There is no part type %s." % c, file=sys.stderr)
                sys.exit(1)

    stat_colour = ""
    path = ""
    for part in parts:
        if args.relpath:
            path = os.path.relpath(part.path)
        else:
            path = part.path

        if args.asset_stat and hasattr(part, "condition"):
            if part.condition == "broken":
                stat_colour = COLOUR_RED
            elif part.condition == "unknown":
                stat_colour = COLOUR_YELLOW
            elif part.condition == "working":
                stat_colour = COLOUR_GREEN
            print(path, stat_colour, part.condition, COLOUR_RESET)
        else:
            print(path)