def options(self, optparse):
        SubCommand.options(self, optparse, modules=globals())

        options = optparse.add_option_group('Remote options')
        options.add_option('-r',
                           '--remote',
                           dest='remote',
                           action='store',
                           help='Set the remote server location')

        options = optparse.add_option_group('Output options')
        options.add_option('-o',
                           '--output',
                           dest='output',
                           action='store',
                           help='Set the output directory')
        options.add_option(
            '--immediate',
            dest='immediate',
            action='store_true',
            help='Set the immediate directory to store the immediate files')
        options.add_option('--gitiles',
                           dest='gitiles',
                           action='store_true',
                           help='Enable gitiles links within the SHA-1')
        options.add_option('--format',
                           dest='format',
                           metavar='TEXT, HTML, ALL',
                           action='store',
                           default='text',
                           help='Set the report format')
示例#2
0
    def get_name(self, options):
        # use options.name with a higher priority if it's set
        if options.name:
            self.set_name(options.name)  # pylint: disable=E1101
        else:
            ulp = urlparse(options.git or '')
            self.set_name(ulp.path.strip('/'))  # pylint: disable=E1101

        return SubCommand.get_name(self, options)
    def execute(self, options, *args, **kws):
        SubCommand.execute(self, options, *args, **kws)

        RaiseExceptionIfOptionMissed(options.output, 'output is not set')

        name, remote = None, options.remote
        if options.remote:
            ulp = urlparse.urlparse(options.remote)
            if ulp.path:
                name = ulp.path.strip('/')
                remote = '%s://%s' % (ulp.scheme, ulp.hostname)
                if ulp.port:
                    remote += ':%d' % ulp.port

        format = options.format and options.format.lower()  # pylint: disable=W0622
        GitDiffSubcmd.generate_report(
            args, GitProject(None, worktree=options.working_dir), name,
            options.output, format, options.pattern, remote, options.immediate,
            options.gitiles)
示例#4
0
    def options(self, optparse):
        SubCommand.options(self,
                           optparse,
                           option_remote=True,
                           option_import=True,
                           modules=globals())

        options = optparse.get_option_group('--refs') or \
            optparse.add_option_group('Remote options')
        options.add_option(
            '--git',
            '--git-url',
            dest='git',
            action='store',
            metavar='GIT_URL',
            help='Set the git repository url to download and import')
        options.add_option('--rev',
                           '--reversion',
                           '--branch',
                           dest='branch',
                           action='store',
                           help='Set the initial revisions to download')
        options = optparse.get_option_group('--all') or \
            optparse.add_option_group('Git options')
        options.add_option(
            '-n',
            '--name',
            '--project-name',
            dest='name',
            action='store',
            metavar='NAME',
            help='Set the project name or local url. If it\'s not set, the '
            'name will be generated from the git name.')
        options.add_option('--bare',
                           dest='bare',
                           action='store_true',
                           help='Clone the bare repository')
        options.add_option('-m',
                           '--mirror',
                           dest='mirror',
                           action='store',
                           metavar='LOCATION',
                           help='Set the git repository mirror location')
示例#5
0
  def options(self, optparse):
    SubCommand.options(self, optparse)

    options = optparse.add_option_group('Pattern options')
    options.add_option(
      '--simple-substitute',
      dest='simple_subst', action='store_true',
      help='Define to use simple variable in option "from" and "to"')
    options.add_option(
      '--group',
      dest='group', action='store',
      help='The set name in value-set file')
    options.add_option(
      '--from',
      dest='fr', action='store', metavar="FROM",
      help='The referred "from" attr in referred elements')
    options.add_option(
      '--to',
      dest='to', action='store',
      help='The referred "to" attr in referred elements')

    options = optparse.add_option_group('File options')
    options.add_option(
      '--xml-file',
      dest='xml_file', action='store',
      help='Set the xml file as import value-sets')
    options.add_option(
      '--inplace',
      dest='inplace', action='store_true',
      help='Update the file in the place')
    options.add_option(
      '--postfix',
      dest='postfix', action='store',
      help='Output the files with specific extension postfix')
    options.add_option(
      '--output',
      dest='output', action='store',
      help='Output the replaced content to the file')
示例#6
0
    def execute(self, options, *args, **kws):  # pylint: disable=R0915
        SubCommand.execute(self, options, option_import=True, *args, **kws)

        logger = Logger.get_logger()  # pylint: disable=E1101

        pkgs, name = list(), None
        for pkg in args:
            pkgname, revision = _split_name(
                pkg, options.pkg_pattern, options.filter_out_chars)

            if name and pkgname != name:
                logger.warn(
                    'Warning: pkgname "%s" mismatched "%s"', pkgname, name)

            if not revision:
                logger.error(
                    'Error: %s failed to be recognized with revision' % pkg)
            else:
                name = pkgname
                pkgs.append((os.path.realpath(pkg), pkgname, revision))

        if len(pkgs) != len(args):
            return

        if not options.keep_order:
            pkgs.sort(_pkg_sort)

        if options.show_order or options.verbose > 0:
            print 'Effective packages (%d)' % len(pkgs)
            print '----------------------------'
            for pkg, pkgname, revision in pkgs:
                print '%s %-15s %s' % (pkgname, '[v%s]' % revision, pkg)
            print

            if options.show_order:
                return

        RaiseExceptionIfOptionMissed(
            options.name, "project name (--name) is not set")
        RaiseExceptionIfOptionMissed(
            options.pkg_pattern, "pkg pattern (--pkg-pattern) is not set")
        RaiseExceptionIfOptionMissed(
            args, "no files or directories are specified to import")

        if not options.tryrun and options.remote:
            gerrit = Gerrit(options.remote)
            gerrit.create_project(
                options.name,
                description=options.description or False,
                options=options)

        branch = options.branch or 'master'

        path, _ = os.path.splitext(os.path.basename(options.name))
        path = os.path.realpath(path)
        if options.offsite and not os.path.exists(path):
            os.makedirs(path)

        remote = '%s/%s' % (options.remote.rstrip(), options.name) \
            if options.remote else None

        project = GitProject(
            options.name,
            worktree=path,
            gitdir='%s/.git' % path,
            revision=branch,
            remote=remote)

        ret = project.init_or_download(
            branch, single_branch=True, offsite=options.offsite)
        if ret != 0:
            logger.error('Failed to init the repo %s' % project)
            return False

        temp = options.temp_directory or tempfile.mkdtemp()
        tags = list()
        filter_out = list([r'\.git/'])
        for fout in options.filter_out or list():
            filter_out.extend(fout.split(','))

        for pkg, pkgname, revision in pkgs:
            workp = pkg

            tmpl = dict({
                'n': pkgname,
                'name': pkgname,
                'N': pkgname.upper(),
                'NAME': pkgname.upper(),
                'v': revision,
                'version': revision,
                'V': revision.upper(),
                'VERSION': revision.upper()})

            if options.message_template:
                message = options.message_template % tmpl
            else:
                message = 'Import %s' % (
                    '%s%s%s' % (
                        pkgname,
                        revision and ' %s' % options.version_prefix,
                        revision))

            if options.use_commit_file:
                message = _read_file_with_escape(
                    pkg, options.enable_escape, message)

            if os.path.isfile(pkg):
                FileUtils.extract_file(pkg, temp)
                workp = temp

            if options.init_path:
                inited = os.path.join(workp, options.init_path)
                if os.path.exists(inited):
                    workp = inited

            if options.washed:
                if workp != temp:
                    os.makedirs(temp)
                    shutil.copytree(workp, temp, symlinks=True)
                    # wash the directory
                    washer = FileWasher(
                        temp, overrideReadOnly=True,
                        eol=options.washer_eol,
                        tab=options.washer_tabsize > 0,
                        trailing=options.washer_trailing)
                    if washer.wash(temp):
                        workp = temp

            if options.auto_detect:
                dname = os.listdir(workp)
                while 0 < len(dname) < 2:
                    workp += '/%s' % dname[0]
                    dname = os.listdir(workp)
                    logger.info('Go into %s' % workp)

            if options.washed:
                diff = FileDiff(project.path, workp, filter_out,
                                enable_sccs_pattern=options.filter_out_sccs)
                if diff.sync(project, logger) > 0:
                    ret = 0

                timestamp = diff.timestamp
            else:
                timestamp = _timestamp(workp)
                FileUtils.rmtree(project.path, ignore_list=(r'^\.git.*',))
                FileUtils.copy_files(workp, project.path)

                ret = project.add('--all', project.path)

            if ret == 0:
                args = list()
                if options.author:
                    args.append('--author="%s"' % options.author)

                args.append('-m')
                args.append(message)
                args.append('--date="%s"' % time.ctime(timestamp))

                ret = project.commit(*args)
                if ret == 0:
                    if options.version_template:
                        tags.append(options.version_template % tmpl)
                    elif options.local:
                        trefs = self.override_value(  # pylint: disable=E1101
                            options.refs, options.tag_refs) or ''
                        if trefs:
                            trefs += '/'

                        tags.append(
                            '%s%s%s' % (
                                trefs, options.version_prefix, revision))
                    else:
                        tags.append(
                            '%s%s' % (options.version_prefix, revision))

                    ret, _ = project.tag(tags[-1])

            if os.path.lexists(temp):
                try:
                    shutil.rmtree(temp)
                except OSError, e:
                    logger.exception(e)
示例#7
0
    def options(self, optparse):
        SubCommand.options(self, optparse, option_import=True,
                           option_remote=True, modules=globals())

        options = optparse.get_option_group('--refs') or \
            optparse.add_option_group('Remote options')
        options.add_option(
            '-b', '--branch',
            dest="branch", action='store', metavar='BRANCH',
            help='Set the branch')
        options.add_option(
            '-n', '--name', '--project-name',
            dest="name", action='store', metavar='NAME',
            help='Set the project name. If it\'s not set, the name will be '
                 'generated from the git name')

        options = optparse.get_option_group('-a') or \
            optparse.add_option_group('Import options')
        options.add_option(
            '--author',
            dest='author', action='store',
            help='Set the commit author')
        options.add_option(
            '--init-path', '--init-import-path',
            dest='init_path', action='store',
            help='Set the initialized path with the provided path or '
                 'extracted package')
        options.add_option(
            '-l', '--local',
            dest='local', action='store_true',
            help='Set locally not to push the stuffs')
        options.add_option(
            '--keep-order', '--keep-file-order', '--skip-file-sort',
            dest='keep_order', action='store_true',
            help='Keep the order of input files or directories without sort')

        options = optparse.add_option_group('File options')
        options.add_option(
            '--auto-detect', '--skip-single-directory',
            dest='auto_detect', action='store_true',
            help='Ignore the root directory from the uncompressed package')
        options.add_option(
            '--vpref', '--version-prefix',
            dest='version_prefix', action='store',
            default='v', metavar='PREFIX',
            help='append the tag prefix ahead of the normal tag, it has no '
                 'effort with the option "tag-template", the default '
                 'is "%default"')
        options.add_option(
            '--temp-directory', '--temporary-directory',
            dest='temp_directory', action='store',
            help='Temporary directory for immediate storage')
        options.add_option(
            '--ppattern', '--pkg-pattern',
            dest='pkg_pattern', action='append', metavar='PATTERN',
            help='setup the matching pattern with the file or directory name '
                 'to pick out the pkgname and the version to decide the '
                 'importing order. The first match will be treated as the '
                 'package name {%(n)s in normal and %(N)s in capital} and '
                 'other will be built as the version {%(v)s or ${V}s}. More '
                 'than one pattern can be accepted')
        options.add_option(
            '--message-template',
            dest='message_template', action='store',
            help='Set the message template with the value from option '
                 '"--ppattern"')
        options.add_option(
            '--enable-escape',
            dest='enable_escape', action='store_true',
            help='Escape the messages with the known items like %sha1, '
                 '%md5, %file, %size, etc')
        options.add_option(
            '--version-template',
            dest='version_template', action='store',
            help='Set the tag template with the value from option '
                 '"--ppattern"')
        options.add_option(
            '--use-commit-file',
            dest='use_commit_file', action='store_true',
            help='Use the file like the imported file as the commit message')

        options = optparse.add_option_group('Filter options')
        options.add_option(
            '--filter-out',
            dest='filter_out', action='append', metavar='FILTER1,FILTER2',
            help='filter out not to import the directories or files which '
                 'match the filter pattern. More than one pattern can be '
                 'accepted')
        options.add_option(
            '--characters', '--filter-out-chars',
            dest='filter_out_chars', action='store',
            metavar='CHARS', default='-.',
            help='filter out the characters in the segments returned from '
                 'the option "ppattern", default: %default')
        options.add_option(
            '--filter-out-sccs',
            dest='filter_out_sccs', action='store_true',
            help='filter out the known sccs meta files including cvs, '
                 'subversion, mercurial. git is excluded as they can be '
                 'resued')

        options = optparse.add_option_group('Other options')
        options.add_option(
            '--show-order',
            dest='show_order', action='store_true',
            help='Show the import order for the listed files')
示例#8
0
    def execute(self, options, *args, **kws):  # pylint: disable=R0915
        SubCommand.execute(self, options, option_import=True, *args, **kws)

        logger = Logger.get_logger()  # pylint: disable=E1101

        ret, _, pkgs = PkgImportSubcmd.build_packages(options, args, logger)
        if not ret:
            return

        if options.show_order or (options.verbose and options.verbose > 0):
            print('Effective packages (%d)' % len(pkgs))
            print('----------------------------')
            for pkg, pkgname, revision in pkgs:
                print('%s %-15s %s' % (pkgname, '[v%s]' % revision, pkg))
            print

            if options.show_order:
                return

        RaiseExceptionIfOptionMissed(
            options.name, "project name (--name) is not set")
        RaiseExceptionIfOptionMissed(
            options.remote or options.offsite, 'remote (--remote) is set')
        RaiseExceptionIfOptionMissed(
            options.pkg_pattern or options.message_template,
            'pkg pattern (--pkg-pattern) is not set')
        RaiseExceptionIfOptionMissed(
            args, "no files or directories are specified to import")

        if not options.dryrun and options.remote:
            gerrit = Gerrit(options.remote, options)
            gerrit.create_project(
                options.name,
                description=options.description or False,
                options=options)

        branch = options.branch or 'master'

        name, _ = os.path.splitext(os.path.basename(options.name))
        path = os.path.join(options.working_dir, name)
        if options.offsite and not os.path.exists(path):
            os.makedirs(path)

        if options.remote:
            ulp = urlparse(options.remote)
            if not ulp.scheme:
                remote = 'git://%s/%s' % (
                    options.remote.strip('/'), options.name)
            else:
                remote = '%s/%s' % (options.remote.strip('/'), options.name)
        else:
            remote = ''

        project = GitProject(
            options.name,
            worktree=path,
            gitdir='%s/.git' % path,
            revision=branch,
            remote=remote)

        optgc = options.extra_values(options.extra_option, 'git-clone')
        ret = project.init_or_download(
            branch, single_branch=True, offsite=options.offsite,
            reference=optgc and optgc.reference)
        if ret != 0:
            logger.error('Failed to init the repo %s' % project)
            return False

        filters = list()
        if options.washed:
            filters = list([r'\.git/'])
            for fout in options.filter_out or list():
                filters.extend(fout.split(','))

        opti = Values.build(
            detect_root=options.auto_detect,
            directory=options.temp_directory,
            filter_sccs=options.filter_out_sccs,
            filters=filters,
            force=options.force,
            local=options.local,
            imports=True,
            prefix=options.version_prefix,
            refs=options.refs,
            tag_refs=options.tag_refs,
            tmpl_escape=options.enable_escape,
            tmpl_file=options.use_commit_file,
            tmpl_message=options.message_template,
            tmpl_version=options.version_template,
            washed=options.washed,
            extra=options.extra_values(options.extra_option, 'git-commit'))

        tags = list()
        for pkg, pkgname, revision in pkgs:
            workplace = pkg
            if options.init_path:
                inited = os.path.join(workplace, options.init_path)
                if os.path.exists(inited):
                    workplace = inited

            _, ptags = PkgImportSubcmd.do_import(
                project, opti, pkgname, workplace, revision, logger=logger)

            if ptags:
                tags.extend(ptags)

        if not ret and not options.local:
            # pylint: disable=E1101
            # push the branches
            if self.override_value(
                    options.branches, options.all):
                ret = project.push_heads(
                    branch,
                    self.override_value(
                        options.refs, options.head_refs),
                    force=options.force, dryrun=options.dryrun)
            # push the tags
            if tags and self.override_value(
                    options.tags, options.all):
                optp = Values.build(fullname=True)
                ret = project.push_tags(
                    tags, self.override_value(
                        options.refs, options.tag_refs),
                    options=optp, force=options.force, dryrun=options.dryrun)
            # pylint: enable=E1101

        return ret == 0
示例#9
0
    def __exit__(self, exc_type, exc_value, traceback):
        tmpl = dict({
            'n': self.name,             'name': self.name,
            'N': self.name.upper(),     'NAME': self.name.upper(),
            'v': self.revision,         'version': self.revision,
            'V': self.revision.upper(), 'VERSION': self.revision.upper()}
        )

        if self.options.tmpl_message:
            message = self.options.tmpl_message
        else:
            message = 'Import %s' % (
                '%s%s%s' % (
                    self.name,
                    (self.name and self.revision) and ' %s' % (
                        self.options.prefix or ''),
                    self.revision))

        message = _handle_message_with_escape(
            '', self.options.tmpl_escape,
            message, dofile=self.options.tmpl_file)

        ret = 0
        if self.count > 0:
            if not self.options.strict:
                self.project.add('--all', '-f', self.project.path)

            args = list()
            optgc = self.options.extra_values(
                self.options.extra_option, 'git-commit')
            # extra is updated in do_import
            optgc.join(self.options.extra)

            if optgc and optgc.author:
                args.append('--author="%s"' % optgc.author)
            if optgc and optgc.date:
                args.append('--date="%s"' % optgc.date.strip('\'"'))
            else:
                args.append('--date="%s"' % time.ctime(self.timestamp))

            args.append('-m')
            args.append(message)

            ret = self.project.commit(*args)

        if self.count > 0 or self.options.force:
            if self.options.tmpl_version:
                self.tags.append(self.options.tmpl_version % tmpl)
            elif self.options.local and self.revision:
                trefs = SubCommand.override_value(  # pylint: disable=E1101
                    self.options.refs, self.options.tag_refs) or ''
                if trefs:
                    trefs += '/'

                self.tags.append(
                    '%s%s%s' % (
                        trefs, self.options.prefix or '', self.revision))
            elif self.revision:
                self.tags.append('%s%s' % (
                    self.options.prefix or '', self.revision))

            if self.tags:
                if self.options.force:
                    ret, _ = self.project.tag(self.tags[-1], '--force')
                else:
                    ret, _ = self.project.tag(self.tags[-1])

        self.ret = ret
        if os.path.lexists(self.tmpdir):
            try:
                shutil.rmtree(self.tmpdir)
            except OSError as e:
                self.logger.exception(e)
示例#10
0
    def execute(self, options, *args, **kws):
        SubCommand.execute(self, options, *args, **kws)

        RaiseExceptionIfOptionMissed(options.git or options.offsite,
                                     'git url (--git-url) is not set')

        ulp = urlparse(options.name or '')
        RaiseExceptionIfOptionMissed(
            ulp.scheme or options.remote,
            'Neither git name (--name) nor remote (--remote) is set')

        logger = self.get_logger()  # pylint: disable=E1101
        if ulp.scheme:
            remote = ulp.hostname
            projectname = ulp.path.strip('/')
        else:
            remote = options.remote
            projectname = self.get_name(options)  # pylint: disable=E1101

        remote = FileUtils.ensure_path(remote,
                                       prefix='git://',
                                       subdir=projectname,
                                       exists=False)

        working_dir = self.get_absolute_working_dir(options)  # pylint: disable=E1101
        project = GitProject(
            options.git,
            worktree=working_dir,
            gitdir=FileUtils.ensure_path(
                working_dir, subdir=None if options.bare else '.git'),
            revision=options.revision,
            remote=remote,
            bare=options.bare,
            pattern=GitCloneSubcmd.get_patterns(options)  # pylint: disable=E1101
        )

        ret = 0
        if not options.offsite:
            optgc = options.extra_values(options.extra_option, 'git-clone')
            ret = project.download(url=options.git,
                                   bare=options.bare,
                                   reference=optgc and optgc.reference)
            if ret != 0:
                raise DownloadError('%s: failed to fetch project' % project)

        ulp = urlparse(remote)
        # creat the project in the remote
        if ulp.scheme in ('ssh', 'git'):
            if not options.dryrun and options.remote and options.repo_create:
                gerrit = Gerrit(options.remote, options)
                gerrit.create_project(ulp.path.strip('/'),
                                      description=options.description,
                                      source=options.git,
                                      options=options)
        else:
            raise ProcessingError('%s: unknown scheme for remote "%s"' %
                                  (project, remote))

        self.do_hook(  # pylint: disable=E1101
            'pre-push',
            options,
            dryrun=options.dryrun)

        optgp = options.extra_values(options.extra_option, 'git-push')

        # push the branches
        if self.override_value(  # pylint: disable=E1101
                options.all, options.heads):
            optp = Values.build(extra=optgp,
                                push_all=options.all
                                or options.revision is None,
                                fullname=options.keep_name)

            res = project.push_heads(
                options.revision,
                self.override_value(  # pylint: disable=E1101
                    options.refs, options.head_refs),
                options.head_pattern,
                options=optp,
                push_all=options.all or options.revision is None,
                fullname=options.keep_name,
                force=options.force,
                dryrun=options.dryrun)

            ret |= res
            if res:
                logger.error('Failed to push heads')

        # push the tags
        if self.override_value(  # pylint: disable=E1101
                options.all, options.tags):
            optp = Values.build(extra=optgp, fullname=options.keep_name)

            res = project.push_tags(
                None if options.all else options.tag,
                self.override_value(  # pylint: disable=E1101
                    options.refs, options.tag_refs),
                options.tag_pattern,
                options=optp,
                force=options.force,
                dryrun=options.dryrun)

            ret |= res
            if res:
                logger.error('Failed to push tags')

        self.do_hook(  # pylint: disable=E1101
            'post-push',
            options,
            dryrun=options.dryrun)

        return ret