def rm_doubled_quotes(entry):
"""Some entries have doubled quotes (translations) within different senses.
Remove the doubled quotes.
This function return True, if the entry has been modified."""
senses = list(findall(entry, 'sense'))
# add quote elements
senses = [(cit, q) for s in senses for cit in findall(s, 'cit')
for q in findall(cit, 'quote')]
if len(senses) <= 1:
return
changed = False
# pair each sense with another and compare their content
for trans1, trans2 in itertools.combinations(senses, 2):
# could have been removed already, so check:
cit1, quote1 = trans1
cit2, quote2 = trans2
if not cit1.findall(quote1.tag) or not cit2.findall(quote2.tag) \
and cit1 is not cit2:
continue # one of them has been removed already
# text of both quotes match, remove second quote
if quote1.text == quote2.text:
cit2.remove(quote2)
changed = True
return changed
python类quote()的实例源码
def delay(func, *args, timeout=None, max_retries=0, queue=None):
arguments = [shlex.quote(arg) for arg in args]
command = 'yawn exec {0.__module__} {0.__name__} {1}'.format(
func, ' '.join(arguments)).strip()
task_name = '{0.__module__}.{0.__name__}({1})'.format(
func, ', '.join(arguments))
if queue:
queue_obj, _ = Queue.objects.get_or_create(name=queue)
else:
queue_obj = Queue.get_default_queue()
template, _ = Template.objects.get_or_create(
name=task_name,
command=command,
queue=queue_obj,
max_retries=max_retries,
timeout=timeout
)
task = Task.objects.create(
template=template
)
task.enqueue()
return task
def pull_origin_master(directory, host=None, port=22):
"""
Pull origin master in a directory on the remote machine
:param str directory: directory on the remote machine to pull origin master in
:param str host: hostname or ip of the remote machine, None for the local machine
:param int port: port to use to connect to the remote machine over ssh
:return int exit_code: exit code of the remote command
"""
log.info("Pulling origin master in {}".format(directory))
pull_origin_master_command = 'cd {}; git pull origin master'.format(
quote(directory)
)
exit_code, _, _ = run_command_print_ready(
pull_origin_master_command, host=host, port=port,
failure_callback=log_failure_factory(
"Failed to pull origin master"
),
buffered=False,
shell=True
)
return exit_code
def reset_hard_head(directory, host=None, port=22):
"""
Reset a checkout to the HEAD of the branch
:param str directory: directory on the remote machine to reset to HEAD in
:param str host: hostname or ip of the remote machine, or None for local
:param int port: port to use to connect to the remote machine over ssh
:return int exit_code: exit code of the remote command
"""
log.info("Resetting to HEAD in {}".format(directory))
reset_hard_command = 'cd {}; git reset --hard ' \
'HEAD'.format(quote(directory))
exit_code, _, _ = run_command_print_ready(
reset_hard_command, host=host, port=port,
failure_callback=log_failure_factory(
"Failed to reset to HEAD"
),
buffered=False,
shell=True
)
return exit_code
def testQuote(self):
safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./'
unicode_sample = '\xe9\xe0\xdf' # e + acute accent, a + grave, sharp s
unsafe = '"`$\\!' + unicode_sample
self.assertEqual(shlex.quote(''), "''")
self.assertEqual(shlex.quote(safeunquoted), safeunquoted)
self.assertEqual(shlex.quote('test file name'), "'test file name'")
for u in unsafe:
self.assertEqual(shlex.quote('test%sname' % u),
"'test%sname'" % u)
for u in unsafe:
self.assertEqual(shlex.quote("test%s'name'" % u),
"'test%s'\"'\"'name'\"'\"''" % u)
# Allow this test to be used with old shlex.py
def get_command(self, file, **options):
# on darwin open returns immediately resulting in the temp
# file removal while app is opening
command = "open -a /Applications/Preview.app"
command = "(%s %s; sleep 20; rm -f %s)&" % (command, quote(file),
quote(file))
return command
def show_file(self, file, **options):
command, executable = self.get_command_ex(file, **options)
command = "(%s %s; rm -f %s)&" % (command, quote(file),
quote(file))
os.system(command)
return 1
# implementations
def get_command_ex(self, file, title=None, **options):
# note: xv is pretty outdated. most modern systems have
# imagemagick's display command instead.
command = executable = "xv"
if title:
command += " -name %s" % quote(title)
return command, executable
def get_command(self, file, **options):
# on darwin open returns immediately resulting in the temp
# file removal while app is opening
command = "open -a /Applications/Preview.app"
command = "(%s %s; sleep 20; rm -f %s)&" % (command, quote(file),
quote(file))
return command
def show_file(self, file, **options):
command, executable = self.get_command_ex(file, **options)
command = "(%s %s; rm -f %s)&" % (command, quote(file),
quote(file))
os.system(command)
return 1
# implementations
def get_command_ex(self, file, title=None, **options):
# note: xv is pretty outdated. most modern systems have
# imagemagick's display command instead.
command = executable = "xv"
if title:
command += " -name %s" % quote(title)
return command, executable
def run(args, shell=False, exit=True):
"""
Run the command ``args``.
Automatically hides the secret GitHub token from the output.
If shell=False (recommended for most commands), args should be a list of
strings. If shell=True, args should be a string of the command to run.
If exit=True, it exits on nonzero returncode. Otherwise it returns the
returncode.
"""
if "GH_TOKEN" in os.environ:
token = get_token()
else:
token = b''
if not shell:
command = ' '.join(map(shlex.quote, args))
else:
command = args
command = command.replace(token.decode('utf-8'), '~'*len(token))
print(blue(command))
sys.stdout.flush()
returncode = run_command_hiding_token(args, token, shell=shell)
if exit and returncode != 0:
sys.exit(red("%s failed: %s" % (command, returncode)))
return returncode
def xcheck_host_prog(conf, name, tool, wafname=None):
wafname = wafname or name
chost, chost_envar = get_chost_stuff(conf)
specific = None
if chost:
specific = os.environ.get('%s_%s' % (chost_envar, name), None)
if specific:
value = Utils.to_list(specific)
conf.env[wafname] += value
conf.msg('Will use cross-compilation %s from %s_%s' \
% (name, chost_envar, name),
" ".join(quote(x) for x in value))
return
else:
envar = os.environ.get('HOST_%s' % name, None)
if envar is not None:
value = Utils.to_list(envar)
conf.env[wafname] = value
conf.msg('Will use cross-compilation %s from HOST_%s' \
% (name, name),
" ".join(quote(x) for x in value))
return
if conf.env[wafname]:
return
value = None
if chost:
value = '%s-%s' % (chost, tool)
if value:
conf.env[wafname] = value
conf.msg('Will use cross-compilation %s from CHOST' \
% wafname, value)
def xcheck_host_envar(conf, name, wafname=None):
wafname = wafname or name
chost, chost_envar = get_chost_stuff(conf)
specific = None
if chost:
specific = os.environ.get('%s_%s' % (chost_envar, name), None)
if specific:
value = Utils.to_list(specific)
conf.env[wafname] += value
conf.msg('Will use cross-compilation %s from %s_%s' \
% (name, chost_envar, name),
" ".join(quote(x) for x in value))
return
envar = os.environ.get('HOST_%s' % name, None)
if envar is None:
return
value = Utils.to_list(envar) if envar != '' else [envar]
conf.env[wafname] = value
conf.msg('Will use cross-compilation %s from HOST_%s' \
% (name, name),
" ".join(quote(x) for x in value))
def write_compilation_database(ctx):
"Write the clang compilation database as JSON"
database_file = ctx.bldnode.make_node('compile_commands.json')
Logs.info("Build commands will be stored in %s" % database_file.path_from(ctx.path))
try:
root = json.load(database_file)
except IOError:
root = []
clang_db = dict((x["file"], x) for x in root)
for task in getattr(ctx, 'clang_compilation_database_tasks', []):
try:
cmd = task.last_cmd
except AttributeError:
continue
directory = getattr(task, 'cwd', ctx.variant_dir)
f_node = task.inputs[0]
filename = os.path.relpath(f_node.abspath(), directory)
cmd = " ".join(map(quote, cmd))
entry = {
"directory": directory,
"command": cmd,
"file": filename,
}
clang_db[filename] = entry
root = list(clang_db.values())
database_file.write(json.dumps(root, indent=2))
def xcheck_host_envar(conf, name, wafname=None):
wafname = wafname or name
chost, chost_envar = get_chost_stuff(conf)
specific = None
if chost:
specific = os.environ.get('%s_%s' % (chost_envar, name), None)
if specific:
value = Utils.to_list(specific)
conf.env[wafname] += value
conf.msg('Will use cross-compilation %s from %s_%s' \
% (name, chost_envar, name),
" ".join(quote(x) for x in value))
return
envar = os.environ.get('HOST_%s' % name, None)
if envar is None:
return
value = Utils.to_list(envar) if envar != '' else [envar]
conf.env[wafname] = value
conf.msg('Will use cross-compilation %s from HOST_%s' \
% (name, name),
" ".join(quote(x) for x in value))
def write_compilation_database(ctx):
"Write the clang compilation database as JSON"
database_file = ctx.bldnode.make_node('compile_commands.json')
Logs.info("Build commands will be stored in %s" % database_file.path_from(ctx.path))
try:
root = json.load(database_file)
except IOError:
root = []
clang_db = dict((x["file"], x) for x in root)
for task in getattr(ctx, 'clang_compilation_database_tasks', []):
try:
cmd = task.last_cmd
except AttributeError:
continue
directory = getattr(task, 'cwd', ctx.variant_dir)
f_node = task.inputs[0]
filename = os.path.relpath(f_node.abspath(), directory)
cmd = " ".join(map(quote, cmd))
entry = {
"directory": directory,
"command": cmd,
"file": filename,
}
clang_db[filename] = entry
root = list(clang_db.values())
database_file.write(json.dumps(root, indent=2))
def xcheck_prog(conf, var, tool, cross=False):
value = os.environ.get(var, '')
value = Utils.to_list(value)
if not value:
return
conf.env[var] = value
if cross:
pretty = 'cross-compilation %s' % var
else:
pretty = var
conf.msg('Will use %s' % pretty,
" ".join(quote(x) for x in value))
def xcheck_envar(conf, name, wafname=None, cross=False):
wafname = wafname or name
value = os.environ.get(name, None)
value = Utils.to_list(value)
if not value:
return
conf.env[wafname] += value
if cross:
pretty = 'cross-compilation %s' % wafname
else:
pretty = wafname
conf.msg('Will use %s' % pretty,
" ".join(quote(x) for x in value))