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 len(args) != 1: gbp.log.err("Need to give exactly one package to import. Try --help.") return 1 try: dirs['tmp_base'] = tempfile.mkdtemp(dir=options.tmp_dir, prefix='import-srpm') except GbpError as err: gbp.log.err(err) return 1 try: srpm = args[0] if options.download: srpm = download_source(srpm, dirs) # 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(dir=dirs['tmp_base'], 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 repo = RpmGitRepository.create(spec.name) os.chdir(repo.path) if repo.bare: set_bare_repo_options(options) # Create more tempdirs dirs['origsrc'] = tempfile.mkdtemp(dir=dirs['tmp_base'], prefix='origsrc_') dirs['packaging_base'] = tempfile.mkdtemp(dir=dirs['tmp_base'], 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 src_tag_format = options.packaging_tag if options.native \ else options.upstream_tag tag_str_fields = dict(spec.version, vendor=options.vendor.lower()) src_tag = repo.version_to_tag(src_tag_format, tag_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, tag_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, "Imported %s" % msg, branch, author=author, committer=committer, create_missing_branch=options.create_missing_branches) repo.create_tag(name=src_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'], "Imported %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, "Imported %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']) gbpc.RemoveTree(dirs['tmp_base'])() if not ret and not skipped: gbp.log.info("Version '%s' imported under '%s'" % (ver_str, spec.name)) return ret
def main(argv): dirs = dict(top=os.path.abspath(os.curdir)) needs_repo = False ret = 1 skipped = False options, pkg, target = parse_all(argv) if not options: return ExitCodes.parse_error try: try: repo = DebianGitRepository('.') # remember if the was repo initially empty repo.empty = repo.is_empty() (clean, out) = repo.is_clean() if not clean and not repo.empty: raise GbpError( "Repository has uncommitted changes, commit these first: %s" % out) except GitRepositoryError: # no repo found, create one needs_repo = True if options.download: dscfile = download_source(pkg, dirs=dirs, unauth=options.allow_unauthenticated) else: dscfile = pkg dsc = DscFile.parse(dscfile) if dsc.pkgformat not in ['1.0', '3.0']: raise GbpError("Importing %s source format not yet supported." % dsc.pkgformat) if options.verbose: print_dsc(dsc) if needs_repo: target = target or dsc.pkg if os.path.exists(target): raise GbpError( "Directory '%s' already exists. If you want to import into it, " "please change into this directory otherwise move it away first." % target) gbp.log.info("No git repository found, creating one.") repo = DebianGitRepository.create(target) repo.empty = True os.chdir(repo.path) repo_setup.set_user_name_and_email(options.repo_user, options.repo_email, repo) if repo.bare: disable_pristine_tar(options, "Bare repository") # unpack dirs['tmp'] = os.path.abspath(tempfile.mkdtemp(dir='..')) source = DebianUpstreamSource(dsc.tgz) source.unpack(dirs['tmp'], options.filters) for (component, tarball) in dsc.additional_tarballs.items(): gbp.log.info("Found component tarball '%s'" % os.path.basename(tarball)) unpack_component_tarball(source.unpacked, component, tarball, options.filters) if repo.find_version(options.debian_tag, dsc.version): gbp.log.warn("Version %s already imported." % dsc.version) if options.allow_same_version: gbp.log.info("Moving tag of version '%s' since import forced" % dsc.version) move_tag_stamp(repo, options.debian_tag, dsc.version) else: raise SkipImport # import if dsc.native: import_native(repo, source, dsc, options) else: imported = False commit = repo.find_version(options.upstream_tag, dsc.upstream_version) if not repo.find_version(options.upstream_tag, dsc.upstream_version): commit = import_upstream(repo, source, dsc, options) imported = True if not repo.has_branch(options.debian_branch): if options.create_missing_branches: repo.create_branch(options.debian_branch, commit) else: raise GbpError( "Branch %s does not exist, use --create-missing-branches" % options.debian_branch) if dsc.diff or dsc.deb_tgz: apply_debian_patch(repo, source, dsc, commit, options) else: gbp.log.warn("Didn't find a diff to apply.") if imported and options.pristine_tar: repo.create_pristine_tar_commits( commit, dsc.tgz, dsc.additional_tarballs.items()) if repo.get_branch() == options.debian_branch or repo.empty: # Update HEAD if we modified the checked out branch repo.force_head(options.debian_branch, hard=True) ret = 0 except KeyboardInterrupt: gbp.log.err("Interrupted. Aborting.") except gbpc.CommandExecFailed: pass # command itself printed an error except GitRepositoryError as msg: gbp.log.err("Git command failed: %s" % msg) except GbpError as err: if str(err): gbp.log.err(err) except SkipImport: skipped = True ret = 0 finally: os.chdir(dirs['top']) for d in ['tmp', 'download']: if d in dirs: gbpc.RemoveTree(dirs[d])() if not ret and not skipped: gbp.log.info("Version '%s' imported under '%s'" % (dsc.version, repo.path)) return ret
def cleanup_tmp_tree(tree): """remove a tree of temporary files""" try: gbpc.RemoveTree(tree)() except gbpc.CommandExecFailed: gbp.log.err("Removal of tmptree %s failed." % tree)
def main(argv): dirs = dict(top=os.path.abspath(os.curdir)) needs_repo = False ret = 0 skipped = False options, args = parse_args(argv) if not options: return 1 try: if len(args) != 1: gbp.log.err( "Need to give exactly one package to import. Try --help.") raise GbpError else: try: repo = DebianGitRepository('.') 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: # no repo found, create one needs_repo = True is_empty = True pkg = args[0] if options.download: dsc = download_source(pkg, dirs=dirs, unauth=options.allow_unauthenticated) else: dsc = pkg src = DscFile.parse(dsc) if src.pkgformat not in ['1.0', '3.0']: raise GbpError( "Importing %s source format not yet supported." % src.pkgformat) if options.verbose: print_dsc(src) if needs_repo: if os.path.exists(src.pkg): raise GbpError( "Directory '%s' already exists. If you want to import into it, " "please change into this directory otherwise move it away first." % src.pkg) gbp.log.info("No git repository found, creating one.") repo = DebianGitRepository.create(src.pkg) os.chdir(repo.path) if repo.bare: disable_pristine_tar(options, "Bare repository") dirs['tmp'] = os.path.abspath(tempfile.mkdtemp(dir='..')) upstream = DebianUpstreamSource(src.tgz) upstream.unpack(dirs['tmp'], options.filters) for tarball in src.additional_tarballs.values(): gbp.log.info("Found component tarball '%s'" % os.path.basename(tarball)) subtarball = DebianUpstreamSource(tarball) subtarball.unpack(upstream.unpacked, options.filters) format = [(options.upstream_tag, "Upstream"), (options.debian_tag, "Debian")][src.native] tag = repo.version_to_tag(format[0], src.upstream_version) msg = "%s version %s" % (format[1], src.upstream_version) if repo.find_version(options.debian_tag, src.version): gbp.log.warn("Version %s already imported." % src.version) if options.allow_same_version: gbp.log.info( "Moving tag of version '%s' since import forced" % src.version) move_tag_stamp(repo, options.debian_tag, src.version) else: raise SkipImport if not repo.find_version(format[0], src.upstream_version): gbp.log.info("Tag %s not found, importing %s tarball" % (tag, format[1])) if is_empty: branch = None else: branch = [options.upstream_branch, options.debian_branch][src.native] if not repo.has_branch(branch): if options.create_missing_branches: gbp.log.info("Creating missing branch '%s'" % branch) repo.create_branch(branch) else: gbp.log.err( no_upstream_branch_msg % branch + "\nAlso check the --create-missing-branches option." ) raise GbpError if src.native: author = get_author_from_changelog(upstream.unpacked) committer = get_committer_from_author(author, options) else: author = committer = {} commit = repo.commit_dir(upstream.unpacked, "Imported %s" % msg, branch, author=author, committer=committer) if not (src.native and options.skip_debian_tag): repo.create_tag(name=tag, msg=msg, commit=commit, sign=options.sign_tags, keyid=options.keyid) if not src.native: if is_empty: repo.create_branch(options.upstream_branch, commit) if options.pristine_tar: generate_pristine_tarballs(repo, src, options.upstream_branch) if (not repo.has_branch(options.debian_branch) and (is_empty or options.create_missing_branches)): repo.create_branch(options.debian_branch, commit) if not src.native: if src.diff or src.deb_tgz: apply_debian_patch(repo, upstream.unpacked, src, options, tag) else: gbp.log.warn("Didn't find a diff to apply.") if repo.get_branch() == options.debian_branch or is_empty: # Update HEAD if we modified the checked out branch repo.force_head(options.debian_branch, hard=True) except KeyboardInterrupt: ret = 1 gbp.log.err("Interrupted. Aborting.") except gbpc.CommandExecFailed: ret = 1 except GitRepositoryError as msg: gbp.log.err("Git command failed: %s" % msg) ret = 1 except GbpError as err: if str(err): gbp.log.err(err) ret = 1 except SkipImport: skipped = True finally: os.chdir(dirs['top']) for d in ['tmp', 'download']: if d in dirs: gbpc.RemoveTree(dirs[d])() if not ret and not skipped: gbp.log.info("Version '%s' imported under '%s'" % (src.version, src.pkg)) return ret
def main(argv): """Main function of the gbp import-bb script""" dirs = dict(top=os.path.abspath(os.curdir)) ret = 0 skipped = False if not bb: return 1 options, args = parse_args(argv) if len(args) == 0 or len(args) > 2: gbp.log.err("Need to give exactly one package to import. Try --help.") return 1 try: dirs['tmp_base'] = tempfile.mkdtemp(dir=options.tmp_dir, prefix='import-bb') tinfoil = init_tinfoil() pkg_bb = guess_pkg(tinfoil, args[0]) dirs['src'] = os.path.abspath(os.path.dirname(pkg_bb)) gbp.log.info("Importing '%s' from '%s'" % (os.path.basename(pkg_bb), dirs['src'])) pkg_data = bb.cache.Cache.loadDataFull(pkg_bb, [], tinfoil.config_data) # Determine target repo dir target_dir = '' if len(args) == 2: target_dir = args[1] else: if 'BUILDDIR' in os.environ: target_dir = os.path.join(os.environ['BUILDDIR'], 'devel') target_dir = os.path.join(target_dir, pkg_data.getVar('PN', True)) # Check the Git repository state repo = init_repo(target_dir) if repo.bare: set_bare_repo_options(options) if repo.is_empty(): options.create_missing_branches = True os.chdir(repo.path) # Create more tempdirs dirs['origsrc'] = tempfile.mkdtemp(dir=dirs['tmp_base'], prefix='origsrc_') dirs['packaging_base'] = tempfile.mkdtemp(dir=dirs['tmp_base'], prefix='packaging_') dirs['packaging'] = os.path.join(dirs['packaging_base'], options.meta_dir) # Copy (local) packaging files to tmp dir remote_srcs = bb_get_files(pkg_data, dirs['packaging']) version_dict = pkg_version(pkg_data) tag_str_fields = dict(version_dict, vendor=options.vendor.lower()) ver_str = RpmPkgPolicy.compose_full_version(version_dict) # Check if the same version of the package is already imported 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 # Import upstream sources import_upstream_sources(repo, pkg_data, remote_srcs, dirs, options) # Import packaging files gbp.log.info("Importing local meta/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) commit = repo.commit_dir(dirs['packaging_base'], "Imported %s" % msg, branch, create_missing_branch=options.create_missing_branches) # 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 len(err.__str__()): gbp.log.err(err) ret = 1 except SkipImport: skipped = True finally: os.chdir(dirs['top']) gbpc.RemoveTree(dirs['tmp_base'])() if not ret and not skipped: gbp.log.info("Version '%s' imported under '%s'" % (ver_str, repo.path)) return ret
def main(argv): dirs = dict(top=os.path.abspath(os.curdir)) dscs = [] ret = 0 verbose = False dsc_key = cmp_to_key(DscCompareVersions()) use_debsnap = False try: import_args = argv[1:] if '--verbose' in import_args: verbose = True gbp.log.setup(False, verbose) if '--ignore-repo-config' in import_args: set_gbp_conf_files() import_args.remove('--ignore-repo-config') # Not using Configparser since we want to pass all unknown options # unaltered to gbp import-dsc if '--debsnap' in import_args: use_debsnap = True import_args.remove('--debsnap') if import_args == []: print_help() raise GbpError pkg = import_args[-1] import_args = import_args[:-1] else: for arg in argv[::-1]: if arg.endswith('.dsc'): dscs.append(DscFile.parse(arg)) import_args.remove(arg) if not use_debsnap and not dscs: print_help() raise GbpError if use_debsnap: dirs['tmp'] = os.path.abspath(tempfile.mkdtemp()) dscs = [ DscFile.parse(f) for f in fetch_snapshots(pkg, dirs['tmp']) ] dscs.sort(key=dsc_key) importer = GitImportDsc(import_args) try: repo = GitRepository('.') (clean, out) = repo.is_clean() if not clean: gbp.log.err("Repository has uncommitted changes, " "commit these first: ") raise GbpError(out) else: dirs['pkg'] = dirs['top'] except GitRepositoryError: # no git repository there yet dirs['pkg'] = os.path.join(dirs['top'], dscs[0].pkg) if importer.importdsc(dscs[0]): raise GbpError("Failed to import '%s'" % dscs[0].dscfile) os.chdir(dirs['pkg']) for dsc in dscs[1:]: if importer.importdsc(dsc): raise GbpError("Failed to import '%s'" % dscs[0].dscfile) except KeyboardInterrupt: ret = 1 gbp.log.err("Interrupted. Aborting.") except (GbpError, gbpc.CommandExecFailed, GitRepositoryError) as err: if str(err): gbp.log.err(err) ret = 1 finally: if 'tmp' in dirs: gbpc.RemoveTree(dirs['tmp'])() os.chdir(dirs['top']) if not ret: gbp.log.info('Everything imported under %s' % dirs['pkg']) return ret