_id stringlengths 2 7 | title stringlengths 1 88 | partition stringclasses 3
values | text stringlengths 31 13.1k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q18300 | create_supercut | train | def create_supercut(composition, outputfile, padding):
"""Concatenate video clips together and output finished video file to the
output directory.
"""
print("[+] Creating clips.")
demo_supercut(composition, padding)
# add padding when necessary
for (clip, nextclip) in zip(composition, composition[1:]):
if ((nextclip['file'] == clip['file']) and (nextclip['start'] < clip['end'])):
nextclip['start'] += padding
# put all clips together:
all_filenames = set([c['file'] for c | python | {
"resource": ""
} |
q18301 | create_supercut_in_batches | train | def create_supercut_in_batches(composition, outputfile, padding):
"""Create & concatenate video clips in groups of size BATCH_SIZE and output
finished video file to output directory.
"""
total_clips = len(composition)
start_index = 0
end_index = BATCH_SIZE
batch_comp = []
while start_index < total_clips:
filename = outputfile + '.tmp' + str(start_index) + '.mp4'
try:
create_supercut(composition[start_index:end_index], filename, padding)
batch_comp.append(filename)
gc.collect()
start_index += BATCH_SIZE
end_index += BATCH_SIZE
except:
| python | {
"resource": ""
} |
q18302 | search_line | train | def search_line(line, search, searchtype):
"""Return True if search term is found in given line, False otherwise."""
if searchtype == 're' or searchtype == 'word': | python | {
"resource": ""
} |
q18303 | get_subtitle_files | train | def get_subtitle_files(inputfile):
"""Return a list of subtitle files."""
srts = []
for f in inputfile:
filename = f.split('.')
filename[-1] = 'srt'
srt = '.'.join(filename)
if os.path.isfile(srt):
| python | {
"resource": ""
} |
q18304 | get_vtt_files | train | def get_vtt_files(inputfile):
"""Return a list of vtt files."""
vtts = []
for f in inputfile:
filename = f.split('.')
filename = '.'.join(filename[0:-1])
vtt = glob(filename + '*.vtt')
if len(vtt) > 0:
| python | {
"resource": ""
} |
q18305 | videogrep | train | def videogrep(inputfile, outputfile, search, searchtype, maxclips=0, padding=0, test=False, randomize=False, sync=0, use_transcript=False, use_vtt=False, export_clips=False):
"""Search through and find all instances of the search term in an srt or transcript,
create a supercut around that instance, and output a new video file
comprised of those supercuts.
"""
padding = padding / 1000.0
sync = sync / 1000.0
composition = []
foundSearchTerm = False
if use_transcript:
composition = compose_from_transcript(inputfile, search, searchtype)
elif use_vtt:
vtts = get_vtt_files(inputfile)
composition = compose_from_vtt(vtts, search, searchtype)
else:
srts = get_subtitle_files(inputfile)
composition = compose_from_srts(srts, search, searchtype)
# If the search term was not found in any subtitle file...
if len(composition) == 0:
print("[!] Search term '" + search + "'" + " was not found in any file.")
exit(1)
else:
print("[+] Search term '" + search + "'" + " was found in " + str(len(composition)) + " places.")
# apply padding and sync
for c in composition:
c['start'] = c['start'] + sync - padding
| python | {
"resource": ""
} |
q18306 | basic_color | train | def basic_color(code):
"""
16 colors supported
"""
def inner(text, rl=False):
""" Every raw_input with color sequences should be called with
rl=True to avoid readline messed up the length calculation
"""
c = code
| python | {
"resource": ""
} |
q18307 | term_color | train | def term_color(code):
"""
256 colors supported
"""
def inner(text, rl=False):
"""
Every raw_input with color sequences should be called with
rl=True to avoid readline messed up the length calculation
"""
c = code
| python | {
"resource": ""
} |
q18308 | color_func | train | def color_func(func_name):
"""
Call color function base on name
| python | {
"resource": ""
} |
q18309 | MPlayer._send_command | train | def _send_command(self, cmd, expect=None):
"""Send a command to MPlayer.
cmd: the command string
expect: expect the output starts with a certain string
The result, if any, is returned as a string.
"""
if not self.is_alive:
raise NotPlayingError()
logger.debug("Send command to mplayer: " + cmd)
cmd = cmd + "\n"
# In Py3k, TypeErrors will be raised because cmd is a string but stdin
# expects bytes. In Python 2.x on the other hand, UnicodeEncodeErrors
# will be raised if cmd is unicode. In both cases, encoding the string
# will fix the problem.
try:
self.sub_proc.stdin.write(cmd)
except (TypeError, UnicodeEncodeError):
self.sub_proc.stdin.write(cmd.encode('utf-8', 'ignore'))
time.sleep(0.1) # wait for mplayer (better idea?)
# Expect a response for 'get_property' only
if not expect:
return
while True:
try: | python | {
"resource": ""
} |
q18310 | EntitySpec.stream | train | def stream(
self,
accountID,
**kwargs
):
"""
Get a stream of Transactions for an Account starting from when the
request is made.
Args:
accountID:
Account Identifier
Returns:
v20.response.Response containing the results from submitting the
request
"""
request = Request(
'GET',
'/v3/accounts/{accountID}/transactions/stream'
)
request.set_path_param(
'accountID',
accountID
)
request.set_stream(True)
class Parser():
def __init__(self, ctx):
self.ctx = ctx
def __call__(self, line):
j = json.loads(line.decode('utf-8'))
type = j.get("type")
if type is None:
return ("unknown", j)
elif type == "HEARTBEAT":
| python | {
"resource": ""
} |
q18311 | EntitySpec.price | train | def price(
self,
instrument,
**kwargs
):
"""
Fetch a price for an instrument. Accounts are not associated in any way
with this endpoint.
Args:
instrument:
Name of the Instrument
time:
The time at which the desired price is in effect. The current
price is returned if no time is provided.
Returns:
v20.response.Response containing the results from submitting the
request
"""
request = Request(
'GET',
'/v3/instruments/{instrument}/price'
)
request.set_path_param(
'instrument',
instrument
)
request.set_param(
'time',
kwargs.get('time')
)
response = self.ctx.request(request)
if response.content_type is None:
return response
if not response.content_type.startswith("application/json"):
return response
jbody = json.loads(response.raw_body)
parsed_body = {}
#
# Parse responses as defined by the API specification
#
if str(response.status) == "200":
if jbody.get('price') is not None:
parsed_body['price'] = \
self.ctx.pricing_common.Price.from_dict(
jbody['price'],
self.ctx
)
elif str(response.status) == "400":
if jbody.get('errorCode') is not None:
parsed_body['errorCode'] = \
jbody.get('errorCode')
if jbody.get('errorMessage') is not None:
parsed_body['errorMessage'] = \
jbody.get('errorMessage')
elif str(response.status) == "401":
| python | {
"resource": ""
} |
q18312 | EntitySpec.list | train | def list(
self,
accountID,
**kwargs
):
"""
Get a list of Orders for an Account
Args:
accountID:
Account Identifier
ids:
List of Order IDs to retrieve
state:
The state to filter the requested Orders by
instrument:
The instrument to filter the requested orders by
count:
The maximum number of Orders to return
beforeID:
The maximum Order ID to return. If not provided the most recent
Orders in the Account are returned
Returns:
v20.response.Response containing the results from submitting the
request
"""
request = Request(
'GET',
'/v3/accounts/{accountID}/orders'
)
request.set_path_param(
'accountID',
accountID
)
request.set_param(
'ids',
kwargs.get('ids')
)
request.set_param(
'state',
kwargs.get('state')
)
request.set_param(
'instrument',
kwargs.get('instrument')
)
request.set_param(
'count',
kwargs.get('count')
)
request.set_param(
'beforeID',
kwargs.get('beforeID')
)
response = self.ctx.request(request)
if response.content_type is None:
return response
if not response.content_type.startswith("application/json"):
return response
jbody = json.loads(response.raw_body)
parsed_body = {}
#
# Parse responses as defined by the API specification
#
if str(response.status) == "200":
if jbody.get('orders') is not None:
parsed_body['orders'] = [
self.ctx.order.Order.from_dict(d, self.ctx)
for d in jbody.get('orders')
]
if jbody.get('lastTransactionID') is not None:
| python | {
"resource": ""
} |
q18313 | EntitySpec.cancel | train | def cancel(
self,
accountID,
orderSpecifier,
**kwargs
):
"""
Cancel a pending Order in an Account
Args:
accountID:
Account Identifier
orderSpecifier:
The Order Specifier
Returns:
v20.response.Response containing the results from submitting the
request
"""
request = Request(
'PUT',
'/v3/accounts/{accountID}/orders/{orderSpecifier}/cancel'
)
request.set_path_param(
'accountID',
accountID
)
request.set_path_param(
'orderSpecifier',
orderSpecifier
)
response = self.ctx.request(request)
if response.content_type is None:
return response
if not response.content_type.startswith("application/json"):
return response
jbody = json.loads(response.raw_body)
parsed_body = {}
#
# Parse responses as defined by the API specification
#
if str(response.status) == "200":
if jbody.get('orderCancelTransaction') is not None:
parsed_body['orderCancelTransaction'] = \
self.ctx.transaction.OrderCancelTransaction.from_dict(
jbody['orderCancelTransaction'],
self.ctx
)
if jbody.get('relatedTransactionIDs') is not None:
parsed_body['relatedTransactionIDs'] = \
jbody.get('relatedTransactionIDs')
if jbody.get('lastTransactionID') is not None:
parsed_body['lastTransactionID'] = \
jbody.get('lastTransactionID')
elif str(response.status) == "401":
if jbody.get('errorCode') is not None:
parsed_body['errorCode'] = \
jbody.get('errorCode')
if jbody.get('errorMessage') is not None:
parsed_body['errorMessage'] = \
jbody.get('errorMessage')
elif str(response.status) == "404":
if jbody.get('orderCancelRejectTransaction') is not None:
parsed_body['orderCancelRejectTransaction'] = | python | {
"resource": ""
} |
q18314 | EntitySpec.market | train | def market(self, accountID, **kwargs):
"""
Shortcut to create a Market Order in an Account
Args:
accountID : The ID of the Account
kwargs : The arguments to create a MarketOrderRequest
Returns:
v20.response.Response containing the results from submitting
| python | {
"resource": ""
} |
q18315 | EntitySpec.limit | train | def limit(self, accountID, **kwargs):
"""
Shortcut to create a Limit Order in an Account
Args:
accountID : The ID of the Account
kwargs : The arguments to create a LimitOrderRequest
Returns:
v20.response.Response containing the results from submitting
| python | {
"resource": ""
} |
q18316 | EntitySpec.limit_replace | train | def limit_replace(self, accountID, orderID, **kwargs):
"""
Shortcut to replace a pending Limit Order in an Account
Args:
accountID : The ID of the Account
orderID : The ID of the Limit Order to replace
kwargs : The arguments to create a LimitOrderRequest
Returns:
| python | {
"resource": ""
} |
q18317 | EntitySpec.stop | train | def stop(self, accountID, **kwargs):
"""
Shortcut to create a Stop Order in an Account
Args:
accountID : The ID of the Account
kwargs : The arguments to create a StopOrderRequest
Returns:
v20.response.Response containing the results from submitting
| python | {
"resource": ""
} |
q18318 | EntitySpec.stop_replace | train | def stop_replace(self, accountID, orderID, **kwargs):
"""
Shortcut to replace a pending Stop Order in an Account
Args:
accountID : The ID of the Account
orderID : The ID of the Stop Order to replace
kwargs : The arguments to create a StopOrderRequest
Returns:
| python | {
"resource": ""
} |
q18319 | EntitySpec.market_if_touched | train | def market_if_touched(self, accountID, **kwargs):
"""
Shortcut to create a MarketIfTouched Order in an Account
Args:
accountID : The ID of the Account
kwargs : The arguments to create a MarketIfTouchedOrderRequest
Returns:
v20.response.Response containing the results from submitting
| python | {
"resource": ""
} |
q18320 | EntitySpec.market_if_touched_replace | train | def market_if_touched_replace(self, accountID, orderID, **kwargs):
"""
Shortcut to replace a pending MarketIfTouched Order in an Account
Args:
accountID : The ID of the Account
orderID : The ID of the MarketIfTouched Order to replace
kwargs : The arguments to create a MarketIfTouchedOrderRequest
Returns:
| python | {
"resource": ""
} |
q18321 | EntitySpec.take_profit | train | def take_profit(self, accountID, **kwargs):
"""
Shortcut to create a Take Profit Order in an Account
Args:
accountID : The ID of the Account
kwargs : The arguments to create a TakeProfitOrderRequest
Returns:
v20.response.Response containing the results from submitting
| python | {
"resource": ""
} |
q18322 | EntitySpec.take_profit_replace | train | def take_profit_replace(self, accountID, orderID, **kwargs):
"""
Shortcut to replace a pending Take Profit Order in an Account
Args:
accountID : The ID of the Account
orderID : The ID of the Take Profit Order to replace
kwargs : The arguments to create a TakeProfitOrderRequest
Returns:
| python | {
"resource": ""
} |
q18323 | EntitySpec.stop_loss | train | def stop_loss(self, accountID, **kwargs):
"""
Shortcut to create a Stop Loss Order in an Account
Args:
accountID : The ID of the Account
kwargs : The arguments to create a StopLossOrderRequest
Returns:
v20.response.Response containing the results from submitting
| python | {
"resource": ""
} |
q18324 | EntitySpec.stop_loss_replace | train | def stop_loss_replace(self, accountID, orderID, **kwargs):
"""
Shortcut to replace a pending Stop Loss Order in an Account
Args:
accountID : The ID of the Account
orderID : The ID of the Stop Loss Order to replace
kwargs : The arguments to create a StopLossOrderRequest
Returns:
| python | {
"resource": ""
} |
q18325 | EntitySpec.trailing_stop_loss | train | def trailing_stop_loss(self, accountID, **kwargs):
"""
Shortcut to create a Trailing Stop Loss Order in an Account
Args:
accountID : The ID of the Account
kwargs : The | python | {
"resource": ""
} |
q18326 | EntitySpec.trailing_stop_loss_replace | train | def trailing_stop_loss_replace(self, accountID, orderID, **kwargs):
"""
Shortcut to replace a pending Trailing Stop Loss Order in an Account
Args:
accountID : The ID of the Account
orderID : The ID of the Take Profit Order to replace
kwargs : The arguments to create a TrailingStopLossOrderRequest
Returns:
| python | {
"resource": ""
} |
q18327 | Context.set_token | train | def set_token(self, token):
"""
Set the token for the v20 context
Args:
token: The token used to access the v20 REST api
"""
self.token = token
| python | {
"resource": ""
} |
q18328 | Context.set_datetime_format | train | def set_datetime_format(self, format):
"""
Set the Accept-Datetime-Format header to an acceptable
value
Args:
format: UNIX or RFC3339
"""
if not format in ["UNIX", "RFC3339"]:
| python | {
"resource": ""
} |
q18329 | Context.request | train | def request(self, request):
"""
Perform an HTTP request through the context
Args:
request: A v20.request.Request object
Returns:
A v20.response.Response object
"""
url = "{}{}".format(self._base_url, request.path)
timeout = self.poll_timeout
if request.stream is True:
timeout = self.stream_timeout
try:
http_response = self._session.request(
request.method,
url,
headers=self._headers,
params=request.params,
data=request.body,
stream=request.stream,
timeout=timeout
)
except requests.exceptions.ConnectionError:
raise V20ConnectionError(url)
except requests.exceptions.ConnectTimeout:
raise V20Timeout(url, "connect")
except requests.exceptions.ReadTimeout:
raise V20Timeout(url, "read")
request.headers = http_response.request.headers
response = Response(
request,
| python | {
"resource": ""
} |
q18330 | EntitySpec.list | train | def list(
self,
**kwargs
):
"""
Get a list of all Accounts authorized for the provided token.
Args:
Returns:
v20.response.Response containing the results from submitting the
request
"""
request = Request(
'GET',
'/v3/accounts'
)
response = self.ctx.request(request)
if response.content_type is None:
return response
if not response.content_type.startswith("application/json"):
return response
jbody = json.loads(response.raw_body)
parsed_body = {}
#
# Parse responses as defined by the API specification
#
if str(response.status) == "200":
if jbody.get('accounts') is not None:
parsed_body['accounts'] = [
self.ctx.account.AccountProperties.from_dict(d, self.ctx)
for d in jbody.get('accounts')
]
elif str(response.status) == "401":
if jbody.get('errorCode') is not None:
| python | {
"resource": ""
} |
q18331 | EntitySpec.instruments | train | def instruments(
self,
accountID,
**kwargs
):
"""
Get the list of tradeable instruments for the given Account. The list
of tradeable instruments is dependent on the regulatory division that
the Account is located in, thus should be the same for all Accounts
owned by a single user.
Args:
accountID:
Account Identifier
instruments:
List of instruments to query specifically.
Returns:
v20.response.Response containing the results from submitting the
request
"""
request = Request(
'GET',
'/v3/accounts/{accountID}/instruments'
)
request.set_path_param(
'accountID',
accountID
)
request.set_param(
'instruments',
kwargs.get('instruments')
)
response = self.ctx.request(request)
if response.content_type is None:
return response
if not response.content_type.startswith("application/json"):
return response
jbody = json.loads(response.raw_body)
parsed_body = {}
#
# Parse responses as defined by the API specification
#
if str(response.status) == "200":
if jbody.get('instruments') is not None:
parsed_body['instruments'] = [
self.ctx.primitives.Instrument.from_dict(d, self.ctx)
for d in jbody.get('instruments')
]
if jbody.get('lastTransactionID') is not None:
parsed_body['lastTransactionID'] = \
jbody.get('lastTransactionID')
elif str(response.status) == "400":
if jbody.get('errorCode') is not None:
parsed_body['errorCode'] = \
jbody.get('errorCode')
| python | {
"resource": ""
} |
q18332 | EntitySpec.get_info | train | def get_info(
self,
userSpecifier,
**kwargs
):
"""
Fetch the user information for the specified user. This endpoint is
intended to be used by the user themself to obtain their own
information.
Args:
userSpecifier:
The User Specifier
Returns:
v20.response.Response containing the results from submitting the
request
"""
request = Request(
'GET',
'/v3/users/{userSpecifier}'
)
request.set_path_param(
'userSpecifier',
userSpecifier
)
response = self.ctx.request(request)
if response.content_type is None:
return response
if not response.content_type.startswith("application/json"):
return response
jbody = json.loads(response.raw_body)
parsed_body = {}
#
# Parse responses as defined by the API specification
#
if str(response.status) == "200":
if jbody.get('userInfo') is not None:
parsed_body['userInfo'] = \
self.ctx.user.UserInfo.from_dict(
jbody['userInfo'],
self.ctx
)
elif str(response.status) == "401":
if jbody.get('errorCode') is not None:
parsed_body['errorCode'] = \
jbody.get('errorCode')
if jbody.get('errorMessage') is not None:
parsed_body['errorMessage'] = \
jbody.get('errorMessage')
elif str(response.status) == | python | {
"resource": ""
} |
q18333 | EntitySpec.get | train | def get(
self,
accountID,
**kwargs
):
"""
Get pricing information for a specified list of Instruments within an
Account.
Args:
accountID:
Account Identifier
instruments:
List of Instruments to get pricing for.
since:
Date/Time filter to apply to the response. Only prices and home
conversions (if requested) with a time later than this filter
(i.e. the price has changed after the since time) will be
provided, and are filtered independently.
includeUnitsAvailable:
Flag that enables the inclusion of the unitsAvailable field in
the returned Price objects.
includeHomeConversions:
Flag that enables the inclusion of the homeConversions field in
the returned response. An entry will be returned for each
currency in the set of all base and quote currencies present in
the requested instruments list.
Returns:
v20.response.Response containing the results from submitting the
request
"""
request = Request(
'GET',
'/v3/accounts/{accountID}/pricing'
)
request.set_path_param(
'accountID',
accountID
)
request.set_param(
'instruments',
kwargs.get('instruments')
)
request.set_param(
'since',
kwargs.get('since')
)
request.set_param(
'includeUnitsAvailable',
kwargs.get('includeUnitsAvailable')
)
request.set_param(
'includeHomeConversions',
kwargs.get('includeHomeConversions')
)
response = self.ctx.request(request)
if response.content_type is None:
return response
if not response.content_type.startswith("application/json"):
return response
jbody = json.loads(response.raw_body)
parsed_body = {}
#
# Parse responses as defined by the API specification
#
if str(response.status) == "200":
if jbody.get('prices') is not None:
parsed_body['prices'] = [
self.ctx.pricing.ClientPrice.from_dict(d, self.ctx)
for d in jbody.get('prices')
]
if jbody.get('homeConversions') is not None:
| python | {
"resource": ""
} |
q18334 | OutputHandler.output_for_skipping_run_set | train | def output_for_skipping_run_set(self, runSet, reason=None):
'''
This function writes a simple message to terminal and logfile,
when a run set is skipped.
There is no message about skipping a run set in the xml-file.
'''
# print to terminal
util.printOut("\nSkipping run set" +
(" '" + runSet.name + "'" if runSet.name else "") +
(" " + reason if reason else "")
)
| python | {
"resource": ""
} |
q18335 | OutputHandler.writeRunSetInfoToLog | train | def writeRunSetInfoToLog(self, runSet):
"""
This method writes the information about a run set into the txt_file.
"""
runSetInfo = "\n\n"
if runSet.name:
runSetInfo += runSet.name + "\n"
runSetInfo += "Run set {0} of {1} with options '{2}' and propertyfile '{3}'\n\n".format(
runSet.index, len(self.benchmark.run_sets),
" ".join(runSet.options),
runSet.propertyfile)
titleLine = self.create_output_line(runSet, "inputfile", "status", "cpu time",
| python | {
"resource": ""
} |
q18336 | OutputHandler.runs_to_xml | train | def runs_to_xml(self, runSet, runs, blockname=None):
"""
This function creates the XML structure for a list of runs
"""
# copy benchmarkinfo, limits, columntitles, systeminfo from xml_header
runsElem = util.copy_of_xml_element(self.xml_header)
runsElem.set("options", " ".join(runSet.options))
if blockname is not None:
runsElem.set("block", blockname)
| python | {
"resource": ""
} |
q18337 | OutputHandler.add_values_to_run_xml | train | def add_values_to_run_xml(self, run):
"""
This function adds the result values to the XML representation of a run.
"""
runElem = run.xml
for elem in list(runElem):
runElem.remove(elem)
self.add_column_to_xml(runElem, 'status', run.status)
self.add_column_to_xml(runElem, 'cputime', run.cputime)
self.add_column_to_xml(runElem, 'walltime', run.walltime)
self.add_column_to_xml(runElem, '@category', run.category) # hidden
self.add_column_to_xml(runElem, '', | python | {
"resource": ""
} |
q18338 | OutputHandler.add_values_to_run_set_xml | train | def add_values_to_run_set_xml(self, runSet, cputime, walltime, energy):
"""
This function adds the result values to the XML representation of a runSet.
"""
self.add_column_to_xml(runSet.xml, 'cputime', cputime)
self.add_column_to_xml(runSet.xml, 'walltime', walltime)
| python | {
"resource": ""
} |
q18339 | OutputHandler.format_sourcefile_name | train | def format_sourcefile_name(self, fileName, runSet):
'''
Formats the file name of a program for printing on console.
| python | {
"resource": ""
} |
q18340 | OutputHandler._write_pretty_result_xml_to_file | train | def _write_pretty_result_xml_to_file(self, xml, filename):
"""Writes a nicely formatted XML file with DOCTYPE, and compressed if necessary."""
if self.compress_results:
actual_filename = filename + ".bz2"
# Use BZ2File directly or our hack for Python 3.2
open_func = bz2.BZ2File if hasattr(bz2.BZ2File, 'writable') else util.BZ2FileHack
else:
# write content to temp file first to prevent loosing data
# in existing file if writing fails
actual_filename = filename + ".tmp"
open_func = open
with io.TextIOWrapper(open_func(actual_filename, 'wb'), encoding='utf-8') as file:
rough_string = ET.tostring(xml, encoding='unicode')
reparsed = minidom.parseString(rough_string)
doctype = minidom.DOMImplementation().createDocumentType(
'result', RESULT_XML_PUBLIC_ID, RESULT_XML_SYSTEM_ID) | python | {
"resource": ""
} |
q18341 | get_extract_value_function | train | def get_extract_value_function(column_identifier):
"""
returns a function that extracts the value for a column.
"""
def extract_value(run_result):
pos = None
for i, column in enumerate(run_result.columns):
| python | {
"resource": ""
} |
q18342 | check_memory_size | train | def check_memory_size(memLimit, num_of_threads, memoryAssignment, my_cgroups):
"""Check whether the desired amount of parallel benchmarks fits in the memory.
Implemented are checks for memory limits via cgroup controller "memory" and
memory bank restrictions via cgroup controller "cpuset",
as well as whether the system actually has enough memory installed.
@param memLimit: the memory limit in bytes per run
@param num_of_threads: the number of parallel benchmark executions
@param memoryAssignment: the allocation of memory banks to runs (if not present, all banks are assigned to all runs)
"""
try:
# Check amount of memory allowed via cgroups.
def check_limit(actualLimit):
if actualLimit < memLimit:
sys.exit("Cgroups allow only {} bytes of memory to be used, cannot execute runs with {} bytes of memory.".format(actualLimit, memLimit))
elif actualLimit < memLimit * num_of_threads:
sys.exit("Cgroups allow only {} bytes of memory to be used, not enough for {} benchmarks with {} bytes each. Please reduce the number of threads".format(actualLimit, num_of_threads, memLimit))
if not os.path.isdir('/sys/devices/system/node/'):
logging.debug("System without NUMA support in Linux kernel, ignoring memory assignment.")
return
if cgroups.MEMORY in my_cgroups:
| python | {
"resource": ""
} |
q18343 | _get_memory_bank_size | train | def _get_memory_bank_size(memBank):
"""Get the size of a memory bank in bytes."""
fileName = '/sys/devices/system/node/node{0}/meminfo'.format(memBank)
size = None
with open(fileName) as f:
for line in f:
if 'MemTotal' in line:
size = line.split(':')[1].strip()
if size[-3:] != ' kB':
raise ValueError('"{}" in file {} is not a memory size.'.format(size, fileName))
size = int(size[:-3]) * 1024 | python | {
"resource": ""
} |
q18344 | Tool.determine_result | train | def determine_result(self, returncode, returnsignal, output, isTimeout):
"""
Returns a BenchExec result status based on the output of SMACK
"""
splitout = "\n".join(output)
if 'SMACK found no errors' in splitout:
return result.RESULT_TRUE_PROP
errmsg = re.search(r'SMACK found an error(:\s+([^\.]+))?\.', splitout)
if errmsg:
errtype = errmsg.group(2)
if errtype:
if 'invalid pointer dereference' == errtype:
return result.RESULT_FALSE_DEREF
elif 'invalid memory deallocation' == errtype:
| python | {
"resource": ""
} |
q18345 | ContainerExecutor._get_result_files_base | train | def _get_result_files_base(self, temp_dir):
"""Given the temp directory that is created for each run, return the path to the directory
where files created | python | {
"resource": ""
} |
q18346 | ContainerExecutor.execute_run | train | def execute_run(self, args, workingDir=None, output_dir=None, result_files_patterns=[],
rootDir=None, environ=os.environ.copy()):
"""
This method executes the command line and waits for the termination of it,
handling all setup and cleanup.
@param args: the command line to run
@param rootDir: None or a root directory that contains all relevant files for starting a new process
@param workingDir: None or a directory which the execution should use as working directory
@param output_dir: the directory where to write result files (required if result_files_pattern)
@param result_files_patterns: a list of patterns of files to retrieve as result files
"""
# preparations
temp_dir = None
if rootDir is None:
temp_dir = tempfile.mkdtemp(prefix="Benchexec_run_")
pid = None
returnvalue = 0
logging.debug('Starting process.')
try:
pid, result_fn = self._start_execution(args=args,
stdin=None, stdout=None, stderr=None,
env=environ, root_dir=rootDir, cwd=workingDir, temp_dir=temp_dir,
cgroups=Cgroup({}),
output_dir=output_dir, result_files_patterns=result_files_patterns,
| python | {
"resource": ""
} |
q18347 | ContainerExecutor._setup_root_filesystem | train | def _setup_root_filesystem(self, root_dir):
"""Setup the filesystem layout in the given root directory.
Create a copy of the existing proc- and dev-mountpoints in the specified root
directory. Afterwards we chroot into it.
@param root_dir: The path of the root directory that is used to execute the process.
"""
root_dir = root_dir.encode()
# Create an empty proc folder into the root dir. The grandchild still needs a view of
# the old /proc, therefore we do not mount a fresh /proc here.
proc_base = os.path.join(root_dir, b"proc")
util.makedirs(proc_base, exist_ok=True)
dev_base = os.path.join(root_dir, b"dev")
util.makedirs(dev_base, exist_ok=True)
| python | {
"resource": ""
} |
q18348 | ContainerExecutor._transfer_output_files | train | def _transfer_output_files(self, tool_output_dir, working_dir, output_dir, patterns):
"""Transfer files created by the tool in the container to the output directory.
@param tool_output_dir: The directory under which all tool output files are created.
@param working_dir: The absolute working directory of the tool in the container.
@param output_dir: the directory where to write result files
@param patterns: a list of patterns of files to retrieve as result files
"""
assert output_dir and patterns
if any(os.path.isabs(pattern) for pattern in patterns):
base_dir = tool_output_dir
else:
base_dir = tool_output_dir + working_dir
def transfer_file(abs_file):
assert abs_file.startswith(base_dir)
# We ignore (empty) directories, because we create them for hidden dirs etc.
# We ignore device nodes, because overlayfs creates them.
# We also ignore all other files (symlinks, fifos etc.),
# because they are probably irrelevant, and just handle regular files.
file = os.path.join("/", os.path.relpath(abs_file, base_dir))
if (os.path.isfile(abs_file) and not os.path.islink(abs_file) and
not container.is_container_system_config_file(file)):
target = output_dir + file
logging.debug("Transferring output file %s to %s", abs_file, target)
try:
os.makedirs(os.path.dirname(target))
except EnvironmentError:
pass # exist_ok=True not supported on Python 2
try:
# move is more efficient than copy in case both abs_file and target
# are on the same filesystem, and it avoids matching the file again
# with the next pattern.
| python | {
"resource": ""
} |
q18349 | parse_table_definition_file | train | def parse_table_definition_file(file):
'''
Read an parse the XML of a table-definition file.
@return: an ElementTree object for the table definition
'''
logging.info("Reading table definition from '%s'...", file)
if not os.path.isfile(file):
logging.error("File '%s' does not exist.", file)
exit(1)
try:
tableGenFile = ElementTree.ElementTree().parse(file)
except IOError as e:
logging.error('Could not read result file %s: %s', file, e)
exit(1)
except ElementTree.ParseError as e:
| python | {
"resource": ""
} |
q18350 | load_results_from_table_definition | train | def load_results_from_table_definition(table_definition, table_definition_file, options):
"""
Load all results in files that are listed in the given table-definition file.
@return: a list of RunSetResult objects
"""
default_columns = extract_columns_from_table_definition_file(table_definition, table_definition_file)
columns_relevant_for_diff = _get_columns_relevant_for_diff(default_columns)
results = []
for tag in table_definition:
if tag.tag == 'result':
columns = extract_columns_from_table_definition_file(tag, table_definition_file) or default_columns
run_set_id = tag.get('id')
for resultsFile in get_file_list_from_result_tag(tag, table_definition_file):
| python | {
"resource": ""
} |
q18351 | load_results_with_table_definition | train | def load_results_with_table_definition(result_files, table_definition, table_definition_file, options):
"""
Load results from given files with column definitions taken from a table-definition file.
@return: a list of RunSetResult objects
"""
columns = extract_columns_from_table_definition_file(table_definition, table_definition_file)
| python | {
"resource": ""
} |
q18352 | extract_columns_from_table_definition_file | train | def extract_columns_from_table_definition_file(xmltag, table_definition_file):
"""
Extract all columns mentioned in the result tag of a table definition file.
"""
def handle_path(path):
"""Convert path from a path relative to table-definition file."""
if not path or path.startswith("http://") or path.startswith("https://"):
return path
| python | {
"resource": ""
} |
q18353 | _get_columns_relevant_for_diff | train | def _get_columns_relevant_for_diff(columns_to_show):
"""
Extract columns that are relevant for the diff table.
@param columns_to_show: (list) A list of columns that should be shown
@return: (set) Set of columns that are relevant for the diff table. If
none is marked relevant, the column named "status" will be
| python | {
"resource": ""
} |
q18354 | get_task_id | train | def get_task_id(task, base_path_or_url):
"""
Return a unique identifier for a given task.
@param task: the XML element that represents a task
@return a tuple with filename of task as first element
"""
name = task.get('name')
if base_path_or_url:
if Util.is_url(base_path_or_url):
name = urllib.parse.urljoin(base_path_or_url, name)
| python | {
"resource": ""
} |
q18355 | load_tool | train | def load_tool(result):
"""
Load the module with the tool-specific code.
"""
def load_tool_module(tool_module):
if not tool_module:
logging.warning('Cannot extract values from log files for benchmark results %s '
'(missing attribute "toolmodule" on tag "result").',
Util.prettylist(result.attributes['name']))
return None
try:
logging.debug('Loading %s', tool_module)
return __import__(tool_module, fromlist=['Tool']).Tool()
| python | {
"resource": ""
} |
q18356 | load_results | train | def load_results(result_files, options, run_set_id=None, columns=None,
columns_relevant_for_diff=set()):
"""Version of load_result for multiple input files that will be loaded concurrently."""
return parallel.map(
load_result,
result_files,
| python | {
"resource": ""
} |
q18357 | load_result | train | def load_result(result_file, options, run_set_id=None, columns=None,
columns_relevant_for_diff=set()):
"""
Completely handle loading a single result file.
@param result_file the file to parse
@param options additional options
@param run_set_id the identifier of the run set
@param columns the list of columns
@param columns_relevant_for_diff a set of columns that is relevant for
the diff table
@return a fully ready RunSetResult instance or None
"""
xml = parse_results_file(result_file, run_set_id=run_set_id, | python | {
"resource": ""
} |
q18358 | parse_results_file | train | def parse_results_file(resultFile, run_set_id=None, ignore_errors=False):
'''
This function parses an XML file that contains the results of the execution of a run set.
It returns the "result" XML tag.
@param resultFile: The file name of the XML file that contains the results.
@param run_set_id: An optional identifier of this set of results.
'''
logging.info(' %s', resultFile)
url = Util.make_url(resultFile)
parse = ElementTree.ElementTree().parse
try:
with Util.open_url_seekable(url, mode='rb') as f:
try:
try:
resultElem = parse(gzip.GzipFile(fileobj=f))
except IOError:
f.seek(0)
try:
resultElem = parse(bz2.BZ2File(f))
except TypeError:
# Python 3.2 does not support giving a file-like object to BZ2File
resultElem = parse(io.BytesIO(bz2.decompress(f.read())))
except IOError:
f.seek(0)
resultElem = parse(f)
except IOError as e:
logging.error('Could not read result file %s: %s', resultFile, e)
exit(1)
except ElementTree.ParseError as e:
logging.error('Result file %s is invalid: | python | {
"resource": ""
} |
q18359 | merge_task_lists | train | def merge_task_lists(runset_results, tasks):
"""
Set the filelists of all RunSetResult elements so that they contain the same files
in the same order. For missing files a dummy element is inserted.
"""
for runset in runset_results:
# create mapping from id to RunResult object
# Use reversed list such that the first instance of equal tasks end up in dic
dic = dict([(run_result.task_id, run_result) for run_result in reversed(runset.results)])
runset.results = [] # clear and repopulate results
for task in tasks:
run_result = dic.get(task)
if run_result is None:
| python | {
"resource": ""
} |
q18360 | get_rows | train | def get_rows(runSetResults):
"""
Create list of rows with all data. Each row consists of several RunResults.
"""
rows = []
for task_results | python | {
"resource": ""
} |
q18361 | filter_rows_with_differences | train | def filter_rows_with_differences(rows):
"""
Find all rows with differences in the status column.
"""
if not rows:
# empty table
return []
if len(rows[0].results) == 1:
# table with single column
return []
def get_index_of_column(name, cols):
for i in range(0, len(cols)):
if cols[i].title == name:
return i
return -1
def all_equal_result(listOfResults):
relevant_columns = set()
for res in listOfResults:
for relevant_column in res.columns_relevant_for_diff:
relevant_columns.add(relevant_column)
if len(relevant_columns) == 0:
relevant_columns.add("status")
status = []
for col in relevant_columns:
# It's necessary to search for the index of a column every time
# because they can differ between results
status.append(
set(
| python | {
"resource": ""
} |
q18362 | select_relevant_id_columns | train | def select_relevant_id_columns(rows):
"""
Find out which of the entries in Row.id are equal for all given rows.
@return: A list of True/False values according to whether the i-th part of the id is always equal.
"""
relevant_id_columns = [True] # first column (file name) is always relevant
if rows:
prototype_id = rows[0].id
for | python | {
"resource": ""
} |
q18363 | get_regression_count | train | def get_regression_count(rows, ignoreFlappingTimeouts): # for options.dump_counts
"""Count the number of regressions, i.e., differences in status of the two right-most results
where the new one is not "better" than the old one.
Any change in status between error, unknown, and wrong result is a regression.
Different kind of errors or wrong results are also a regression.
"""
def status_is(run_result, status):
# startswith is used because status can be "TIMEOUT (TRUE)" etc., which count as "TIMEOUT"
return run_result.status and run_result.status.startswith(status)
def any_status_is(run_results, status):
for run_result in run_results:
if status_is(run_result, status):
return True
return False
regressions = 0
for row in rows:
if len(row.results) < 2:
return 0 # no regressions at all with only one run
# "new" and "old" are the latest two results
new = row.results[-1]
old = row.results[-2]
if new.category == result.CATEGORY_CORRECT:
continue | python | {
"resource": ""
} |
q18364 | RunSetResult.collect_data | train | def collect_data(self, correct_only):
"""
Load the actual result values from the XML file and the log files.
This may take some time if many log files have to be opened and parsed.
"""
self.results = []
def get_value_from_logfile(lines, identifier):
"""
This method searches for values in lines of the content.
It uses a tool-specific method to so.
"""
return load_tool(self).get_value_from_output(lines, identifier)
# Opening the ZIP archive with the logs for every run is too slow, we cache it.
log_zip_cache = {}
try:
for xml_result, result_file in self._xml_results:
self.results.append(RunResult.create_from_xml(
| python | {
"resource": ""
} |
q18365 | Row.set_relative_path | train | def set_relative_path(self, common_prefix, base_dir):
"""
generate output representation of rows
"""
| python | {
"resource": ""
} |
q18366 | Tool._version_newer_than | train | def _version_newer_than(self, vers):
"""
Determine whether the version is greater than some given version
"""
v = self.version(self.executable())
vers_num = v[:v.index('-')]
if not vers_num[0].isdigit():
# this is the old version which is "older" than any given version
return False
v1 = list(map(int, vers_num.split('.')))
v2 = list(map(int, vers.split('.')))
assert len(v1) == 3
assert len(v2) == 3
| python | {
"resource": ""
} |
q18367 | _get_user_account_info | train | def _get_user_account_info(user):
"""Get the user account info from the passwd database. Only works on Linux.
@param user The name of a user account or a numeric uid prefixed with '#'
@return a tuple that corresponds to the members of the passwd structure
@raise KeyError: If user account is unknown
@raise ValueError: If uid is not a valid number
""" | python | {
"resource": ""
} |
q18368 | _reduce_file_size_if_necessary | train | def _reduce_file_size_if_necessary(fileName, maxSize):
"""
This function shrinks a file.
We remove only the middle part of a file,
the file-start and the file-end remain unchanged.
"""
fileSize = os.path.getsize(fileName)
if maxSize is None:
logging.debug("Size of logfile '%s' is %s bytes, size limit disabled.", fileName, fileSize)
return # disabled, nothing to do
if fileSize < (maxSize + 500): | python | {
"resource": ""
} |
q18369 | _try_join_cancelled_thread | train | def _try_join_cancelled_thread(thread):
"""Join a thread, but if the thread doesn't terminate for some time, ignore it
instead of waiting infinitely."""
thread.join(10)
if thread.is_alive():
| python | {
"resource": ""
} |
q18370 | RunExecutor._init_cgroups | train | def _init_cgroups(self):
"""
This function initializes the cgroups for the limitations and measurements.
"""
self.cgroups = find_my_cgroups()
for subsystem in self._cgroup_subsystems:
self.cgroups.require_subsystem(subsystem)
if subsystem not in self.cgroups:
sys.exit('Required cgroup subsystem "{}" is missing.'.format(subsystem))
# Feature is still experimental, do not warn loudly
self.cgroups.require_subsystem(BLKIO, log_method=logging.debug)
if BLKIO not in self.cgroups:
logging.debug('Cannot measure I/O without blkio cgroup.')
self.cgroups.require_subsystem(CPUACCT)
if CPUACCT not in self.cgroups:
logging.warning('Without cpuacct cgroups, cputime measurement and limit '
'might not work correctly if subprocesses are started.')
self.cgroups.require_subsystem(FREEZER)
if FREEZER not in self.cgroups:
if self._user is not None:
# In sudo mode, we absolutely need at least one cgroup subsystem
# to be able to find the process where we need to send signals to
sys.exit('Cannot reliably kill sub-processes without freezer cgroup,'
+ ' this is necessary if --user is specified.'
+ ' Please enable this cgroup or do not specify --user.')
else:
logging.warning('Cannot reliably kill sub-processes without freezer cgroup.')
self.cgroups.require_subsystem(MEMORY)
if MEMORY not in self.cgroups:
logging.warning('Cannot measure memory consumption without memory cgroup.')
else:
if systeminfo.has_swap() and (
not self.cgroups.has_value(MEMORY, 'memsw.max_usage_in_bytes')):
logging.warning(
'Kernel misses feature for accounting swap memory, but machine has swap. '
'Memory usage may be measured inaccurately. '
| python | {
"resource": ""
} |
q18371 | RunExecutor._build_cmdline | train | def _build_cmdline(self, args, env={}):
"""
Build the final command line for executing the given command,
using sudo if necessary.
"""
| python | {
"resource": ""
} |
q18372 | RunExecutor._kill_process | train | def _kill_process(self, pid, cgroups=None, sig=signal.SIGKILL):
"""
Try to send signal to given process, either directly of with sudo.
Because we cannot send signals to the sudo process itself,
this method checks whether the target is the sudo process
and redirects the signal to sudo's child in this case.
"""
if self._user is not None:
if not cgroups:
cgroups = find_cgroups_of_process(pid)
# In case we started a tool with sudo, we cannot kill the started
# process itself, because sudo always runs as root.
# So if we are asked to kill the started process itself (the first | python | {
"resource": ""
} |
q18373 | RunExecutor._listdir | train | def _listdir(self, path):
"""Return the list of files in a directory, assuming that our user can read it."""
if self._user is None:
return os.listdir(path)
else:
args = | python | {
"resource": ""
} |
q18374 | RunExecutor._setup_cgroups | train | def _setup_cgroups(self, my_cpus, memlimit, memory_nodes, cgroup_values):
"""
This method creates the CGroups for the following execution.
@param my_cpus: None or a list of the CPU cores to use
@param memlimit: None or memory limit in bytes
@param memory_nodes: None or a list of memory nodes of a NUMA system to use
@param cgroup_values: dict of additional values to set
@return cgroups: a map of all the necessary cgroups for the following execution.
Please add the process of the following execution to all those cgroups!
"""
logging.debug("Setting up cgroups for run.")
# Setup cgroups, need a single call to create_cgroup() for all subsystems
subsystems = [BLKIO, CPUACCT, FREEZER, MEMORY] + self._cgroup_subsystems
if my_cpus is not None or memory_nodes is not None:
subsystems.append(CPUSET)
subsystems = [s for s in subsystems if s in self.cgroups]
cgroups = self.cgroups.create_fresh_child_cgroup(*subsystems)
logging.debug("Created cgroups %s.", cgroups)
# First, set user-specified values such that they get overridden by our settings if necessary.
for ((subsystem, option), value) in cgroup_values.items():
try:
cgroups.set_value(subsystem, option, value)
except EnvironmentError as e:
cgroups.remove()
sys.exit('{} for setting cgroup option {}.{} to "{}" (error code {}).'
.format(e.strerror, subsystem, option, value, e.errno))
logging.debug('Cgroup value %s.%s was set to "%s", new value is now "%s".',
subsystem, option, value, cgroups.get_value(subsystem, option))
# Setup cpuset cgroup if necessary to limit the CPU cores/memory nodes to be used.
if my_cpus is not None:
my_cpus_str = ','.join(map(str, my_cpus))
cgroups.set_value(CPUSET, 'cpus', my_cpus_str)
my_cpus_str = cgroups.get_value(CPUSET, 'cpus')
logging.debug('Using cpu cores [%s].', my_cpus_str)
if memory_nodes is not None:
cgroups.set_value(CPUSET, 'mems', ','.join(map(str, memory_nodes)))
memory_nodesStr = cgroups.get_value(CPUSET, 'mems')
logging.debug('Using memory nodes [%s].', memory_nodesStr)
# Setup memory limit
if memlimit is not None:
limit = 'limit_in_bytes'
cgroups.set_value(MEMORY, limit, memlimit)
swap_limit = 'memsw.limit_in_bytes'
# We need swap limit because otherwise the kernel just starts swapping
# out our process if the limit is reached.
| python | {
"resource": ""
} |
q18375 | RunExecutor._create_temp_dir | train | def _create_temp_dir(self):
"""Create a temporary directory for the run."""
if self._user is None:
base_dir = tempfile.mkdtemp(prefix="BenchExec_run_")
else:
create_temp_dir = self._build_cmdline([
'python', '-c',
| python | {
"resource": ""
} |
q18376 | RunExecutor._cleanup_temp_dir | train | def _cleanup_temp_dir(self, base_dir):
"""Delete given temporary directory and all its contents."""
if self._should_cleanup_temp_dir:
logging.debug('Cleaning up temporary directory %s.', base_dir)
if self._user is None:
util.rmtree(base_dir, onerror=util.log_rmtree_error)
else:
rm = subprocess.Popen(self._build_cmdline(['rm', '-rf', '--', base_dir]),
stderr=subprocess.PIPE)
| python | {
"resource": ""
} |
q18377 | RunExecutor._setup_environment | train | def _setup_environment(self, environments):
"""Return map with desired environment variables for run."""
# If keepEnv is set or sudo is used, start from a fresh environment,
# otherwise with the current one.
# keepEnv specifies variables to copy from the current environment,
# newEnv specifies variables to set to a new value,
# additionalEnv specifies variables where some value should be appended, and
# clearEnv specifies variables to delete.
if self._user is not None or environments.get("keepEnv", None) is not None:
run_environment = {}
else:
run_environment = os.environ.copy()
for key, value in environments.get("keepEnv", {}).items():
| python | {
"resource": ""
} |
q18378 | RunExecutor._setup_output_file | train | def _setup_output_file(self, output_filename, args, write_header=True):
"""Open and prepare output file."""
# write command line into outputFile
# (without environment variables, they are documented by benchexec)
try:
| python | {
"resource": ""
} |
q18379 | RunExecutor._setup_cgroup_time_limit | train | def _setup_cgroup_time_limit(self, hardtimelimit, softtimelimit, walltimelimit,
cgroups, cores, pid_to_kill):
"""Start time-limit handler.
@return None or the time-limit handler for calling cancel()
"""
# hard time limit with cgroups is optional (additionally enforce by ulimit)
cgroup_hardtimelimit = hardtimelimit if CPUACCT in cgroups else None
if any([cgroup_hardtimelimit, softtimelimit, walltimelimit]):
# Start a timer to periodically check timelimit
timelimitThread = _TimelimitThread(cgroups=cgroups,
hardtimelimit=cgroup_hardtimelimit,
softtimelimit=softtimelimit,
| python | {
"resource": ""
} |
q18380 | RunExecutor._setup_cgroup_memory_limit | train | def _setup_cgroup_memory_limit(self, memlimit, cgroups, pid_to_kill):
"""Start memory-limit handler.
@return None or the memory-limit handler for calling cancel()
"""
if memlimit is not None:
try:
oomThread = oomhandler.KillProcessOnOomThread(
cgroups=cgroups, pid_to_kill=pid_to_kill,
callbackFn=self._set_termination_reason,
kill_process_fn=self._kill_process)
| python | {
"resource": ""
} |
q18381 | RunExecutor._setup_ulimit_time_limit | train | def _setup_ulimit_time_limit(self, hardtimelimit, cgroups):
"""Setup time limit with ulimit for the current process."""
if hardtimelimit is not None:
# Also use ulimit for CPU time limit as a fallback if cgroups don't work.
if CPUACCT in cgroups:
# Use a slightly higher limit to ensure cgroups get used
# (otherwise we cannot detect the timeout properly).
| python | {
"resource": ""
} |
q18382 | RunExecutor._setup_file_hierarchy_limit | train | def _setup_file_hierarchy_limit(
self, files_count_limit, files_size_limit, temp_dir, cgroups, pid_to_kill):
"""Start thread that enforces any file-hiearchy limits."""
if files_count_limit is not None or files_size_limit is not None:
file_hierarchy_limit_thread = FileHierarchyLimitThread(
self._get_result_files_base(temp_dir),
files_count_limit=files_count_limit,
files_size_limit=files_size_limit,
cgroups=cgroups,
| python | {
"resource": ""
} |
q18383 | RunExecutor._get_cgroup_measurements | train | def _get_cgroup_measurements(self, cgroups, ru_child, result):
"""
This method calculates the exact results for time and memory measurements.
It is not important to call this method as soon as possible after the run.
"""
logging.debug("Getting cgroup measurements.")
cputime_wait = ru_child.ru_utime + ru_child.ru_stime if ru_child else 0
cputime_cgroups = None
if CPUACCT in cgroups:
# We want to read the value from the cgroup.
# The documentation warns about outdated values.
# So we read twice with 0.1s time difference,
# and continue reading as long as the values differ.
# This has never happened except when interrupting the script with Ctrl+C,
# but just try to be on the safe side here.
tmp = cgroups.read_cputime()
tmp2 = None
while tmp != tmp2:
time.sleep(0.1)
tmp2 = tmp
tmp = cgroups.read_cputime()
cputime_cgroups = tmp
# Usually cputime_cgroups seems to be 0.01s greater than cputime_wait.
# Furthermore, cputime_wait might miss some subprocesses,
# therefore we expect cputime_cgroups to be always greater (and more correct).
# However, sometimes cputime_wait is a little bit bigger than cputime2.
# For small values, this is probably because cputime_wait counts since fork,
# whereas cputime_cgroups counts only after cgroups.add_task()
# (so overhead from runexecutor is correctly excluded in cputime_cgroups).
# For large values, a difference may also indicate a problem with cgroups,
# for example another process moving our benchmarked process between cgroups,
# thus we warn if the difference is substantial and take the larger cputime_wait value.
if cputime_wait > 0.5 and (cputime_wait * 0.95) > cputime_cgroups:
logging.warning(
'Cputime measured by wait was %s, cputime measured by cgroup was only %s, '
'perhaps measurement is flawed.',
| python | {
"resource": ""
} |
q18384 | RunExecutor.check_for_new_files_in_home | train | def check_for_new_files_in_home(self):
"""Check that the user account's home directory now does not contain more files than
when this instance was created, and warn otherwise.
Does nothing if no user account was given to RunExecutor.
@return set of newly created files
"""
if not self._user:
return None
try:
created_files = set(self._listdir(self._home_dir)).difference(self._home_dir_content)
except (subprocess.CalledProcessError, IOError):
# Probably home directory does not exist
created_files = []
| python | {
"resource": ""
} |
q18385 | sethostname | train | def sethostname(name):
"""Set the host name of the machine."""
# TODO: replace with socket.sethostname, which is available from Python 3.3
| python | {
"resource": ""
} |
q18386 | check_cgroup_availability | train | def check_cgroup_availability(wait=1):
"""
Basic utility to check the availability and permissions of cgroups.
This will log some warnings for the user if necessary.
On some systems, daemons such as cgrulesengd might interfere with the cgroups
of a process soon after it was started. Thus this function starts a process,
waits a configurable amount of time, and check whether the cgroups have been changed.
@param wait: a non-negative int that is interpreted as seconds to wait during the check
@raise SystemExit: if cgroups are not usable
"""
logging.basicConfig(format="%(levelname)s: %(message)s")
runexecutor = RunExecutor()
my_cgroups = runexecutor.cgroups
if not (CPUACCT in my_cgroups and
CPUSET in my_cgroups and
# FREEZER in my_cgroups and # For now, we do not require freezer
MEMORY in my_cgroups):
sys.exit(1)
with tempfile.NamedTemporaryFile(mode='rt') as tmp:
runexecutor.execute_run(['sh', '-c', 'sleep {0}; cat /proc/self/cgroup'.format(wait)], | python | {
"resource": ""
} |
q18387 | Tool.allInText | train | def allInText(self, words, text):
"""
This function checks, if all the words appear in the given order in the text.
"""
index = 0
for word in words:
| python | {
"resource": ""
} |
q18388 | printOut | train | def printOut(value, end='\n'):
"""
This function prints the given String immediately and flushes the output.
| python | {
"resource": ""
} |
q18389 | is_code | train | def is_code(filename):
"""
This function returns True, if a line of the file contains bracket '{'.
"""
with open(filename, "r") as file:
for line in file:
# ignore comments and empty lines
if not is_comment(line) \
| python | {
"resource": ""
} |
q18390 | get_list_from_xml | train | def get_list_from_xml(elem, tag="option", attributes=["name"]):
'''
This function searches for all "option"-tags and returns a list with all attributes and texts.
'''
return | python | {
"resource": ""
} |
q18391 | parse_int_list | train | def parse_int_list(s):
"""
Parse a comma-separated list of strings.
The list may additionally contain ranges such as "1-5",
which will be expanded into "1,2,3,4,5".
"""
result = []
for item in s.split(','):
item = item.strip().split('-')
if len(item) == 1:
| python | {
"resource": ""
} |
q18392 | split_number_and_unit | train | def split_number_and_unit(s):
"""Parse a string that consists of a integer number and an optional unit.
@param s a non-empty string that starts with an int and is followed by some letters
@return a triple of the number (as int) and the unit
"""
if not s:
raise | python | {
"resource": ""
} |
q18393 | parse_memory_value | train | def parse_memory_value(s):
"""Parse a string that contains a number of bytes, optionally with a unit like MB.
@return the number of bytes encoded by the string
"""
number, unit = split_number_and_unit(s)
if not unit or unit == 'B':
return number
elif unit == 'kB':
return number * _BYTE_FACTOR
elif unit == 'MB':
return number * _BYTE_FACTOR * | python | {
"resource": ""
} |
q18394 | parse_timespan_value | train | def parse_timespan_value(s):
"""Parse a string that contains a time span, optionally with a unit like s.
@return the number of seconds encoded by the string
"""
number, unit = split_number_and_unit(s)
if not unit or unit == "s":
return number
elif unit == "min":
| python | {
"resource": ""
} |
q18395 | expand_filename_pattern | train | def expand_filename_pattern(pattern, base_dir):
"""
Expand a file name pattern containing wildcards, environment variables etc.
@param pattern: The pattern string to expand.
@param base_dir: The directory where relative paths are based on.
@return: A list of file names (possibly empty).
"""
# 'join' ignores base_dir, if expandedPattern is absolute.
# 'normpath' replaces 'A/foo/../B' with 'A/B', for pretty printing only
| python | {
"resource": ""
} |
q18396 | substitute_vars | train | def substitute_vars(template, replacements):
"""Replace certain keys with respective values in a string.
@param template: the string in which replacements should be made
@param replacements: a dict or a list of pairs of keys and values
"""
result = template
for (key, value) in replacements:
| python | {
"resource": ""
} |
q18397 | rmtree | train | def rmtree(path, ignore_errors=False, onerror=None):
"""Same as shutil.rmtree, but supports directories without write or execute permissions."""
if ignore_errors:
def onerror(*args):
pass
elif onerror is None:
def onerror(*args):
raise
for root, dirs, unused_files in os.walk(path):
for directory in dirs:
try:
abs_directory = os.path.join(root, directory)
| python | {
"resource": ""
} |
q18398 | copy_all_lines_from_to | train | def copy_all_lines_from_to(inputFile, outputFile):
"""Copy all lines from an input file object to an output file | python | {
"resource": ""
} |
q18399 | write_file | train | def write_file(content, *path):
"""
Simply write some content to a file, overriding the file if necessary.
""" | python | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.