Esempio n. 1
0
    def test_read_write(self):
        # writer must create the exact same file as the one read before
        for filename in ("git_config", "git_config_global"):
            file_obj = self._to_memcache(fixture_path(filename))
            w_config = GitConfigParser(file_obj, read_only=False)
            w_config.read()  # enforce reading
            assert w_config._sections
            w_config.write()  # enforce writing

            # we stripped lines when reading, so the results differ
            assert file_obj.getvalue()
            self.assertEqual(
                file_obj.getvalue(),
                self._to_memcache(fixture_path(filename)).getvalue())

            # creating an additional config writer must fail due to exclusive access
            self.failUnlessRaises(IOError,
                                  GitConfigParser,
                                  file_obj,
                                  read_only=False)

            # should still have a lock and be able to make changes
            assert w_config._lock._has_lock()

            # changes should be written right away
            sname = "my_section"
            oname = "mykey"
            val = "myvalue"
            w_config.add_section(sname)
            assert w_config.has_section(sname)
            w_config.set(sname, oname, val)
            assert w_config.has_option(sname, oname)
            assert w_config.get(sname, oname) == val

            sname_new = "new_section"
            oname_new = "new_key"
            ival = 10
            w_config.set_value(sname_new, oname_new, ival)
            assert w_config.get_value(sname_new, oname_new) == ival

            file_obj.seek(0)
            r_config = GitConfigParser(file_obj, read_only=True)
            assert r_config.has_section(sname)
            assert r_config.has_option(sname, oname)
            assert r_config.get(sname, oname) == val
            w_config.release()
Esempio n. 2
0
 def test_multi_line_config(self):
     file_obj = self._to_memcache(fixture_path("git_config_with_comments"))
     config = GitConfigParser(file_obj, read_only=False)
     ev = "ruby -e '\n"
     ev += "		system %(git), %(merge-file), %(--marker-size=%L), %(%A), %(%O), %(%B)\n"
     ev += "		b = File.read(%(%A))\n"
     ev += "		b.sub!(/^<+ .*\\nActiveRecord::Schema\\.define.:version => (\\d+). do\\n=+\\nActiveRecord::Schema\\."
     ev += "define.:version => (\\d+). do\\n>+ .*/) do\n"
     ev += "		  %(ActiveRecord::Schema.define(:version => #{[$1, $2].max}) do)\n"
     ev += "		end\n"
     ev += "		File.open(%(%A), %(w)) {|f| f.write(b)}\n"
     ev += "		exit 1 if b.include?(%(<)*%L)'"
     assert_equal(config.get('merge "railsschema"', 'driver'), ev)
     assert_equal(config.get('alias', 'lg'),
                  "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset'"
                  " --abbrev-commit --date=relative")
     assert len(config.sections()) == 23
Esempio n. 3
0
 def test_complex_aliases(self):
     file_obj = self._to_memcache(fixture_path('.gitconfig'))
     w_config = GitConfigParser(file_obj, read_only=False)
     self.assertEqual(w_config.get('alias', 'rbi'),
                      '"!g() { git rebase -i origin/${1:-master} ; } ; g"')
     w_config.release()
     self.assertEqual(
         file_obj.getvalue(),
         self._to_memcache(fixture_path('.gitconfig')).getvalue())
Esempio n. 4
0
    def test_read_write(self):
        # writer must create the exact same file as the one read before
        for filename in ("git_config", "git_config_global"):
            file_obj = self._to_memcache(fixture_path(filename))
            w_config = GitConfigParser(file_obj, read_only=False)
            w_config.read()                 # enforce reading
            assert w_config._sections
            w_config.write()                # enforce writing

            # we stripped lines when reading, so the results differ
            assert file_obj.getvalue()
            self.assertEqual(file_obj.getvalue(), self._to_memcache(fixture_path(filename)).getvalue())

            # creating an additional config writer must fail due to exclusive access
            self.failUnlessRaises(IOError, GitConfigParser, file_obj, read_only=False)

            # should still have a lock and be able to make changes
            assert w_config._lock._has_lock()

            # changes should be written right away
            sname = "my_section"
            oname = "mykey"
            val = "myvalue"
            w_config.add_section(sname)
            assert w_config.has_section(sname)
            w_config.set(sname, oname, val)
            assert w_config.has_option(sname, oname)
            assert w_config.get(sname, oname) == val

            sname_new = "new_section"
            oname_new = "new_key"
            ival = 10
            w_config.set_value(sname_new, oname_new, ival)
            assert w_config.get_value(sname_new, oname_new) == ival

            file_obj.seek(0)
            r_config = GitConfigParser(file_obj, read_only=True)
            assert r_config.has_section(sname)
            assert r_config.has_option(sname, oname)
            assert r_config.get(sname, oname) == val
            w_config.release()
Esempio n. 5
0
    def test_base(self):
        path_repo = fixture_path("git_config")
        path_global = fixture_path("git_config_global")
        r_config = GitConfigParser([path_repo, path_global], read_only=True)
        assert r_config.read_only
        num_sections = 0
        num_options = 0

        # test reader methods
        assert r_config._is_initialized is False
        for section in r_config.sections():
            num_sections += 1
            for option in r_config.options(section):
                num_options += 1
                val = r_config.get(section, option)
                val_typed = r_config.get_value(section, option)
                assert isinstance(val_typed, (
                    bool,
                    int,
                    float,
                ) + string_types)
                assert val
                assert "\n" not in option
                assert "\n" not in val

                # writing must fail
                with self.assertRaises(IOError):
                    r_config.set(section, option, None)
                with self.assertRaises(IOError):
                    r_config.remove_option(section, option)
            # END for each option
            with self.assertRaises(IOError):
                r_config.remove_section(section)
        # END for each section
        assert num_sections and num_options
        assert r_config._is_initialized is True

        # get value which doesnt exist, with default
        default = "my default value"
        assert r_config.get_value("doesnt", "exist", default) == default

        # it raises if there is no default though
        with self.assertRaises(cp.NoSectionError):
            r_config.get_value("doesnt", "exist")
Esempio n. 6
0
    def test_base(self):
        path_repo = fixture_path("git_config")
        path_global = fixture_path("git_config_global")
        r_config = GitConfigParser([path_repo, path_global], read_only=True)
        assert r_config.read_only
        num_sections = 0
        num_options = 0

        # test reader methods
        assert r_config._is_initialized is False
        for section in r_config.sections():
            num_sections += 1
            for option in r_config.options(section):
                num_options += 1
                val = r_config.get(section, option)
                val_typed = r_config.get_value(section, option)
                assert isinstance(val_typed, (bool, int, float, ) + string_types)
                assert val
                assert "\n" not in option
                assert "\n" not in val

                # writing must fail
                with self.assertRaises(IOError):
                    r_config.set(section, option, None)
                with self.assertRaises(IOError):
                    r_config.remove_option(section, option)
            # END for each option
            with self.assertRaises(IOError):
                r_config.remove_section(section)
        # END for each section
        assert num_sections and num_options
        assert r_config._is_initialized is True

        # get value which doesnt exist, with default
        default = "my default value"
        assert r_config.get_value("doesnt", "exist", default) == default

        # it raises if there is no default though
        with self.assertRaises(cp.NoSectionError):
            r_config.get_value("doesnt", "exist")
Esempio n. 7
0
def _parse_gitmodules(dspath):
    gitmodule_path = opj(dspath, ".gitmodules")
    parser = GitConfigParser(gitmodule_path)
    mods = {}
    for sec in parser.sections():
        try:
            modpath = parser.get(sec, 'path')
        except Exception:
            lgr.debug("Failed to get '%s.path', skipping section", sec)
            continue
        if not modpath or not sec.startswith('submodule '):
            continue
        modpath = normpath(opj(dspath, modpath))
        modprops = {'gitmodule_{}'.format(opt): parser.get_value(sec, opt)
                    for opt in parser.options(sec)
                    if not (opt.startswith('__') or opt == 'path')}
        modprops['gitmodule_name'] = sec[11:-1]
        mods[modpath] = modprops
    # make sure we let go of any resources held be the parser
    # we cannot rely on __del__
    parser.release()
    return mods
Esempio n. 8
0
def _parse_gitmodules(dspath):
    gitmodule_path = opj(dspath, ".gitmodules")
    parser = GitConfigParser(gitmodule_path)
    mods = {}
    for sec in parser.sections():
        try:
            modpath = parser.get(sec, 'path')
        except Exception:
            lgr.debug("Failed to get '%s.path', skipping section", sec)
            continue
        if not modpath or not sec.startswith('submodule '):
            continue
        modpath = normpath(opj(dspath, modpath))
        modprops = {'gitmodule_{}'.format(opt): parser.get_value(sec, opt)
                    for opt in parser.options(sec)
                    if not (opt.startswith('__') or opt == 'path')}
        modprops['gitmodule_name'] = sec[11:-1]
        mods[modpath] = modprops
    # make sure we let go of any resources held be the parser
    # we cannot rely on __del__
    parser.release()
    return mods
Esempio n. 9
0
 def test_complex_aliases(self):
     file_obj = self._to_memcache(fixture_path('.gitconfig'))
     w_config = GitConfigParser(file_obj, read_only=False)
     self.assertEqual(w_config.get('alias', 'rbi'), '"!g() { git rebase -i origin/${1:-master} ; } ; g"')
     w_config.release()
     self.assertEqual(file_obj.getvalue(), self._to_memcache(fixture_path('.gitconfig')).getvalue())
Esempio n. 10
0
    "benkrikler": 'ben',
}
# LZ has no public repositories at the moment, hence the two are the same
GITHUB_BASE = '[email protected]:'
GITHUB_SSH = '[email protected]:'
PROJECTS = {
    'BACCARAT': GITHUB_BASE + "sim/BACCARAT.git",
    'DER': GITHUB_BASE + "sim/ElectronicsSimulation.git",
    'TDRAnalysis': GITHUB_BASE + "sim/TDRAnalysis.git",
    'PhotonDetection': GITHUB_BASE + "physics/PhotonDetection.git",
    #'TDRScience': GITHUB_BASE + "TDRScience.git", # this one is 700 MB large
}

globalconfig = GitConfigParser(
    [os.path.normpath(os.path.expanduser("~/.gitconfig"))], read_only=True)
USER = globalconfig.get('user', 'name')

DEV_PATH = os.environ.get('DEV_PATH')

for alias, git_url in six.iteritems(PROJECTS):
    repo_dir = os.path.join(DEV_PATH, alias)
    if os.path.exists(repo_dir):
        print(">> Repo {0} already exists".format(repo_dir))
        continue
    print('>> Cloning {0}'.format(git_url))
    Repo.clone_from(git_url, repo_dir)
    repo = Repo(repo_dir)
    # rename origin
    repo.remotes.origin.rename('upstream')
    group = re.findall(':(.*?)/', git_url, re.DOTALL)[0]
    print('group', group)
Esempio n. 11
0
def cmd_lib_init(ctx,
                 name=None,
                 author_name=None,
                 author_email=None,
                 origin_version=None,
                 origin_url=None,
                 provide_main=False,
                 workdir=None,
                 force_create=False,
                 no_input=False,
                 soft_pack=False,
                 initial_branch=None):
    """
    Initialize a new Unikraft library.
    """
    # TODO: Implement adding dependencies at CLI
    dependencies = list()

    if workdir is None and name is None:
        libdir = os.getcwd()
        name = os.path.basename(libdir)

    elif workdir is None:
        libdir = os.path.join(os.getcwd(), name)

    elif name is None:
        libdir = workdir
        name = os.path.basename(libdir)

    # Check if the directory is non-empty and prompt for validation
    if util.is_dir_empty(libdir) is False and ctx.obj.assume_yes is False:
        if click.confirm(
                '%s is a non-empty directory, would you like to continue?' %
                libdir):  # noqa: E501
            force_create = True
        else:
            logger.critical("Cannot create directory: %s" % libdir)
            sys.exit(1)

    gitconfig = None

    try:
        if author_name is None or author_email is None:
            # Attempt reading author and email from .git/config
            if os.path.exists(os.path.join(libdir, '.git')):
                gitconfig = GitConfigParser(
                    [os.path.normpath(os.path.join(libdir, GITCONFIG_LOCAL))],
                    read_only=True)

            # Attempt reading default author and email from ~/.gitconfig
            else:
                gitconfig = GitConfigParser(
                    [os.path.normpath(os.path.expanduser(GITCONFIG_GLOBAL))],
                    read_only=True)

        if author_name is None:
            author_name = gitconfig.get("user", "name")

        if author_email is None:
            author_email = gitconfig.get("user", "email")

    except (NoSectionError, NoOptionError):
        pass

    if no_input is False:
        if origin_url is None:
            origin_url = click.prompt(
                "source (Use %s for automatic versioning)" % URL_VERSION)

    try:
        kraft_lib_init(name=name,
                       libdir=libdir,
                       author_name=author_name,
                       author_email=author_email,
                       origin_url=origin_url,
                       origin_version=origin_version,
                       dependencies=dependencies,
                       provide_main=provide_main,
                       force_create=force_create,
                       no_input=no_input,
                       soft_pack=soft_pack,
                       initial_branch=initial_branch)
    except Exception as e:
        logger.critical(str(e))

        if ctx.obj.verbose:
            import traceback
            logger.critical(traceback.format_exc())

        sys.exit(1)