def main(argv): """Script main function""" options, args = parse_args(argv) if not options: return ExitCodes.parse_error try: init_tmpdir(options.tmp_dir, prefix='rpm-ch_') load_customizations(options.customization_file) editor_cmd = determine_editor(options) repo = RpmGitRepository('.') check_branch(repo, options) # Find and parse spec file spec = parse_spec_file(repo, options) # Find and parse changelog file ch_file = parse_changelog_file(repo, spec, options) since = get_start_commit(ch_file.changelog, repo, options) # Get range of commits from where to generate changes if args: gbp.log.info("Only looking for changes in '%s'" % ", ".join(args)) commits = repo.get_commits(since=since, until='HEAD', paths=args, options=options.git_log.split(" ")) commits.reverse() if not commits: gbp.log.info("No changes detected from %s to %s." % (since, 'HEAD')) # Do the actual update entries = entries_from_commits(ch_file.changelog, repo, commits, options) update_changelog(ch_file.changelog, entries, repo, spec, options) # Write to file ch_file.write() if editor_cmd: gbpc.Command(editor_cmd, [ch_file.path])() except (GbpError, GitRepositoryError, ChangelogError, NoSpecError) as err: if len(err.__str__()): gbp.log.err(err) return 1 except KeyboardInterrupt: gbp.log.err("Interrupted. Aborting.") return 1 finally: del_tmpdir() return 0
def main(argv): """Script main function""" options, args = parse_args(argv) if not options: return ExitCodes.parse_error try: init_tmpdir(options.tmp_dir, prefix='rpm-ch_') load_customizations(options.customization_file) editor_cmd = determine_editor(options) repo = RpmGitRepository('.') check_branch(repo, options) # Find and parse spec file spec = parse_spec_file(repo, options) # Find and parse changelog file ch_file = parse_changelog_file(repo, spec, options) since = get_start_commit(ch_file.changelog, repo, options) # Get range of commits from where to generate changes if args: gbp.log.info("Only looking for changes in '%s'" % ", ".join(args)) commits = repo.get_commits(since=since, until='HEAD', paths=args, options=options.git_log.split(" ")) commits.reverse() if not commits: gbp.log.info("No changes detected from %s to %s." % (since, 'HEAD')) # Do the actual update entries = entries_from_commits(ch_file.changelog, repo, commits, options) update_changelog(ch_file.changelog, entries, repo, spec, options) # Write to file ch_file.write() if editor_cmd: gbpc.Command(editor_cmd, [ch_file.path])() except (GbpError, GitRepositoryError, ChangelogError, NoSpecError) as err: if len(err.__str__()): gbp.log.err(err) return 1 finally: del_tmpdir() return 0
def main(argv): """Entry point for gbp-buildpackage-rpm""" retval = 0 prefix = "git-" spec = None options, gbp_args, builder_args = parse_args(argv, prefix) if not options: return 1 try: repo = RpmGitRepository(os.path.curdir) except GitRepositoryError: gbp.log.err("%s is not a git repository" % (os.path.abspath('.'))) return 1 # Determine tree-ish to be exported try: tree = get_tree(repo, options.export) except GbpError as err: gbp.log.err('Failed to determine export treeish: %s' % err) return 1 # Re-parse config options with using the per-tree config file(s) from the # exported tree-ish options, gbp_args, builder_args = parse_args(argv, prefix, tree) branch = get_current_branch(repo) try: init_tmpdir(options.tmp_dir, prefix='buildpackage-rpm_') tree = get_tree(repo, options.export) spec = parse_spec(options, repo, treeish=tree) Command(options.cleaner, shell=True)() if not options.ignore_new: ret, out = repo.is_clean() if not ret: gbp.log.err("You have uncommitted changes in your source tree:") gbp.log.err(out) raise GbpError("Use --git-ignore-new to ignore.") if not options.ignore_new and not options.ignore_branch: if branch != options.packaging_branch: gbp.log.err("You are not on branch '%s' but on '%s'" % (options.packaging_branch, branch)) raise GbpError("Use --git-ignore-branch to ignore or " "--git-packaging-branch to set the branch name.") # Dump from git to a temporary directory: packaging_tree = '%s:%s' % (tree, options.packaging_dir) dump_dir = tempfile.mkdtemp(prefix='packaging_') gbp.log.debug("Dumping packaging files to '%s'" % dump_dir) if not dump_tree(repo, dump_dir, packaging_tree, False, False): raise GbpError # Re-parse spec from dump dir to get version etc. spec = rpm.SpecFile(os.path.join(dump_dir, spec.specfile)) if not options.tag_only: # Setup builder opts setup_builder(options, builder_args) if options.use_mock: setup_mock(options) # Prepare final export dirs export_dir = makedir(options.export_dir) source_dir = makedir(os.path.join(export_dir, options.export_sourcedir)) spec_dir = makedir(os.path.join(export_dir, options.export_specdir)) # Move packaging files to final export dir gbp.log.debug("Exporting packaging files from '%s' to '%s'" % (dump_dir, export_dir)) for fname in os.listdir(dump_dir): src = os.path.join(dump_dir, fname) if fname == spec.specfile: dst = os.path.join(spec_dir, fname) else: dst = os.path.join(source_dir, fname) try: shutil.copy2(src, dst) except IOError as err: raise GbpError("Error exporting packaging files: %s" % err) spec.specdir = os.path.abspath(spec_dir) # Get/build the orig tarball if is_native(repo, options): if spec.orig_src and not options.no_create_orig: # Just build source archive from the exported tree gbp.log.info("Creating (native) source archive %s from '%s'" % (spec.orig_src['filename'], tree)) if spec.orig_src['compression']: gbp.log.debug("Building source archive with " "compression '%s -%s'" % (spec.orig_src['compression'], options.comp_level)) orig_prefix = spec.orig_src['prefix'] if not git_archive(repo, spec, source_dir, tree, orig_prefix, options.comp_level, options.with_submodules): raise GbpError("Cannot create source tarball at '%s'" % source_dir) # Non-native packages: create orig tarball from upstream elif spec.orig_src: prepare_upstream_tarball(repo, spec, options, source_dir) # Run postexport hook if options.postexport: RunAtCommand(options.postexport, shell=True, extra_env={'GBP_GIT_DIR': repo.git_dir, 'GBP_TMP_DIR': export_dir} )(dir=export_dir) # Do actual build if not options.no_build and not options.tag_only: if options.prebuild: RunAtCommand(options.prebuild, shell=True, extra_env={'GBP_GIT_DIR': repo.git_dir, 'GBP_BUILD_DIR': export_dir} )(dir=export_dir) # Finally build the package: if options.builder.startswith("rpmbuild"): builder_args.append(os.path.join(spec.specdir, spec.specfile)) else: builder_args.append(spec.specfile) RunAtCommand(options.builder, builder_args, shell=True, extra_env={'GBP_BUILD_DIR': export_dir} )(dir=export_dir) if options.postbuild: changes = os.path.abspath("%s/%s.changes" % (source_dir, spec.name)) gbp.log.debug("Looking for changes file %s" % changes) Command(options.postbuild, shell=True, extra_env={'GBP_CHANGES_FILE': changes, 'GBP_BUILD_DIR': export_dir})() # Tag (note: tags the exported version) if options.tag or options.tag_only: gbp.log.info("Tagging %s" % rpm.compose_version_str(spec.version)) tag = create_packaging_tag(repo, tree, spec.name, spec.version, options) vcs_info = get_vcs_info(repo, tag) if options.posttag: sha = repo.rev_parse("%s^{}" % tag) Command(options.posttag, shell=True, extra_env={'GBP_TAG': tag, 'GBP_BRANCH': branch, 'GBP_SHA1': sha})() else: vcs_info = get_vcs_info(repo, tree) except CommandExecFailed: retval = 1 except GitRepositoryError as err: gbp.log.err("Git command failed: %s" % err) retval = 1 except GbpAutoGenerateError as err: if len(err.__str__()): gbp.log.err(err) retval = 2 except GbpError as err: if len(err.__str__()): gbp.log.err(err) retval = 1 finally: drop_index() del_tmpdir() if not options.tag_only: if spec and options.notify: summary = "Gbp-rpm %s" % ["failed", "successful"][not retval] message = ("Build of %s %s %s" % (spec.name, rpm.compose_version_str(spec.version), ["failed", "succeeded"][not retval])) if not gbp.notifications.notify(summary, message, options.notify): gbp.log.err("Failed to send notification") retval = 1 return retval
def main(argv): """Main function for the gbp pq-rpm command""" retval = 0 (options, args) = parse_args(argv) if not options: return ExitCodes.parse_error gbp.log.setup(options.color, options.verbose, options.color_scheme) if len(args) < 2: gbp.log.err("No action given.") return 1 else: action = args[1] if args[1] in ["export", "import", "rebase", "drop", "switch", "convert"]: pass elif args[1] in ["apply"]: if len(args) != 3: gbp.log.err("No patch name given.") return 1 else: patchfile = args[2] else: gbp.log.err("Unknown action '%s'." % args[1]) return 1 try: repo = RpmGitRepository(os.path.curdir) except GitRepositoryError: gbp.log.err("%s is not a git repository" % (os.path.abspath('.'))) return 1 try: # Create base temporary directory for this run init_tmpdir(options.tmp_dir, prefix='pq-rpm_') current = repo.get_branch() if action == "export": export_patches(repo, options) elif action == "import": import_spec_patches(repo, options) elif action == "drop": drop_pq(repo, current) elif action == "rebase": rebase_pq(repo, options) elif action == "apply": patch = Patch(patchfile) apply_single_patch(repo, current, patch, fallback_author=None) elif action == "switch": switch_pq(repo, current) except KeyboardInterrupt: retval = 1 gbp.log.err("Interrupted. Aborting.") except CommandExecFailed: retval = 1 except GitRepositoryError as err: gbp.log.err("Git command failed: %s" % err) retval = 1 except GbpError as err: if str(err): gbp.log.err(err) retval = 1 finally: del_tmpdir() return retval
def main(argv): """Main function of the git-import-srpm script""" dirs = dict(top=os.path.abspath(os.curdir)) ret = 0 skipped = False options, args = parse_args(argv) if not options: return ExitCodes.parse_error if len(args) == 1: srpm = args[0] target = None elif len(args) == 2: srpm = args[0] target = args[1] else: gbp.log.err("Need to give exactly one package to import. Try --help.") return 1 try: dirs['tmp_base'] = init_tmpdir(options.tmp_dir, 'import-srpm_') except GbpError as err: gbp.log.err(err) return 1 try: if options.download: srpm = download_source(srpm) # Real srpm, we need to unpack, first true_srcrpm = False if not os.path.isdir(srpm) and not srpm.endswith(".spec"): src = parse_srpm(srpm) true_srcrpm = True dirs['pkgextract'] = tempfile.mkdtemp(prefix='pkgextract_') gbp.log.info("Extracting src rpm to '%s'" % dirs['pkgextract']) src.unpack(dirs['pkgextract']) preferred_spec = src.name + '.spec' srpm = dirs['pkgextract'] elif os.path.isdir(srpm): preferred_spec = os.path.basename(srpm.rstrip('/')) + '.spec' else: preferred_spec = None # Find and parse spec file if os.path.isdir(srpm): gbp.log.debug("Trying to import an unpacked srpm from '%s'" % srpm) dirs['src'] = os.path.abspath(srpm) spec = guess_spec(srpm, True, preferred_spec) else: gbp.log.debug("Trying to import an srpm from '%s' with spec " "file '%s'" % (os.path.dirname(srpm), srpm)) dirs['src'] = os.path.abspath(os.path.dirname(srpm)) spec = SpecFile(srpm) # Check the repository state try: repo = RpmGitRepository('.') is_empty = repo.is_empty() (clean, out) = repo.is_clean() if not clean and not is_empty: gbp.log.err("Repository has uncommitted changes, commit " "these first: ") raise GbpError(out) except GitRepositoryError: gbp.log.info("No git repository found, creating one.") is_empty = True target = target or spec.name repo = RpmGitRepository.create(target) os.chdir(repo.path) repo_setup.set_user_name_and_email(options.repo_user, options.repo_email, repo) if repo.bare: set_bare_repo_options(options) # Create more tempdirs dirs['origsrc'] = tempfile.mkdtemp(prefix='origsrc_') dirs['packaging_base'] = tempfile.mkdtemp(prefix='packaging_') dirs['packaging'] = os.path.join(dirs['packaging_base'], options.packaging_dir) try: os.mkdir(dirs['packaging']) except OSError as err: if err.errno != errno.EEXIST: raise if true_srcrpm: # For true src.rpm we just take everything files = os.listdir(dirs['src']) else: # Need to copy files to the packaging directory given by caller files = [os.path.basename(patch.path) for patch in spec.patchseries(unapplied=True, ignored=True)] for filename in spec.sources().values(): files.append(os.path.basename(filename)) files.append(os.path.join(spec.specdir, spec.specfile)) # Don't copy orig source archive, though if spec.orig_src and spec.orig_src['filename'] in files: files.remove(spec.orig_src['filename']) for fname in files: fpath = os.path.join(dirs['src'], fname) if os.path.exists(fpath): shutil.copy2(fpath, dirs['packaging']) else: gbp.log.err("File '%s' listed in spec not found" % fname) raise GbpError # Unpack orig source archive if spec.orig_src: orig_tarball = os.path.join(dirs['src'], spec.orig_src['filename']) sources = RpmUpstreamSource(orig_tarball) sources.unpack(dirs['origsrc'], options.filters) else: sources = None tag_str_fields = dict(spec.version, vendor=options.vendor.lower()) if options.native: src_tag_format = options.packaging_tag src_tag = repo.version_to_tag(src_tag_format, tag_str_fields) upstream_tag = src_tag upstream_str_fields = tag_str_fields else: src_tag_format = options.upstream_tag src_tag = repo.version_to_tag(src_tag_format, tag_str_fields) upstream_str_fields = filter_version(tag_str_fields, 'release', 'epoch') upstream_tag = repo.version_to_tag(src_tag_format, upstream_str_fields) ver_str = compose_version_str(spec.version) if repo.find_version(options.packaging_tag, tag_str_fields): gbp.log.warn("Version %s already imported." % ver_str) if options.allow_same_version: gbp.log.info("Moving tag of version '%s' since import forced" % ver_str) move_tag_stamp(repo, options.packaging_tag, tag_str_fields) else: raise SkipImport if is_empty: options.create_missing_branches = True # Determine author and committer info, currently same info is used # for both sources and packaging files author = None if spec.packager: match = re.match(r'(?P<name>.*[^ ])\s*<(?P<email>\S*)>', spec.packager.strip()) if match: author = GitModifier(match.group('name'), match.group('email')) if not author: author = GitModifier() gbp.log.debug("Couldn't determine packager info") committer = committer_from_author(author, options) # Import sources if sources: src_commit = repo.find_version(src_tag_format, upstream_str_fields) if not src_commit: gbp.log.info("Tag %s not found, importing sources" % src_tag) branch = [options.upstream_branch, options.packaging_branch][options.native] if not repo.has_branch(branch): if options.create_missing_branches: gbp.log.info("Will create missing branch '%s'" % branch) else: gbp.log.err(no_upstream_branch_msg % branch + "\n" "Also check the --create-missing-branches option.") raise GbpError src_vendor = "Native" if options.native else "Upstream" msg = "%s version %s" % (src_vendor, spec.upstreamversion) src_commit = repo.commit_dir(sources.unpacked, "Import %s" % msg, branch, author=author, committer=committer, create_missing_branch=options.create_missing_branches) repo.create_tag(name=src_tag if options.native else upstream_tag, msg=msg, commit=src_commit, sign=options.sign_tags, keyid=options.keyid) if not options.native: if options.pristine_tar: archive_fmt = parse_archive_filename(orig_tarball)[1] if archive_fmt == 'tar': repo.pristine_tar.commit(orig_tarball, 'refs/heads/%s' % options.upstream_branch) else: gbp.log.warn('Ignoring pristine-tar, %s archives ' 'not supported' % archive_fmt) else: gbp.log.info("No orig source archive imported") # Import packaging files. For native packages we assume that also # packaging files are found in the source tarball if not options.native or not sources: gbp.log.info("Importing packaging files") branch = options.packaging_branch if not repo.has_branch(branch): if options.create_missing_branches: gbp.log.info("Will create missing branch '%s'" % branch) else: gbp.log.err(no_packaging_branch_msg % branch + "\n" "Also check the --create-missing-branches " "option.") raise GbpError tag = repo.version_to_tag(options.packaging_tag, tag_str_fields) msg = "%s release %s" % (options.vendor, ver_str) if options.orphan_packaging or not sources: commit = repo.commit_dir(dirs['packaging_base'], "Import %s" % msg, branch, author=author, committer=committer, create_missing_branch=options.create_missing_branches) else: # Copy packaging files to the unpacked sources dir try: pkgsubdir = os.path.join(sources.unpacked, options.packaging_dir) os.mkdir(pkgsubdir) except OSError as err: if err.errno != errno.EEXIST: raise for fname in os.listdir(dirs['packaging']): shutil.copy2(os.path.join(dirs['packaging'], fname), pkgsubdir) commit = repo.commit_dir(sources.unpacked, "Import %s" % msg, branch, other_parents=[src_commit], author=author, committer=committer, create_missing_branch=options.create_missing_branches) # Import patches on top of the source tree # (only for non-native packages with non-orphan packaging) force_to_branch_head(repo, options.packaging_branch) # Create packaging tag repo.create_tag(name=tag, msg=msg, commit=commit, sign=options.sign_tags, keyid=options.keyid) force_to_branch_head(repo, options.packaging_branch) except KeyboardInterrupt: ret = 1 gbp.log.err("Interrupted. Aborting.") except gbpc.CommandExecFailed: ret = 1 except GitRepositoryError as err: gbp.log.err("Git command failed: %s" % err) ret = 1 except GbpError as err: if str(err): gbp.log.err(err) ret = 1 except NoSpecError as err: gbp.log.err("Failed determine spec file: %s" % err) ret = 1 except SkipImport: skipped = True finally: os.chdir(dirs['top']) del_tmpdir() if not ret and not skipped: gbp.log.info("Version '%s' imported under '%s'" % (ver_str, repo.path)) return ret
def main(argv): """Entry point for gbp-buildpackage-rpm""" retval = 0 prefix = "git-" spec = None options, gbp_args, builder_args = parse_args(argv, prefix) if not options: return 1 try: repo = RpmGitRepository(os.path.curdir) except GitRepositoryError: gbp.log.err("%s is not a git repository" % (os.path.abspath('.'))) return 1 # Determine tree-ish to be exported try: tree = get_tree(repo, options.export) except GbpError as err: gbp.log.err('Failed to determine export treeish: %s' % err) return 1 # Re-parse config options with using the per-tree config file(s) from the # exported tree-ish options, gbp_args, builder_args = parse_args(argv, prefix, tree) branch = get_current_branch(repo) try: init_tmpdir(options.tmp_dir, prefix='buildpackage-rpm_') tree = get_tree(repo, options.export) spec = parse_spec(options, repo, treeish=tree) Command(options.cleaner, shell=True)() if not options.ignore_new: ret, out = repo.is_clean() if not ret: gbp.log.err( "You have uncommitted changes in your source tree:") gbp.log.err(out) raise GbpError("Use --git-ignore-new to ignore.") if not options.ignore_new and not options.ignore_branch: if branch != options.packaging_branch: gbp.log.err("You are not on branch '%s' but on '%s'" % (options.packaging_branch, branch)) raise GbpError( "Use --git-ignore-branch to ignore or " "--git-packaging-branch to set the branch name.") # Dump from git to a temporary directory: packaging_tree = '%s:%s' % (tree, options.packaging_dir) dump_dir = tempfile.mkdtemp(prefix='packaging_') gbp.log.debug("Dumping packaging files to '%s'" % dump_dir) if not dump_tree(repo, dump_dir, packaging_tree, False, False): raise GbpError # Re-parse spec from dump dir to get version etc. spec = rpm.SpecFile(os.path.join(dump_dir, spec.specfile)) if not options.tag_only: # Setup builder opts setup_builder(options, builder_args) if options.use_mock: setup_mock(options) # Prepare final export dirs export_dir = makedir(options.export_dir) source_dir = makedir( os.path.join(export_dir, options.export_sourcedir)) spec_dir = makedir(os.path.join(export_dir, options.export_specdir)) # Move packaging files to final export dir gbp.log.debug("Exporting packaging files from '%s' to '%s'" % (dump_dir, export_dir)) for fname in os.listdir(dump_dir): src = os.path.join(dump_dir, fname) if fname == spec.specfile: dst = os.path.join(spec_dir, fname) else: dst = os.path.join(source_dir, fname) try: shutil.copy2(src, dst) except IOError as err: raise GbpError("Error exporting packaging files: %s" % err) spec.specdir = os.path.abspath(spec_dir) # Get/build the orig tarball if is_native(repo, options): if spec.orig_src and not options.no_create_orig: # Just build source archive from the exported tree gbp.log.info( "Creating (native) source archive %s from '%s'" % (spec.orig_src['filename'], tree)) if spec.orig_src['compression']: gbp.log.debug( "Building source archive with " "compression '%s -%s'" % (spec.orig_src['compression'], options.comp_level)) orig_prefix = spec.orig_src['prefix'] if not git_archive(repo, spec, source_dir, tree, orig_prefix, options.comp_level, options.with_submodules): raise GbpError("Cannot create source tarball at '%s'" % source_dir) # Non-native packages: create orig tarball from upstream elif spec.orig_src: prepare_upstream_tarball(repo, spec, options, source_dir) # Run postexport hook if options.postexport: RunAtCommand(options.postexport, shell=True, extra_env={ 'GBP_GIT_DIR': repo.git_dir, 'GBP_TMP_DIR': export_dir })(dir=export_dir) # Do actual build if not options.no_build and not options.tag_only: if options.prebuild: RunAtCommand(options.prebuild, shell=True, extra_env={ 'GBP_GIT_DIR': repo.git_dir, 'GBP_BUILD_DIR': export_dir })(dir=export_dir) # Finally build the package: if options.builder.startswith("rpmbuild"): builder_args.append( os.path.join(spec.specdir, spec.specfile)) else: builder_args.append(spec.specfile) RunAtCommand(options.builder, builder_args, shell=True, extra_env={'GBP_BUILD_DIR': export_dir})(dir=export_dir) if options.postbuild: changes = os.path.abspath("%s/%s.changes" % (source_dir, spec.name)) gbp.log.debug("Looking for changes file %s" % changes) Command(options.postbuild, shell=True, extra_env={ 'GBP_CHANGES_FILE': changes, 'GBP_BUILD_DIR': export_dir })() # Tag (note: tags the exported version) if options.tag or options.tag_only: gbp.log.info("Tagging %s" % rpm.compose_version_str(spec.version)) tag = create_packaging_tag(repo, tree, spec.name, spec.version, options) vcs_info = get_vcs_info(repo, tag) if options.posttag: sha = repo.rev_parse("%s^{}" % tag) Command(options.posttag, shell=True, extra_env={ 'GBP_TAG': tag, 'GBP_BRANCH': branch, 'GBP_SHA1': sha })() else: vcs_info = get_vcs_info(repo, tree) except CommandExecFailed: retval = 1 except GitRepositoryError as err: gbp.log.err("Git command failed: %s" % err) retval = 1 except GbpAutoGenerateError as err: if len(err.__str__()): gbp.log.err(err) retval = 2 except GbpError as err: if len(err.__str__()): gbp.log.err(err) retval = 1 finally: drop_index() del_tmpdir() if not options.tag_only: if spec and options.notify: summary = "Gbp-rpm %s" % ["failed", "successful"][not retval] message = ("Build of %s %s %s" % (spec.name, rpm.compose_version_str( spec.version), ["failed", "succeeded"][not retval])) if not gbp.notifications.notify(summary, message, options.notify): gbp.log.err("Failed to send notification") retval = 1 return retval
def main(argv): """Main function of the git-import-srpm script""" dirs = dict(top=os.path.abspath(os.curdir)) ret = 0 skipped = False options, args = parse_args(argv) if not options: return ExitCodes.parse_error if len(args) == 1: srpm = args[0] target = None elif len(args) == 2: srpm = args[0] target = args[1] else: gbp.log.err("Need to give exactly one package to import. Try --help.") return 1 try: dirs['tmp_base'] = init_tmpdir(options.tmp_dir, 'import-srpm_') except GbpError as err: gbp.log.err(err) return 1 try: if options.download: srpm = download_source(srpm) # Real srpm, we need to unpack, first true_srcrpm = False if not os.path.isdir(srpm) and not srpm.endswith(".spec"): src = parse_srpm(srpm) true_srcrpm = True dirs['pkgextract'] = tempfile.mkdtemp(prefix='pkgextract_') gbp.log.info("Extracting src rpm to '%s'" % dirs['pkgextract']) src.unpack(dirs['pkgextract']) preferred_spec = src.name + '.spec' srpm = dirs['pkgextract'] elif os.path.isdir(srpm): preferred_spec = os.path.basename(srpm.rstrip('/')) + '.spec' else: preferred_spec = None # Find and parse spec file if os.path.isdir(srpm): gbp.log.debug("Trying to import an unpacked srpm from '%s'" % srpm) dirs['src'] = os.path.abspath(srpm) spec = guess_spec(srpm, True, preferred_spec) else: gbp.log.debug("Trying to import an srpm from '%s' with spec " "file '%s'" % (os.path.dirname(srpm), srpm)) dirs['src'] = os.path.abspath(os.path.dirname(srpm)) spec = SpecFile(srpm) # Check the repository state try: repo = RpmGitRepository('.') is_empty = repo.is_empty() (clean, out) = repo.is_clean() if not clean and not is_empty: gbp.log.err("Repository has uncommitted changes, commit " "these first: ") raise GbpError(out) except GitRepositoryError: gbp.log.info("No git repository found, creating one.") is_empty = True target = target or spec.name repo = RpmGitRepository.create(target) os.chdir(repo.path) repo_setup.set_user_name_and_email(options.repo_user, options.repo_email, repo) if repo.bare: set_bare_repo_options(options) # Create more tempdirs dirs['origsrc'] = tempfile.mkdtemp(prefix='origsrc_') dirs['packaging_base'] = tempfile.mkdtemp(prefix='packaging_') dirs['packaging'] = os.path.join(dirs['packaging_base'], options.packaging_dir) try: os.mkdir(dirs['packaging']) except OSError as err: if err.errno != errno.EEXIST: raise if true_srcrpm: # For true src.rpm we just take everything files = os.listdir(dirs['src']) else: # Need to copy files to the packaging directory given by caller files = [os.path.basename(patch.path) for patch in spec.patchseries(unapplied=True, ignored=True)] for filename in spec.sources().values(): files.append(os.path.basename(filename)) files.append(os.path.join(spec.specdir, spec.specfile)) # Don't copy orig source archive, though if spec.orig_src and spec.orig_src['filename'] in files: files.remove(spec.orig_src['filename']) for fname in files: fpath = os.path.join(dirs['src'], fname) if os.path.exists(fpath): shutil.copy2(fpath, dirs['packaging']) else: gbp.log.err("File '%s' listed in spec not found" % fname) raise GbpError # Unpack orig source archive if spec.orig_src: orig_tarball = os.path.join(dirs['src'], spec.orig_src['filename']) sources = RpmUpstreamSource(orig_tarball) sources.unpack(dirs['origsrc'], options.filters) else: sources = None tag_str_fields = dict(spec.version, vendor=options.vendor.lower()) if options.native: upstream_tag_format = options.packaging_tag upstream_str_fields = tag_str_fields else: upstream_tag_format = options.upstream_tag upstream_str_fields = filter_version(tag_str_fields, 'release', 'epoch') upstream_tag = repo.version_to_tag(upstream_tag_format, upstream_str_fields) full_version = compose_version_str(spec.version) if repo.find_version(options.packaging_tag, tag_str_fields): gbp.log.warn("Version %s already imported." % full_version) if options.allow_same_version: gbp.log.info("Moving tag of version '%s' since import forced" % full_version) move_tag_stamp(repo, options.packaging_tag, tag_str_fields) else: raise SkipImport if is_empty: options.create_missing_branches = True # Determine author and committer info, currently same info is used # for both sources and packaging files author = None if spec.packager: match = re.match(r'(?P<name>.*[^ ])\s*<(?P<email>\S*)>', spec.packager.strip()) if match: author = GitModifier(match.group('name'), match.group('email')) if not author: author = GitModifier() gbp.log.debug("Couldn't determine packager info") committer = committer_from_author(author, options) # Import sources if sources: upstream_commit = repo.find_version(upstream_tag_format, upstream_str_fields) if not upstream_commit: gbp.log.info("Tag %s not found, importing sources" % upstream_tag) branch = [options.upstream_branch, options.packaging_branch][options.native] if not repo.has_branch(branch): if options.create_missing_branches: gbp.log.info("Will create missing branch '%s'" % branch) else: gbp.log.err(no_upstream_branch_msg % branch + "\n" "Also check the --create-missing-branches option.") raise GbpError upstream_vendor = "Native" if options.native else "Upstream" upstream_version = full_version if options.native else spec.upstreamversion msg = "%s version %s" % (upstream_vendor, upstream_version) if options.vcs_tag: vcs_tag = repo.version_to_tag(options.vcs_tag, upstream_str_fields) parents = [repo.rev_parse("%s^{}" % vcs_tag)] else: parents = None upstream_commit = repo.commit_dir(sources.unpacked, "Import %s" % msg, branch, other_parents=parents, author=author, committer=committer, create_missing_branch=options.create_missing_branches) if not (options.native and options.skip_packaging_tag): repo.create_tag(name=upstream_tag, msg=msg, commit=upstream_commit, sign=options.sign_tags, keyid=options.keyid) if not options.native: if options.pristine_tar: archive_fmt = Archive.parse_filename(orig_tarball)[1] if archive_fmt == 'tar': repo.pristine_tar.commit(orig_tarball, 'refs/heads/%s' % options.upstream_branch) else: gbp.log.warn('Ignoring pristine-tar, %s archives ' 'not supported' % archive_fmt) else: gbp.log.info("No orig source archive imported") # Import packaging files. For native packages we assume that also # packaging files are found in the source tarball if not options.native or not sources: gbp.log.info("Importing packaging files") branch = options.packaging_branch if not repo.has_branch(branch): if options.create_missing_branches: gbp.log.info("Will create missing branch '%s'" % branch) else: gbp.log.err(no_packaging_branch_msg % branch + "\n" "Also check the --create-missing-branches " "option.") raise GbpError msg = "%s release %s" % (options.vendor, full_version) if options.orphan_packaging or not sources: commit = repo.commit_dir(dirs['packaging_base'], "Import %s" % msg, branch, author=author, committer=committer, create_missing_branch=options.create_missing_branches) else: # Copy packaging files to the unpacked sources dir try: pkgsubdir = os.path.join(sources.unpacked, options.packaging_dir) os.mkdir(pkgsubdir) except OSError as err: if err.errno != errno.EEXIST: raise for fname in os.listdir(dirs['packaging']): shutil.copy2(os.path.join(dirs['packaging'], fname), pkgsubdir) commit = repo.commit_dir(sources.unpacked, "Import %s" % msg, branch, other_parents=[upstream_commit], author=author, committer=committer, create_missing_branch=options.create_missing_branches) # Import patches on top of the source tree # (only for non-native packages with non-orphan packaging) force_to_branch_head(repo, options.packaging_branch) # Create packaging tag if not options.skip_packaging_tag: tag = repo.version_to_tag(options.packaging_tag, tag_str_fields) repo.create_tag(name=tag, msg=msg, commit=commit, sign=options.sign_tags, keyid=options.keyid) force_to_branch_head(repo, options.packaging_branch) except KeyboardInterrupt: ret = 1 gbp.log.err("Interrupted. Aborting.") except gbpc.CommandExecFailed: ret = 1 except GitRepositoryError as err: gbp.log.err("Git command failed: %s" % err) ret = 1 except GbpError as err: if str(err): gbp.log.err(err) ret = 1 except NoSpecError as err: gbp.log.err("Failed determine spec file: %s" % err) ret = 1 except SkipImport: skipped = True finally: os.chdir(dirs['top']) del_tmpdir() if not ret and not skipped: gbp.log.info("Version '%s' imported under '%s'" % (full_version, repo.path)) return ret