Пример #1
0
def prepare_local_dir(in_dir):
    init(in_dir)
    add(in_dir, in_dir)
    commit(in_dir, "adding files")
    branch_create(in_dir, 'gh-pages')

    r = open_repo(in_dir)
    r.reset_index(r[b"refs/heads/gh-pages"].tree)
    r.refs.set_symbolic_ref(b"HEAD", b"refs/heads/gh-pages")
Пример #2
0
    def run(self, args):
        opts, args = getopt(args, "", ["bare"])
        opts = dict(opts)

        if args == []:
            path = os.getcwd()
        else:
            path = args[0]

        porcelain.init(path, bare=("--bare" in opts))
Пример #3
0
Файл: g.py Проект: xrdavies/avts
    def create_repo(self, localpath, repo_name):
        # change dir
        ori_dir = os.getcwd()
        os.chdir(localpath)

        print(ori_dir, localpath, os.getcwd())
        # create local repo
        repo = git.init('.')

        # set correct user to commit
        config = repo.get_config()
        config.set('user', 'name', self.config['NAME'])
        config.set('user', 'email', self.config['EMAIL'])
        config.write_to_path()

        # add untracked files to git index
        stats = git.status(repo)
        git.add(repo, stats.untracked)

        # commit
        git.commit(repo, 'Initialized')

        # create github repo
        github_repo = None
        try:
            github_repo = self.organization.get_repo(repo_name)
        except:
            github_repo = self.organization.create_repo(repo_name)
            pass

        # push to github repo
        git.push(repo, github_repo.ssh_url, 'master')

        os.chdir(ori_dir)
        pass
def example():
    print '=== Create folders and blobs ==='
    os.mkdir('git_test')
    repo = Repo.init('git_test')
    for x in range(5):
        r = Repo('git_test')
        s = Snapshot(r)

        root = s.root()
        print '$ ls'
        print root.nodes()
        dir1 = root.add_treenode('dir1')
        blob = dir1.add_blobnode('file1', 'content%s' % x)
        blob.save()

        dir2 = dir1.add_treenode('dir2')
        blob = dir2.add_blobnode('file2', 'content%s' % x)
        blob.save()
        dir2.save()

        dir1.save()
        root.save()

        print 'commit:', s.commit('testuser', message='test commit: %s' % x).id
        print '$ cat dir1/file1'
        print root.get_node('dir1').get_node('file1').read_data()
        print dir1.get_node('dir2').get_node('file2').read_data()
	print

    #Using Porcelain
    #Initiating a new repo
    repo = porcelain.init("myrepo")


    #cloning a repo from a server
    porcelain.clone("https://github.com/sugarlabs/browse-activity.git","browse_activity_clone")


    #local cloning
    porcelain.clone("/home/vikram/browse_activity_clone","browse_activity_clone_1")
    print
    print "Commiting"
    open("myrepo/testfile","w").write("data")
    porcelain.add(repo,"myrepo/testfile")
    porcelain.commit(repo,"A sample commit")

    open("myrepo/testfile","w").write("data1")
    porcelain.add(repo,"myrepo/testfile")
    porcelain.commit(repo,"Second commit")

    open("myrepo/testfile1","w").write("sugar-labs")
    porcelain.add(repo,"myrepo/testfile1")
    porcelain.commit(repo,"First commit")
    
    print "Commit Logs:"
    print porcelain.log(repo)
def example():
    print '=== Create folders and blobs ==='
    os.mkdir('git_test')
    repo = Repo.init('git_test')
    for x in range(5):
        r = Repo('git_test')
        s = Snapshot(r)

        root = s.root()
        print '$ ls'
        print root.nodes()
        dir1 = root.add_treenode('dir1')
        blob = dir1.add_blobnode('file1', 'content%s' % x)
        blob.save()

        dir2 = dir1.add_treenode('dir2')
        blob = dir2.add_blobnode('file2', 'content%s' % x)
        blob.save()
        dir2.save()

        dir1.save()
        root.save()

        print 'commit:', s.commit('testuser', message='test commit: %s' % x).id
        print '$ cat dir1/file1'
        print root.get_node('dir1').get_node('file1').read_data()
        print dir1.get_node('dir2').get_node('file2').read_data()
        print

    #Using Porcelain
    #Initiating a new repo
    repo = porcelain.init("myrepo")

    #cloning a repo from a server
    porcelain.clone("https://github.com/sugarlabs/browse-activity.git",
                    "browse_activity_clone")

    #local cloning
    porcelain.clone("/home/vikram/browse_activity_clone",
                    "browse_activity_clone_1")
    print
    print "Commiting"
    open("myrepo/testfile", "w").write("data")
    porcelain.add(repo, "myrepo/testfile")
    porcelain.commit(repo, "A sample commit")

    open("myrepo/testfile", "w").write("data1")
    porcelain.add(repo, "myrepo/testfile")
    porcelain.commit(repo, "Second commit")

    open("myrepo/testfile1", "w").write("sugar-labs")
    porcelain.add(repo, "myrepo/testfile1")
    porcelain.commit(repo, "First commit")

    print "Commit Logs:"
    print porcelain.log(repo)
Пример #6
0
def add(ctx, project):
    """
    This commands adds a project to primer
    """
    base = ctx.obj['base']
    primer_dir = pathlib.Path(ctx.obj['confdir'])
    primer_project_dir = primer_dir / 'projects'
    project_dir = pathlib.Path(project)
    if base:
        project_dir = base / project_dir
    if project_dir.exists():
        click.echo(click.style(
            "Project folder {} already exists, doing nothing!".format(project),
            fg='red'),
                   err=True)
        sys.exit(1)
    if not primer_project_dir.exists():
        primer_project_dir.mkdir(parents=True, exist_ok=True)
        repo = porcelain.init(str(primer_dir))
    else:
        repo = Repo(str(primer_dir))
    projects_yml = primer_dir / 'projects.yml'
    if projects_yml.exists():
        with projects_yml.open('r') as yml_file:
            projects_def = yaml.load(yml_file, Loader=Loader)
            projects = projects_def['primer']['projects']
            if project in projects:
                click.echo(click.style(
                    "Project {} already defined!".format(project), fg='red'),
                           err=True)
                sys.exit(1)
            projects.append(project)
    else:
        projects_def = OrderedDict()
        projects_def['version'] = 1
        projects_def['primer'] = {'projects': [project]}
    with projects_yml.open('w') as yml_file:
        yaml.dump(projects_def,
                  yml_file,
                  default_flow_style=False,
                  Dumper=Dumper)
    project_dir.mkdir(parents=True, exist_ok=True)
    header = OrderedDict()
    header['version'] = 1
    header['primer'] = {'repositories': {}, 'directory': str(project_dir)}
    primer_yml = primer_project_dir / '{}.yml'.format(project)
    with primer_yml.open('w') as yml_file:
        yaml.dump(header, yml_file, default_flow_style=False, Dumper=Dumper)
    porcelain.add(repo, primer_yml)
    porcelain.commit(repo, message="added {}".format(project))
    click.echo(
        click.style(
            "Applied primer on {0}!\nYou can start adding repositories to {0} now."
            .format(project),
            fg='green'))
Пример #7
0
 def local_init(self, repo_name, activity):
     self.activity = activity
     self.repo_name = repo_name
     try:
         self.repo = p.init(repo_name)
         self.current_dir = os.getcwd()
         self.repo_path = self.current_dir + '/' + self.repo_name
         print self.repo_path
         print "Local Repo Created"
     except:
         print "Repo already exist, delete it first"
Пример #8
0
	def local_init(self, repo_name, activity):
		self.activity = activity
		self.repo_name = repo_name
		try:
			self.repo = p.init(repo_name)
			self.current_dir = os.getcwd()
			self.repo_path = self.current_dir + '/' + self.repo_name
			print self.repo_path
			print "Local Repo Created"
		except:
			print "Repo already exist, delete it first"
Пример #9
0
def process(input):
    gh_username = setup.get_gh_username()
    gh_password = setup.decrypt_password()
    gh = github.Github(gh_username, gh_password)
    click.echo(chalk.blue('you are in git module'))
    click.echo('input = %s' % input)
    CONFIG_FILE_PATH = config_file_paths['CONFIG_FILE_PATH']
    CONFIG_FOLDER_PATH = get_folder_path_from_file_path(CONFIG_FILE_PATH)
    repo = porcelain.init(CONFIG_FOLDER_PATH)
    porcelain.add(repo)
    porcelain.commit(repo, "A sample commit")
    porcelain.remote_add(repo, ".yoda", "https://github.com/manparvesh/.yoda")
    porcelain.push(repo, "https://github.com/manparvesh/.yoda")
Пример #10
0
 def __init__(self, name, repo, path=None, ext='md', content_meta=None,
              columns=None, **kwargs):
     try:
         self.repo = open_repo(repo)
     except NotGitRepository:
         self.repo = init(repo)
     self.path = repo
     self.ext = ext
     self.content_meta = content_meta or {}
     if path is None:
         path = name
     if path:
         self.path = os.path.join(self.path, path)
     self.path = self.path
     columns = columns or COLUMNS[:]
     super().__init__(name, columns=columns, **kwargs)
Пример #11
0
    def push(self):
        QApplication.setOverrideCursor(Qt.WaitCursor)
        username = self.username.text()
        userid = self.userid.text()
        password = self.password.text()
        reponame = self.reponame.text()
        settings = QSettings(QSettings.IniFormat, QSettings.UserScope,
                             QCoreApplication.organizationName(),
                             QCoreApplication.applicationName())
        settings.setValue("github.userid", userid)
        settings.setValue("github.username", username)
        settings.setValue("github.reponame", reponame)
        os.chdir(self.site_path)

        gitpath = os.path.join(self.site_path, ".git")
        if os.path.exists(gitpath):
            repo = porcelain.open_repo(self.site_path)
        else:
            repo = porcelain.init(self.site_path)
        for r, dirs, files in os.walk(self.site_path):
            for f in files:
                p = os.path.join(r, f)[len(self.site_path) + 1:]
                if not ".git" in p:
                    porcelain.add(repo, p)
                    self.html = "<p>adding: " + p + "</p>" + self.html
                    self.browser.setHtml(self.html)
                    QCoreApplication.processEvents()
        self.html = "<p>Commiting changes</p>" + self.html
        self.browser.setHtml(self.html)
        QCoreApplication.processEvents()
        porcelain.commit(repo, b"A sample commit")

        self.html = "<p>Pushing to server</p>" + self.html
        self.browser.setHtml(self.html)
        QCoreApplication.processEvents()

        porcelain.push(
            self.site_path, "https://" + userid + ":" + password +
            "@github.com/" + username + "/" + reponame + ".git", "master")

        self.html = "<p>Ready</p>" + self.html
        self.browser.setHtml(self.html)
        QCoreApplication.processEvents()

        QApplication.restoreOverrideCursor()
Пример #12
0
    def save(self):

        primer_dir = pathlib.Path(self.confdir)
        primer_project_dir = primer_dir / 'projects'
        project_dir = pathlib.Path(self.name)
        project = self.name
        if self.base:
            project_dir = self.base / project_dir
        if project_dir.exists():
            raise exceptions.ProjectFolderExistsError(self.name)
        if not primer_project_dir.exists():
            primer_project_dir.mkdir(parents=True, exist_ok=True)
            repo = porcelain.init(str(primer_dir))
        else:
            repo = Repo(str(primer_dir))
        projects_yml = primer_dir / 'projects.yml'
        if projects_yml.exists():
            with projects_yml.open('r') as yml_file:
                projects_def = yaml.load(yml_file, Loader=Loader)
                projects = projects_def['primer']['projects']
                if project in projects:
                    raise exceptions.ProjectExistsError(self.name)
                projects.append(project)
        else:
            projects_def = OrderedDict()
            projects_def['version'] = 1
            projects_def['primer'] = {'projects': [self.name]}
        with projects_yml.open('w') as yml_file:
            yaml.dump(projects_def,
                      yml_file,
                      default_flow_style=False,
                      Dumper=Dumper)
        project_dir.mkdir(parents=True, exist_ok=True)
        header = OrderedDict()
        header['version'] = 1
        header['primer'] = {'repositories': {}, 'directory': str(project_dir)}
        primer_yml = primer_project_dir / '{}.yml'.format(self.name)
        with primer_yml.open('w') as yml_file:
            yaml.dump(header,
                      yml_file,
                      default_flow_style=False,
                      Dumper=Dumper)
        porcelain.add(repo, primer_yml)
        porcelain.commit(repo, message="added {}".format(self.name))
Пример #13
0
def git_repo_fixture(path=None):
    """Fixture creating git repo.
    Can be used as a pytest fixture, in such case path where repo is created
    is random, created using tempfile module.
    If used as a function can be provided with path where the repo will be
    created

    :param path: path where repo will be created
    :type path: string
    :returns: porcelain object

    """
    if not path:
        path = mkdtemp()
    repo = porcelain.init(path)
    config = repo.get_config()
    config.set(b"user", b"name", b"Mr. Git")
    config.set(b"user", b"email", b"*****@*****.**")
    config.write_to_path()
    return repo
Пример #14
0
def git_repo_fixture(path=None):
    """Fixture creating git repo.
    Can be used as a pytest fixture, in such case path where repo is created
    is random, created using tempfile module.
    If used as a function can be provided with path where the repo will be
    created

    :param path: path where repo will be created
    :type path: string
    :returns: porcelain object

    """
    if not path:
        path = mkdtemp()
    repo = porcelain.init(path)
    config = repo.get_config()
    config.set(b'user', b'name', b'Mr. Git')
    config.set(b'user', b'email', b'*****@*****.**')
    config.write_to_path()
    return repo
Пример #15
0
 # try to get user info from git config
 username = useremail = None
 if args.author == USERNAME:
     username = get_gitconfig(GIT_GLOBAL, 'user', 'name')
     if username:
         args.author = username
 if args.email == USEREMAIL:
     useremail = get_gitconfig(GIT_GLOBAL, 'user', 'email')
     if useremail:
         args.email = useremail
 with open(pkg_init, 'w') as init:
     init.write('"""\nThis is the %s package.\n"""\n' % project_pkg)
     init.write(INIT_CONTENT % (args.author, args.email))
 LOGGER.info('Package file created: %s.', pkg_init)
 if args.git:
     repo = porcelain.init(project_name)
     LOGGER.info('Project Git repository initialized: %s.', repo)
     porcelain.add(repo, os.path.relpath(pkg_init, project_name))
     LOGGER.info('Project package added to index')
     # if global git config username and email not set, use args if specified
     kwargs = {}
     if not username:
         username = args.author
     if not useremail:
         useremail = args.email
     if not os.path.exists(GIT_GLOBAL):
         conf = repo.get_config()
         conf.set('user', 'name', username)
         conf.set('user', 'email', useremail)
         conf.write_to_path()
     else:
Пример #16
0
 def _make_remote(self):
     target = os.path.join(self.working_dir, 'remote')
     porcelain.init(path=target, bare=True)
     return target
Пример #17
0
 def open_repo(self):
     try:
         return porcelain.open_repo(self.repo_path)
     except NotGitRepository:
         return porcelain.init(self.repo_path)
Пример #18
0
def main(appliances=[],
         credentials=[],
         timeout=120,
         no_check_hostname=False,
         base_dir=default_base_dir,
         comment=default_comment,
         page=False,
         no_highlight_diff=False):
    """
    track_autoconfig.py

    Description:

    Store persisted domain configuration in a local git repository
    for auditing purposes.
    
    Usage:
        
        :::bash
        $ mast contrib/track_autoconfig.py --appliances <HOSTNAMES> --credentials <USER:PASS> --base-dir tmp/config

    Parameters:
    
    * `-a, --appliances` - The hostname(s), ip addresse(s), environment name(s)
    or alias(es) of the appliances you would like to affect. For details
    on configuring environments please see the comments in
    `environments.conf` located in `$MAST_HOME/etc/default`. For details
    on configuring aliases please see the comments in `hosts.conf` located
    in `$MAST_HOME/etc/default`.
    * `-c, --credentials`: The credentials to use for authenticating to the
    appliances. Should be either one set to use for all appliances
    or one set for each appliance. Credentials should be in the form
    `username:password` and should be provided in a space-seperated list
    if multiple are provided. If you would prefer to not use plain-text
    passwords, you can use the output of
    `$ mast-system xor <username:password>`.
    * `-t, --timeout`: The timeout in seconds to wait for a response from
    an appliance for any single request. __NOTE__ Program execution may
    halt if a timeout is reached.
    * `-n, --no-check-hostname`: If specified SSL verification will be turned
    off when sending commands to the appliances.
    * `-b, --base-dir`: The base directory where to store the downloaded
    files. Files will actually be stored in a subdirectory of `base_dir`
    named after the hostname in the form of `base_dir/<hostname>`    
    * `-p, --page`: If specified, page the output when too long to display
    at once
    * `-N, --no-highlight-diff`: If specified, the output of the diff will
    be syntax-highlighted
    """
    base_dir = os.path.abspath(base_dir)
    if not os.path.exists(base_dir):
        os.makedirs(base_dir)
    try:
        repo = git.Repo(base_dir)
        first_sha1 = repo.head()
        print first_sha1
    except NotGitRepository:
        print "Initializing git repository"
        git.init(base_dir)
        git.add(base_dir)
        first_sha1 = git.commit(base_dir, message="Initial Commit")
        print first_sha1
    except KeyError:
        git.add(base_dir)
        git.commit(base_dir, message="Initial Commit")
    print
    pull_autoconfig(appliances=appliances,
                    credentials=credentials,
                    timeout=timeout,
                    no_check_hostname=no_check_hostname,
                    base_dir=base_dir)
    git.add(base_dir)
    print git.status(base_dir)
    second_sha1 = git.commit(base_dir, message=comment)
    print second_sha1
    print "\n\nDIFF\n\n"
    
    tmp = StringIO()
    git.show(base_dir, outstream=tmp)
    tmp.seek(0)

    if no_highlight_diff:
        out = tmp.read()
    else:
        out = highlight(tmp.read(), DiffLexer(), TerminalFormatter())

    if page:
        pprint.page(out)
    else:
        print out
Пример #19
0
def main(appliances=[],
         credentials=[],
         timeout=120,
         no_check_hostname=False,
         base_dir=default_base_dir,
         comment=default_comment,
         persisted=False,
         recursive=False,
         no_strip_timestamp=False,
         page=False):
    """
    track_getconfig.py

    Description:

    Store running or persisted domain configuration in a local git
    repository for auditing purposes.
    
    Usage:
        
        :::bash
        $ mast contrib/track_getconfig.py --appliances <HOSTNAMES> --credentials <USER:PASS> --base-dir tmp/config

    Parameters:
    
    * `-a, --appliances` - The hostname(s), ip addresse(s), environment name(s)
    or alias(es) of the appliances you would like to affect. For details
    on configuring environments please see the comments in
    `environments.conf` located in `$MAST_HOME/etc/default`. For details
    on configuring aliases please see the comments in `hosts.conf` located
    in `$MAST_HOME/etc/default`.
    * `-c, --credentials`: The credentials to use for authenticating to the
    appliances. Should be either one set to use for all appliances
    or one set for each appliance. Credentials should be in the form
    `username:password` and should be provided in a space-seperated list
    if multiple are provided. If you would prefer to not use plain-text
    passwords, you can use the output of
    `$ mast-system xor <username:password>`.
    * `-t, --timeout`: The timeout in seconds to wait for a response from
    an appliance for any single request. __NOTE__ Program execution may
    halt if a timeout is reached.
    * `-n, --no-check-hostname`: If specified SSL verification will be turned
    off when sending commands to the appliances.
    * `-b, --base-dir`: The base directory where to store the downloaded
    files. Files will actually be stored in a subdirectory of `base_dir`
    named after the hostname in the form of `base_dir/<hostname>`    
    * `-p, --persisted`: If specified, the persisted configuration will
    be retrieved as opposed to the running configuration (which is the
    default)
    * `-r, --recursive`: If specified, the configuration will be retrieved
    recursively to the extent of the facilities provided by DataPower
    * `-N, --no-strip-timestamp`: If specified, the timestamp will not be
    stripped from the XML document, This is done because we are tracking
    this information with git and stripping this out allows us to alert
    on any changes in the repository as opposed to special-casing the
    difference in timestamp.
    * `-P, --page`: If specified, page the output when too long to display
    at once
    """
    base_dir = os.path.abspath(base_dir)
    if not os.path.exists(base_dir):
        os.makedirs(base_dir)
    try:
        repo = git.Repo(base_dir)
        repo.head()
    except NotGitRepository:
        print "Initializing git repository"
        git.init(base_dir)
        git.add(base_dir)
        git.commit(base_dir, message="Initial Commit")
    except KeyError:
        git.add(base_dir)
        git.commit(base_dir, message="Initial Commit")
    
    check_hostname = not no_check_hostname
    env = datapower.Environment(appliances,
                                credentials,
                                timeout=timeout,
                                check_hostname=check_hostname)

    for appliance in env.appliances:
        appliance_directory = os.path.join(base_dir, appliance.hostname)
        if not os.path.exists(appliance_directory):
            os.mkdir(appliance_directory)
        for domain in appliance.domains:
            config = appliance.get_config(domain=domain,
                                          recursive=recursive,
                                          persisted=persisted)

            config = config.pretty
            if no_strip_timestamp:
                pass
            else:
                config = re.sub(r"^.*?<dp:timestamp>.*?</dp:timestamp>.*?$",
                                r"",
                                config,
                                flags=re.MULTILINE)

            filename = os.path.join(appliance_directory,
                                    "{}.xml".format(domain))
            with open(filename, "wb") as fout:
                fout.write(config)

    git.add(base_dir)
    print git.status(base_dir)
    git.commit(base_dir, message=comment)
    
    tmp = StringIO()
    git.show(base_dir, outstream=tmp)
    tmp.seek(0)
    out = highlight(tmp.read(), DiffLexer(), TerminalFormatter())
    if page:
        pprint.page(out)
    else:
        print out
Пример #20
0
def main(appliances=[],
         credentials=[],
         timeout=120,
         no_check_hostname=False,
         base_dir=default_base_dir,
         comment=default_comment,
         page=False,
         no_highlight_diff=False):
    """
    track_autoconfig.py

    Description:

    Store persisted domain configuration in a local git repository
    for auditing purposes.

    Usage:

        :::bash
        $ mast contrib/track_autoconfig.py --appliances <HOSTNAMES> --credentials <USER:PASS> --base-dir tmp/config

    Parameters:

    * `-a, --appliances` - The hostname(s), ip addresse(s), environment name(s)
    or alias(es) of the appliances you would like to affect. For details
    on configuring environments please see the comments in
    `environments.conf` located in `$MAST_HOME/etc/default`. For details
    on configuring aliases please see the comments in `hosts.conf` located
    in `$MAST_HOME/etc/default`.
    * `-c, --credentials`: The credentials to use for authenticating to the
    appliances. Should be either one set to use for all appliances
    or one set for each appliance. Credentials should be in the form
    `username:password` and should be provided in a space-seperated list
    if multiple are provided. If you would prefer to not use plain-text
    passwords, you can use the output of
    `$ mast-system xor <username:password>`.
    * `-t, --timeout`: The timeout in seconds to wait for a response from
    an appliance for any single request. __NOTE__ Program execution may
    halt if a timeout is reached.
    * `-n, --no-check-hostname`: If specified SSL verification will be turned
    off when sending commands to the appliances.
    * `-b, --base-dir`: The base directory where to store the downloaded
    files. Files will actually be stored in a subdirectory of `base_dir`
    named after the hostname in the form of `base_dir/<hostname>`
    * `-p, --page`: If specified, page the output when too long to display
    at once
    * `-N, --no-highlight-diff`: If specified, the output of the diff will
    be syntax-highlighted
    """
    base_dir = os.path.abspath(base_dir)
    if not os.path.exists(base_dir):
        os.makedirs(base_dir)
    try:
        repo = git.Repo(base_dir)
        first_sha1 = repo.head()
        print first_sha1
    except NotGitRepository:
        print "Initializing git repository"
        git.init(base_dir)
        git.add(base_dir)
        first_sha1 = git.commit(base_dir, message="Initial Commit")
        print first_sha1
    except KeyError:
        git.add(base_dir)
        git.commit(base_dir, message="Initial Commit")
    print
    pull_autoconfig(appliances=appliances,
                    credentials=credentials,
                    timeout=timeout,
                    no_check_hostname=no_check_hostname,
                    base_dir=base_dir)
    git.add(base_dir)
    print git.status(base_dir)
    second_sha1 = git.commit(base_dir, message=comment)
    print second_sha1
    print "\n\nDIFF\n\n"

    tmp = StringIO()
    git.show(base_dir, outstream=tmp)
    tmp.seek(0)

    if no_highlight_diff:
        out = tmp.read()
    else:
        out = highlight(tmp.read(), DiffLexer(), TerminalFormatter())

    if page:
        pprint.page(out)
    else:
        print out
Пример #21
0
def performCommit(localRepo, bookid, bookinfo, bookroot, bookfiles):
    has_error = False
    staged = []
    added = []
    ignored = []
    # convert url paths to os specific paths
    repo_home = pathof(localRepo)
    repo_home = repo_home.replace("/", os.sep)
    repo_path = os.path.join(repo_home, "epub_" + bookid)
    book_home = pathof(bookroot)
    book_home = book_home.replace("/", os.sep)
    # convert from bookpaths to os relative file paths
    filepaths = []
    for bkpath in bookfiles:
        afile = pathof(bkpath)
        afile = afile.replace("/", os.sep)
        filepaths.append(afile)

    cdir = os.getcwd()
    if os.path.exists(repo_path):
        # handle updating the staged files and commiting and tagging
        # first collect info to determine files to delete form repo
        # current tag, etc
        os.chdir(repo_path)
        # determine the new tag
        tags = porcelain.list_tags(repo='.')
        tagname = "V%04d" % (len(tags) + 1)
        tagmessage = "Tag: " + tagname
        message = "updating to " + tagname
        # extra parameters must be passed as bytes if annotated is true
        tagname = utf8_str(tagname)
        message = utf8_str(message)
        tagmessage = utf8_str(tagmessage)
        # delete files that are no longer needed from staging area
        tracked = []
        tracked = porcelain.ls_files(repo='.')
        files_to_delete = []
        for afile in tracked:
            afile = pathof(afile)
            if afile not in filepaths:
                if afile not in ["mimetype", ".gitignore", ".bookinfo"]:
                    files_to_delete.append(afile)
        if len(files_to_delete) > 0:
            porcelain.rm(repo='.', paths=files_to_delete)
        # copy over current files
        copy_book_contents_to_destination(book_home, filepaths, repo_path)
        (staged, unstaged, untracked) = porcelain.status(repo='.')
        files_to_update = []
        for afile in unstaged:
            afile = pathof(afile)
            files_to_update.append(afile)
        for afile in untracked:
            afile = pathof(afile)
            files_to_update.append(afile)
        (added, ignored) = porcelain.add(repo='.', paths=files_to_update)
        commit_sha1 = porcelain.commit(repo='.',
                                       message=message,
                                       author=_SIGIL,
                                       committer=_SIGIL)
        # create annotated tags so we can get a date history
        tag = porcelain.tag_create(repo='.',
                                   tag=tagname,
                                   message=tagmessage,
                                   annotated=True,
                                   author=_SIGIL)
        os.chdir(cdir)
        add_bookinfo(repo_path, bookinfo, bookid, unicode_str(tagname))
    else:
        # this will be an initial commit to this repo
        tagname = b"V0001"
        tagmessage = b'First Tag'
        message = b"Initial Commit"
        os.makedirs(repo_path)
        add_gitignore(repo_path)
        add_gitattributes(repo_path)
        cdir = os.getcwd()
        os.chdir(repo_path)
        r = porcelain.init(path='.', bare=False)
        staged = copy_book_contents_to_destination(book_home, filepaths,
                                                   repo_path)
        (added, ignored) = porcelain.add(repo='.', paths=staged)
        # it seems author, committer, messages, and tagname only work with bytes if annotated=True
        commit_sha1 = porcelain.commit(repo='.',
                                       message=message,
                                       author=_SIGIL,
                                       committer=_SIGIL)
        tag = porcelain.tag_create(repo='.',
                                   tag=tagname,
                                   message=tagmessage,
                                   annotated=True,
                                   author=_SIGIL)
        os.chdir(cdir)
        add_bookinfo(repo_path, bookinfo, bookid, unicode_str(tagname))
    result = "\n".join(added)
    result = result + "***********" + "\n".join(ignored)
    if not has_error:
        return result
    return ''
Пример #22
0
import shutil

from dulwich import porcelain

# make sure we're starting fresh
shutil.rmtree("demo_repo", ignore_errors=True)

repo = porcelain.init("demo_repo")

# create an initial commit, with our content.
with open("demo_repo/a_file.txt", "wb") as f:
    f.write(b"Blobfish are people too\n")
porcelain.add(repo, "demo_repo/a_file.txt")  # note path!
porcelain.commit(repo, b"The beginning", author=b"meejah <*****@*****.**>")

# make a single change
with open("demo_repo/a_file.txt", "wb") as f:
    f.write(b"Blobfish are fish, not people\n")
porcelain.add(repo, "demo_repo/a_file.txt")
porcelain.commit(
    repo,
    b"blobfish are aquatic animals",
    author=b"meejah <*****@*****.**>",
)
Пример #23
0
 def init_new(self, workspace, **kw):
     # Dulwich.porcelain doesn't re-initialise a repository as true git does.
     if not isdir(join(workspace.working_dir, self.marker)):
         porcelain.init(path=workspace.working_dir)
Пример #24
0
                email = user.email

except Exception, e:
    import traceback
    stack = traceback.format_exc()

from dulwich import porcelain

scriptDir = sys.path[0]
repoPath = os.path.abspath(os.path.join(scriptDir, '..', '..', REPOSITORY))

print >> sys.stderr, "repo path: " + str(repoPath)

try:  #try to run a command
    if command == "init":
        porcelain.init(repoPath)
        porcelain.show(repoPath)
        with open(repoPath + '/.gitignore', 'w') as f:
            f.write('local/\n')
            f.write('*.sw?\n')
            f.close
    elif command == "ls":
        print "x,type,hash,path"
        porcelain.ls_tree(repoPath)
        #orcelain.show('..')
    elif command == "add":
        if paths == '':
            porcelain.add(repoPath)  # file(s) to add, defaults to app name
        else:
            porcelain.add(repoPath, paths)
        print "next steps:\nrun '|git status' to show what is ready to commit etc and '|git commit' to commit"
Пример #25
0
 def init_new(self, workspace, **kw):
     # Dulwich.porcelain doesn't re-initialise a repository as true git does.
     if not isdir(join(workspace.working_dir, self.marker)):
         porcelain.init(path=workspace.working_dir)
Пример #26
0
 def init(cls, path):
     git.init(path)
     return cls(path)
Пример #27
0
def create_new_repo(path):
    repo = porcelain.init(path)
    return repo
Пример #28
0
    return staged['modify'] + staged['add']


def mock_git_interface(add=tuple()):
    git = MagicMock()
    git.status.return_value = GitStatus(dict(add=add, modify=[], delete=[]),
                                        [], [])
    return git


with description(DulwichAnkiRepo) as self:
    with context('init'):
        with it("should use existing repo if it's present"):
            with TemporaryDirectory() as dir_name:
                repo_path = Path(dir_name)
                dulwich_repo = porcelain.init(repo_path)

                assert_repo_exists_at_given_path_after_init(repo_path)

        with it('should create a new repo if it does not exist'):
            with TemporaryDirectory() as dir_name:
                repo_path = Path(dir_name)

                assert_repo_exists_at_given_path_after_init(repo_path)

    with context('add_all'):
        with it("should add new files"):
            with TemporaryDirectory() as dir_name:
                repo, file = repo_with_new_file(dir_name)

                expect(staged_files(repo)).to(contain(str(file.name).encode()))
Пример #29
0
    staged = porcelain.status(repository.dulwich_repo).staged
    return staged['modify'] + staged['add']


def mock_git_interface(add=tuple()):
    git = MagicMock()
    git.status.return_value = GitStatus(dict(add=add, modify=[], delete=[]), [], [])
    return git


with description(DulwichAnkiRepo) as self:
    with context('init'):
        with it("should use existing repo if it's present"):
            with TemporaryDirectory() as dir_name:
                repo_path = Path(dir_name)
                dulwich_repo = porcelain.init(repo_path)

                assert_repo_exists_at_given_path_after_init(repo_path)

        with it('should create a new repo if it does not exist'):
            with TemporaryDirectory() as dir_name:
                repo_path = Path(dir_name)

                assert_repo_exists_at_given_path_after_init(repo_path)

    with context('add_all'):
        with it("should add new files"):
            with TemporaryDirectory() as dir_name:
                repo, file = repo_with_new_file(dir_name)

                expect(staged_files(repo)).to(contain(str(file.name).encode()))
Пример #30
0
 def create_decision_repo(self, path):
     os.makedirs(path)
     repo = porcelain.init(path)
     self.repo = repo
Пример #31
0
def main(appliances=[],
         credentials=[],
         timeout=120,
         no_check_hostname=False,
         base_dir=default_base_dir,
         comment=default_comment,
         persisted=False,
         recursive=False,
         no_strip_timestamp=False,
         page=False):
    """
    track_getconfig.py

    Description:

    Store running or persisted domain configuration in a local git
    repository for auditing purposes.

    Usage:

        :::bash
        $ mast contrib/track_getconfig.py --appliances <HOSTNAMES> --credentials <USER:PASS> --base-dir tmp/config

    Parameters:

    * `-a, --appliances` - The hostname(s), ip addresse(s), environment name(s)
    or alias(es) of the appliances you would like to affect. For details
    on configuring environments please see the comments in
    `environments.conf` located in `$MAST_HOME/etc/default`. For details
    on configuring aliases please see the comments in `hosts.conf` located
    in `$MAST_HOME/etc/default`.
    * `-c, --credentials`: The credentials to use for authenticating to the
    appliances. Should be either one set to use for all appliances
    or one set for each appliance. Credentials should be in the form
    `username:password` and should be provided in a space-seperated list
    if multiple are provided. If you would prefer to not use plain-text
    passwords, you can use the output of
    `$ mast-system xor <username:password>`.
    * `-t, --timeout`: The timeout in seconds to wait for a response from
    an appliance for any single request. __NOTE__ Program execution may
    halt if a timeout is reached.
    * `-n, --no-check-hostname`: If specified SSL verification will be turned
    off when sending commands to the appliances.
    * `-b, --base-dir`: The base directory where to store the downloaded
    files. Files will actually be stored in a subdirectory of `base_dir`
    named after the hostname in the form of `base_dir/<hostname>`
    * `-p, --persisted`: If specified, the persisted configuration will
    be retrieved as opposed to the running configuration (which is the
    default)
    * `-r, --recursive`: If specified, the configuration will be retrieved
    recursively to the extent of the facilities provided by DataPower
    * `-N, --no-strip-timestamp`: If specified, the timestamp will not be
    stripped from the XML document, This is done because we are tracking
    this information with git and stripping this out allows us to alert
    on any changes in the repository as opposed to special-casing the
    difference in timestamp.
    * `-P, --page`: If specified, page the output when too long to display
    at once
    """
    base_dir = os.path.abspath(base_dir)
    if not os.path.exists(base_dir):
        os.makedirs(base_dir)
    try:
        repo = git.Repo(base_dir)
        repo.head()
    except NotGitRepository:
        print "Initializing git repository"
        git.init(base_dir)
        git.add(base_dir)
        git.commit(base_dir, message="Initial Commit")
    except KeyError:
        git.add(base_dir)
        git.commit(base_dir, message="Initial Commit")

    check_hostname = not no_check_hostname
    env = datapower.Environment(appliances,
                                credentials,
                                timeout=timeout,
                                check_hostname=check_hostname)

    for appliance in env.appliances:
        appliance_directory = os.path.join(base_dir, appliance.hostname)
        if not os.path.exists(appliance_directory):
            os.mkdir(appliance_directory)
        for domain in appliance.domains:
            config = appliance.get_config(domain=domain,
                                          recursive=recursive,
                                          persisted=persisted)

            config = config.pretty
            if no_strip_timestamp:
                pass
            else:
                config = re.sub(r"^.*?<dp:timestamp>.*?</dp:timestamp>.*?$",
                                r"",
                                config,
                                flags=re.MULTILINE)

            filename = os.path.join(appliance_directory,
                                    "{}.xml".format(domain))
            with open(filename, "wb") as fout:
                fout.write(config)

    git.add(base_dir)
    print git.status(base_dir)
    git.commit(base_dir, message=comment)

    tmp = StringIO()
    git.show(base_dir, outstream=tmp)
    tmp.seek(0)
    out = highlight(tmp.read(), DiffLexer(), TerminalFormatter())
    if page:
        pprint.page(out)
    else:
        print out
Пример #32
0
 def test_bare(self):
     repo_dir = tempfile.mkdtemp()
     self.addCleanup(shutil.rmtree, repo_dir)
     porcelain.init(repo_dir, bare=True)
Пример #33
0
 def _make_remote(self):
     target = os.path.join(self.working_dir, 'remote')
     porcelain.init(path=target, bare=True)
     return target