def patches_base_ref(default=exception.CantGuess): """Return a git reference to patches branch base. Returns first part of .spec's patches_base is found, otherwise return Version(+%{milestone}). """ ref = None try: spec = specfile.Spec() ref, _ = spec.get_patches_base(expand_macros=True) if ref: ref, _ = tag2version(ref) else: ref = spec.get_tag('Version', expand_macros=True) milestone = spec.get_milestone() if milestone: ref += milestone if not ref: raise exception.CantGuess(msg="got empty .spec Version") except Exception as ex: if default is exception.CantGuess: raise exception.CantGuess(what="current package version", why=str(ex)) else: return default tag_style = version_tag_style(ref) return version2tag(ref, tag_style=tag_style)
def get_diff_range(diff_range=None, patches_branch=None, branch=None): vtag_from, vtag_to = None, None if diff_range: n = len(diff_range) if n > 2: raise exception.InvalidUsage(why="diff only supports one or two " "positional parameters.") if n == 2: vtag_from, vtag_to = diff_range else: vtag_to = diff_range[0] if not vtag_from: if not patches_branch: if not branch: branch = guess.current_branch() patches_branch = guess.patches_branch(branch) if not git.ref_exists('refs/remotes/%s' % patches_branch): msg = ("Patches branch not found: %s\n" "Can't guess current version.\n\n" "a) provide git tags/refs yourself a la:\n" " $ rdopkg reqdiff 1.1.1 2.2.2\n\n" "b) add git remote with expected patches branch" % patches_branch) raise exception.CantGuess(msg=msg) vtag_from = git.get_latest_tag(branch=patches_branch) if not vtag_to: upstream_branch = guess.upstream_branch() vtag_to = git.get_latest_tag(branch=upstream_branch) return { 'version_tag_from': vtag_from, 'version_tag_to': vtag_to }
def current_branch(default=exception.CantGuess): try: branch = git.current_branch() except exception.CommandFailed: if default is exception.CantGuess: raise exception.CantGuess( what="branch", why="git command failed (not in a git repo?)") else: return default if not branch: if default is exception.CantGuess: raise exception.CantGuess(what="branch", why="git command returned no output") else: return default return branch
def package(default=exception.CantGuess): pkg = os.path.basename(os.getcwd()) if not pkg: if default is exception.CantGuess: raise exception.CantGuess(what="package", why="failed to parse current directory") else: return default return pkg
def osreleasedist(branch=None, default=exception.CantGuess): if branch is None: try: branch = current_branch() except exception.CantGuess as ex: if default is exception.CantGuess: raise exception.CantGuess(what="release", why=ex.kwargs['why']) else: return default rls, dist = osreleasedist_rdoinfo(branch) if rls: return rls, dist if default is exception.CantGuess: raise exception.CantGuess(what="release", why="unknown branch '%s'" % branch) else: return default
def copr_check(release=None, dist=None): osdist = guess.osdist() if osdist == 'RHOS': helpers.confirm("Look like you're trying to build RHOS package in " "public copr.\nProceed anyway?") if not release: raise exception.CantGuess(what='release', why="Specify with -r/--release") if not dist: builds = guess.builds(release=release) for dist_, src in builds: if src.startswith('copr/jruzicka'): dist = dist_ log.info("Autodetected dist: %s" % dist) break if not dist: raise exception.CantGuess(what='dist', why="Specify with -d/--dist") return {'dist': dist}
def ensure_patches_branch(patches_branch=None, local_patches=False, bump_only=False, patches_style=None): if local_patches or bump_only: return if not patches_branch: raise exception.CantGuess( what='remote patches branch', why="Specify with --patches-branch or use --local-patches") if not git.ref_exists('refs/remotes/%s' % patches_branch): raise exception.ConfigError( what=("remote patches branch '%s' not found. \n\n" "Specify with -p/--patches-branch, use -l/--local-patches, " "or skip patches branch operations with -b/--bump-only" % patches_branch))
def fetch_patches_branch(local_patches_branch, gerrit_patches_chain, force=False): review_n = _review_number(gerrit_patches_chain) gerrit_host, gerrit_port = guess.gerrit_from_repo() query = GerritQuery(gerrit_host, gerrit_port) review = query('--current-patch-set', review_n) current_ps = review.get('currentPatchSet', {}) patchset_n = current_ps.get('number') if not patchset_n: raise exception.CantGuess( msg='Failed to determine current patch set for review: %s' % gerrit_patches_chain) gerrit_ref = _review_ref(review_n, patchset_n) git('fetch', 'patches', gerrit_ref) approvals = current_ps.get('approvals', []) jenkins = [ a for a in approvals if a.get('type') == 'Verified' and a.get('by', {}).get('username') == 'jenkins' ] code_reviews = [ int(a.get('Value', 0)) for a in approvals if a.get('type') == 'Code-Review' ] if not jenkins: verified = 0 else: verified = int(jenkins[0]['value']) if verified != 1: if force: log.warn("Ref %s has not been validated by CI." % gerrit_patches_chain) helpers.confirm("Do you want to continue anyway?", default_yes=False) else: raise exception.UnverifiedPatch() if any(cr < 0 for cr in code_reviews): log.warn("Ref %s has at least one negative review." % gerrit_patches_chain) helpers.confirm("Do you want to continue anyway?", default_yes=False) git('update-ref', 'refs/heads/%s' % local_patches_branch, 'FETCH_HEAD')
def reqcheck(version, spec=False): if version.upper() == 'XXX': if 'upstream' in git.remotes(): current_branch = git.current_branch() branch = current_branch.replace('rpm-', '') if branch != 'master': branch = 'stable/{}'.format(branch) version = 'upstream/{}'.format(branch) check = _reqs.reqcheck_spec(ref=version) else: m = re.search(r'/([^/]+)_distro', os.getcwd()) if not m: raise exception.CantGuess(what="requirements.txt location", why="failed to parse current path") path = '../%s/requirements.txt' % m.group(1) log.info("Delorean detected. Using %s" % path) check = _reqs.reqcheck_spec(reqs_txt=path) else: check = _reqs.reqcheck_spec(ref=version) format = None if spec: format = 'spec' _reqs.print_reqcheck(*check, format=format)
def new_version_setup(patches_branch=None, local_patches=False, version=None, new_version=None, version_tag_style=None, new_sources=None, no_new_sources=None): args = {} if new_version: # support both version and tag ver, _ = guess.tag2version(new_version) if ver != new_version: new_version = ver args['new_version'] = new_version new_version_tag = guess.version2tag(new_version, version_tag_style) else: ub = guess.upstream_branch() if not git.ref_exists('refs/remotes/%s' % ub): msg = ("Upstream branch not found: %s\n" "Can't guess latest version.\n\n" "a) provide new version (git tag) yourself\n" " $ rdopkg new-version 1.2.3\n\n" "b) add upstream git remote:\n" " $ git remote add -f upstream GIT_URL\n" % ub) raise exception.CantGuess(msg=msg) new_version_tag = git.get_latest_tag(ub) new_version, _ = guess.tag2version(new_version_tag) args['new_version'] = new_version log.info("Latest version detected from %s: %s" % (ub, new_version)) if version == new_version: helpers.confirm( msg="It seems the package is already at version %s\n\n" "Run new-version anyway?" % version, default_yes=False) args['changes'] = ['Update to %s' % new_version] args['new_patches_base'] = new_version_tag spec = specfile.Spec() rpm_version = spec.get_tag('Version') rpm_milestone = spec.get_milestone() new_rpm_version, new_milestone = specfile.version_parts(new_version) args['new_rpm_version'] = new_rpm_version if new_milestone: args['new_milestone'] = new_milestone if (rpm_version != new_rpm_version or bool(new_milestone) != bool(rpm_milestone)): if new_milestone: args['new_release'] = '0.1' else: args['new_release'] = '1' if not local_patches: if not patches_branch or \ not git.ref_exists('refs/remotes/' + patches_branch): log.warn("Patches branch '%s' not found. Running in --bump-only " "mode." % patches_branch) args['bump_only'] = True if new_sources or no_new_sources: if new_sources and no_new_sources: raise exception.InvalidUsage( msg="DOES NOT COMPUTE: both -n and -N don't make sense.") # new_sources == not no_new_sources else: new_sources = guess.new_sources() args['new_sources'] = new_sources return args
def email(): email = git('config', 'user.email', log_cmd=False, fatal=False) if not email: raise exception.CantGuess(what="user email", why='git config user.email not set') return email
def user(): user = git('config', 'user.name', log_cmd=False, fatal=False) if not user: raise exception.CantGuess(what="user name", why='git config user.name not set') return user
def reqquery(reqs_file=None, reqs_ref=None, spec=False, filter=None, dump=None, dump_file=None, load=None, load_file=None, verbose=False): if not (reqs_ref or reqs_file or spec or load or load_file): reqs_ref = guess.patches_base_ref() if not (bool(reqs_ref) ^ bool(reqs_file) ^ bool(spec) ^ bool(load) ^ bool(load_file)): raise exception.InvalidUsage( why="Only one requirements source (-r/-R/-s/-l/-L) can be " "selected.") if dump and dump_file: raise exception.InvalidUsage( why="Only one dump method (-d/-D) can be selected.") if dump: dump_file = 'requirements.yml' if load: load_file = 'requirements.yml' # get query results as requested if load_file: log.info("Loading query results from file: %s" % load_file) r = yaml.load(open(load_file)) else: release, dist = None, None if not filter: try: release, dist = guess.osreleasedist() log.info('Autodetected filter: %s/%s' % (release, dist)) except exception.CantGuess as ex: raise exception.CantGuess( msg='%s\n\nPlease select RELEASE(/DIST) filter to query.' % str(ex)) else: release, _, dist = filter.partition('/') module2pkg = True if reqs_file: log.info("Querying requirements file: %s" % reqs_file) reqs = _reqs.get_reqs_from_path(reqs_file) elif reqs_ref: log.info("Querying requirements file from git: " "%s -- requirements.txt" % reqs_ref) reqs = _reqs.get_reqs_from_ref(reqs_ref) else: log.info("Querying .spec file") module2pkg = False reqs = _reqs.get_reqs_from_spec(as_objects=True) log.info('') r = _reqs.reqquery(reqs, release=release, dist=dist, module2pkg=module2pkg, verbose=verbose) if dump_file: log.info("Saving query results to file: %s" % dump_file) yaml.dump(r, open(dump_file, 'w')) _reqs.print_reqquery(r)