def cmd_version(cfg, args): """ Generates gitver's version string and license information and prints it to the stdout. """ v = ('v' + gitver_version) if gitver_version is not None else 'n/a' b = gitver_buildid if gitver_buildid is not None else 'n/a' term.out("This is gitver " + bold(v)) term.out("Full build ID is " + bold(b)) from gitver import __license__ term.out(__license__)
def load_user_config(): """ Returns the gitver's configuration: tries to read the stored configuration file and merges it with the default one, ensuring a valid configuration is always returned. """ try: with open(CFGFILE, 'r') as f: data = '' for line in f: l = line.strip() if not l.startswith('#'): data += l user = json.loads(data) except IOError: user = dict() except (ValueError, KeyError) as v: term.err("An error occured parsing the configuration file \"" + CFGFILE + "\": " + v.message + "\nPlease check its syntax or rename it and generate the " "default one with the " + bold("gitver init") + " command.") sys.exit(1) # merge user with defaults return dict(default_config, **user)
def cmd_list_templates(cfg, args): """ Generates a list of available templates by inspecting the gitver's template directory and prints it to the stdout. """ tpls = [f for f in os.listdir(TPLDIR) if os.path.isfile(template_path(f))] if len(tpls) > 0: term.out("Available templates:") for t in tpls: term.out(" " + bold(t) + " (" + template_path(t) + ")") else: term.out("No templates available in " + TPLDIR)
def parse_templates(cfg, templates, repo, next_custom, preview): """ Parse one or more templates, substitute placeholder variables with real values and write the result to the file specified in the template. If preview is True, then the output will be written to the stdout while informative messages will be output to the stderr. """ for t in templates.split(' '): tpath = template_path(t) if os.path.exists(tpath): with open(tpath, 'r') as fp: lines = fp.readlines() if len(lines) < 2: term.err("The template \"" + t + "\" is not valid, aborting.") return if not lines[0].startswith('#'): term.err("The template \"" + t + "\" doesn't define any valid " "output, aborting.") return output = str(lines[0]).strip(' #\n') # resolve relative paths to the project's root if not os.path.isabs(output): output = os.path.join(PRJ_ROOT, output) outdir = os.path.dirname(output) if not os.path.exists(outdir): term.err("The template output directory \"" + outdir + "\" doesn't exists.") term.info("Processing template \"" + bold(t) + "\" for " + output + "...") lines = lines[1:] xformed = Template("".join(lines)) vstring = build_version_string(cfg, repo, False, next_custom) args = build_format_args(cfg, repo, next_custom) keywords = { 'CURRENT_VERSION': vstring, 'MAJOR': args['maj'], 'MINOR': args['min'], 'PATCH': args['patch'], 'REV': args['rev'], 'REV_PREFIX': args['rev_prefix'], 'BUILD_ID': args['build_id'], 'FULL_BUILD_ID': args['build_id_full'], 'COMMIT_COUNT': args['commit_count'], 'COMMIT_COUNT_STR': str(args['commit_count']) if args['commit_count'] > 0 else '', 'COMMIT_COUNT_PREFIX': args['commit_count_prefix'], 'META_PR': args['meta_pr'], 'META_PR_PREFIX': args['meta_pr_prefix'] } try: res = xformed.substitute(keywords) except KeyError as e: term.err("Unknown key \"" + e.message + "\" found, aborting.") sys.exit(1) if not preview: try: fp = open(output, 'w') fp.write(res) fp.close() except IOError: term.err("Couldn't write file \"" + output + "\"") sys.exit(1) else: term.out(res) wrote_bytes = len(res) if preview else os.stat(output).st_size term.info("Done, " + str(wrote_bytes) + " bytes written.") else: term.err("Couldn't find the \"" + t + "\" template") sys.exit(1)
def check_config_dir(): # checks if configuration directory exists if not os.path.exists(CFGDIR): term.err("Please run " + bold("gitver init") + " first.") sys.exit(1)
def check_config(): # check config directory exists if not os.path.exists(CFGDIR): print "Please run " + bold("gitver init") + " first." sys.exit(1)