Skip to content
Snippets Groups Projects
Commit 06523a7e authored by Boris Feld's avatar Boris Feld
Browse files

Extract all make-reference functions into a separate library module

parent d00044f3
No related branches found
No related tags found
No related merge requests found
import json
import subprocess
def gather_repo_stats(repo, repo_id, repo_source):
"""gather repo related stats and returns a json-encodable dict
The dict contains:
- the repo_id given in parameter
- is the repo enabled (True by default)
- the repo_source given in parameter
- the hgversion taken from the hg binary
- the number of revisions and hidden revisions
- the number of heads and hidden heads
- the number of named branches and hidden named branches
- the format info taken from `hg debugformat`
"""
hgversion = shell_exec(["hg", "version", "-T", "{ver}"])
numrevs = count_line_exec(["hg", "-R", repo, "log", "-T", "{rev}\n"])
numrevshidden = count_line_exec(
["hg", "-R", repo, "--hidden", "log", "-T", "{rev}\n"]
)
numheads = count_line_exec(
["hg", "-R", repo, "heads", "-t", "-T", "{rev}\n"]
)
numheadshidden = count_line_exec(
["hg", "-R", repo, "--hidden", "heads", "-t", "-T", "{rev}\n"]
)
numbranches = count_line_exec(
["hg", "-R", repo, "branches", "-c", "-T", "{rev}\n"]
)
numbrancheshidden = count_line_exec(
["hg", "-R", repo, "--hidden", "branches", "-c", "-T", "{rev}\n"]
)
# Format information
raw_format_data = json.loads(
shell_exec(["hg", "-R", repo, "debugformat", "-T", "json"])
)
format_data = {}
for format_value in raw_format_data:
repo_value = format_value["repo"]
if isinstance(repo_value, basestring):
# String values should be valid ASCII
repo_value = repo_value.encode("ascii")
# Keys should be valid ASCII
format_data[format_value["name"].encode("ascii")] = repo_value
# Generate YAML file
return {
"reference-repo": {
"id": repo_id,
"enabled": True,
"source": repo_source,
"hg-version": hgversion,
"number-revisions": {"visible": numrevs, "all": numrevshidden},
"number-heads": {"visible": numheads, "all": numheadshidden},
"number-named-branch": {
"visible": numbranches,
"all": numbrancheshidden,
},
"format-info": format_data,
}
}
def shell_exec(command, env=None):
"""return the standard output of a given command
- stderr is discarded
- non-zero return code will raise `subprocess.CalledProcessError`
"""
process = subprocess.Popen(
command, stdout=subprocess.PIPE, shell=False, env=env
)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
raise subprocess.CalledProcessError(retcode, command)
return output
def count_line_exec(command, env=None):
"""execute the given command and count the number of stdout lines
Similar to `cmd | wc -l`
"""
output = shell_exec(command, env)
return len(output.splitlines())
#!/usr/bin/env python
import argparse
import json
import os
import os.path
import shutil
......@@ -5,7 +4,6 @@
import os
import os.path
import shutil
import subprocess
import sys
import tarfile
from glob import glob
......@@ -9,7 +7,7 @@
import sys
import tarfile
from glob import glob
from os.path import join
from os.path import abspath, basename, dirname, join
import yaml
......@@ -13,35 +11,6 @@
import yaml
def gather_repo_stats(repo, repo_id, repo_source):
""" Gather repo related stats
"""
hgversion = shell_exec(["hg", "version", "-T", "{ver}"])
numrevs = count_line_exec(["hg", "-R", repo, "log", "-T", "{rev}\n"])
numrevshidden = count_line_exec(
["hg", "-R", repo, "--hidden", "log", "-T", "{rev}\n"]
)
numheads = count_line_exec(
["hg", "-R", repo, "heads", "-t", "-T", "{rev}\n"]
)
numheadshidden = count_line_exec(
["hg", "-R", repo, "--hidden", "heads", "-t", "-T", "{rev}\n"]
)
numbranches = count_line_exec(
["hg", "-R", repo, "branches", "-c", "-T", "{rev}\n"]
)
numbrancheshidden = count_line_exec(
["hg", "-R", repo, "--hidden", "branches", "-c", "-T", "{rev}\n"]
)
# Format information
raw_format_data = json.loads(
shell_exec(["hg", "-R", repo, "debugformat", "-T", "json"])
)
format_data = {}
parent_dir = abspath(join(dirname(abspath(__file__)), ".."))
sys.path.insert(0, join(parent_dir, "lib"))
......@@ -47,45 +16,5 @@
for format_value in raw_format_data:
repo_value = format_value["repo"]
if isinstance(repo_value, basestring):
# String values should be valid ASCII
repo_value = repo_value.encode("ascii")
# Keys should be valid ASCII
format_data[format_value["name"].encode("ascii")] = repo_value
# Generate YAML file
return {
"reference-repo": {
"id": repo_id,
"enabled": True,
"source": repo_source,
"hg-version": hgversion,
"number-revisions": {"visible": numrevs, "all": numrevshidden},
"number-heads": {"visible": numheads, "all": numheadshidden},
"number-named-branch": {
"visible": numbranches,
"all": numbrancheshidden,
},
"format-info": format_data,
}
}
def shell_exec(command, env=None):
# Simulate bash script printing the commands it runs
process = subprocess.Popen(
command, stdout=subprocess.PIPE, shell=False, env=env
)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
raise subprocess.CalledProcessError(retcode, command)
return output
def count_line_exec(command, env=None):
output = shell_exec(command, env)
return len(output.splitlines())
from scmperf_lib import gather_repo_stats, shell_exec # isort:skip
def main(args):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment